Module: Enumerable

Defined in:
lib/mgmg.rb,
lib/mgmg/search.rb

Instance Method Summary collapse

Instance Method Details

#armor_search(para, target, smith, comp, opt: Mgmg::Option.new) ⇒ Object



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/mgmg/search.rb', line 230

def armor_search(para, target, smith, comp, opt: Mgmg::Option.new)
	opt = opt.dup.set_default(self)
	if opt.armor_max < opt.armor_min
		raise ArgumentError, "armor_min <= armor_max is needed, (armor_min, armor_max) = (#{opt.armor_min}, #{opt.armor_max}) are given"
	elsif opt.cut_exp < Float::INFINITY
		begin
			opt.armor_max = [opt.armor_max, Mgmg.invexp3(opt.cut_exp, smith, comp)].min
		rescue
			raise Mgmg::SearchCutException
		end
	end
	if opt.irep.para_call(para, smith, opt.armor_max, comp) < target
		raise Mgmg::SearchCutException
	elsif target <= opt.irep.para_call(para, smith, opt.armor_min, comp)
		return opt.armor_min
	end
	while 1 < opt.armor_max - opt.armor_min do
		armor = (opt.armor_max - opt.armor_min).div(2) + opt.armor_min
		if opt.irep.para_call(para, smith, armor, comp) < target
			opt.armor_min = armor
		else
			opt.armor_max = armor
		end
	end
	opt.armor_max
end

#build(smith = -1,, armor = smith, comp = armor.tap{armor=smith}, opt: Mgmg::Option.new) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/mgmg.rb', line 155

def build(smith=-1, armor=smith, comp=armor.tap{armor=smith}, opt: Mgmg::Option.new)
	opt = opt.dup
	rein = opt.reinforcement
	opt.reinforcement = []
	if self.empty?
		Mgmg::Equip::Zero
	else
		self.sum(Mgmg::Equip::Zero) do |str|
			if Mgmg::EquipPosition[str.build(opt:).kind] == 0
				str.build(smith, comp, opt:)
			else
				str.build(armor, comp, opt:)
			end
		end.reinforce(*rein)
	end
end

#comp_search(para, target, smith, armor, opt: Mgmg::Option.new) ⇒ Object



301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/mgmg/search.rb', line 301

def comp_search(para, target, smith, armor, opt: Mgmg::Option.new)
	opt = opt.dup.set_default(self)
	if opt.comp_max < opt.comp_min
		raise ArgumentError, "comp_min <= comp_max is needed, (comp_min, comp_max) = (#{opt.comp_min}, #{opt.comp_max}) are given"
	end
	if target <= opt.irep.para_call(para, smith, armor, opt.comp_min)
		return opt.comp_min
	elsif opt.irep.para_call(para, smith, armor, opt.comp_max) < target
		raise Mgmg::SearchCutException, "given comp_max=#{opt.comp_max} does not satisfies the target"
	end
	while 1 < opt.comp_max - opt.comp_min do
		comp = (opt.comp_max - opt.comp_min).div(2) + opt.comp_min
		if opt.irep.para_call(para, smith, armor, comp) < target
			opt.comp_min = comp
		else
			opt.comp_max = comp
		end
	end
	opt.comp_max
end

#find_max(para, max_exp, opt: Mgmg::Option.new) ⇒ Object



469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
# File 'lib/mgmg/search.rb', line 469

