lib/mopti/nelder_mead.rb in mopti-0.2.2 vs lib/mopti/nelder_mead.rb in mopti-0.2.3
- old
+ new
@@ -59,11 +59,11 @@
def each
return to_enum(__method__) unless block_given?
x = @x_init.dup
n = x.size
- max_iter = @max_iter || 200 * n
+ max_iter = @max_iter || (200 * n)
alpha = 1.0
beta = n > 1 ? 1 + 2.fdiv(n) : 2.0
gamma = n > 1 ? 0.75 - 1.fdiv(2 * n) : 0.5
delta = n > 1 ? 1 - 1.fdiv(n) : 0.5
@@ -88,17 +88,17 @@
n_iter = 0
while n_iter < max_iter
break if ((sim[1..-1, true] - sim[0, true]).abs.flatten.max <= @xtol) && ((fsim[0] - fsim[1..-1]).abs.max <= @ftol)
xbar = sim[0...-1, true].sum(0) / n
- xr = xbar + alpha * (xbar - sim[-1, true])
+ xr = xbar + (alpha * (xbar - sim[-1, true]))
fr = func(xr, @args)
n_fev += 1
shrink = true
if fr < fsim[0]
- xe = xbar + beta * (xr - xbar)
+ xe = xbar + (beta * (xr - xbar))
fe = func(xe, @args)
n_fev += 1
shrink = false
if fe < fr
sim[-1, true] = xe
@@ -110,20 +110,20 @@
elsif fr < fsim[-2]
shrink = false
sim[-1, true] = xr
fsim[-1] = fr
elsif fr < fsim[-1]
- xoc = xbar + gamma * (xr - xbar)
+ xoc = xbar + (gamma * (xr - xbar))
foc = func(xoc, @args)
n_fev += 1
if foc <= fr
shrink = false
sim[-1, true] = xoc
fsim[-1] = foc
end
else
- xic = xbar - gamma * (xr - xbar)
+ xic = xbar - (gamma * (xr - xbar))
fic = func(xic, @args)
n_fev += 1
if fic < fsim[-1]
shrink = false
sim[-1, true] = xic
@@ -131,10 +131,10 @@
end
end
if shrink
(1..n).to_a.each do |j|
- sim[j, true] = sim[0, true] + delta * (sim[j, true] - sim[0, true])
+ sim[j, true] = sim[0, true] + (delta * (sim[j, true] - sim[0, true]))
fsim[j] = func(sim[j, true], @args)
n_fev += 1
end
end