• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Python solvers.fmin函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中mystic.solvers.fmin函数的典型用法代码示例。如果您正苦于以下问题:Python fmin函数的具体用法?Python fmin怎么用?Python fmin使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了fmin函数的19个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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)
开发者ID:uqfoundation,项目名称:mystic,代码行数:25,代码来源:test_constraints.py


示例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-')")
开发者ID:jcfr,项目名称:mystic,代码行数:11,代码来源:sam_corana2.py


示例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-')")
开发者ID:Magellen,项目名称:mystic,代码行数:13,代码来源:sam_rosenbrock.py


示例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
开发者ID:Magellen,项目名称:mystic,代码行数:14,代码来源:rosetta_parabola.py


示例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
开发者ID:Magellen,项目名称:mystic,代码行数:17,代码来源:sam_mogi.py


示例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)
开发者ID:uqfoundation,项目名称:mystic,代码行数:17,代码来源:test_constraints.py


示例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)
开发者ID:uqfoundation,项目名称:mystic,代码行数:22,代码来源:test_constraints.py


示例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
开发者ID:Magellen,项目名称:mystic,代码行数:30,代码来源:test_dejong4.py


示例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
开发者ID:Magellen,项目名称:mystic,代码行数:30,代码来源:example04.py


示例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
开发者ID:Magellen,项目名称:mystic,代码行数:30,代码来源:test_zimmermann.py


示例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
开发者ID:jcfr,项目名称:mystic,代码行数:30,代码来源:test_griewangk.py


示例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
开发者ID:cdeil,项目名称:mystic,代码行数:30,代码来源:test_dejong3.py


示例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
开发者ID:Magellen,项目名称:mystic,代码行数:30,代码来源:test_circle.py


示例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
开发者ID:cdeil,项目名称:mystic,代码行数:31,代码来源:example03.py


示例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)
开发者ID:Magellen,项目名称:mystic,代码行数:31,代码来源:test_wavy.py


示例16: almostEqual

    # test iters <= maxiter
    assert my_x[2] <= maxiter
  return 

if __name__ == '__main__':
  x0 = [0,0,0]

  # check solutions versus results based on the random_seed
# print "comparing against known results"
  sol = solvers.diffev(rosen, x0, npop=40, disp=0, full_output=True)
  assert almostEqual(sol[1], 0.0020640145337293249, tol=3e-3)
  sol = solvers.diffev2(rosen, x0, npop=40, disp=0, full_output=True)
  assert almostEqual(sol[1], 0.0017516784703663288, tol=3e-3)
  sol = solvers.fmin_powell(rosen, x0, disp=0, full_output=True)
  assert almostEqual(sol[1], 8.3173488898295291e-23)
  sol = solvers.fmin(rosen, x0, disp=0, full_output=True)
  assert almostEqual(sol[1], 1.1605792769954724e-09)

  solver2 = 'diffev2'
  for solver in ['diffev']:
#   print "comparing %s and %s from mystic" % (solver, solver2)
    test_solvers(solver, solver2, x0, npop=40)
    test_solvers(solver, solver2, x0, npop=40, maxiter=None, maxfun=0)
    test_solvers(solver, solver2, x0, npop=40, maxiter=None, maxfun=1)
    test_solvers(solver, solver2, x0, npop=40, maxiter=None, maxfun=2)
    test_solvers(solver, solver2, x0, npop=40, maxiter=None, maxfun=9)
    test_solvers(solver, solver2, x0, npop=40, maxiter=0)
    test_solvers(solver, solver2, x0, npop=40, maxiter=1)
    test_solvers(solver, solver2, x0, npop=40, maxiter=2)
    test_solvers(solver, solver2, x0, npop=40, maxiter=9)
开发者ID:cdeil,项目名称:mystic,代码行数:30,代码来源:test_solver_compare.py


示例17: print

    solver.Solve(corana, termination=VTR(0.00000001), strategy=Rand1Exp,\
                 CrossProbability=0.5, ScalingFactor=0.9)

    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)

    try:
        from mystic.solvers import fmin
       #from mystic._scipyoptimize import fmin
        import random
        print( "\nScipy: ")
        sol = fmin(corana, [random.random() for j in range(4)], full_output=0, retall=1)
        print("solution: %s" % sol[-1][0])
        print("\nCorana 1 with Scipy")
        sol = fmin(corana1, [random.random()], full_output=1, retall=1)
        print("solution: %s" % sol[-1][0])
    except:
        pass

# end of file
开发者ID:uqfoundation,项目名称:mystic,代码行数:29,代码来源:test_corana.py


示例18: McKerns

#!/usr/bin/env python
#
# Author: Mike McKerns (mmckerns @caltech and @uqfoundation)
# Copyright (c) 1997-2014 California Institute of Technology.
# License: 3-clause BSD.  The full license text is available at:
#  - http://trac.mystic.cacr.caltech.edu/project/mystic/browser/mystic/LICENSE
"""
Testing the polynomial fitting problem of [1] using scipy's Nelder-Mead algorithm.

Reference:

[1] Storn, R. and Price, K. Differential Evolution - A Simple and Efficient
Heuristic for Global Optimization over Continuous Spaces. Journal of Global
Optimization 11: 341-359, 1997.
"""

from test_ffit import Chebyshev8, ChebyshevCost, plot_solution, print_solution

if __name__ == '__main__':
    from mystic.solvers import fmin
   #from mystic._scipyoptimize import fmin
    import random
    random.seed(123)
    x = [random.uniform(-100,100) + Chebyshev8[i] for i in range(9)]
    solution = fmin(ChebyshevCost, x)
    print_solution(solution)
    plot_solution(solution)

# end of file
开发者ID:cdeil,项目名称:mystic,代码行数:29,代码来源:test_ffitC.py


示例19: fmin

from mystic.models import rosen

# tools
import pylab


if __name__ == '__main__':

    print "Nelder-Mead Simplex"
    print "==================="

    # initial guess
    x0 = [0.8,1.2,0.7]

    # use Nelder-Mead to minimize the Rosenbrock function
    solution = fmin(rosen,x0)
    print solution
 
    # plot the Rosenbrock function (one plot per axis)
    x = [0.01*i for i in range(200)]
    pylab.plot(x,[rosen([i,1.,1.]) for i in x])
    pylab.plot(x,[rosen([1.,i,1.]) for i in x])
    pylab.plot(x,[rosen([1.,1.,i]) for i in x])

    # plot the solved minimum (for x)
    pylab.plot([solution[0]],[rosen(solution)],'bo')

    # draw the plot
    pylab.title("minimium of Rosenbrock's function")
    pylab.xlabel("x, y, z")
    pylab.ylabel("f(i) = Rosenbrock's function")
开发者ID:jcfr,项目名称:mystic,代码行数:31,代码来源:example02.py



注:本文中的mystic.solvers.fmin函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python solvers.fmin_powell函数代码示例发布时间:2022-05-27
下一篇:
Python solvers.diffev2函数代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap