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



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/mgmg/search.rb', line 216

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



154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/mgmg.rb', line 154

def build(smith=-1, armor=smith, comp=armor.tap{armor=smith}, opt: Mgmg::Option.new)
	opt = opt.dup
	rein = opt.reinforcement
	opt.reinforcement = []
	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

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



287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/mgmg/search.rb', line 287

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 ArgumentError, "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



585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
# File 'lib/mgmg/search.rb', line 585

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
	th = max - (max*opt.comp_ext[3]).ceil
	(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
	ret
end

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



166
167
168
169
170
# File 'lib/mgmg.rb', line 166

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



190
191
192
193
194
195
196
# File 'lib/mgmg.rb', line 190

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



208
209
210
211
212
213
214
215
216
217
218
# File 'lib/mgmg.rb', line 208

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



275
276
277
278
279
# File 'lib/mgmg.rb', line 275

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



219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/mgmg.rb', line 219

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



249
250
251
# File 'lib/mgmg.rb', line 249

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



252
253
254
255
256
257
258
259
260
261
262
# File 'lib/mgmg.rb', line 252

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



263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/mgmg.rb', line 263

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



187
188
189
# File 'lib/mgmg.rb', line 187

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

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



197
198
199
200
201
202
203
204
205
206
207
# File 'lib/mgmg.rb', line 197

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



242
243
244
245
246
247
248
249
250
251
252
253
254
255
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
# File 'lib/mgmg/search.rb', line 242

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



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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
# File 'lib/mgmg/search.rb', line 475

def search(para, target, opt: Mgmg::Option.new)
	opt = opt.dup.set_default(self)
	opt.comp_min = comp_search(para, target, opt.smith_max, opt.armor_max, opt:)
	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 ( return search_aonly(para, target, opt:) )
	opt.armor_max = armor_search(para, target, opt.smith_min, opt.comp_min, opt: opt_nocut) rescue ( return search_sonly(para, target, opt:) )
	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:)
	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
	opt.cut_exp = exp_best + (exp_best*opt.comp_ext[0]).to_i.clamp(opt.comp_ext[1], opt.comp_ext[2])
	(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
	if ret.nil?
		max = opt.irep.para_call(para, *find_max(para, opt.cut_exp, opt:))
		raise Mgmg::SearchCutException, "the maximum output with given cut_exp=#{opt.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, opt: Mgmg::Option.new) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/mgmg.rb', line 171

def show(smith=-1, armor=smith, comp=armor.tap{armor=smith}, para: :power, 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
	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 "  #{self.join(', ')}"
	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



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/mgmg/search.rb', line 190

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



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

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