def test_constrain(): from mystic.math.measures import mean, spread from mystic.math.measures import impose_mean, impose_spread def mean_constraint(x, mean=0.0): return impose_mean(mean, x) def range_constraint(x, spread=1.0): return impose_spread(spread, x) @inner(inner=range_constraint, kwds={'spread':5.0}) @inner(inner=mean_constraint, kwds={'mean':5.0}) def constraints(x): return x def cost(x): return abs(sum(x) - 5.0) from mystic.solvers import fmin_powell from numpy import array x = array([1,2,3,4,5]) y = fmin_powell(cost, x, constraints=constraints, disp=False) assert mean(y) == 5.0 assert spread(y) == 5.0 assert almostEqual(cost(y), 4*(5.0))
def test_constrained_solve(): from mystic.math.measures import mean, spread @with_spread(5.0) @with_mean(5.0) def constraints(x): return x def cost(x): return abs(sum(x) - 5.0) from mystic.solvers import fmin_powell from numpy import array x = array([1,2,3,4,5]) y = fmin_powell(cost, x, constraints=constraints, disp=False) assert almostEqual(mean(y), 5.0, tol=1e-15) assert almostEqual(spread(y), 5.0, tol=1e-15) assert almostEqual(cost(y), 4*(5.0), tol=1e-6)
def impose_feasible(cutoff, data, guess=None, **kwds): """impose shortness on a given list of parameters w,x,y. Optimization on w,x,y over the given bounds seeks sum(infeasibility) = 0. (this function is not ???-preserving) Inputs: cutoff -- maximum acceptable deviation from shortness data -- a dataset of observed points (these points are 'static') guess -- the scenario providing an initial guess at feasibility, or a tuple of dimensions of the target scenario Additional Inputs: tol -- acceptable optimizer termination before sum(infeasibility) = 0. bounds -- a tuple of sample bounds: bounds = (lower_bounds, upper_bounds) constraints -- a function that takes a flat list parameters x' = constraints(x) Outputs: pm -- a scenario with desired shortness """ from numpy import sum, asarray from mystic.math.legacydata import dataset from mystic.math.distance import lipschitz_distance, infeasibility, _npts if guess is None: message = "Requires a guess scenario, or a tuple of scenario dimensions." raise TypeError, message # get initial guess if hasattr(guess, 'pts'): # guess is a scenario pts = guess.pts # number of x guess = guess.flatten(all=True) else: pts = guess # guess is given as a tuple of 'pts' guess = None npts = _npts(pts) # number of Y long_form = len(pts) - list(pts).count(2) # can use '2^K compressed format' # prepare bounds for solver bounds = kwds.pop('bounds', None) # if bounds are not set, use the default optimizer bounds if bounds is None: lower_bounds = []; upper_bounds = [] for n in pts: # bounds for n*x in each dimension (x2 due to weights) lower_bounds += [None]*n * 2 upper_bounds += [None]*n * 2 # also need bounds for npts*y values lower_bounds += [None]*npts upper_bounds += [None]*npts bounds = lower_bounds, upper_bounds bounds = asarray(bounds).T # plug in the 'constraints' function: param' = constraints(param) # constraints should impose_mean(y,w), and possibly sum(weights) constraints = kwds.pop('constraints', None) # default is no constraints if not constraints: # if None (default), there are no constraints constraints = lambda x: x _self = kwds.pop('with_self', True) # default includes self in shortness if _self is not False: _self = True # tolerance for optimization on sum(y) tol = kwds.pop('tol', 0.0) # default npop = kwds.pop('npop', 20) #XXX: tune npop? maxiter = kwds.pop('maxiter', 1000) #XXX: tune maxiter? # if no guess was made, then use bounds constraints if guess is None: if npop: guess = bounds else: # fmin_powell needs a list params (not bounds) guess = [(a + b)/2. for (a,b) in bounds] # construct cost function to reduce sum(lipschitz_distance) def cost(rv): """compute cost from a 1-d array of model parameters, where: cost = | sum(lipschitz_distance) | """ _data = dataset() _pm = scenario() _pm.load(rv, pts) # here rv is param: w,x,y if not long_form: positions = _pm.select(*range(npts)) else: positions = _pm.positions _data.load( data.coords, data.values ) # LOAD static if _self: _data.load( positions, _pm.values ) # LOAD dynamic _data.lipschitz = data.lipschitz # LOAD L Rv = lipschitz_distance(_data.lipschitz, _pm, _data, tol=cutoff, **kwds) v = infeasibility(Rv, cutoff) return abs(sum(v)) # construct and configure optimizer debug = False #!!! maxfun = 1e+6 crossover = 0.9; percent_change = 0.9 ftol = abs(tol); gtol = None if debug: print "lower bounds: %s" % bounds.T[0] print "upper bounds: %s" % bounds.T[1] # print "initial value: %s" % guess # use optimization to get feasible points from mystic.solvers import diffev2, fmin_powell from mystic.monitors import Monitor, VerboseMonitor from mystic.strategy import Best1Bin, Best1Exp evalmon = Monitor(); stepmon = Monitor(); strategy = Best1Exp if debug: stepmon = VerboseMonitor(10) #!!! if npop: # use VTR results = diffev2(cost, guess, npop, ftol=ftol, gtol=gtol, bounds=bounds,\ maxiter=maxiter, maxfun=maxfun, constraints=constraints,\ cross=crossover, scale=percent_change, strategy=strategy,\ evalmon=evalmon, itermon=stepmon,\ full_output=1, disp=0, handler=False) else: # use VTR results = fmin_powell(cost, guess, ftol=ftol, gtol=gtol, bounds=bounds,\ maxiter=maxiter, maxfun=maxfun, constraints=constraints,\ evalmon=evalmon, itermon=stepmon,\ full_output=1, disp=0, handler=False) # repack the results pm = scenario() pm.load(results[0], pts) # params: w,x,y #if debug: print "final cost: %s" % results[1] if debug and results[2] >= maxiter: # iterations print "Warning: constraints solver terminated at maximum iterations" #func_evals = results[3] # evaluation return pm
# assert my_x[3] <= maxfun if maxiter is not None: # test iters <= maxiter assert my_x[2] <= maxiter return if __name__ == '__main__': x0 = [0,0,0] # check solutions versus results based on the random_seed # print "comparing against known results" sol = solvers.diffev(rosen, x0, npop=40, disp=0, full_output=True) assert almostEqual(sol[1], 0.0020640145337293249, tol=3e-3) sol = solvers.diffev2(rosen, x0, npop=40, disp=0, full_output=True) assert almostEqual(sol[1], 0.0017516784703663288, tol=3e-3) sol = solvers.fmin_powell(rosen, x0, disp=0, full_output=True) assert almostEqual(sol[1], 8.3173488898295291e-23) sol = solvers.fmin(rosen, x0, disp=0, full_output=True) assert almostEqual(sol[1], 1.1605792769954724e-09) solver2 = 'diffev2' for solver in ['diffev']: # print "comparing %s and %s from mystic" % (solver, solver2) test_solvers(solver, solver2, x0, npop=40) test_solvers(solver, solver2, x0, npop=40, maxiter=None, maxfun=0) test_solvers(solver, solver2, x0, npop=40, maxiter=None, maxfun=1) test_solvers(solver, solver2, x0, npop=40, maxiter=None, maxfun=2) test_solvers(solver, solver2, x0, npop=40, maxiter=None, maxfun=9) test_solvers(solver, solver2, x0, npop=40, maxiter=0) test_solvers(solver, solver2, x0, npop=40, maxiter=1) test_solvers(solver, solver2, x0, npop=40, maxiter=2)
result = diffev2(objective, args=args, x0=bounds, bounds=bounds, npop=40, ftol=1e-8, gtol=100, disp=False, full_output=True) #, itermon=mon) # print result[0], xs assert almostEqual(result[0], xs, rel=1e-1) assert almostEqual(result[1], ys, rel=1e-1) result = fmin_powell(objective, args=args, x0=[0.0, 0.0], bounds=bounds, disp=False, full_output=True) # print result[0], xs assert almostEqual(result[0], xs, rel=1e-1) assert almostEqual(result[1], ys, rel=1e-1) # mon = VerboseMonitor(10) result = diffev2(objective, args=args, x0=bounds_, bounds=bounds_, npop=40, ftol=1e-8, gtol=100, disp=False,
if __name__ == '__main__': print "Powell's Method" print "===============" # initial guess import random from mystic.tools import random_seed random_seed(123) ndim = 9 x0 = [random.uniform(-100, 100) for i in range(ndim)] # draw frame and exact coefficients plot_exact() # use Powell's method to solve 8th-order Chebyshev coefficients solution = fmin_powell(chebyshev8cost, x0) # use pretty print for polynomials print poly1d(solution) # compare solution with actual 8th-order Chebyshev coefficients print "\nActual Coefficients:\n %s\n" % poly1d(chebyshev8coeffs) # plot solution versus exact coefficients plot_solution(solution) getch() #XXX: or pylab.show() ? # end of file
ys_ = objective(xs_, x, y) if __name__ == '__main__': from mystic.solvers import diffev2, fmin_powell from mystic.math import almostEqual # from mystic.monitors import VerboseMonitor # mon = VerboseMonitor(10) result = diffev2(objective, args=args, x0=bounds, bounds=bounds, npop=40, ftol=1e-8, gtol=100, disp=False, full_output=True)#, itermon=mon) # print result[0], xs assert almostEqual(result[0], xs, rel=1e-1) assert almostEqual(result[1], ys, rel=1e-1) result = fmin_powell(objective, args=args, x0=[0.0,0.0], bounds=bounds, disp=False, full_output=True) # print result[0], xs assert almostEqual(result[0], xs, rel=1e-1) assert almostEqual(result[1], ys, rel=1e-1) # mon = VerboseMonitor(10) result = diffev2(objective, args=args, x0=bounds_, bounds=bounds_, npop=40, ftol=1e-8, gtol=100, disp=False, full_output=True)#, itermon=mon) # print result[0], xs_ assert almostEqual(result[0], xs_, tol=1e-1) assert almostEqual(result[1], ys_, rel=1e-1) result = fmin_powell(objective, args=args, x0=[0.0,0.0,0.0], bounds=bounds_, disp=False, full_output=True) # print result[0], xs_ assert almostEqual(result[0], xs_, tol=1e-1) assert almostEqual(result[1], ys_, rel=1e-1)
def radius(model, point, ytol=0.0, xtol=0.0, ipop=None, imax=None): """graphical distance between a single point x,y and a model F(x')""" # given a single point x,y: find the radius = |y - F(x')| + delta # radius is just a minimization over x' of |y - F(x')| + delta # where we apply a constraints function (of box constraints) of # |x - x'| <= xtol (for each i in x) # # if hausdorff = some iterable, delta = |x - x'|/hausdorff # if hausdorff = True, delta = |x - x'|/spread(x); using the dataset range # if hausdorff = False, delta = 0.0 # # if ipop, then DE else Powell; ytol is used in VTR(ytol) # and will terminate when cost <= ytol x,y = _get_xy(point) y = asarray(y) # catch cases where yptp or y will cause issues in normalization #if not isfinite(yptp): return 0.0 #FIXME: correct? shouldn't happen #if yptp == 0: from numpy import inf; return inf #FIXME: this is bad # build the cost function if hausdorff: # distance in all directions def cost(rv): '''cost = |y - F(x')| + |x - x'| for each x,y (point in dataset)''' _y = model(rv) if not isfinite(_y): return abs(_y) errs = seterr(invalid='ignore', divide='ignore') # turn off warning z = abs((asarray(x) - rv)/ptp) # normalize by range m = abs(y - _y)/yptp # normalize by range seterr(invalid=errs['invalid'], divide=errs['divide']) # turn on warning return m + sum(z[isfinite(z)]) else: # vertical distance only def cost(rv): '''cost = |y - F(x')| for each x,y (point in dataset)''' return abs(y - model(rv)) if debug: print("rv: %s" % str(x)) print("cost: %s" % cost(x)) # if xtol=0, radius is difference in x,y and x,F(x); skip the optimization try: if not imax or not max(xtol): #iterables return cost(x) except TypeError: if not xtol: #non-iterables return cost(x) # set the range constraints xtol = asarray(xtol) bounds = list(zip( x - xtol, x + xtol )) if debug: print("lower: %s" % str(zip(*bounds)[0])) print("upper: %s" % str(zip(*bounds)[1])) # optimize where initially x' = x stepmon = Monitor() if debug: stepmon = VerboseMonitor(1) #XXX: edit settings? MINMAX = 1 #XXX: confirm MINMAX=1 is minimization ftol = ytol gtol = None # use VTRCOG if ipop: results = diffev2(cost, bounds, ipop, ftol=ftol, gtol=gtol, \ itermon = stepmon, maxiter=imax, bounds=bounds, \ full_output=1, disp=0, handler=False) else: results = fmin_powell(cost, x, ftol=ftol, gtol=gtol, \ itermon = stepmon, maxiter=imax, bounds=bounds, \ full_output=1, disp=0, handler=False) #solved = results[0] # x' func_opt = MINMAX * results[1] # cost(x') if debug: print("solved: %s" % results[0]) print("cost: %s" % func_opt) # get the minimum distance |y - F(x')| return func_opt
A = ones((1,npt)) b = ones(1) # # generic: build a constraint where (A*x == b) # from mystic.symbolic import linear_symbolic, solve, \ # generate_solvers as solvers, generate_constraint as constraint # norm = constraint(solvers(solve(linear_symbolic(A,b),target=['x0']))) # specific: build a constraint where (sum(x) == 1.0) from mystic.math.measures import normalize def norm(x): return normalize(x, mass=1.0) # solve the constrained quadratic programming problem from mystic.solvers import fmin_powell x = fmin_powell(objective, UB/npt, args=(H, f), bounds=zip(LB,UB), \ constraints=norm, ftol=1e-8, disp=1) # find support vectors sv = where(x > 0.001)[0] print "support vectors: ", sv # compare solved center and radius to generating center and radius center = dot(x,xy) R = linalg.norm(xy[sv[0],:]-center) print "x0, y0: (%f, %f) @ R0 = %f" % (x0,y0,R0) print "center: (%f, %f) @ R = %f" % (center[0],center[1],R) plot(xy, sv, x0, y0, R0, center, R) # EOF
constraint = lambda x: func(x) @quadratic_inequality(constraint) def penalty(x): return 0.0 mon = VerboseMonitor(50) solution = diffev2(objective, x0, penalty=penalty, bounds=bounds, itermon=mon, gtol=100, maxiter=1000, maxfun=10000, npop=40) print(solution) mon = VerboseMonitor(50) solution = fmin_powell(objective, x0, penalty=penalty, bounds=bounds, itermon=mon, gtol=100, maxiter=1000, maxfun=10000) print(solution)
def zipFit(V, P, Q, Vn=240.0, solver='fmin_powell'): """Solve for ZIP coefficients usable by GridLAB-D. V: voltage magnitude array P: real power array Q: reactive power array Vn: nominal voltage solver: either 'fmin_powell' to use mystic's modified scipy fmin_powell solver, or 'SLSQP' to use scipy's sequential least squares programming solver. """ # Estimate nominal power Sn = estimateNominalPower(P=P, Q=Q) # Massage into standard polynomial format Vbar = V / Vn Pbar = P / Sn Qbar = Q / Sn # Initial parameters for ZIP model # TODO: initialize from previously computed coefficients to start. # Why are we multiply by 1/18? par0 = np.ones(6) * (1 / 18) # Solve. if solver == 'fmin_powell': sol = my.fmin_powell(Object, args=(Vbar, Pbar, Qbar), x0=par0, contraint=Constrain, disp=False, gtol=GTOL, ftol=FTOL, full_output=True) # Extract the polynomial coefficients p = sol[0][0:3] q = sol[0][3:6] # Get the value of the objective function (so the squared error) err = sol[1] elif solver == 'SLSQP': sol = minimize(Object, par0, args=(Vbar, Pbar, Qbar), method='SLSQP', constraints={ 'type': 'eq', 'fun': Constrain }, bounds=None, options={'ftol': FTOL}) # Extract the polynomial coefficients p = sol.x[0:3] q = sol.x[3:6] # Get the value of the objective function (so the squared error) err = sol.fun else: raise UserWarning( 'Given solver, {}, is not implemented.'.format(solver)) # Some code Bin had in? #cons2 = {'type':'eq', 'fun': Constrain2} #cons3 = {'type':'eq', 'fun': Constrain3} """ if abs(Constrain(opti.x))>0.75: opti = minimize(Object, par0, args=(V1bar, P1bar, Q1bar), method='SLSQP', constraints = cons2, bounds = None) if abs(Constrain(opti.x))>2.0: opti = minimize(Object, par0, args=(V1bar, P1bar, Q1bar), method='SLSQP', constraints = cons3, bounds = None) """ # Convert the polynomial coefficients to GridLAB-D format (fractions and # power factors) coeff = polyToGLD(p, q) # Collect other useful information coeff['base_power'] = Sn coeff['error'] = err coeff['poly'] = (*p, *q) return coeff
def impose_valid(cutoff, model, guess=None, **kwds): """impose model validity on a given list of parameters w,x,y Optimization on w,x,y over the given bounds seeks sum(infeasibility) = 0. (this function is not ???-preserving) Inputs: cutoff -- maximum acceptable model invalidity |y - F(x')|; a single value model -- the model function, y' = F(x'), that approximates reality, y = G(x) guess -- the scenario providing an initial guess at validity, or a tuple of dimensions of the target scenario Additional Inputs: hausdorff -- norm; where if given, ytol = |y - F(x')| + |x - x'|/norm xtol -- acceptable pointwise graphical distance of model from reality tol -- acceptable optimizer termination before sum(infeasibility) = 0. bounds -- a tuple of sample bounds: bounds = (lower_bounds, upper_bounds) constraints -- a function that takes a flat list parameters x' = constraints(x) Outputs: pm -- a scenario with desired model validity Notes: xtol defines the n-dimensional base of a pilar of height cutoff, centered at each point. The region inside the pilar defines the space where a "valid" model must intersect. If xtol is not specified, then the base of the pilar will be a dirac at x' = x. This function performs an optimization to find a set of points where the model is valid. Here, tol is used to set the optimization termination for the sum(graphical_distances), while cutoff is used in defining the graphical_distance between x,y and x',F(x'). """ from numpy import sum as _sum, asarray from mystic.math.distance import graphical_distance, infeasibility, _npts if guess is None: message = "Requires a guess scenario, or a tuple of scenario dimensions." raise TypeError, message # get initial guess if hasattr(guess, 'pts'): # guess is a scenario pts = guess.pts # number of x guess = guess.flatten(all=True) else: pts = guess # guess is given as a tuple of 'pts' guess = None npts = _npts(pts) # number of Y # prepare bounds for solver bounds = kwds.pop('bounds', None) # if bounds are not set, use the default optimizer bounds if bounds is None: lower_bounds = []; upper_bounds = [] for n in pts: # bounds for n*x in each dimension (x2 due to weights) lower_bounds += [None]*n * 2 upper_bounds += [None]*n * 2 # also need bounds for npts*y values lower_bounds += [None]*npts upper_bounds += [None]*npts bounds = lower_bounds, upper_bounds bounds = asarray(bounds).T # plug in the 'constraints' function: param' = constraints(param) constraints = kwds.pop('constraints', None) # default is no constraints if not constraints: # if None (default), there are no constraints constraints = lambda x: x # 'wiggle room' tolerances ipop = kwds.pop('ipop', 10) #XXX: tune ipop (inner optimization)? imax = kwds.pop('imax', 10) #XXX: tune imax (inner optimization)? # tolerance for optimization on sum(y) tol = kwds.pop('tol', 0.0) # default npop = kwds.pop('npop', 20) #XXX: tune npop (outer optimization)? maxiter = kwds.pop('maxiter', 1000) #XXX: tune maxiter (outer optimization)? # if no guess was made, then use bounds constraints if guess is None: if npop: guess = bounds else: # fmin_powell needs a list params (not bounds) guess = [(a + b)/2. for (a,b) in bounds] # construct cost function to reduce sum(infeasibility) def cost(rv): """compute cost from a 1-d array of model parameters, where: cost = | sum( infeasibility ) | """ # converting rv to scenario points = scenario() points.load(rv, pts) # calculate infeasibility Rv = graphical_distance(model, points, ytol=cutoff, ipop=ipop, \ imax=imax, **kwds) v = infeasibility(Rv, cutoff) # converting v to E return _sum(v) #XXX: abs ? # construct and configure optimizer debug = False #!!! maxfun = 1e+6 crossover = 0.9; percent_change = 0.8 ftol = abs(tol); gtol = None #XXX: optimally, should be VTRCOG... if debug: print "lower bounds: %s" % bounds.T[0] print "upper bounds: %s" % bounds.T[1] # print "initial value: %s" % guess # use optimization to get model-valid points from mystic.solvers import diffev2, fmin_powell from mystic.monitors import Monitor, VerboseMonitor from mystic.strategy import Best1Bin, Best1Exp evalmon = Monitor(); stepmon = Monitor(); strategy = Best1Exp if debug: stepmon = VerboseMonitor(2) #!!! if npop: # use VTR results = diffev2(cost, guess, npop, ftol=ftol, gtol=gtol, bounds=bounds,\ maxiter=maxiter, maxfun=maxfun, constraints=constraints,\ cross=crossover, scale=percent_change, strategy=strategy,\ evalmon=evalmon, itermon=stepmon,\ full_output=1, disp=0, handler=False) else: # use VTR results = fmin_powell(cost, guess, ftol=ftol, gtol=gtol, bounds=bounds,\ maxiter=maxiter, maxfun=maxfun, constraints=constraints,\ evalmon=evalmon, itermon=stepmon,\ full_output=1, disp=0, handler=False) # repack the results pm = scenario() pm.load(results[0], pts) # params: w,x,y #if debug: print "final cost: %s" % results[1] if debug and results[2] >= maxiter: # iterations print "Warning: constraints solver terminated at maximum iterations" #func_evals = results[3] # evaluation return pm
#x[3] is the slack variable def func_value(d): curve_vec=[] for val in d: curve = (0.3 * val) + ((2 * (val ** (3/2))) / 3) curve_vec.append(curve) return curve_vec def func(x): curve = func_value(x[0:3]) return -(sum(np.dot(curve,production))-Q+x[3]) objective = lambda x: sum(np.dot(x[0:3],C))+1000*x[3] constraint = lambda x: func(x) @quadratic_inequality(constraint) def penalty(x): return 0.0 mon = VerboseMonitor(50) solution = diffev2(objective,x0,penalty=penalty,bounds=bounds,itermon=mon,gtol=100, maxiter=1000, maxfun=10000, npop=40) print(solution) mon = VerboseMonitor(50) solution = fmin_powell(objective,x0,penalty=penalty,bounds=bounds,itermon=mon,gtol=100, maxiter=1000, maxfun=10000) print(solution)
@quadratic_inequality(penalty1) @quadratic_inequality(penalty2) @quadratic_inequality(penalty3) @quadratic_inequality(penalty4) @quadratic_inequality(penalty5) @quadratic_inequality(penalty6) @quadratic_inequality(penalty7) @quadratic_inequality(penalty8) def penalty(x): return 0.0 solver = as_constraint(penalty) if __name__ == '__main__': x = [0]*len(xs) from mystic.solvers import fmin_powell from mystic.math import almostEqual result = fmin_powell(objective, x0=x, bounds=bounds, penalty=penalty, maxiter=1000, maxfun=100000, ftol=1e-12, xtol=1e-12, gtol=10, disp=False, full_output=True) assert almostEqual(result[0], xs, tol=1e-2) assert almostEqual(result[1], ys, rel=1e-2) # EOF
# Copyright (c) 1997-2016 California Institute of Technology. # Copyright (c) 2016-2019 The Uncertainty Quantification Foundation. # License: 3-clause BSD. The full license text is available at: # - https://github.com/uqfoundation/mystic/blob/master/LICENSE from optqp import _objective, bounds, xs, ys from mystic.penalty import quadratic_equality from mystic.constraints import with_penalty @with_penalty(quadratic_equality, k=1e4) def penalty(x): # == 0.0 return x[0]**3 - x[1] if __name__ == '__main__': from mystic.solvers import diffev2, fmin_powell from mystic.math import almostEqual result = diffev2(_objective, x0=bounds, bounds=bounds, penalty=penalty, npop=40, ftol=1e-8, gtol=100, disp=False, full_output=True) assert almostEqual(result[0], xs, rel=2e-2) assert almostEqual(result[1], ys, rel=2e-2) result = fmin_powell(_objective, x0=[-1.0,1.0], bounds=bounds, penalty=penalty, disp=False, full_output=True) assert almostEqual(result[0], xs, rel=2e-2) assert almostEqual(result[1], ys, rel=2e-2) # EOD
-3.0*x0 + 6.0*x1 + 12.0*(x8-8)**2 - 7.0*x9 <= 0.0 """ cf = generate_constraint( generate_solvers(simplify(equations, target=['x5', 'x3']))) pf = generate_penalty(generate_conditions(equations)) if __name__ == '__main__': x = [0] * len(xs) from mystic.solvers import fmin_powell from mystic.math import almostEqual from mystic.monitors import VerboseMonitor mon = VerboseMonitor(10) result = fmin_powell(objective, x0=x, bounds=bounds, penalty=pf, maxiter=1000, maxfun=100000, ftol=1e-12, xtol=1e-12, gtol=10, disp=False, full_output=True) assert almostEqual(result[0], xs, tol=1e-2) assert almostEqual(result[1], ys, rel=1e-2) # EOF
pens = ms.generate_penalty(ms.generate_conditions(eqns), k=1e3) bounds = [(0., None), (0., 4.)] # get the objective def objective(x): x = np.asarray(x) return x[0]**2 + 4*x[1]**2 - 32*x[1] + 64 x0 = np.random.rand(2) # compare against the exact minimum xs = np.array([2., 3.]) ys = objective(xs) sol = my.fmin_powell(objective, x0, constraint=cons, penalty=pens, disp=False, bounds=bounds, gtol=3, ftol=1e-6, full_output=True) assert mm.almostEqual(sol[0], xs, tol=1e-2) assert mm.almostEqual(sol[1], ys, tol=1e-2) sol = my.diffev(objective, bounds, constraint=cons, penalty=pens, disp=False, bounds=bounds, npop=10, gtol=100, ftol=1e-6, full_output=True) assert mm.almostEqual(sol[0], xs, tol=1e-2) assert mm.almostEqual(sol[1], ys, tol=1e-2) # EOF
bounds = [(None, None),(1.0, None)] # with penalty='penalty' applied, solution is: xs = [1,1] ys = -1.0 from mystic.symbolic import generate_conditions, generate_penalty pf = generate_penalty(generate_conditions(equations), k=1e4) from mystic.symbolic import generate_constraint, generate_solvers, solve cf = generate_constraint(generate_solvers(solve(equations))) # inverted objective, used in solving for the maximum _objective = lambda x: -objective(x) if __name__ == '__main__': from mystic.solvers import diffev2, fmin_powell from mystic.math import almostEqual result = diffev2(_objective, x0=bounds, bounds=bounds, constraint=cf, penalty=pf, npop=40, ftol=1e-8, gtol=100, disp=False, full_output=True) assert almostEqual(result[0], xs, rel=2e-2) assert almostEqual(result[1], ys, rel=2e-2) result = fmin_powell(_objective, x0=[-1.0,1.0], bounds=bounds, constraint=cf, penalty=pf, disp=False, full_output=True) assert almostEqual(result[0], xs, rel=2e-2) assert almostEqual(result[1], ys, rel=2e-2) # EOF
def zipFit(V, P, Q, Vn=240.0, solver='SLSQP', par0=PAR0): """Solve for ZIP coefficients usable by GridLAB-D. V: voltage magnitude array P: real power array Q: reactive power array Vn: nominal voltage solver: either 'fmin_powell' to use mystic's modified scipy fmin_powell solver, or 'SLSQP' to use scipy's sequential least squares programming solver. par0: Initial guess. Should be array of a1, a2, a3, b1, b2, b3 """ # Estimate nominal power. Sn = estimateNominalPower(P=P, Q=Q) # Massage into standard polynomial format. Vbar = V / Vn Pbar = P / Sn Qbar = Q / Sn # Solve. if solver == 'fmin_powell': ''' sol = my.fmin_powell(ZIPObjective, args=(Vbar, Pbar, Qbar), x0=par0, bounds=BOUNDS, contraints=ZIPConstraint, disp=False, gtol=GTOL, ftol=FTOL, full_output=True) ''' sol = my.fmin_powell(ZIPObjective, args=(Vbar, Pbar, Qbar), x0=par0, bounds=BOUNDS, contraints={ 'type': 'eq', 'fun': ZIPConstraint }, disp=False, ftol=FTOL, full_output=True) ''' # Penalty doesn't seem to work well (vs constraint). sol = my.fmin_powell(ZIPObjective, args=(Vbar, Pbar, Qbar), x0=par0, bounds=BOUNDS, penalty=ConstrainMystic, disp=False, ftol=FTOL, full_output=True) ''' # Track the polynomial solution for assignment later. poly = sol[0] # Get the value of the objective function (so the squared error) err = sol[1] # Check warnings. # TODO: handle failures. if sol[4] == 1: print('fmin_powell failed: maximum number of function iterations.') elif sol[4] == 2: print('fmin_powell failed: maximum number of iterations.') elif solver == 'SLSQP': sol = minimize(ZIPObjective, par0, args=(Vbar, Pbar, Qbar), method='SLSQP', constraints={ 'type': 'eq', 'fun': ZIPConstraint }, bounds=BOUNDS, options={ 'ftol': FTOL, 'maxiter': MAXITER }) # Track the polynomial solution for assignment later. poly = sol.x # Get the value of the objective function (so the squared error) err = sol.fun if not sol.success: # Failed to solve. For now, just print. # TODO: handle failures. #print('SLSQP failed: {}'.format(sol.message)) pass else: raise UserWarning( 'Given solver, {}, is not implemented.'.format(solver)) # Extract the polynomial coefficients p = np.array(poly[0:3]) q = np.array(poly[3:6]) # Convert the polynomial coefficients to GridLAB-D format (fractions and # power factors) coeff = polyToGLD(p, q) # Collect other useful information coeff['base_power'] = Sn coeff['error'] = err coeff['poly'] = poly return coeff
2.0*x0 + 2.0*x1 + x9 + x10 - 10.0 <= 0.0 2.0*x0 + 2.0*x2 + x9 + x11 - 10.0 <= 0.0 2.0*x1 + 2.0*x2 + x10 + x11 - 10.0 <= 0.0 -8.0*x0 + x9 <= 0.0 -8.0*x1 + x10 <= 0.0 -8.0*x2 + x11 <= 0.0 -2.0*x3 - x4 + x9 <= 0.0 -2.0*x5 - x6 + x10 <= 0.0 -2.0*x7 - x8 + x11 <= 0.0 """ cf = generate_constraint(generate_solvers(simplify(equations))) pf = generate_penalty(generate_conditions(equations)) if __name__ == '__main__': x = [0] * len(xs) from mystic.solvers import fmin_powell from mystic.math import almostEqual result = fmin_powell(objective, x0=x, bounds=bounds, constraints=cf, disp=False, full_output=True) assert almostEqual(result[0], xs, tol=1e-2) assert almostEqual(result[1], ys, tol=1e-2) # EOF
from g01 import objective, bounds, xs, ys from g01_alt import penalty1, penalty2, penalty3, penalty4, penalty5, \ penalty6, penalty7, penalty8, penalty9 from mystic.constraints import as_constraint, combined from mystic.penalty import linear_inequality penalties = (penalty1,penalty2,penalty3,penalty4,penalty5,\ penalty6,penalty7,penalty8,penalty9) penalty = combined(*[linear_inequality(pi)(lambda x: 0.) for pi in penalties]) solver = as_constraint(penalty) if __name__ == '__main__': x = [0] * len(xs) from mystic.solvers import fmin_powell, diffev from mystic.math import almostEqual result = fmin_powell(objective, x0=x, bounds=bounds, penalty=penalty, disp=False, full_output=True) assert almostEqual(result[0], xs, tol=1e-2) assert almostEqual(result[1], ys, tol=1e-2) # EOF
if __name__ == '__main__': from mystic.solvers import diffev2, fmin_powell from mystic.math import almostEqual result = diffev2(_objective, x0=bounds, bounds=bounds, constraint=cf, penalty=pf, npop=40, ftol=1e-8, gtol=100, disp=False, full_output=True) assert almostEqual(result[0], xs, rel=2e-2) assert almostEqual(result[1], ys, rel=2e-2) result = fmin_powell(_objective, x0=[-1.0, 1.0], bounds=bounds, constraint=cf, penalty=pf, disp=False, full_output=True) assert almostEqual(result[0], xs, rel=2e-2) assert almostEqual(result[1], ys, rel=2e-2) # EOF
x0 - 2*x1 - 4.0 <= 0.0 """ bounds = [(None, None),(0.0, None)] # with penalty='penalty' applied, solution is: xs = [0.5, 1.5] ys = 2.5 from mystic.symbolic import generate_conditions, generate_penalty pf = generate_penalty(generate_conditions(equations), k=1e3) from mystic.symbolic import generate_constraint, generate_solvers, simplify cf = generate_constraint(generate_solvers(simplify(equations))) if __name__ == '__main__': from mystic.solvers import diffev2, fmin_powell from mystic.math import almostEqual result = diffev2(objective, x0=bounds, bounds=bounds, penalty=pf, constraint=cf, npop=40, disp=False, full_output=True, ftol=1e-10, gtol=100) assert almostEqual(result[0], xs, rel=1e-2) assert almostEqual(result[1], ys, rel=1e-2) result = fmin_powell(objective, x0=[0.0,0.0], bounds=bounds, penalty=pf, constraint=cf, disp=False, full_output=True, gtol=3) assert almostEqual(result[0], xs, rel=1e-2) assert almostEqual(result[1], ys, rel=1e-2) # EOF
# License: 3-clause BSD. The full license text is available at: # - https://github.com/uqfoundation/mystic/blob/master/LICENSE """ Example: - Minimize Rosenbrock's Function with Powell's method. Demonstrates: - standard models - minimal solver interface """ # Powell's Directonal solver from mystic.solvers import fmin_powell # Rosenbrock function from mystic.models import rosen if __name__ == '__main__': print("Powell's Method") print("===============") # initial guess x0 = [0.8, 1.2, 0.7] # use Powell's method to minimize the Rosenbrock function solution = fmin_powell(rosen, x0) print(solution) # end of file
# - https://github.com/uqfoundation/mystic/blob/master/LICENSE """ Example: - Minimize Rosenbrock's Function with Powell's method. Demonstrates: - standard models - minimal solver interface """ # Powell's Directonal solver from mystic.solvers import fmin_powell # Rosenbrock function from mystic.models import rosen if __name__ == '__main__': print("Powell's Method") print("===============") # initial guess x0 = [0.8,1.2,0.7] # use Powell's method to minimize the Rosenbrock function solution = fmin_powell(rosen,x0) print(solution) # end of file
# Rosenbrock function from mystic.models import rosen iter = 0 # plot the parameter trajectories def print_params(params): global iter from numpy import asarray print "Generation %d has best fit parameters: %s" % (iter, asarray(params)) iter += 1 return if __name__ == '__main__': # initial guess x0 = [0.8, 1.2, 0.7] print_params(x0) # use Powell's method to minimize the Rosenbrock function solution = fmin_powell(rosen, x0, disp=1, callback=print_params, handler=True) print solution # end of file
print "Powell's Method" print "===============" # target and initial guess target = [-1.,4.,-5.,20.,5.] x0 = [-1.,2.,-3.,10.,5.] # generate 'observed' data x,datapts = noisy_data(target) # plot observed data plot_frame() plot_data(x,datapts) # generate cost function costfunction = PolyCostFactory(x,datapts,len(target)) # use Powell's method to solve 5th-order polynomial coefficients solution = fmin_powell(costfunction,x0) # compare solution with actual target 5th-order polynomial coefficients print "\nSolved Coefficients:\n %s\n" % ForwardPolyFactory(solution) print "Target Coefficients:\n %s\n" % ForwardPolyFactory(target) # plot solution versus target coefficients plot_solution(solution) getch() # end of file
from mystic.solvers import fmin_powell # Rosenbrock function from mystic.models import rosen # tools from mystic.monitors import VerboseMonitor if __name__ == '__main__': print "Powell's Method" print "===============" # initial guess x0 = [0.8, 1.2, 0.7] # define constraints function def constraints(x): # constrain the last x_i to be the same value as the first x_i x[-1] = x[0] return x # configure monitor stepmon = VerboseMonitor(1) # use Powell's method to minimize the Rosenbrock function solution = fmin_powell(rosen, x0, constraints=constraints, itermon=stepmon) print solution # end of file
b = ones(1) # # generic: build a constraint where (A*x == b) # from mystic.symbolic import linear_symbolic, solve, \ # generate_solvers as solvers, generate_constraint as constraint # norm = constraint(solvers(solve(linear_symbolic(A,b),target=['x0']))) # specific: build a constraint where (sum(x) == 1.0) from mystic.math.measures import normalize def norm(x): return normalize(x, mass=1.0) # solve the constrained quadratic programming problem from mystic.solvers import fmin_powell x = fmin_powell(objective, UB/npt, args=(H, f), bounds=zip(LB,UB), \ constraints=norm, ftol=1e-8, disp=1) # find support vectors sv = where(x > 0.001)[0] print "support vectors: ", sv # compare solved center and radius to generating center and radius center = dot(x, xy) R = linalg.norm(xy[sv[0], :] - center) print "x0, y0: (%f, %f) @ R0 = %f" % (x0, y0, R0) print "center: (%f, %f) @ R = %f" % (center[0], center[1], R) plot(xy, sv, x0, y0, R0, center, R) # EOF
bounds = [(13, 100), (0, 100)] # with penalty='penalty' applied, solution is: xs = [14.095, 0.84296079] ys = -6961.81387628 from mystic.symbolic import generate_constraint, generate_solvers, simplify from mystic.symbolic import generate_penalty, generate_conditions equations = """ (x0 - 5)**2 + (x1 - 5)**2 - 100 >= 0.0 (x0 - 6)**2 + (x1 - 5)**2 - 82.81 <= 0.0 """ cf = generate_constraint(generate_solvers(simplify(equations))) pf = generate_penalty(generate_conditions(equations), k=1e12) if __name__ == "__main__": x = [0] * len(xs) from mystic.solvers import fmin_powell from mystic.math import almostEqual result = fmin_powell(objective, x0=x, bounds=bounds, constraints=cf, penalty=pf, disp=False, full_output=True) assert almostEqual(result[0], xs, tol=1e-2) assert almostEqual(result[1], ys, rel=1e-2) # EOF
print "Powell's Method" print "===============" # target and initial guess target = [-1., 4., -5., 20., 5.] x0 = [-1., 2., -3., 10., 5.] # generate 'observed' data x, datapts = noisy_data(target) # plot observed data plot_frame() plot_data(x, datapts) # generate cost function costfunction = PolyCostFactory(x, datapts, len(target)) # use Powell's method to solve 5th-order polynomial coefficients solution = fmin_powell(costfunction, x0) # compare solution with actual target 5th-order polynomial coefficients print "\nSolved Coefficients:\n %s\n" % ForwardPolyFactory(solution) print "Target Coefficients:\n %s\n" % ForwardPolyFactory(target) # plot solution versus target coefficients plot_solution(solution) getch() # end of file
- parameter trajectories using callback """ # Powell's Directonal solver from mystic.solvers import fmin_powell # Rosenbrock function from mystic.models import rosen iter = 0 # plot the parameter trajectories def print_params(params): global iter from numpy import asarray print("Generation %d has best fit parameters: %s" % (iter,asarray(params))) iter += 1 return if __name__ == '__main__': # initial guess x0 = [0.8,1.2,0.7] print_params(x0) # use Powell's method to minimize the Rosenbrock function solution = fmin_powell(rosen,x0,disp=1,callback=print_params,handler=True) print(solution) # end of file
if maxiter is not None: # test iters <= maxiter assert my_x[2] <= maxiter return if __name__ == '__main__': x0 = [0, 0, 0] # check solutions versus results based on the random_seed # print("comparing against known results") sol = solvers.diffev(rosen, x0, npop=40, disp=0, full_output=True) assert almostEqual(sol[1], 0.0020640145337293249, tol=3e-3) sol = solvers.diffev2(rosen, x0, npop=40, disp=0, full_output=True) assert (almostEqual(sol[1], 0.0017516784703663288, tol=3e-3) or almostEqual(sol[1], 0.00496876027278, tol=3e-3)) # python3.x sol = solvers.fmin_powell(rosen, x0, disp=0, full_output=True) assert almostEqual(sol[1], 8.3173488898295291e-23) sol = solvers.fmin(rosen, x0, disp=0, full_output=True) assert almostEqual(sol[1], 1.1605792769954724e-09) solver2 = 'diffev2' for solver in ['diffev']: # print("comparing %s and %s from mystic" % (solver, solver2)) test_solvers(solver, solver2, x0, npop=40) test_solvers(solver, solver2, x0, npop=40, maxiter=None, maxfun=0) test_solvers(solver, solver2, x0, npop=40, maxiter=None, maxfun=1) test_solvers(solver, solver2, x0, npop=40, maxiter=None, maxfun=2) test_solvers(solver, solver2, x0, npop=40, maxiter=None, maxfun=9) test_solvers(solver, solver2, x0, npop=40, maxiter=0) test_solvers(solver, solver2, x0, npop=40, maxiter=1) test_solvers(solver, solver2, x0, npop=40, maxiter=2)
if __name__ == '__main__': print("Powell's Method") print("===============") # initial guess import random from mystic.tools import random_seed random_seed(123) ndim = 9 x0 = [random.uniform(-100,100) for i in range(ndim)] # draw frame and exact coefficients plot_exact() # use Powell's method to solve 8th-order Chebyshev coefficients solution = fmin_powell(chebyshev8cost,x0) # use pretty print for polynomials print(poly1d(solution)) # compare solution with actual 8th-order Chebyshev coefficients print("\nActual Coefficients:\n %s\n" % poly1d(chebyshev8coeffs)) # plot solution versus exact coefficients plot_solution(solution) getch() #XXX: or plt.show() ? # end of file
print "Powell's Method" print "===============" # initial guess x0 = [0.8,1.2,0.7] # define constraints factory function def constraints_factory(target): # define constraints function def constraints(x): # constrain the last x_i to be the same value as the first x_i x[-1] = x[0] # constrain x such that mean(x) == target if not almostEqual(mean(x), target): x = impose_mean(target, x) return x return constraints # configure constraints function constraints = constraints_factory(1.0) # configure monitor stepmon = VerboseMonitor(1) # use Powell's method to minimize the Rosenbrock function solution = fmin_powell(rosen,x0,constraints=constraints,itermon=stepmon) print solution # end of file
-2*x0 + 2*x1 <= -2 2*x0 - 4*x1 <= 0''' eqn = ''' x0**3 - x1 == 0''' cons = ms.generate_constraint( ms.generate_solvers(ms.simplify(eqn, target='x1'))) pens = ms.generate_penalty(ms.generate_conditions(ieqn), k=1e3) bounds = [(0., None), (1., None)] # get the objective def objective(x, sign=1): x = np.asarray(x) return sign * (2 * x[0] * x[1] + 2 * x[0] - x[0]**2 - 2 * x[1]**2) # solve x0 = np.random.rand(2) sol = my.fmin_powell(objective, x0, constraint=cons, penalty=pens, disp=True, bounds=bounds, gtol=3, ftol=1e-6, full_output=True, args=(-1, )) print('x* = %s; f(x*) = %s' % (sol[0], -sol[1]))
cf = generate_constraint(generate_solvers(simplify(equations))) # inverted objective, used in solving for the maximum _objective = lambda x: -objective(x) if __name__ == '__main__': from mystic.solvers import diffev2, fmin_powell from mystic.math import almostEqual result = diffev2(objective, x0=bounds, bounds=bounds, constraint=cf, penalty=pf, npop=40, disp=False, full_output=True) assert almostEqual(result[0], xs, rel=1e-2) assert almostEqual(result[1], ys, rel=1e-2) result = fmin_powell(objective, x0=[0.0,0.0], bounds=bounds, constraint=cf, penalty=pf, disp=False, full_output=True) assert almostEqual(result[0], xs, rel=1e-2) assert almostEqual(result[1], ys, rel=1e-2) # alternately, solving for the maximum result = diffev2(_objective, x0=bounds, bounds=bounds, constraint=cf, penalty=pf, npop=40, disp=False, full_output=True) assert almostEqual( result[0], _xs, rel=1e-2) assert almostEqual(-result[1], _ys, rel=1e-2) result = fmin_powell(_objective, x0=[0,0], bounds=bounds, constraint=cf, penalty=pf, npop=40, disp=False, full_output=True) assert almostEqual( result[0], _xs, rel=1e-2) assert almostEqual(-result[1], _ys, rel=1e-2) # EOF
result = diffev2(objective, x0=bounds, bounds=bounds, constraint=cf, penalty=pf, npop=40, disp=False, full_output=True) assert almostEqual(result[0], xs, rel=1e-2) assert almostEqual(result[1], ys, rel=1e-2) result = fmin_powell(objective, x0=[0.0, 0.0], bounds=bounds, constraint=cf, penalty=pf, disp=False, full_output=True) assert almostEqual(result[0], xs, rel=1e-2) assert almostEqual(result[1], ys, rel=1e-2) # alternately, solving for the maximum result = diffev2(_objective, x0=bounds, bounds=bounds, constraint=cf, penalty=pf, npop=40, disp=False, full_output=True)