def find_max(para, max_exp, opt: Mgmg::Option.new)
	opt = opt.dup.set_default(self)
	exp = Mgmg.exp(opt.smith_min, opt.armor_min, opt.comp_min)
	raise Mgmg::SearchCutException, "the recipe requires #{exp.comma3} experiment points, which exceeds given max_exp=#{max_exp.comma3}" if max_exp < exp
	ret = [Mgmg.invexp3(max_exp, opt.armor_min, opt.comp_min), opt.armor_min, opt.comp_min]
	max = opt.irep.para_call(para, *ret)
	eo = opt.irep.eo_para(para)
	opt.comp_max = [opt.comp_max, Mgmg.invexp3c(max_exp, opt.smith_min, opt.armor_min)].min
	comps = Mgmg.fib_init(opt.comp_min, opt.comp_max)
	values = comps.map do |comp|
		cur, ret, max = eval_comp_fm(para, comp, eo, opt, ret, max, max_exp)
		cur
	end
	while 3 < comps[3]-comps[0]
		if values[2] <= values[1]
			comp = comps[0] + comps[2]-comps[1]
			comps = [comps[0], comp, comps[1], comps[2]]
			cur, ret, max = eval_comp_fm(para, comp, eo, opt, ret, max, max_exp)
			values = [values[0], cur, values[1], values[2]]
		else
			comp = comps[1] + comps[3]-comps[2]
			comps = [comps[1], comps[2], comp, comps[3]]
			cur, ret, max = eval_comp_fm(para, comp, eo, opt, ret, max, max_exp)
			values = [values[1], values[2], cur, values[3]]
		end
	end
	values = values.filter(&:finite?)
	diff = values.max-values.min
	if 0 < diff
		th = max - diff*opt.fib_ext[1]
		(comps[0]-1).downto(opt.comp_min) do |comp|
			next if ( eo & (2**(comp&1)) == 0 )
			cur, ret, max = eval_comp_fm(para, comp, eo, opt, ret, max, max_exp)
			break if cur < th
		end
		(comps[3]+1).upto(opt.comp_max) do |comp|
			next if ( eo & (2**(comp&1)) == 0 )
			cur, ret, max = eval_comp_fm(para, comp, eo, opt, ret, max, max_exp)
			break if cur < th
		end
	end
	ret
end

#ir(opt: Mgmg::Option.new) ⇒ Object



171
172
173
174
175
# File 'lib/mgmg.rb', line 171

def ir(opt: Mgmg::Option.new)
	self.sum(Mgmg::IR::Zero) do |str|
		str.ir(opt:)
	end.add_reinforcement(opt.reinforcement)
end

#max_weight(include_outsourcing = false, opt: Mgmg::Option.new) ⇒ Object



196
197
198
199
200
201
202
# File 'lib/mgmg.rb', line 196

def max_weight(include_outsourcing=false, opt: Mgmg::Option.new)
	if include_outsourcing
		build(-1, opt:).weight
	else
		build(*min_smith(opt:), -1, opt:).weight
	end
end

#max_weights(include_outsourcing = false, opt: Mgmg::Option.new) ⇒ Object



214
215
216
217
218
219
220
221
222
223
224
# File 'lib/mgmg.rb', line 214

def max_weights(include_outsourcing=false, opt: Mgmg::Option.new)
	weapons, armors = [], []
	each do |str|
		if Mgmg::EquipPosition[str.build(opt:).kind] == 0
			weapons << str
		else
			armors << str
		end
	end
	[weapons.max_weight(include_outsourcing, opt:), armors.max_weight(include_outsourcing, opt:)]
end

#min_comp(opt: Mgmg::Option.new) ⇒ Object



281
282
283
284
285
# File 'lib/mgmg.rb', line 281

def min_comp(opt: Mgmg::Option.new)
	self.map do |str|
		Mgmg::Equip.min_comp(str, opt:)
	end.append(-1).max
end

#min_level(ws = 0, wa = ws, include_outsourcing = false, opt: Mgmg::Option.new) ⇒ Object



225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/mgmg.rb', line 225

def min_level(ws=0, wa=ws, include_outsourcing=false, opt: Mgmg::Option.new)
	weapons, armors = [], []
	each do |str|
		if Mgmg::EquipPosition[str.build(opt:).kind] == 0
			weapons << str
		else
			armors << str
		end
	end
	ms, ma = min_smith(opt:)
	rs = min_level_sub(ws, ms, 0, weapons, include_outsourcing, opt:)
	ra = min_level_sub(wa, ma, 1, armors, include_outsourcing, opt:)
	[rs, ra]
