def optimize(cost,_bounds,_constraints): from mystic.solvers import DifferentialEvolutionSolver2 from mystic.termination import ChangeOverGeneration as COG from mystic.strategy import Best1Exp from mystic.monitors import VerboseMonitor, Monitor from mystic.tools import random_seed from mystic.termination import Or, CollapseWeight, CollapsePosition, state if debug: random_seed(123) # or 666 to force impose_unweighted reweighting stepmon = VerboseMonitor(1,1) else: stepmon = VerboseMonitor(10) if verbose else Monitor() stepmon._npts = npts evalmon = Monitor() lb,ub = _bounds ndim = len(lb) solver = DifferentialEvolutionSolver2(ndim,npop) solver.SetRandomInitialPoints(min=lb,max=ub) solver.SetStrictRanges(min=lb,max=ub) solver.SetEvaluationLimits(maxiter,maxfun) solver.SetEvaluationMonitor(evalmon) solver.SetGenerationMonitor(stepmon) solver.SetConstraints(_constraints) tol = convergence_tol term = Or(COG(tol,ngen), CollapseWeight(), CollapsePosition()) solver.Solve(cost,termination=term,strategy=Best1Exp, disp=verbose, \ CrossProbability=crossover,ScalingFactor=percent_change) #while collapse and solver.Collapse(verbose): #XXX: total_evaluations? # if debug: print(state(solver._termination).keys()) # solver.Solve() #XXX: cost, term, strategy, cross, scale ? # if debug: solver.SaveSolver('debug.pkl') solved = solver.bestSolution #print("solved: %s" % solver.Solution()) func_max = MINMAX * solver.bestEnergy #NOTE: -solution assumes -Max #func_max = 1.0 + MINMAX*solver.bestEnergy #NOTE: 1-sol => 1-success = fail func_evals = solver.evaluations from mystic.munge import write_support_file write_support_file(stepmon, npts=npts) return solved, func_max, func_evals
def optimize(cost, _bounds, _constraints): from mystic.solvers import DifferentialEvolutionSolver2 from mystic.termination import ChangeOverGeneration as COG from mystic.strategy import Best1Exp from mystic.monitors import VerboseMonitor, Monitor from mystic.tools import random_seed #random_seed(123) stepmon = VerboseMonitor(2) #stepmon = Monitor() evalmon = Monitor() lb, ub = _bounds ndim = len(lb) solver = DifferentialEvolutionSolver2(ndim, npop) solver.SetRandomInitialPoints(min=lb, max=ub) solver.SetStrictRanges(min=lb, max=ub) solver.SetEvaluationLimits(maxiter, maxfun) solver.SetEvaluationMonitor(evalmon) solver.SetGenerationMonitor(stepmon) solver.SetConstraints(_constraints) tol = convergence_tol solver.Solve(cost,termination=COG(tol,ngen),strategy=Best1Exp, \ CrossProbability=crossover,ScalingFactor=percent_change) solved = solver.bestSolution #print("solved: %s" % solver.Solution()) func_max = MINMAX * solver.bestEnergy #NOTE: -solution assumes -Max #func_max = 1.0 + MINMAX*solver.bestEnergy #NOTE: 1-sol => 1-success = fail func_evals = solver.evaluations from mystic.munge import write_support_file write_support_file(stepmon, npts=npts) return solved, func_max, func_evals
def optimize(cost,_bounds,_constraints): from mystic.solvers import DifferentialEvolutionSolver2 from mystic.termination import ChangeOverGeneration as COG from mystic.strategy import Best1Exp from mystic.monitors import VerboseMonitor, Monitor from mystic.tools import random_seed #random_seed(123) stepmon = VerboseMonitor(2) #stepmon = Monitor() evalmon = Monitor() lb,ub = _bounds ndim = len(lb) solver = DifferentialEvolutionSolver2(ndim,npop) solver.SetRandomInitialPoints(min=lb,max=ub) solver.SetStrictRanges(min=lb,max=ub) solver.SetEvaluationLimits(maxiter,maxfun) solver.SetEvaluationMonitor(evalmon) solver.SetGenerationMonitor(stepmon) solver.SetConstraints(_constraints) tol = convergence_tol solver.Solve(cost,termination=COG(tol,ngen),strategy=Best1Exp, \ CrossProbability=crossover,ScalingFactor=percent_change) solved = solver.bestSolution #print("solved: %s" % solver.Solution()) func_max = MINMAX * solver.bestEnergy #NOTE: -solution assumes -Max #func_max = 1.0 + MINMAX*solver.bestEnergy #NOTE: 1-sol => 1-success = fail func_evals = solver.evaluations from mystic.munge import write_support_file write_support_file(stepmon, npts=npts) return solved, func_max, func_evals
plot_exact() # configure monitor stepmon = VerboseLoggingMonitor(1, 2) # use buckshot-Powell to solve 8th-order Chebyshev coefficients solver = BuckshotSolver(ndim, npts) solver.SetNestedSolver(PowellDirectionalSolver) solver.SetMapper(Pool().map) solver.SetGenerationMonitor(stepmon) solver.SetStrictRanges(min=[-300] * ndim, max=[300] * ndim) solver.Solve(chebyshev8cost, NCOG(1e-4), disp=1) solution = solver.Solution() shutdown() # help multiprocessing shutdown all workers # write 'convergence' support file from mystic.munge import write_support_file write_support_file(solver._stepmon) #XXX: only saves the 'best' # use pretty print for polynomials print(poly1d(solution)) # compare solution with actual 8th-order Chebyshev coefficients print("\nActual Coefficients:\n %s\n" % poly1d(chebyshev8coeffs)) # plot solution versus exact coefficients plot_solution(solution) getch() # end of file
#monitor = Monitor(all=True) #monitor = Monitor(all=False) #monitor = VerboseMonitor(1,1) #monitor = VerboseMonitor(1,1, all=True) #monitor = VerboseMonitor(1,1, all=False) #monitor = VerboseMonitor(0,1) monitor = VerboseMonitor(1, 0) #monitor = LoggingMonitor(1) #monitor = LoggingMonitor(1, all=True) #monitor = LoggingMonitor(1, all=False) #monitor = VerboseLoggingMonitor(1) #monitor = VerboseLoggingMonitor(0,1) #test0(monitor) #test1(monitor) test2(monitor) # GenerationMonitor works like test0 #test2(monitor, diffenv=False) # (to make like test1, add enclosing []) #test2(monitor, diffenv=True) # these are for "MonitorPlotter(s)"; need to adapt log.py plotters for test1 write_support_file(monitor, 'paramlog1.py') # plot with 'support_*.py' #write_converge_file(monitor,'paramlog2.py') #XXX: no existing plotters? #write_raw_file(monitor,'paramlog3.py') #XXX: no existing plotters? import os for fname in ('paramlog1.py', ): if os.path.exists(fname): os.remove(fname) # EOF
#monitor = Monitor(all=False) #monitor = VerboseMonitor(1,1) #monitor = VerboseMonitor(1,1, all=True) #monitor = VerboseMonitor(1,1, all=False) #monitor = VerboseMonitor(0,1) monitor = VerboseMonitor(1,0) #monitor = LoggingMonitor(1) #monitor = LoggingMonitor(1, all=True) #monitor = LoggingMonitor(1, all=False) #monitor = VerboseLoggingMonitor(1) #monitor = VerboseLoggingMonitor(0,1) #test0(monitor) #test1(monitor) test2(monitor) # GenerationMonitor works like test0 #test2(monitor, diffenv=False) # (to make like test1, add enclosing []) #test2(monitor, diffenv=True) # these are for "MonitorPlotter(s)"; need to adapt log.py plotters for test1 write_support_file(monitor,'paramlog1.py') # plot with 'support_*.py' #write_converge_file(monitor,'paramlog2.py') #XXX: no existing plotters? #write_raw_file(monitor,'paramlog3.py') #XXX: no existing plotters? import os for fname in ('paramlog1.py',): if os.path.exists(fname): os.remove(fname) # EOF
# draw frame and exact coefficients plot_exact() # configure monitor stepmon = VerboseLoggingMonitor(1,2) # use buckshot-Powell to solve 8th-order Chebyshev coefficients solver = BuckshotSolver(ndim, npts) solver.SetNestedSolver(PowellDirectionalSolver) solver.SetMapper(Pool().map) solver.SetGenerationMonitor(stepmon) solver.SetStrictRanges(min=[-300]*ndim, max=[300]*ndim) solver.Solve(chebyshev8cost, NCOG(1e-4), disp=1) solution = solver.Solution() # write 'convergence' support file from mystic.munge import write_support_file write_support_file(solver._stepmon) #XXX: only saves the 'best' # use pretty print for polynomials print poly1d(solution) # compare solution with actual 8th-order Chebyshev coefficients print "\nActual Coefficients:\n %s\n" % poly1d(chebyshev8coeffs) # plot solution versus exact coefficients plot_solution(solution) getch() # end of file