Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/mgmg.rb,
lib/mgmg/search.rb

Instance Method Summary collapse

Instance Method Details

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



68
69
70
# File 'lib/mgmg.rb', line 68

def build(smith=-1, comp=smith, opt: Mgmg::Option.new)
	Mgmg::Equip.build(self, smith, comp, left_associative: opt.left_associative, include_system_equips: opt.include_system_equips).reinforce(*opt.reinforcement)
end

#buster_optimize(smith = nil, comp = smith, opt: Mgmg::Option.new) ⇒ Object



140
141
142
# File 'lib/mgmg.rb', line 140

def buster_optimize(smith=nil, comp=smith, opt: Mgmg::Option.new)
	Mgmg::Optimize.buster_optimize(self, smith, comp, opt:)
end

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



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/mgmg/search.rb', line 29

def comp_search(para, target, smith, 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, opt.comp_min)
		return opt.comp_min
	elsif opt.irep.para_call(para, smith, 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, comp) < target
			opt.comp_min = comp
		else
			opt.comp_max = comp
		end
	end
	opt.comp_max
end

#eff(para, smith, comp = smith, opt: Mgmg::Option.new) ⇒ Object



109
110
111
112
113
114
115
116
# File 'lib/mgmg.rb', line 109

def eff(para, smith, comp=smith, opt: Mgmg::Option.new)
	a = build(smith, comp, opt:).para_call(para)
	b = build(smith+1, comp, opt:).para_call(para)
	c = build(smith, comp+2, opt:).para_call(para)
	sden = smith==0 ? 1 : 2*smith-1
	cden = comp==0 ? 4 : 8*comp
	[(b-a).quo(sden), (c-a).quo(cden)]
end

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



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/mgmg/search.rb', line 154

def find_max(para, max_exp, opt: Mgmg::Option.new)
	opt = opt.dup.set_default(self)
	exp = Mgmg.exp(opt.smith_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.invexp2(max_exp, opt.comp_min), opt.comp_min]
	max = opt.irep.para_call(para, *ret)
	eo = opt.irep.eo_para(para)
	opt.comp_max = Mgmg.invexp2c(max_exp, opt.smith_min)
	comps = Mgmg.fib_init(opt.comp_min+1, opt.comp_max)
	values = comps.map do |comp|
		cur, smith = eval_comp_fm(para, comp, eo, opt, max, max_exp)
		ret, max = [smith, comp], cur if ( max < cur || ( max == cur && Mgmg.exp(smith, comp) < Mgmg.exp(*ret) ) )
		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, smith = eval_comp_fm(para, comp, eo, opt, max, max_exp)
			ret, max = [smith, comp], cur if ( max < cur || ( max == cur && Mgmg.exp(smith, comp) < Mgmg.exp(*ret) ) )
			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, smith = eval_comp_fm(para, comp, eo, opt, max, max_exp)
			ret, max = [smith, comp], cur if ( max < cur || ( max == cur && Mgmg.exp(smith, comp) < Mgmg.exp(*ret) ) )
			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, smith = eval_comp_fm(para, comp, eo, opt, max, max_exp)
			ret, max = [smith, comp], cur if ( max < cur || ( max == cur && Mgmg.exp(smith, comp) < Mgmg.exp(*ret) ) )
			break if cur < th
		end
		(comps[3]+1).upto(opt.comp_max) do |comp|
			next if ( eo & (2**(comp&1)) == 0 )
			cur, smith = eval_comp_fm(para, comp, eo, opt, max, max_exp)
			ret, max = [smith, comp], cur if ( max < cur || ( max == cur && Mgmg.exp(smith, comp) < Mgmg.exp(*ret) ) )
			break if cur < th
		end
	end
	ret
end

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



71
72
73
# File 'lib/mgmg.rb', line 71

def ir(opt: Mgmg::Option.new)
	Mgmg::IR.build(self, left_associative: opt.left_associative, reinforcement: opt.reinforcement, include_system_equips: opt.include_system_equips)
end

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



25
26
27
28
29
30
31
# File 'lib/mgmg.rb', line 25

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

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



65
66
67
# File 'lib/mgmg.rb', line 65

def min_comp(opt: Mgmg::Option.new)
	Mgmg::Equip.min_comp(self, opt:)
end

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



32
33
34
35
36
37
38
# File 'lib/mgmg.rb', line 32

def min_level(w=0, include_outsourcing=false, opt: Mgmg::Option.new)
	key = [self.dup.freeze, w, include_outsourcing, opt.left_associative].freeze
	return Mgmg::CacheMLS[key] if Mgmg::CacheMLS.has_key?(key)
	ret = __min_level_sub(w, include_outsourcing, opt)
	Mgmg::CacheMLS.store(key, ret)
	ret
end

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



56
57
58
# File 'lib/mgmg.rb', line 56

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



59
60
61
# File 'lib/mgmg.rb', line 59

def min_levels_max(w=1, opt: Mgmg::Option.new)
	min_levels(w, opt:).values.append(-1).max
end

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



62
63
64
# File 'lib/mgmg.rb', line 62

def min_smith(opt: Mgmg::Option.new)
	Mgmg::Equip.min_smith(self, opt:)
end

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



22
23
24
# File 'lib/mgmg.rb', line 22

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

#peff(para, smith, comp = smith, opt: Mgmg::Option.new) ⇒ Object