end

#min_levels(w = 1, opt: Mgmg::Option.new) ⇒ Object



255
256
257
# File 'lib/mgmg.rb', line 255

def min_levels(w=1, opt: Mgmg::Option.new)
	build(opt:).min_levels(w)
end

#min_levels_max(w = 1, opt: Mgmg::Option.new) ⇒ Object



258
259
260
261
262
263
264
265
266
267
268
# File 'lib/mgmg.rb', line 258

def min_levels_max(w=1, opt: Mgmg::Option.new)
	ret = [-1, -1]
	min_levels(w, opt:).each do |str, level|
		if Mgmg::EquipPosition[str.build(opt:).kind] == 0
			ret[0] = [ret[0], level].max
		else
			ret[1] = [ret[1], level].max
		end
	end
	ret
end

#min_smith(opt: Mgmg::Option.new) ⇒ Object



269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/mgmg.rb', line 269

def min_smith(opt: Mgmg::Option.new)
	ret = [-1, -1]
	self.each do |str|
		s = Mgmg::Equip.min_smith(str, opt:)
		if Mgmg::EquipPosition[str.build(opt:).kind] == 0
			ret[0] = [ret[0], s].max
		else
			ret[1] = [ret[1], s].max
		end
	end
	ret
end

#min_weight(opt: Mgmg::Option.new) ⇒ Object



193
194
195
# File 'lib/mgmg.rb', line 193

def min_weight(opt: Mgmg::Option.new)
	build(*build(opt:).min_levels_max, -1, opt:).weight
end

#min_weights(opt: Mgmg::Option.new) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
# File 'lib/mgmg.rb', line 203

def min_weights(opt: Mgmg::Option.new)
	weapons, armors = [], []
	each do |str|
		if Mgmg::EquipPosition[str.build(opt:).kind] == 0
			weapons << str
		else
			armors << str
		end
	end
	[weapons.min_weight(opt:), armors.min_weight(opt:)]
end

#sa_search(para, target, comp, opt: Mgmg::Option.new) ⇒ Object



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/mgmg/search.rb', line 256

def sa_search(para, target, comp, opt: Mgmg::Option.new)
	opt = opt.dup.set_default(self)
	opt_nocut = opt.dup; opt_nocut.cut_exp = Float::INFINITY
	opt.smith_min = smith_search(para, target, opt.armor_max, comp, opt: opt_nocut)
	opt.armor_min = armor_search(para, target, opt.smith_max, comp, opt: opt_nocut)
	raise Mgmg::SearchCutException if opt.cut_exp < Mgmg.exp(opt.smith_min, opt.armor_min, comp)
	opt.smith_max = smith_search(para, target, opt.armor_min, comp, opt: opt_nocut)
	opt.armor_max = armor_search(para, target, opt.smith_min, comp, opt: opt_nocut)
	ret = nil
	exp = Mgmg.exp(opt.smith_min, opt.armor_max, comp)
	opt.cut_exp, ret = exp, [opt.smith_min, opt.armor_max] if exp <= opt.cut_exp
	exp2 = Mgmg.exp(opt.smith_max, opt.armor_min, comp)
	if exp2 < exp
		opt.cut_exp, ret = exp2, [opt.smith_max, opt.armor_min] if exp2 <= opt.cut_exp
		(opt.armor_min+1).upto(opt.armor_max-1) do |armor|
			break if opt.cut_exp < Mgmg.exp(opt.smith_min, armor, comp)
			smith = smith_search(para, target, armor, comp, opt:)
			exp = Mgmg.exp(smith, armor, comp)
			if exp < opt.cut_exp
				opt.cut_exp, ret = exp, [smith, armor]
			elsif exp == opt.cut_exp
				if ret.nil? or opt.irep.para_call(para, *ret, comp) < opt.irep.para_call(para, smith, armor, comp) then
					ret = [smith, armor]
				end
			end
		rescue Mgmg::SearchCutException
		end
	else
		(opt.smith_min+1).upto(opt.smith_max-1) do |smith|
			break if opt.cut_exp < Mgmg.exp(smith, opt.armor_min, comp)
			armor = armor_search(para, target, smith, comp, opt:)
			exp = Mgmg.exp(smith, armor, comp)
			if exp < opt.cut_exp
				opt.cut_exp, ret = exp, [smith, armor]
			elsif exp == opt.cut_exp
				if ret.nil? or opt.irep.para_call(para, *ret, comp) < opt.irep.para_call(para, smith, armor, comp) then
					ret = [smith, armor]
				end
			end
		rescue Mgmg::SearchCutException
		end
	end
	raise Mgmg::SearchCutException if ret.nil?
	ret
