def main(): solver = DifferentialEvolutionSolver(ND, NP) solver.SetRandomInitialPoints() solver.SetEvaluationLimits(generations=MAX_GENERATIONS) solver.SetGenerationMonitor(VerboseMonitor(10)) #strategy = Best1Exp #strategy = Best1Bin #strategy = Best2Bin strategy = Best2Exp solver.Solve(ChebyshevCost, termination=VTR(0.0001), strategy=strategy, \ CrossProbability=1.0, ScalingFactor=0.6) solution = solver.Solution() print("\nsolved: ") print(poly1d(solution)) print("\ntarget: ") print(poly1d(Chebyshev16)) #print("actual coefficients vs computed:") #for actual,computed in zip(Chebyshev16, solution): # print("%f %f" % (actual, computed)) plot_solution(solution, Chebyshev16)
#!/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
[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 mystic.solvers import diffev from test_ffit import plot_solution, print_solution, Chebyshev8, ChebyshevCost from mystic.tools import random_seed random_seed(123) ND = 9 NP = ND * 10 MAX_GENERATIONS = ND * NP def main(): range = [(-100.0, 100.0)] * ND solution = diffev(ChebyshevCost,range,NP,bounds=None,ftol=0.01,\ maxiter=MAX_GENERATIONS,cross=1.0,scale=0.9) return solution if __name__ == '__main__': #plot_solution(Chebyshev8) solution = main() print_solution(solution) plot_solution(solution) # end of file