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 test04(terminate, func=lambda x: x[0], info=False, debug=False): from mystic.solvers import DifferentialEvolutionSolver as DE solver = DE(1, 5) solver.SetRandomInitialPoints() solver.SetEvaluationLimits(8) solver.Solve(func, VTR()) if debug: verbosity(solver) return terminate(solver, info)
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=[-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=[-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(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=[-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 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 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 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
#!/usr/bin/env python # # Author: Mike McKerns (mmckerns @caltech and @uqfoundation) # Copyright (c) 2018-2022 The Uncertainty Quantification Foundation. # License: 3-clause BSD. The full license text is available at: # - https://github.com/uqfoundation/mystic/blob/master/LICENSE from mystic.solvers import DifferentialEvolutionSolver from mystic.models import rosen from mystic.tools import solver_bounds from mystic.termination import ChangeOverGeneration as COG, Or, CollapseCost from mystic.monitors import VerboseLoggingMonitor as Monitor solver = DifferentialEvolutionSolver(3, 40) solver.SetRandomInitialPoints([-100] * 3, [100] * 3) mon = Monitor(1) options = dict(limit=1.95, samples=50, clip=False) mask = {} #solver_bounds(solver) stop = Or(CollapseCost(mask=mask, **options), COG(generations=50)) solver.SetGenerationMonitor(mon) solver.SetTermination(stop) solver.SetObjective(rosen) solver.Solve() print(solver.Terminated(info=True)) print('%s @' % solver.bestEnergy) print(solver.bestSolution)
@suppressed(1e-2) def conserve(x): 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)
def solve(constraints, guess=None, nvars=None, solver=None, \ lower_bounds=None, upper_bounds=None, termination=None): """Use optimization to find a solution to a set of constraints. Inputs: constraints -- a constraints solver function or a penalty function Additional Inputs: guess -- list of parameter values proposed to solve the constraints. lower_bounds -- list of lower bounds on solution values. upper_bounds -- list of upper bounds on solution values. nvars -- number of parameter values. solver -- the mystic solver to use in the optimization termination -- the mystic termination to use in the optimization NOTE: The resulting constraints will likely be more expensive to evaluate and less accurate than writing the constraints solver from scratch. NOTE: The ensemble solvers are available, using the default NestedSolver, where the keyword 'guess' can be used to set the number of solvers. NOTE: The default solver is 'diffev', with npop=min(40, ndim*5). The default termination is ChangeOverGeneration(), and the default guess is randomly selected points between the upper and lower bounds. """ npts = 8 if type(guess) is int: npts, guess = guess, None ndim = 1 #XXX: better, increase in while loop catching IndexError ? if nvars is not None: ndim = nvars elif guess is not None: ndim = len(guess) elif lower_bounds is not None: ndim = len(lower_bounds) elif upper_bounds is not None: ndim = len(upper_bounds) def cost(x): return 1. #XXX: don't allow solver string as a short-cut? #FIXME: add ensemble solvers ensemble = False if solver is None or solver == 'diffev': from mystic.solvers import DifferentialEvolutionSolver as TheSolver solver = TheSolver(ndim, min(40, ndim * 5)) elif solver == 'diffev2': from mystic.solvers import DifferentialEvolutionSolver2 as TheSolver solver = TheSolver(ndim, min(40, ndim * 5)) elif solver == 'fmin_powell': #XXX: better as the default? (it's not random) from mystic.solvers import PowellDirectionalSolver as TheSolver solver = TheSolver(ndim) elif solver == 'fmin': from mystic.solvers import NelderMeadSimplexSolver as TheSolver solver = TheSolver(ndim) elif solver == 'buckshot': from mystic.solvers import BuckshotSolver as TheSolver solver = TheSolver(ndim, max(8, npts)) #XXX: needs better default? ensemble = True elif solver == 'lattice': from mystic.solvers import LatticeSolver as TheSolver solver = TheSolver(ndim, max(8, npts)) #XXX: needs better default? ensemble = True if termination is None: from mystic.termination import ChangeOverGeneration as COG termination = COG() if not ensemble: if guess is not None: solver.SetInitialPoints(guess) #XXX: nice if 'diffev' had methods else: solver.SetRandomInitialPoints(lower_bounds, upper_bounds) if lower_bounds or upper_bounds: solver.SetStrictRanges(lower_bounds, upper_bounds) if hasattr(constraints, 'iter') and hasattr(constraints, 'error'): solver.SetPenalty(constraints) #i.e. is a penalty function else: # is a constraints solver solver.SetConstraints(constraints) from numpy import seterr settings = seterr(all='ignore') solver.Solve(cost, termination) seterr(**settings) soln = solver.bestSolution from numpy import ndarray, array if isinstance(soln, ndarray) and not isinstance(guess, ndarray): soln = soln.tolist() elif isinstance(guess, ndarray) and not isinstance(soln, ndarray): soln = array(soln) #XXX: or always return a list ? return soln #XXX: check with 'issolution' ?
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)
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') ix = ct.collapse_as(m)
def solve(constraints, guess=None, nvars=None, solver=None, \ lower_bounds=None, upper_bounds=None, termination=None): """Use optimization to find a solution to a set of constraints. Inputs: constraints -- a constraints solver function or a penalty function Additional Inputs: guess -- list of parameter values proposed to solve the constraints. lower_bounds -- list of lower bounds on solution values. upper_bounds -- list of upper bounds on solution values. nvars -- number of parameter values. solver -- the mystic solver to use in the optimization termination -- the mystic termination to use in the optimization NOTE: The resulting constraints will likely be more expensive to evaluate and less accurate than writing the constraints solver from scratch. """ ndim = 1 #XXX: better, increase in while loop catching IndexError ? if nvars is not None: ndim = nvars elif guess is not None: ndim = len(guess) elif lower_bounds is not None: ndim = len(lower_bounds) elif upper_bounds is not None: ndim = len(upper_bounds) def cost(x): return 1. #XXX: don't allow solver string as a short-cut? if solver is None or solver == 'diffev': from mystic.solvers import DifferentialEvolutionSolver as TheSolver solver = TheSolver(ndim, min(40, ndim * 5)) elif solver == 'diffev2': from mystic.solvers import DifferentialEvolutionSolver2 as TheSolver solver = TheSolver(ndim, min(40, ndim * 5)) elif solver == 'fmin_powell': #XXX: better as the default? (it's not random) from mystic.solvers import PowellDirectionalSolver as TheSolver solver = TheSolver(ndim) elif solver == 'fmin': from mystic.solvers import NelderMeadSimplexSolver as TheSolver solver = TheSolver(ndim) if termination is None: from mystic.termination import ChangeOverGeneration as COG termination = COG() if guess != None: solver.SetInitialPoints(guess) #XXX: nice if 'diffev' also had methods else: solver.SetRandomInitialPoints(lower_bounds, upper_bounds) if lower_bounds or upper_bounds: solver.SetStrictRanges(lower_bounds, upper_bounds) if hasattr(constraints, 'iter') and hasattr(constraints, 'error'): solver.SetPenalty(constraints) #i.e. is a penalty function else: # is a constraints solver solver.SetConstraints(constraints) solver.Solve(cost, termination) soln = solver.bestSolution from numpy import ndarray, array if isinstance(soln, ndarray) and not isinstance(guess, ndarray): soln = soln.tolist() elif isinstance(guess, ndarray) and not isinstance(soln, ndarray): soln = array(soln) #XXX: or always return a list ? return soln #XXX: check with 'issolution' ?
@suppressed(1e-5) def conserve(x): return constrain(x) from mystic.monitors import VerboseMonitor mon = VerboseMonitor(10) # solve for alpha from mystic.solvers import DifferentialEvolutionSolver as DESolver from mystic.termination import Or, ChangeOverGeneration, CollapseAt ndim = len(lb) npop = 3 * N stop = Or(ChangeOverGeneration(1e-8, 200), CollapseAt(0.0)) solver = DESolver(ndim, npop) solver.SetRandomInitialPoints(min=_b, 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: %s' % alpha) print("constraint A*x == 0: %s" % inner(Aeq, alpha)) print("minimum 0.5*x'Qx + b'*x: %s" % solver.bestEnergy) # calculate support vectors and regression function sv1 = SupportVectors(alpha[:nx]) sv2 = SupportVectors(alpha[nx:]) R = RegressionFunction(x, y, alpha, svr_epsilon, lk)