end

#search(para, target, opt: Mgmg::Option.new) ⇒ Object



343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# File 'lib/mgmg/search.rb', line 343

def search(para, target, opt: Mgmg::Option.new)
	org_cut_exp = opt.cut_exp
	opt = opt.dup.set_default(self)
	begin
		opt.comp_min = comp_search(para, target, opt.smith_max, opt.armor_max, opt:)
	rescue Mgmg::SearchCutException
		foo = opt.irep.para_call(para, opt.smith_max, opt.armor_max, opt.comp_max)
		raise Mgmg::SearchCutException, "#{self} could not reach target=#{target} until (smith_max, armor_max, comp_max)=(#{opt.smith_max}, #{opt.armor_max}, #{opt.comp_max}), which yields #{foo.comma3}"
	end
	opt_nocut = opt.dup; opt_nocut.cut_exp = Float::INFINITY
	opt.smith_max = ( smith_search(para, target, opt.armor_min, opt.comp_min, opt: opt_nocut) rescue opt.smith_max )
	opt.armor_max = ( armor_search(para, target, opt.smith_min, opt.comp_min, opt: opt_nocut) rescue opt.armor_max )
	opt.smith_min = smith_search(para, target, opt.armor_max, opt.comp_max, opt: opt_nocut)
	opt.armor_min = armor_search(para, target, opt.smith_max, opt.comp_max, opt: opt_nocut)
	raise Mgmg::SearchCutException if opt.cut_exp < Mgmg.exp(opt.smith_min, opt.armor_min, opt.comp_min)
	opt.comp_max = ( comp_search(para, target, opt.smith_min, opt.armor_min, opt:) rescue opt.comp_max )
	exp = Mgmg.exp(opt.smith_min, opt.armor_min, opt.comp_max)
	opt.cut_exp, ret = exp, [opt.smith_min, opt.armor_min,opt. comp_max] if exp <= opt.cut_exp
	eo = opt.irep.eo_para(para)
	comps = Mgmg.fib_init(opt.comp_min, opt.comp_max)
	values = comps.map do |comp|
		r, e = eval_comp_sa(para, target, comp, opt_nocut, eo)
		opt.cut_exp, ret = e, r if e < opt.cut_exp
		e
	end
	while 3 < comps[3]-comps[0]
		if values[1] <= values[2]
			comp = comps[0] + comps[2]-comps[1]
			comps = [comps[0], comp, comps[1], comps[2]]
			r, e = eval_comp_sa(para, target, comp, opt_nocut, eo)
			opt.cut_exp, ret = e, r if e < opt.cut_exp
			values = [values[0], e, values[1], values[2]]
		else
			comp = comps[1] + comps[3]-comps[2]
			comps = [comps[1], comps[2], comp, comps[3]]
			r, e = eval_comp_sa(para, target, comp, opt_nocut, eo)
			opt.cut_exp, ret = e, r if e < opt.cut_exp
			values = [values[1], values[2], e, values[3]]
		end
	end
	exp_best = opt.cut_exp
	values = values.filter(&:finite?)
	diff = values.max-values.min
	if 0 < diff
		opt.cut_exp = exp_best + diff*opt.fib_ext[0]
		(comps[0]-1).downto(opt.comp_min) do |comp|
			exp_best, ret = fine_sa(exp_best, ret, para, target, comp, opt, eo)
		rescue Mgmg::SearchCutException
			break
		end
		(comps[3]+1).upto(opt.comp_max) do |comp|
			exp_best, ret = fine_sa(exp_best, ret, para, target, comp, opt, eo)
		rescue Mgmg::SearchCutException
			break
		end
	end
	if ret.nil?
		max = opt.irep.para_call(para, *find_max(para, org_cut_exp, opt:))
		raise Mgmg::SearchCutException, "the maximum output with given cut_exp=#{org_cut_exp.comma3} is #{max.comma3}, which does not reach given target=#{target.comma3}"
	end
	ret
