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 main(start,end,filename): #Import Experimental Data [Ref,p_su_exp,rp_exp,N_exp,Wdot_exp,eta_is_exp] = Import(start,end,filename,sheet_num = 0) data = np.array([rp_exp,N_exp,p_su_exp]) #Set solver ND = 13 NP = ND*10 MAX_GENERATIONS = 3000 minrange = [-10,-100,-10,-10,-10,-10,-10,0,0,-100,-10,0,0] maxrange = [10,1,1,10,1,1,10,10,10,5,10,0.8,5000] solver = DifferentialEvolutionSolver(ND, NP) solver.SetRandomInitialPoints(min = [0.1]*ND, max = [5]*ND) solver.SetStrictRanges(min=minrange, max=maxrange) solver.SetEvaluationLimits(generations=MAX_GENERATIONS) pf = CalibrationPacejkaEq(data, eta_is_exp, nnum = 13, nden = 1) solver.Solve(pf.function, termination=VTR(1e-8), strategy=Rand1Exp,\ CrossProbability=0.9, ScalingFactor=0.9) coeff_solution = solver.Solution() print 'DE coefficients:', coeff_solution eta_is_exp_fit = pf.eval(coeff_solution) parity_plot(eta_is_exp_fit,eta_is_exp,Ref) return pf.eval(coeff_solution)
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
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
def main(): solver = DifferentialEvolutionSolver(ND, NP) solver.SetRandomInitialPoints(min = [-65.536]*ND, max = [65.536]*ND) solver.SetEvaluationLimits(generations=MAX_GENERATIONS) solver.Solve(DeJong5, termination=VTR(0.0000001), strategy=Rand1Exp, \ CrossProbability=0.5, ScalingFactor=0.9) solution = solver.Solution() print solution
def main(self, *args, **kwds): solver = DifferentialEvolutionSolver(self.mod.ND, self.mod.NP) costfunction = self.mod.cost termination = self.mod.termination MAX_GENERATIONS = self.mod.MAX_GENERATIONS solver.SetRandomInitialPoints(min=self.mod.min, max=self.mod.max) solver.SetEvaluationLimits(generations=MAX_GENERATIONS) solver.Solve(costfunction, termination, strategy=self.strategy,\ CrossProbability=self.probability, \ ScalingFactor=self.scale) self.solution = solver.Solution() return
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)
def main(): solver = DifferentialEvolutionSolver(ND, NP) solver.SetRandomInitialPoints(min=[-2.0] * ND, max=[2.0] * ND) solver.SetEvaluationLimits(generations=MAX_GENERATIONS) strategy = Best1Exp #strategy = Best1Bin solver.Solve(fOsc3D,termination=ChangeOverGeneration(1e-5, 30), \ strategy=strategy,CrossProbability=1.0,ScalingFactor=0.9) return solver.Solution()
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=[0] * ND, max=[2] * ND) solver.SetEvaluationLimits(generations=MAX_GENERATIONS) solver.Solve(rosen, termination = VTR(0.0001), \ CrossProbability=0.5, ScalingFactor=0.6, disp=1) solution = solver.bestSolution #print("Current function value: %s" % solver.bestEnergy) #print("Iterations: %s" % solver.generations) #print("Function evaluations: %s" % solver.evaluations) print(solution)
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 main(): solver = DifferentialEvolutionSolver(ND, NP) solver.SetRandomInitialPoints(min=[-100.0] * ND, max=[100.0] * ND) solver.SetEvaluationLimits(generations=MAX_GENERATIONS) solver.enable_signal_handler() strategy = Best1Bin stepmon = VerboseMonitor(1) solver.SetGenerationMonitor(stepmon) solver.Solve(wavy, ChangeOverGeneration(generations=50), \ strategy=strategy, CrossProbability=1.0, ScalingFactor=0.9, \ sigint_callback = plot_solution) solution = solver.Solution() return solution, solver
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
def de_solve(CF, a4=None, a5=None): """solve with DE for given cost funciton; fix a4 and/or a5 if provided""" minrange = [0, 100, 100, 1, 1] maxrange = [100, 2000, 200, 200, 200] interval = 10 if a5 != None: minrange[4] = maxrange[4] = a5 interval = 20 if a4 != None: minrange[3] = maxrange[3] = a4 if interval == 20: interval = 1000 solver = DifferentialEvolutionSolver(ND, NP) solver.enable_signal_handler() stepmon = MyMonitor(interval) 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=50)) solution = solver.Solution() return solution, stepmon
def test_DifferentialEvolutionSolver_COG(self): # Default for this solver from mystic.solvers import DifferentialEvolutionSolver from mystic.termination import ChangeOverGeneration as COG self.solver = DifferentialEvolutionSolver(self.ND, self.NP) self.term = COG() self._run_solver()
def test_DifferentialEvolutionSolver_VTR(self): from mystic.solvers import DifferentialEvolutionSolver from mystic.termination import VTR self.solver = DifferentialEvolutionSolver(self.ND, self.NP) self.term = VTR() self._run_solver()
def test_me(info='self'): from mystic.solvers import DifferentialEvolutionSolver s = DifferentialEvolutionSolver(4, 4) from mystic.termination import VTR from mystic.termination import ChangeOverGeneration from mystic.termination import NormalizedChangeOverGeneration v = VTR() c = ChangeOverGeneration() n = NormalizedChangeOverGeneration() if disp: print("define conditions...") _v = When(v) _c = When(c) _n = When(n) v_and_c = And(v, c) v_and_n = And(v, n) v_or_c = Or(v, c) v_or_n = Or(v, n) v_and_c__or_n = Or(v_and_c, _n) v_and_c__or_c = Or(v_and_c, _c) v_and_n__or_n = Or(v_and_n, _n) c_and_v = And(c, v) c_or_v = Or(c, v) c_or__c_and_v = Or(_c, c_and_v) assert len(_v) == len(_c) == len(_n) == 1 assert list(_v).index(v) == list(_c).index(c) == list(_n).index(n) == 0 assert list(_v).count(v) == list(_c).count(c) == list(_n).count(n) == 1 assert v in _v and c in _c and n in _n assert v in v_and_c and c in v_and_c assert v in v_and_n and n in v_and_n assert v in v_or_c and c in v_or_c assert v in v_or_n and n in v_or_n assert len(v_and_c) == len(v_and_n) == len(v_or_c) == len(v_or_n) == 2 assert len(v_and_c__or_n) == len(v_and_c__or_c) == len(v_and_n__or_n) == 2 assert v not in v_and_c__or_n and c not in v_and_c__or_n and n not in v_and_c__or_n assert v_and_c in v_and_c__or_n and _n in v_and_c__or_n assert v_and_c in v_and_c__or_c and _c in v_and_c__or_c assert v_and_n in v_and_n__or_n and _n in v_and_n__or_n assert c in c_and_v and v in c_and_v assert c in c_or_v and v in c_or_v assert list(c_and_v).index(c) == list(v_and_c).index(v) assert list(c_and_v).index(v) == list(v_and_c).index(c) assert list(c_or_v).index(c) == list(v_or_c).index(v) assert list(c_or_v).index(v) == list(v_or_c).index(c) assert c_and_v in c_or__c_and_v and _c in c_or__c_and_v if disp: print("_v:%s" % _v) print("_c:%s" % _c) print("_n:%s" % _n) print("v_and_c:%s" % v_and_c) print("v_and_n:%s" % v_and_n) print("v_or_c:%s" % v_or_c) print("v_or_n:%s" % v_or_n) print("v_and_c__or_n:%s" % v_and_c__or_n) print("v_and_c__or_c:%s" % v_and_c__or_c) print("v_and_n__or_n:%s" % v_and_n__or_n) print("c_and_v:%s" % c_and_v) print("c_or_v:%s" % c_or_v) print("c_or__c_and_v:%s" % c_or__c_and_v) if disp: print("initial conditions...") _v_and_c = v_and_c(s, info) _v_and_n = v_and_n(s, info) _v_or_c = v_or_c(s, info) _v_or_n = v_or_n(s, info) assert not _v_and_c assert not _v_and_n assert not _v_or_c assert not _v_or_n if disp: print("v_and_c:%s" % _v_and_c) print("v_and_n:%s" % _v_and_n) print("v_or_c:%s" % _v_or_c) print("v_or_n:%s" % _v_or_n) if disp: print("after convergence toward zero...") s.energy_history = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] # _v and _n are True, _c is False _v_and_c = v_and_c(s, info) _v_and_n = v_and_n(s, info) _v_or_c = v_or_c(s, info) _v_or_n = v_or_n(s, info) assert not _v_and_c assert _v_and_n assert _v_or_c assert _v_or_n if info == 'self': assert v in _v_and_n and n in _v_and_n assert v in _v_or_c and c not in _v_or_c assert v in _v_or_n and n in _v_or_n if disp: print("v_and_c:%s" % _v_and_c) print("v_and_n:%s" % _v_and_n) print("v_or_c:%s" % _v_or_c) print("v_or_n:%s" % _v_or_n) if disp: print("nested compound termination...") # _v and _n are True, _c is False _v_and_c__or_n = v_and_c__or_n(s, info) _v_and_c__or_c = v_and_c__or_c(s, info) _v_and_n__or_n = v_and_n__or_n(s, info) assert _v_and_c__or_n assert not _v_and_c__or_c assert _v_and_n__or_n if info == 'self': assert _n in _v_and_c__or_n and v_and_c not in _v_and_c__or_n assert v not in _v_and_c__or_n and c not in _v_and_c__or_n assert v_and_n in _v_and_n__or_n and _n in _v_and_n__or_n assert v not in _v_and_n__or_n and n not in _v_and_n__or_n if disp: print("v_and_c__or_n:%s" % _v_and_c__or_n) print("v_and_c__or_c:%s" % _v_and_c__or_c) print("v_and_n__or_n:%s" % _v_and_n__or_n) if disp: print("individual conditions...") __v = _v(s, info) __c = _c(s, info) __n = _n(s, info) assert __v and __n assert not __c if info == 'self': assert v in __v and n in __n if disp: print("v:%s" % __v) print("c:%s" % __c) print("n:%s" % __n) if disp: print("conditions with false first...") _c_and_v = c_and_v(s, info) _c_or_v = c_or_v(s, info) _c_or__c_and_v = c_or__c_and_v(s, info) assert not _c_and_v assert _c_or_v assert not _c_or__c_and_v if info == 'self': assert v in _c_or_v and c not in _c_or_v if disp: print("c_and_v:%s" % _c_and_v) print("c_or_v:%s" % _c_or_v) print("c_or__c_and_v:%s" % _c_or__c_and_v)
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)
# - https://github.com/uqfoundation/mystic/blob/master/LICENSE import mystic.collapse as ct import mystic.mask as ma import mystic as my m = my.monitors._load('_log.py') # cleanup *pyc import os try: os.remove('_log.pyc') except OSError: pass import mystic.termination as mt from mystic.solvers import DifferentialEvolutionSolver solver = DifferentialEvolutionSolver(2 * sum(m._npts)) solver.SetRandomInitialPoints() solver.SetGenerationMonitor(m) ############################################## # print('collapse_at') ix = ct.collapse_at(m) # (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 16, 17) assert not ct.collapse_at(m, mask=ix) ix = ct.collapse_at(m, target=0.0) # (1, 7, 8, 9, 10, 11, 12, 14) assert not ct.collapse_at(m, target=0.0, mask=ix) ix = ct.collapse_at(m, target=m.x[-1]) assert not ct.collapse_at(m, target=m.x[-1], mask=ix) # print('collapse_as')
assert solver._state == None assert LoadSolver(solver._state) == None solver = NelderMeadSimplexSolver(3) solver.SetRandomInitialPoints([0., 0., 0.], [10., 10., 10.]) term = VTR() solver.SetSaveFrequency(10, tmpfile) solver.Solve(rosen, term) x = solver.bestSolution y = solver.bestEnergy _solver = LoadSolver(tmpfile) os.remove(tmpfile) assert all(x == _solver.bestSolution) assert y == _solver.bestEnergy solver = DifferentialEvolutionSolver(3, 40) solver.SetRandomInitialPoints([0., 0., 0.], [10., 10., 10.]) term = VTR() solver.Solve(rosen, term) x = solver.bestSolution y = solver.bestEnergy assert solver._state == None assert LoadSolver(solver._state) == None solver = DifferentialEvolutionSolver(3, 40) solver.SetRandomInitialPoints([0., 0., 0.], [10., 10., 10.]) term = VTR() solver.SetSaveFrequency(10, tmpfile) solver.Solve(rosen, term) x = solver.bestSolution y = solver.bestEnergy
def __when(func): "factory for single termination condition cast as a compound condition" return __and(func) from mystic.termination import And, Or, When if __name__ == '__main__': info = 'self' #False #True _all = And #__and _any = Or #__or _one = When #__when from mystic.solvers import DifferentialEvolutionSolver s = DifferentialEvolutionSolver(4, 4) from mystic.termination import VTR from mystic.termination import ChangeOverGeneration from mystic.termination import NormalizedChangeOverGeneration v = VTR() c = ChangeOverGeneration() n = NormalizedChangeOverGeneration() print "define conditions..." _v = _one(v) _c = _one(c) _n = _one(n) vAc = _all(v, c) vAn = _all(v, n) vOc = _any(v, c)
def test_DifferentialEvolutionSolver_NCOG(self): from mystic.solvers import DifferentialEvolutionSolver from mystic.termination import NormalizedChangeOverGeneration as NCOG self.solver = DifferentialEvolutionSolver(self.ND, self.NP) self.term = NCOG() self._run_solver()
ssow = Monitor() #ssow= VerboseMonitor(1) # import random # xinit = [random.random() for j in range(ND)] 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')
def test_DifferentialEvolutionSolver_CRT(self): from mystic.solvers import DifferentialEvolutionSolver from mystic.termination import CandidateRelativeTolerance as CRT self.solver = DifferentialEvolutionSolver(self.ND, self.NP) self.term = CRT() self._run_solver()
x,y = data[i] if abs((xx-x)*(xx-x)+(yy-y)*(yy-y) - rr*rr) < 0.01: svl.append(i) return svl # DEsolver inputs MAX_GENERATIONS = 2000 ND, NP = 3, 30 # dimension, population size minrange = [0., 0., 0.] maxrange = [50., 50., 10.] # prepare DESolver from mystic.solvers import DifferentialEvolutionSolver2 \ as DifferentialEvolutionSolver from mystic.termination import ChangeOverGeneration, VTR solver = DifferentialEvolutionSolver(ND, NP) solver.enable_signal_handler() solver.SetRandomInitialPoints(min=minrange,max=maxrange) solver.SetEvaluationLimits(generations=MAX_GENERATIONS) solver.Solve(cost, termination=ChangeOverGeneration(generations=100)) if __name__ == '__main__': # x0, y0, R0 #guess = [1,1,1] # bad initial guess #guess = [5,5,1] # ok guess guess = [10,15,5] # good initial guess # plot training set & training set boundary pylab.plot(xy[:,0],xy[:,1],'k+',markersize=6)