117
118
119
# File 'lib/mgmg.rb', line 117

def peff(para, smith, comp=smith, opt: Mgmg::Option.new)
	poly(para, opt:).eff(smith, comp)
end

#phydef_optimize(smith = nil, comp = smith, opt: Mgmg::Option.new) ⇒ Object



137
138
139
# File 'lib/mgmg.rb', line 137

def phydef_optimize(smith=nil, comp=smith, opt: Mgmg::Option.new)
	Mgmg::Optimize.phydef_optimize(self, smith, comp, opt:)
end

#poly(para = :cost, opt: Mgmg::Option.new) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/mgmg.rb', line 74

def poly(para=:cost, opt: Mgmg::Option.new)
	case para
	when :atkstr
		self.poly(:attack, opt:) + self.poly(:str, opt:)
	when :atk_sd
		self.poly(:attack, opt:) + self.poly(:str, opt:).quo(2) + self.poly(:dex, opt:).quo(2)
	when :dex_as
		self.poly(:dex, opt:) + self.poly(:attack, opt:).quo(2) + self.poly(:str, opt:).quo(2)
	when :mag_das
		self.poly(:magic, opt:) + self.poly(:dex_as, opt:).quo(2)
	when :magic2
		self.poly(:magic, opt:).scalar(2)
	when :magmag
		self.poly(:magdef, opt:) + self.poly(:magic, opt:).quo(2)
	when :pmdef
		pd = self.poly(:phydef, opt:)
		md = self.poly(:magmag, opt:)
		pd <= md ? pd : md
	when :hs
		self.poly(:hp, opt:) + self.poly(:str, opt:)
	when :cost
		if opt.include_system_equips and Mgmg::SystemEquip.has_key?(self) then
			return Mgmg::TPolynomial.new(Mgmg::Mat.new(1, 1, 0.quo(1)), 28, 0, 12, 12)
		end
		built = self.build(-1, opt:)
		const = (built.star**2) * ( /\+/.match(self) ? 5 : ( built.kind < 8 ? 2 : 1 ) )
		ret = poly(:attack, opt:) + poly(:phydef, opt:) + poly(:magdef, opt:)
		ret += poly(:hp, opt:).quo(4) + poly(:mp, opt:).quo(4)
		ret += poly(:str, opt:) + poly(:dex, opt:) + poly(:speed, opt:) + poly(:magic, opt:)
		ret.mat.body[0][0] += const
		ret
	else
		Mgmg::TPolynomial.build(self, para, left_associative: opt.left_associative, include_system_equips: opt.include_system_equips)
	end
end

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



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/mgmg/search.rb', line 71

def search(para, target, opt: Mgmg::Option.new)
	org_cut_exp = opt.cut_exp
	opt = opt.dup.set_default(self)
	opt_nocut = opt.dup; opt_nocut.cut_exp = Float::INFINITY
	begin
		opt.comp_min = comp_search(para, target, opt.smith_max, opt:)
	rescue Mgmg::SearchCutException
		foo = opt.irep.para_call(para, opt.smith_max, opt.comp_max)
		raise Mgmg::SearchCutException, "#{self} could not reach target=#{target} until (smith_max, comp_max)=(#{opt.smith_max}, #{opt.comp_max}), which yields #{foo.comma3}"
	end
	opt.smith_max = smith_search(para, target, opt.comp_min, opt: opt_nocut)
	opt.smith_min = smith_search(para, target, opt.comp_max, opt: opt_nocut)
	raise Mgmg::SearchCutException if opt.cut_exp < Mgmg.exp(opt.smith_min, opt.comp_min)
	opt.comp_max = comp_search(para, target, opt.smith_min, opt:)
	ret = nil
	exp = Mgmg.exp(opt.smith_min, opt.comp_max)
	opt.cut_exp, ret = exp, [opt.smith_min, opt.comp_max] if exp <= opt.cut_exp
	exp = Mgmg.exp(opt.smith_max, opt.comp_min)
	opt.cut_exp, ret = exp, [opt.smith_max, opt.comp_min] if ( exp < opt.cut_exp || (ret.nil? && 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(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(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(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(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(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,, comp = smith, para: :power, name: nil, opt: Mgmg::Option.new) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/mgmg.rb', line 120

def show(smith=-1, comp=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 if name.nil?
	built = build(smith, comp, opt:)
	pstr = '%.3f' % built.para_call(para)
	pstr.sub!(/\.?0+\Z/, '')
	puts "With levels (#{smith}, #{comp}: #{Mgmg.exp(smith, 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, comp, opt: Mgmg::Option.new) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/mgmg/search.rb', line 3

def smith_search(para, target, 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.invexp2(opt.cut_exp, comp)].min
		rescue
			raise Mgmg::SearchCutException
		end
	end
	if target <= opt.irep.para_call(para, opt.smith_min, comp)
		return opt.smith_min
	elsif opt.irep.para_call(para, opt.smith_max, comp) < target
		raise Mgmg::SearchCutException
	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, 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

Raises:



17
18
19
20
21
# File 'lib/mgmg.rb', line 17

def to_recipe(para=:power, allow_over20: false, **kw)
	ret = Mgmg::Recipe.new(self, para, **kw)
	raise Mgmg::Over20Error, ret.ir.star if (!allow_over20 and 20<ret.ir.star)
	ret
end