end

#show(smith = -1,, armor = smith, comp = armor.tap{armor=smith}, para: :power, name: nil, opt: Mgmg::Option.new) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/mgmg.rb', line 176

def show(smith=-1, armor=smith, comp=armor.tap{armor=smith}, para: :power, name: nil, opt: Mgmg::Option.new)
	rein = case opt.reinforcement
	when Array
		opt.reinforcement.map{|r| Mgmg::Reinforcement.compile(r)}
	else
		[Mgmg::Reinforcement.compile(opt.reinforcement)]
	end
	name = self.join(' ') if name.nil?
	built = self.build(smith, armor, comp, opt:)
	pstr = '%.3f' % built.para_call(para)
	pstr.sub!(/\.?0+\Z/, '')
	puts "With levels (#{smith}, #{armor}, #{comp}: #{Mgmg.exp(smith, armor, comp).comma3}), building"
	puts "  #{name}"
	rein = rein.empty? ? '' : "reinforced by {#{rein.join(',')}} "
	puts "#{rein}yields (#{pstr}, #{built.total_cost})"
	puts "  #{built}"
end

#smith_search(para, target, armor, comp, opt: Mgmg::Option.new) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/mgmg/search.rb', line 204

def smith_search(para, target, armor, comp, opt: Mgmg::Option.new)
	opt = opt.dup.set_default(self)
	if opt.smith_max < opt.smith_min
		raise ArgumentError, "smith_min <= smith_max is needed, (smith_min, smith_max) = (#{opt.smith_min}, #{opt.smith_max}) are given"
	elsif opt.cut_exp < Float::INFINITY
		begin
			opt.smith_max = [opt.smith_max, Mgmg.invexp3(opt.cut_exp, armor, comp)].min
		rescue
			raise Mgmg::SearchCutException
		end
	end
	if opt.irep.para_call(para, opt.smith_max, armor, comp) < target
		raise Mgmg::SearchCutException
	elsif target <= opt.irep.para_call(para, opt.smith_min, armor, comp)
		return opt.smith_min
	end
	while 1 < opt.smith_max - opt.smith_min do
		smith = (opt.smith_max - opt.smith_min).div(2) + opt.smith_min
		if opt.irep.para_call(para, smith, armor, comp) < target
			opt.smith_min = smith
		else
			opt.smith_max = smith
		end
	end
	opt.smith_max
end

#to_recipe(para = :power, allow_over20: false, **kw) ⇒ Object



146
147
148
149
150
151
152
153
154
# File 'lib/mgmg.rb', line 146

def to_recipe(para=:power, allow_over20: false, **kw)
	unless allow_over20
		self.each do |e|
			foo = e.build(-1).star
			raise Mgmg::Over20Error, foo if 20 < foo
		end
	end
	Mgmg::Recipe.new(self, para, **kw)
end