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)
def de_solve(CF): solver = DifferentialEvolutionSolver(ND, NP) solver.enable_signal_handler() stepmon = Monitor() solver.SetRandomInitialPoints(min=minrange, max=maxrange) solver.SetStrictRanges(min=minrange, max=maxrange) solver.SetEvaluationLimits(generations=MAX_GENERATIONS) solver.SetGenerationMonitor(stepmon) termination = ChangeOverGeneration(generations=generations) solver.Solve(CF, termination=termination, strategy=Rand1Exp, \ sigint_callback = plot_sol(solver)) solution = solver.Solution() return solution, stepmon
def main(): solver = DifferentialEvolutionSolver(ND, NP) solver.SetRandomInitialPoints(min=[-100.0] * ND, max=[100.0] * ND) solver.SetEvaluationLimits(generations=MAX_GENERATIONS) solver.SetGenerationMonitor(VerboseMonitor(30)) solver.enable_signal_handler() strategy = Best1Exp #strategy = Best1Bin solver.Solve(ChebyshevCost, termination=VTR(0.01), strategy=strategy, \ CrossProbability=1.0, ScalingFactor=0.9, \ sigint_callback=plot_solution) solution = solver.Solution() return solution
def de_solve(CF): solver = DifferentialEvolutionSolver(ND, NP) solver.enable_signal_handler() stepmon = VerboseMonitor(10,50) minrange = [-100., -100., -100.]; maxrange = [100., 100., 100.]; solver.SetRandomInitialPoints(min = minrange, max = maxrange) solver.SetStrictRanges(min = minrange, max = maxrange) solver.SetEvaluationLimits(generations=MAX_GENERATIONS) solver.SetGenerationMonitor(stepmon) solver.Solve(CF, termination=ChangeOverGeneration(generations=300),\ CrossProbability=0.5, ScalingFactor=0.5,\ sigint_callback=plot_sol) solution = solver.Solution() return solution, stepmon
def de_solve(): solver = DifferentialEvolutionSolver(ND, NP) stepmon = Monitor() minrange = [-1000., -1000., -100., -10.]; maxrange = [1000., 1000., 100., 10.]; solver.SetRandomInitialPoints(min = minrange, max = maxrange) solver.SetEvaluationLimits(generations=MAX_GENERATIONS) solver.SetGenerationMonitor(stepmon) #termination = VTR(0.0000029) termination = ChangeOverGeneration(generations=100) solver.Solve(cost_function, termination=termination, \ CrossProbability=0.5, ScalingFactor=0.5) solution = solver.Solution() return solution, stepmon
return constrain(x) from mystic.monitors import VerboseMonitor mon = VerboseMonitor(10) # solve the dual for alpha from mystic.solvers import DifferentialEvolutionSolver as DESolver from mystic.termination import Or, ChangeOverGeneration, CollapseAt ndim = len(lb) npop = nx * 3 stop = Or(ChangeOverGeneration(1e-8, 200), CollapseAt(0.0)) solver = DESolver(ndim, npop) solver.SetRandomInitialPoints(min=lb, max=_b) solver.SetStrictRanges(min=lb, max=ub) solver.SetGenerationMonitor(mon) solver.SetConstraints(conserve) solver.SetTermination(stop) solver.Solve(objective, ExtraArgs=(Q, b), disp=1) alpha = solver.bestSolution print 'solved x: ', alpha print "constraint A*x == 0: ", inner(Aeq, alpha) print "minimum 0.5*x'Qx + b'*x: ", solver.bestEnergy # calculate weight vectors, support vectors, and bias wv = WeightVector(alpha, X, y) sv1, sv2 = SupportVectors(alpha, y, eps=1e-6) bias = Bias(alpha, X, y) ym = (y.flatten() < 0).nonzero()[0]
def test_griewangk(): """Test Griewangk's function, which has many local minima. Testing Griewangk: Expected: x=[0.]*10 and f=0 Using DifferentialEvolutionSolver: Solution: [ 8.87516194e-09 7.26058147e-09 1.02076001e-08 1.54219038e-08 -1.54328461e-08 2.34589663e-08 2.02809360e-08 -1.36385836e-08 1.38670373e-08 1.59668900e-08] f value: 0.0 Iterations: 4120 Function evaluations: 205669 Time elapsed: 34.4936850071 seconds Using DifferentialEvolutionSolver2: Solution: [ -2.02709316e-09 3.22017968e-09 1.55275472e-08 5.26739541e-09 -2.18490470e-08 3.73725584e-09 -1.02315312e-09 1.24680355e-08 -9.47898116e-09 2.22243557e-08] f value: 0.0 Iterations: 4011 Function evaluations: 200215 Time elapsed: 32.8412370682 seconds """ print "Testing Griewangk:" print "Expected: x=[0.]*10 and f=0" from mystic.models import griewangk as costfunc ndim = 10 lb = [-400.]*ndim ub = [400.]*ndim maxiter = 10000 seed = 123 # Re-seed for each solver to have them all start at same x0 # DifferentialEvolutionSolver print "\nUsing DifferentialEvolutionSolver:" npop = 50 random_seed(seed) from mystic.solvers import DifferentialEvolutionSolver from mystic.termination import ChangeOverGeneration as COG from mystic.termination import CandidateRelativeTolerance as CRT from mystic.termination import VTR from mystic.strategy import Rand1Bin, Best1Bin, Rand1Exp esow = Monitor() ssow = Monitor() solver = DifferentialEvolutionSolver(ndim, npop) solver.SetRandomInitialPoints(lb, ub) solver.SetStrictRanges(lb, ub) solver.SetEvaluationLimits(generations=maxiter) solver.SetEvaluationMonitor(esow) solver.SetGenerationMonitor(ssow) solver.enable_signal_handler() #term = COG(1e-10) #term = CRT() term = VTR(0.) time1 = time.time() # Is this an ok way of timing? solver.Solve(costfunc, term, strategy=Rand1Exp, \ CrossProbability=0.3, ScalingFactor=1.0) sol = solver.Solution() time_elapsed = time.time() - time1 fx = solver.bestEnergy print "Solution: ", sol print "f value: ", fx print "Iterations: ", solver.generations print "Function evaluations: ", len(esow.x) print "Time elapsed: ", time_elapsed, " seconds" assert almostEqual(fx, 0.0, tol=3e-3) # DifferentialEvolutionSolver2 print "\nUsing DifferentialEvolutionSolver2:" npop = 50 random_seed(seed) from mystic.solvers import DifferentialEvolutionSolver2 from mystic.termination import ChangeOverGeneration as COG from mystic.termination import CandidateRelativeTolerance as CRT from mystic.termination import VTR from mystic.strategy import Rand1Bin, Best1Bin, Rand1Exp esow = Monitor() ssow = Monitor() solver = DifferentialEvolutionSolver2(ndim, npop) solver.SetRandomInitialPoints(lb, ub) solver.SetStrictRanges(lb, ub) solver.SetEvaluationLimits(generations=maxiter) solver.SetEvaluationMonitor(esow) solver.SetGenerationMonitor(ssow) #term = COG(1e-10) #term = CRT() term = VTR(0.) time1 = time.time() # Is this an ok way of timing? solver.Solve(costfunc, term, strategy=Rand1Exp, \ CrossProbability=0.3, ScalingFactor=1.0) sol = solver.Solution() time_elapsed = time.time() - time1 fx = solver.bestEnergy print "Solution: ", sol print "f value: ", fx print "Iterations: ", solver.generations print "Function evaluations: ", len(esow.x) print "Time elapsed: ", time_elapsed, " seconds" assert almostEqual(fx, 0.0, tol=3e-3)
def test_rosenbrock(): """Test the 2-dimensional Rosenbrock function. Testing 2-D Rosenbrock: Expected: x=[1., 1.] and f=0 Using DifferentialEvolutionSolver: Solution: [ 1.00000037 1.0000007 ] f value: 2.29478683682e-13 Iterations: 99 Function evaluations: 3996 Time elapsed: 0.582273006439 seconds Using DifferentialEvolutionSolver2: Solution: [ 0.99999999 0.99999999] f value: 3.84824937598e-15 Iterations: 100 Function evaluations: 4040 Time elapsed: 0.577210903168 seconds Using NelderMeadSimplexSolver: Solution: [ 0.99999921 1.00000171] f value: 1.08732211477e-09 Iterations: 70 Function evaluations: 130 Time elapsed: 0.0190329551697 seconds Using PowellDirectionalSolver: Solution: [ 1. 1.] f value: 0.0 Iterations: 28 Function evaluations: 859 Time elapsed: 0.113857030869 seconds """ print "Testing 2-D Rosenbrock:" print "Expected: x=[1., 1.] and f=0" from mystic.models import rosen as costfunc ndim = 2 lb = [-5.]*ndim ub = [5.]*ndim x0 = [2., 3.] maxiter = 10000 # DifferentialEvolutionSolver print "\nUsing DifferentialEvolutionSolver:" npop = 40 from mystic.solvers import DifferentialEvolutionSolver from mystic.termination import ChangeOverGeneration as COG from mystic.strategy import Rand1Bin esow = Monitor() ssow = Monitor() solver = DifferentialEvolutionSolver(ndim, npop) solver.SetInitialPoints(x0) solver.SetStrictRanges(lb, ub) solver.SetEvaluationLimits(generations=maxiter) solver.SetEvaluationMonitor(esow) solver.SetGenerationMonitor(ssow) term = COG(1e-10) time1 = time.time() # Is this an ok way of timing? solver.Solve(costfunc, term, strategy=Rand1Bin) sol = solver.Solution() time_elapsed = time.time() - time1 fx = solver.bestEnergy print "Solution: ", sol print "f value: ", fx print "Iterations: ", solver.generations print "Function evaluations: ", len(esow.x) print "Time elapsed: ", time_elapsed, " seconds" assert almostEqual(fx, 2.29478683682e-13, tol=3e-3) # DifferentialEvolutionSolver2 print "\nUsing DifferentialEvolutionSolver2:" npop = 40 from mystic.solvers import DifferentialEvolutionSolver2 from mystic.termination import ChangeOverGeneration as COG from mystic.strategy import Rand1Bin esow = Monitor() ssow = Monitor() solver = DifferentialEvolutionSolver2(ndim, npop) solver.SetInitialPoints(x0) solver.SetStrictRanges(lb, ub) solver.SetEvaluationLimits(generations=maxiter) solver.SetEvaluationMonitor(esow) solver.SetGenerationMonitor(ssow) term = COG(1e-10) time1 = time.time() # Is this an ok way of timing? solver.Solve(costfunc, term, strategy=Rand1Bin) sol = solver.Solution() time_elapsed = time.time() - time1 fx = solver.bestEnergy print "Solution: ", sol print "f value: ", fx print "Iterations: ", solver.generations print "Function evaluations: ", len(esow.x) print "Time elapsed: ", time_elapsed, " seconds" assert almostEqual(fx, 3.84824937598e-15, tol=3e-3) # NelderMeadSimplexSolver print "\nUsing NelderMeadSimplexSolver:" from mystic.solvers import NelderMeadSimplexSolver from mystic.termination import CandidateRelativeTolerance as CRT esow = Monitor() ssow = Monitor() solver = NelderMeadSimplexSolver(ndim) solver.SetInitialPoints(x0) solver.SetStrictRanges(lb, ub) solver.SetEvaluationLimits(generations=maxiter) solver.SetEvaluationMonitor(esow) solver.SetGenerationMonitor(ssow) term = CRT() time1 = time.time() # Is this an ok way of timing? solver.Solve(costfunc, term) sol = solver.Solution() time_elapsed = time.time() - time1 fx = solver.bestEnergy print "Solution: ", sol print "f value: ", fx print "Iterations: ", solver.generations print "Function evaluations: ", len(esow.x) print "Time elapsed: ", time_elapsed, " seconds" assert almostEqual(fx, 1.08732211477e-09, tol=3e-3) # PowellDirectionalSolver print "\nUsing PowellDirectionalSolver:" from mystic.solvers import PowellDirectionalSolver from mystic.termination import NormalizedChangeOverGeneration as NCOG esow = Monitor() ssow = Monitor() solver = PowellDirectionalSolver(ndim) solver.SetInitialPoints(x0) solver.SetStrictRanges(lb, ub) solver.SetEvaluationLimits(generations=maxiter) solver.SetEvaluationMonitor(esow) solver.SetGenerationMonitor(ssow) term = NCOG(1e-10) time1 = time.time() # Is this an ok way of timing? solver.Solve(costfunc, term) sol = solver.Solution() time_elapsed = time.time() - time1 fx = solver.bestEnergy print "Solution: ", sol print "f value: ", fx print "Iterations: ", solver.generations print "Function evaluations: ", len(esow.x) print "Time elapsed: ", time_elapsed, " seconds" assert almostEqual(fx, 0.0, tol=3e-3)
xinit = [0.8, 1.2, 0.7] # xinit = [0.8,1.2,1.7] #... better when using "bad" range min = [-0.999, -0.999, 0.999] #XXX: behaves badly when large range max = [200.001, 100.001, inf] #... for >=1 x0 out of bounds; (up xtol) # min = [-0.999, -0.999, -0.999] # max = [200.001, 100.001, inf] # min = [-0.999, -0.999, 0.999] #XXX: tight range and non-randomness # max = [2.001, 1.001, 1.001] #...: is _bad_ for DE solvers #print(diffev(rosen,xinit,NP,retall=0,full_output=0)) solver = DifferentialEvolutionSolver(len(xinit), NP) solver.SetInitialPoints(xinit) solver.SetStrictRanges(min, max) solver.SetEvaluationLimits(generations=MAX_GENERATIONS) solver.SetEvaluationMonitor(esow) solver.SetGenerationMonitor(ssow) solver.Solve(rosen, VTR(0.0001), \ CrossProbability=0.5, ScalingFactor=0.6, disp=1) sol = solver.bestSolution print(sol) #print("Current function value: %s" % solver.bestEnergy) #print("Iterations: %s" % solver.generations) #print("Function evaluations: %s" % solver.evaluations) times.append(time.time() - start) algor.append('Differential Evolution\t') for k, t in zip(algor, times): print("%s\t -- took %s" % (k, t)) #print(len(esow.x))