本文整理汇总了Python中mystic.solvers.fmin函数的典型用法代码示例。如果您正苦于以下问题:Python fmin函数的具体用法?Python fmin怎么用?Python fmin使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了fmin函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_penalize
def test_penalize():
from mystic.math.measures import mean, spread
def mean_constraint(x, target):
return mean(x) - target
def range_constraint(x, target):
return spread(x) - target
@quadratic_equality(condition=range_constraint, kwds={'target':5.0})
@quadratic_equality(condition=mean_constraint, kwds={'target':5.0})
def penalty(x):
return 0.0
def cost(x):
return abs(sum(x) - 5.0)
from mystic.solvers import fmin
from numpy import array
x = array([1,2,3,4,5])
y = fmin(cost, x, penalty=penalty, disp=False)
assert round(mean(y)) == 5.0
assert round(spread(y)) == 5.0
assert round(cost(y)) == 4*(5.0)
示例2: run_once
def run_once():
simplex = Monitor()
solver = fmin(2)
solver.SetRandomInitialPoints([0,0],[2,2])
solver.SetGenerationMonitor(simplex)
solver.Solve(Corana2, termination=CRT())
sol = solver.Solution()
for x in simplex.x:
sam.putarray('x',x)
sam.eval("plot(x([1,2,3,1],1),x([1,2,3,1],2),'w-')")
示例3: run_once
def run_once(x0,x1):
simplex = Monitor()
xinit = [x0, x1]
solver = fmin(len(xinit))
solver.SetInitialPoints(xinit)
solver.SetGenerationMonitor(simplex)
solver.Solve(rosen, termination=CRT())
sol = solver.Solution()
for x in simplex.x:
sam.putarray('x',x)
sam.eval("plot(x([1,2,3,1],1),x([1,2,3,1],2),'w-')")
示例4: mystic_optimize
def mystic_optimize(point):
from mystic.monitors import Monitor, VerboseMonitor
from mystic.solvers import NelderMeadSimplexSolver as fmin
from mystic.termination import CandidateRelativeTolerance as CRT
simplex, esow = VerboseMonitor(50), Monitor()
solver = fmin(len(point))
solver.SetInitialPoints(point)
min = [-100,-100,-100]; max = [100,100,100]
solver.SetStrictRanges(min,max)
solver.SetEvaluationMonitor(esow)
solver.SetGenerationMonitor(simplex)
solver.Solve(cost_function, CRT(1e-7,1e-7))
solution = solver.Solution()
return solution
示例5: run_once_xv
def run_once_xv():
simplex = Monitor()
y1 = y0*random.uniform(0.5,1.5)
z1 = z0*random.uniform(0.5,1.5)
xinit = [random.uniform(x0-40,x0+40), y1, z1, random.uniform(v0-0.1,v0+0.1)]
solver = fmin(len(xinit))
solver.SetInitialPoints(xinit)
solver.SetGenerationMonitor(simplex)
solver.Solve(cost_function, termination=CRT())
sol = solver.Solution()
print(sol)
for x in simplex.x:
sam.putarray('x',x)
sam.eval("plot(x([1,2,3,1],1),x([1,2,3,1],2),'w-','LineWidth',2)")
return sol
示例6: test_with_penalty
def test_with_penalty():
from mystic.math.measures import mean, spread
@with_penalty(quadratic_equality, kwds={'target':5.0})
def penalty(x, target):
return mean(x) - target
def cost(x):
return abs(sum(x) - 5.0)
from mystic.solvers import fmin
from numpy import array
x = array([1,2,3,4,5])
y = fmin(cost, x, penalty=penalty, disp=False)
assert round(mean(y)) == 5.0
assert round(cost(y)) == 4*(5.0)
示例7: test_as_penalty
def test_as_penalty():
from mystic.math.measures import mean, spread
@with_spread(5.0)
@with_mean(5.0)
def constraint(x):
return x
penalty = as_penalty(constraint)
from numpy import array
x = array([1,2,3,4,5])
def cost(x):
return abs(sum(x) - 5.0)
from mystic.solvers import fmin
y = fmin(cost, x, penalty=penalty, disp=False)
assert round(mean(y)) == 5.0
assert round(spread(y)) == 5.0
assert round(cost(y)) == 4*(5.0)
示例8: main
MAX_GENERATIONS = 2500
def main():
solver = DifferentialEvolutionSolver(ND, NP)
solver.SetRandomInitialPoints(min = [-1.28]*ND, max = [1.28]*ND)
solver.SetEvaluationLimits(generations=MAX_GENERATIONS)
solver.Solve(DeJong4, termination=VTR(15), strategy=Rand1Exp, \
CrossProbability=0.3, ScalingFactor=1.0)
solution = solver.Solution()
print(solution)
if __name__ == '__main__':
from timeit import Timer
# optimize with DESolver
t = Timer("main()", "from __main__ import main")
timetaken = t.timeit(number=1)
print("CPU Time: %s\n" % timetaken)
# optimize with fmin
from mystic.solvers import fmin
print(fmin(DeJong4, [0 for i in range(ND)]))
# end of file
示例9: print
xval.append(params[0])
yval.append(params[1])
zval.append(params[2])
pylab.plot(step,xval,'b-')
pylab.plot(step,yval,'g-')
pylab.plot(step,zval,'r-')
pylab.legend(["x", "y", "z"])
pylab.draw()
iter += 1
return
if __name__ == '__main__':
# initial guess
x0 = [0.8,1.2,0.7]
# suggest that the user interacts with the solver
print("NOTE: while solver is running, press 'Ctrl-C' in console window")
getch()
plot_frame()
# use Nelder-Mead to minimize the Rosenbrock function
solution = fmin(rosen,x0,disp=1,callback=plot_params,handler=True)
print(solution)
# don't exit until user is ready
getch()
# end of file
示例10: print
solution = solver.Solution()
print(solution)
if __name__ == '__main__':
from timeit import Timer
t = Timer("main()", "from __main__ import main")
timetaken = t.timeit(number=1)
print("CPU Time: %s" % timetaken)
from mystic.monitors import Monitor
from mystic.solvers import NelderMeadSimplexSolver as fmin
from mystic.termination import CandidateRelativeTolerance as CRT
import random
simplex = Monitor()
esow = Monitor()
xinit = [random.uniform(0,5) for j in range(ND)]
solver = fmin(len(xinit))
solver.SetInitialPoints(xinit)
solver.SetEvaluationMonitor(esow)
solver.SetGenerationMonitor(simplex)
solver.Solve(CostFunction, CRT())
sol = solver.Solution()
print("fmin solution: %s" % sol)
# end of file
示例11: main
def main():
solver = DifferentialEvolutionSolver(ND, NP)
solver.SetRandomInitialPoints(min = [-400.0]*ND, max = [400.0]*ND)
solver.SetEvaluationLimits(generations=MAX_GENERATIONS)
solver.Solve(Griewangk_cost, termination=VTR(0.00001), strategy=Rand1Exp,\
CrossProbability=0.3, ScalingFactor=1.0)
solution = solver.Solution()
print solution
if __name__ == '__main__':
from mystic.solvers import fmin
from timeit import Timer
t = Timer("main()", "from __main__ import main")
timetaken = t.timeit(number=1)
print "CPU Time: %s" % timetaken
import random
print "Scipy fmin"
for i in [400,200,100,40,20,10,4,2,1]:
print "\ninitializing with range (-%d, %d)" % (i,i)
sol = fmin(Griewangk_cost, [random.uniform(-i,i) for j in range(10)])
print "sol: ", sol
print "cost: ", Griewangk_cost(sol)
# end of file
示例12: main
MAX_GENERATIONS = 2500
def main():
solver = DifferentialEvolutionSolver(ND, NP)
solver.SetRandomInitialPoints(min = [-5.12]*ND, max = [5.12]*ND)
solver.SetEvaluationLimits(generations=MAX_GENERATIONS)
solver.Solve(DeJong3, termination=VTR(0.00001), \
CrossProbability=0.3, ScalingFactor=1.0)
solution = solver.Solution()
print solution
if __name__ == '__main__':
from timeit import Timer
# optimize with DESolver
t = Timer("main()", "from __main__ import main")
timetaken = t.timeit(number=1)
print "CPU Time: %s\n" % timetaken
# optimize with fmin
from mystic.solvers import fmin
print fmin(DeJong3, [0 for i in range(ND)])
# end of file
示例13: print
legend = ['random points','generating circle : %f' % R0]
pylab.axis('equal')
# solve with mystic's differential evolution solver
solution = solver.Solution()
sx, sy, sr = solution
print("DEsol : (%f, %f) @ R = %f" % (sx, sy, sr))
# plot DEsolver solution
c = circle(sx, sy, sr)
pylab.plot(c[:,0],c[:,1],'b-',linewidth=2)
legend.append('DE optimal : %f' % sr)
# solve with scipy.fmin
from mystic.solvers import fmin
sol = fmin(cost, guess)
print("scipy.fmin sol: %s" % sol)
ax, ay, ar = sol
# plot scipy.fmin solution
c = circle(ax, ay, ar)
pylab.plot(c[:,0],c[:,1],'g-',linewidth=2)
legend.append('Nelder-Mead : %f' % ar)
# solve with scipy.brute
#from mystic._scipyoptimize import brute
#ranges = tuple(zip(minrange,maxrange))
#sol = brute(cost, ranges, Ns=NP)
#print("scipy.brute sol: %s" % sol)
#bx, by, br = sol
示例14: fmin
from mystic.solvers import fmin
# Rosenbrock function
from mystic.models import rosen
# tools
import pylab
if __name__ == '__main__':
# initial guess
x0 = [0.8,1.2,0.7]
# use Nelder-Mead to minimize the Rosenbrock function
solution = fmin(rosen,x0,disp=0,retall=1)
allvecs = solution[-1]
# plot the parameter trajectories
pylab.plot([i[0] for i in allvecs])
pylab.plot([i[1] for i in allvecs])
pylab.plot([i[2] for i in allvecs])
# draw the plot
pylab.title("Rosenbrock parameter convergence")
pylab.xlabel("Nelder-Mead solver iterations")
pylab.ylabel("parameter value")
pylab.legend(["x", "y", "z"])
pylab.show()
# end of file
示例15: VerboseMonitor
stepmon = VerboseMonitor(1)
solver.SetGenerationMonitor(stepmon)
#solver.SetReducer(sum, arraylike=True) # reduce wavy's multi-valued return
solver.Solve(wavy, ChangeOverGeneration(generations=50), \
strategy=strategy, CrossProbability=1.0, ScalingFactor=0.9, \
sigint_callback = plot_solution)
solution = solver.Solution()
return solution, solver
if __name__ == '__main__':
#solution = main()
scipysol = fmin(wavy, [0.1])
desol, solver = main()
#plot_solution(scipysol)
#plot_solution(desol)
print("fmin: %s %s" % (scipysol, wavy(scipysol)))
print("dife: %s %s" % (desol, wavy(desol)))
try:
import pylab
x = arange(-40,40,0.01)
pylab.plot(x,wavy(x))
pylab.plot(scipysol, wavy(scipysol), 'r+',markersize=8)
pylab.plot(desol, wavy(desol), 'bo',markersize=8)
pylab.legend(('|x + 3 sin(x+pi)|','fmin','dife'))
if hasattr(solver, 'genealogy'):
xx = solver.genealogy
pylab.plot(xx[4], wavy(xx[4]), 'g-',markersize=3)