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=[-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 = Monitor() minrange = [-1000., -1000., -100., -1.]*2; maxrange = [1000., 1000., 100., 1.]*2; solver.SetRandomInitialPoints(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 runme(): # instantiate the solver _solver = NelderMeadSimplexSolver(3) lb, ub = [0., 0., 0.], [10., 10., 10.] _solver.SetRandomInitialPoints(lb, ub) _solver.SetEvaluationLimits(1000) # add a monitor stream stepmon = VerboseMonitor(1) _solver.SetGenerationMonitor(stepmon) # configure the bounds _solver.SetStrictRanges(lb, ub) # configure stop conditions term = Or(VTR(), ChangeOverGeneration()) _solver.SetTermination(term) # add a periodic dump to an archive tmpfile = 'mysolver.pkl' _solver.SetSaveFrequency(10, tmpfile) # run the optimizer _solver.Solve(rosen) # get results x = _solver.bestSolution y = _solver.bestEnergy # load the saved solver solver = LoadSolver(tmpfile) #os.remove(tmpfile) # obligatory check that state is the same assert all(x == solver.bestSolution) assert y == solver.bestEnergy # modify the termination condition term = VTR(0.0001) solver.SetTermination(term) # run the optimizer solver.Solve(rosen) os.remove(tmpfile) # check the solver serializes _s = dill.dumps(solver) return dill.loads(_s)
@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)
assert solver._state == None assert LoadSolver(solver._state) == None solver = DifferentialEvolutionSolver(3, 40) solver.SetRandomInitialPoints([0., 0., 0.], [10., 10., 10.]) term = When(VTR()) solver.SetSaveFrequency(10, tmpfile) solver.SetTermination(term) solver.Solve(rosen) 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 = Or(VTR(), ChangeOverGeneration()) solver.SetSaveFrequency(10, tmpfile) solver.SetTermination(term) solver.Solve(rosen) x = solver.bestSolution y = solver.bestEnergy _solver = LoadSolver(tmpfile) os.remove(tmpfile) assert all(x == _solver.bestSolution) assert y == _solver.bestEnergy # EOF
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) vOn = _any(v, n) vAc_On = _any(vAc, _n) vAc_Oc = _any(vAc, _c) vAn_On = _any(vAn, _n) cAv = _all(c, v) cOv = _any(c, v)
def diffev(cost, x0, npop=4, args=(), bounds=None, ftol=5e-3, gtol=None, maxiter=None, maxfun=None, cross=0.9, scale=0.8, full_output=0, disp=1, retall=0, callback=None, **kwds): """Minimize a function using differential evolution. Uses a differential evolution algorithm to find the minimum of a function of one or more variables. Mimics a ``scipy.optimize`` style interface. Args: cost (func): the function or method to be minimized: ``y = cost(x)``. x0 (ndarray): the initial guess parameter vector ``x`` if desired start is a single point, otherwise takes a list of (min,max) bounds that define a region from which random initial points are drawn. npop (int, default=4): size of the trial solution population. args (tuple, default=()): extra arguments for cost. bounds (list(tuple), default=None): list of pairs of bounds (min,max), one for each parameter. ftol (float, default=5e-3): acceptable relative error in ``cost(xopt)`` for convergence. gtol (float, default=None): maximum iterations to run without improvement. maxiter (int, default=None): the maximum number of iterations to perform. maxfun (int, default=None): the maximum number of function evaluations. cross (float, default=0.9): the probability of cross-parameter mutations. scale (float, default=0.8): multiplier for mutations on the trial solution. full_output (bool, default=False): True if fval and warnflag are desired. disp (bool, default=True): if True, print convergence messages. retall (bool, default=False): True if allvecs is desired. callback (func, default=None): function to call after each iteration. The interface is ``callback(xk)``, with xk the current parameter vector. handler (bool, default=False): if True, enable handling interrupt signals. strategy (strategy, default=None): override the default mutation strategy. itermon (monitor, default=None): override the default GenerationMonitor. evalmon (monitor, default=None): override the default EvaluationMonitor. constraints (func, default=None): a function ``xk' = constraints(xk)``, where xk is the current parameter vector, and xk' is a parameter vector that satisfies the encoded constraints. penalty (func, default=None): a function ``y = penalty(xk)``, where xk is the current parameter vector, and ``y' == 0`` when the encoded constraints are satisfied (and ``y' > 0`` otherwise). map (func, default=None): a (parallel) map function ``y = map(f, x)``. Returns: ``(xopt, {fopt, iter, funcalls, warnflag}, {allvecs})`` Notes: - xopt (*ndarray*): the minimizer of the cost function - fopt (*float*): value of cost function at minimum: ``fopt = cost(xopt)`` - iter (*int*): number of iterations - funcalls (*int*): number of function calls - warnflag (*int*): warning flag: - ``1 : Maximum number of function evaluations`` - ``2 : Maximum number of iterations`` - allvecs (*list*): a list of solutions at each iteration """ invariant_current = kwds[ 'invariant_current'] if 'invariant_current' in kwds else False handler = kwds['handler'] if 'handler' in kwds else False from mystic.strategy import Best1Bin strategy = kwds['strategy'] if 'strategy' in kwds else Best1Bin from mystic.monitors import Monitor stepmon = kwds['itermon'] if 'itermon' in kwds else Monitor() evalmon = kwds['evalmon'] if 'evalmon' in kwds else Monitor() if gtol: #if number of generations provided, use ChangeOverGeneration from mystic.termination import ChangeOverGeneration termination = ChangeOverGeneration(ftol, gtol) else: from mystic.termination import VTRChangeOverGeneration termination = VTRChangeOverGeneration(ftol) ND = len(x0) if invariant_current: #use Solver2, not Solver1 solver = DifferentialEvolutionSolver2(ND, npop) else: solver = DifferentialEvolutionSolver(ND, npop) solver.SetEvaluationLimits(maxiter, maxfun) solver.SetEvaluationMonitor(evalmon) solver.SetGenerationMonitor(stepmon) if 'penalty' in kwds: solver.SetPenalty(kwds['penalty']) if 'constraints' in kwds: solver.SetConstraints(kwds['constraints']) if bounds is not None: minb, maxb = unpair(bounds) solver.SetStrictRanges(minb, maxb) try: #x0 passed as 1D array of (min,max) pairs minb, maxb = unpair(x0) solver.SetRandomInitialPoints(minb, maxb) except: #x0 passed as 1D array of initial parameter values solver.SetInitialPoints(x0) _map = kwds['map'] if 'map' in kwds else None if _map: solver.SetMapper(_map) if handler: solver.enable_signal_handler() #TODO: allow sigint_callbacks for all minimal interfaces ? solver.Solve(cost, termination=termination, strategy=strategy, \ #sigint_callback=other_callback,\ CrossProbability=cross, ScalingFactor=scale, \ ExtraArgs=args, callback=callback) solution = solver.Solution() # code below here pushes output to scipy.optimize.fmin interface #x = list(solver.bestSolution) x = solver.bestSolution fval = solver.bestEnergy warnflag = 0 fcalls = solver.evaluations iterations = solver.generations allvecs = stepmon.x if fcalls >= solver._maxfun: warnflag = 1 if disp: print("Warning: Maximum number of function evaluations has "\ "been exceeded.") elif iterations >= solver._maxiter: warnflag = 2 if disp: print("Warning: Maximum number of iterations has been exceeded") else: if disp: print("Optimization terminated successfully.") print(" Current function value: %f" % fval) print(" Iterations: %d" % iterations) print(" Function evaluations: %d" % fcalls) if full_output: retlist = x, fval, iterations, fcalls, warnflag if retall: retlist += (allvecs, ) else: retlist = x if retall: retlist = (x, allvecs) return retlist
# 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) c = circle(x0, y0, R0) pylab.plot(c[:,0],c[:,1],'r-',linewidth=2) legend = ['random points','generating circle : %f' % R0] pylab.axis('equal')
def solve(measurements, method): """Find reasonable marker positions based on a set of measurements.""" print(method) marker_measurements = measurements if np.size(measurements) == 21: marker_measurements = measurements[(21 - 15) :] # m0 has known positions (0, 0, 0) # m1 has unknown x-position # All others have unknown xy-positions num_params = 0 + 1 + 2 + 2 + 2 + 2 bound = 400.0 lower_bound = [ 0.0, 0.0, 0.0, 0.0, 0.0, -bound, 0.0, -bound, 0.0, ] upper_bound = [ bound, bound, bound, bound, bound, bound, bound, bound, bound, ] def costx_no_nozzle(posvec): """Identical to cost_no_nozzle, except the shape of inputs""" positions = posvec2matrix_no_nozzle(posvec) return cost_no_nozzle(positions, marker_measurements) guess_0 = [0.0] * num_params intermediate_cost = 0.0 intermediate_solution = [] if method == "SLSQP": sol = scipy.optimize.minimize( costx_no_nozzle, guess_0, method="SLSQP", bounds=list(zip(lower_bound, upper_bound)), tol=1e-20, options={"disp": True, "ftol": 1e-40, "eps": 1e-10, "maxiter": 500}, ) intermediate_cost = sol.fun intermediate_solution = sol.x elif method == "L-BFGS-B": sol = scipy.optimize.minimize( costx_no_nozzle, guess_0, method="L-BFGS-B", bounds=list(zip(lower_bound, upper_bound)), options={"disp": True, "ftol": 1e-12, "gtol": 1e-12, "maxiter": 50000, "maxfun": 1000000}, ) intermediate_cost = sol.fun intermediate_solution = sol.x elif method == "PowellDirectionalSolver": from mystic.solvers import PowellDirectionalSolver from mystic.termination import Or, CollapseAt, CollapseAs from mystic.termination import ChangeOverGeneration as COG from mystic.monitors import VerboseMonitor from mystic.termination import VTR, And, Or solver = PowellDirectionalSolver(num_params) solver.SetRandomInitialPoints(lower_bound, upper_bound) solver.SetEvaluationLimits(evaluations=3200000, generations=100000) solver.SetTermination(Or(VTR(1e-25), COG(1e-10, 20))) solver.SetStrictRanges(lower_bound, upper_bound) solver.SetGenerationMonitor(VerboseMonitor(5)) solver.Solve(costx_no_nozzle) intermediate_cost = solver.bestEnergy intermediate_solution = solver.bestSolution elif method == "differentialEvolutionSolver": from mystic.solvers import DifferentialEvolutionSolver2 from mystic.monitors import VerboseMonitor from mystic.termination import VTR, ChangeOverGeneration, And, Or from mystic.strategy import Best1Exp, Best1Bin stop = Or(VTR(1e-18), ChangeOverGeneration(1e-9, 500)) npop = 3 stepmon = VerboseMonitor(100) solver = DifferentialEvolutionSolver2(num_params, npop) solver.SetEvaluationLimits(evaluations=3200000, generations=100000) solver.SetRandomInitialPoints(lower_bound, upper_bound) solver.SetStrictRanges(lower_bound, upper_bound) solver.SetGenerationMonitor(stepmon) solver.Solve( costx_no_nozzle, termination=stop, strategy=Best1Bin, ) intermediate_cost = solver.bestEnergy intermediate_solution = solver.bestSolution else: print("Method %s is not supported!" % method) sys.exit(1) print("Best intermediate cost: ", intermediate_cost) print("Best intermediate positions: \n%s" % posvec2matrix_no_nozzle(intermediate_solution)) if np.size(measurements) == 15: print("Got only 15 samples, so will not try to find nozzle position\n") return nozzle_measurements = measurements[: (21 - 15)] # Look for nozzle's xyz-offset relative to marker 0 num_params = 3 lower_bound = [ 0.0, 0.0, -bound, ] upper_bound = [bound, bound, 0.0] def costx_nozzle(posvec): """Identical to cost_nozzle, except the shape of inputs""" positions = posvec2matrix_nozzle(posvec, intermediate_solution) return cost_nozzle(positions, measurements) guess_0 = [0.0, 0.0, 0.0] final_cost = 0.0 final_solution = [] if method == "SLSQP": sol = scipy.optimize.minimize( costx_nozzle, guess_0, method="SLSQP", bounds=list(zip(lower_bound, upper_bound)), tol=1e-20, options={"disp": True, "ftol": 1e-40, "eps": 1e-10, "maxiter": 500}, ) final_cost = sol.fun final_solution = sol.x elif method == "L-BFGS-B": sol = scipy.optimize.minimize( costx_nozzle, guess_0, method="L-BFGS-B", bounds=list(zip(lower_bound, upper_bound)), options={"disp": True, "ftol": 1e-12, "gtol": 1e-12, "maxiter": 50000, "maxfun": 1000000}, ) final_cost = sol.fun final_solution = sol.x elif method == "PowellDirectionalSolver": from mystic.solvers import PowellDirectionalSolver from mystic.termination import Or, CollapseAt, CollapseAs from mystic.termination import ChangeOverGeneration as COG from mystic.monitors import VerboseMonitor from mystic.termination import VTR, And, Or solver = PowellDirectionalSolver(num_params) solver.SetRandomInitialPoints(lower_bound, upper_bound) solver.SetEvaluationLimits(evaluations=3200000, generations=100000) solver.SetTermination(Or(VTR(1e-25), COG(1e-10, 20))) solver.SetStrictRanges(lower_bound, upper_bound) solver.SetGenerationMonitor(VerboseMonitor(5)) solver.Solve(costx_nozzle) final_cost = solver.bestEnergy final_solution = solver.bestSolution elif method == "differentialEvolutionSolver": from mystic.solvers import DifferentialEvolutionSolver2 from mystic.monitors import VerboseMonitor from mystic.termination import VTR, ChangeOverGeneration, And, Or from mystic.strategy import Best1Exp, Best1Bin stop = Or(VTR(1e-18), ChangeOverGeneration(1e-9, 500)) npop = 3 stepmon = VerboseMonitor(100) solver = DifferentialEvolutionSolver2(num_params, npop) solver.SetEvaluationLimits(evaluations=3200000, generations=100000) solver.SetRandomInitialPoints(lower_bound, upper_bound) solver.SetStrictRanges(lower_bound, upper_bound) solver.SetGenerationMonitor(stepmon) solver.Solve( costx_nozzle, termination=stop, strategy=Best1Bin, ) final_cost = solver.bestEnergy final_solution = solver.bestSolution print("Best final cost: ", final_cost) print("Best final positions:") final = posvec2matrix_nozzle(final_solution, intermediate_solution)[1:] for num in range(0, 6): print( "{0: 8.3f} {1: 8.3f} {2: 8.3f} <!-- Marker {3} -->".format(final[num][0], final[num][1], final[num][2], num) )
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 diffev(cost,x0,npop=4,args=(),bounds=None,ftol=5e-3,gtol=None, maxiter=None,maxfun=None,cross=0.9,scale=0.8, full_output=0,disp=1,retall=0,callback=None,**kwds): """Minimize a function using differential evolution. Description: Uses a differential evolution algorith to find the minimum of a function of one or more variables. Mimics a scipy.optimize style interface. Inputs: cost -- the Python function or method to be minimized. x0 -- the initial guess (ndarray), if desired to start from a set point; otherwise takes an array of (min,max) bounds, for when random initial points are desired npop -- size of the trial solution population. Additional Inputs: args -- extra arguments for cost. bounds -- list - n pairs of bounds (min,max), one pair for each parameter. ftol -- number - acceptable relative error in cost(xopt) for convergence. gtol -- number - maximum number of iterations to run without improvement. maxiter -- number - the maximum number of iterations to perform. maxfun -- number - the maximum number of function evaluations. cross -- number - the probability of cross-parameter mutations scale -- number - multiplier for impact of mutations on trial solution. full_output -- number - non-zero if fval and warnflag outputs are desired. disp -- number - non-zero to print convergence messages. retall -- number - non-zero to return list of solutions at each iteration. callback -- an optional user-supplied function to call after each iteration. It is called as callback(xk), where xk is the current parameter vector. handler -- boolean - enable/disable handling of interrupt signal strategy -- strategy - override the default mutation strategy itermon -- monitor - override the default GenerationMonitor evalmon -- monitor - override the default EvaluationMonitor constraints -- an optional user-supplied function. It is called as constraints(xk), where xk is the current parameter vector. This function must return xk', a parameter vector that satisfies the encoded constraints. penalty -- an optional user-supplied function. It is called as penalty(xk), where xk is the current parameter vector. This function should return y', with y' == 0 when the encoded constraints are satisfied, and y' > 0 otherwise. Returns: (xopt, {fopt, iter, funcalls, warnflag}, {allvecs}) xopt -- ndarray - minimizer of function fopt -- number - value of function at minimum: fopt = cost(xopt) iter -- number - number of iterations funcalls -- number - number of function calls warnflag -- number - Integer warning flag: 1 : 'Maximum number of function evaluations.' 2 : 'Maximum number of iterations.' allvecs -- list - a list of solutions at each iteration """ invariant_current = False if kwds.has_key('invariant_current'): invariant_current = kwds['invariant_current'] handler = False if kwds.has_key('handler'): handler = kwds['handler'] from mystic.strategy import Best1Bin strategy = Best1Bin if kwds.has_key('strategy'): strategy = kwds['strategy'] from mystic.monitors import Monitor stepmon = Monitor() evalmon = Monitor() if kwds.has_key('itermon'): stepmon = kwds['itermon'] if kwds.has_key('evalmon'): evalmon = kwds['evalmon'] if gtol: #if number of generations provided, use ChangeOverGeneration from mystic.termination import ChangeOverGeneration termination = ChangeOverGeneration(ftol,gtol) else: from mystic.termination import VTRChangeOverGeneration termination = VTRChangeOverGeneration(ftol) ND = len(x0) if invariant_current: #use Solver2, not Solver1 solver = DifferentialEvolutionSolver2(ND,npop) else: solver = DifferentialEvolutionSolver(ND,npop) solver.SetEvaluationLimits(maxiter,maxfun) solver.SetEvaluationMonitor(evalmon) solver.SetGenerationMonitor(stepmon) if kwds.has_key('penalty'): penalty = kwds['penalty'] solver.SetPenalty(penalty) if kwds.has_key('constraints'): constraints = kwds['constraints'] solver.SetConstraints(constraints) if bounds is not None: minb,maxb = unpair(bounds) solver.SetStrictRanges(minb,maxb) try: #x0 passed as 1D array of (min,max) pairs minb,maxb = unpair(x0) solver.SetRandomInitialPoints(minb,maxb) except: #x0 passed as 1D array of initial parameter values solver.SetInitialPoints(x0) if handler: solver.enable_signal_handler() #TODO: allow sigint_callbacks for all minimal interfaces ? solver.Solve(cost,termination=termination,strategy=strategy,\ #sigint_callback=other_callback,\ CrossProbability=cross,ScalingFactor=scale,\ ExtraArgs=args,callback=callback) solution = solver.Solution() # code below here pushes output to scipy.optimize.fmin interface #x = list(solver.bestSolution) x = solver.bestSolution fval = solver.bestEnergy warnflag = 0 fcalls = solver.evaluations iterations = solver.generations allvecs = stepmon.x if fcalls >= solver._maxfun: warnflag = 1 if disp: print "Warning: Maximum number of function evaluations has "\ "been exceeded." elif iterations >= solver._maxiter: warnflag = 2 if disp: print "Warning: Maximum number of iterations has been exceeded" else: if disp: print "Optimization terminated successfully." print " Current function value: %f" % fval print " Iterations: %d" % iterations print " Function evaluations: %d" % fcalls if full_output: retlist = x, fval, iterations, fcalls, warnflag if retall: retlist += (allvecs,) else: retlist = x if retall: retlist = (x, allvecs) return retlist