def mysticCompute(self): #for b in range(10**5, budget, ba): for b in range(0, int(self.ba), int(self.ba / self.intermediate)): equations = "{} <= {}".format( "+".join(([ "x{}".format(i) for i in range(len(self.componentIndices)) ])), b) #print(equations) cf = generate_constraint(generate_solvers(simplify(equations))) pf = generate_penalty(generate_conditions(equations)) x0 = [ b / len(self.componentIndices) for i in self.componentIndices ] bounds = [(0, b) for i in self.componentIndices] result = diffev(self.sys_avail, x0=x0, bounds=bounds, constraints=cf, penalty=pf, npop=10, disp=True) #print(result) self.results.append(result) self.avail_.append(-self.sys_avail(result)) self.budget_.append(b) #print(self.n_gamma(result)) self.eta_.append(self.n_gamma(result)) return self.results
def _symbolic(): import mystic.symbolic as ms eqn = """x0 - 2*x1 > 0 x3 - 3*x2 < 0""" eqn = ms.simplify(eqn, target=['x1', 'x3']) cons = ms.generate_constraint(ms.generate_solvers(eqn)) return cons
def impose_reweighted_variance(v, samples, weights=None, solver=None): """impose a variance on a list of points by reweighting weights""" ndim = len(samples) if weights is None: weights = [1.0 / ndim] * ndim if solver is None or solver == 'fmin': from mystic.solvers import fmin as solver elif solver == 'fmin_powell': from mystic.solvers import fmin_powell as solver elif solver == 'diffev': from mystic.solvers import diffev as solver elif solver == 'diffev2': from mystic.solvers import diffev2 as solver norm = sum(weights) m = mean(samples, weights) inequality = "" equality = "" equality2 = "" equality3 = "" for i in range(ndim): inequality += "x%s >= 0.0\n" % (i) # positive equality += "x%s + " % (i) # normalized equality2 += "%s * x%s + " % (float(samples[i]), (i)) # mean equality3 += "x%s*(%s-%s)**2 + " % ((i), float(samples[i]), m) # var equality += "0.0 = %s\n" % float(norm) equality += equality2 + "0.0 = %s*%s\n" % (float(norm), m) equality += equality3 + "0.0 = %s*%s\n" % (float(norm), v) penalties = generate_penalty(generate_conditions(inequality)) constrain = generate_constraint(generate_solvers(solve(equality))) def cost(x): return sum(x) results = solver(cost, weights, constraints=constrain, \ penalty=penalties, disp=False, full_output=True) wts = list(results[0]) _norm = results[1] # should have _norm == norm warn = results[4] # nonzero if didn't converge #XXX: better to fail immediately if xlo < m < xhi... or the below? if warn or not almostEqual(_norm, norm): print "Warning: could not impose mean through reweighting" return None #impose_variance(v, samples, weights), weights return wts #samples, wts # "mean-preserving"
def impose_reweighted_variance(v, samples, weights=None, solver=None): """impose a variance on a list of points by reweighting weights""" ndim = len(samples) if weights is None: weights = [1.0/ndim] * ndim if solver is None or solver == 'fmin': from mystic.solvers import fmin as solver elif solver == 'fmin_powell': from mystic.solvers import fmin_powell as solver elif solver == 'diffev': from mystic.solvers import diffev as solver elif solver == 'diffev2': from mystic.solvers import diffev2 as solver norm = sum(weights) m = mean(samples, weights) inequality = "" equality = ""; equality2 = ""; equality3 = "" for i in range(ndim): inequality += "x%s >= 0.0\n" % (i) # positive equality += "x%s + " % (i) # normalized equality2 += "%s * x%s + " % (float(samples[i]),(i)) # mean equality3 += "x%s*(%s-%s)**2 + " % ((i),float(samples[i]),m) # var equality += "0.0 = %s\n" % float(norm) equality += equality2 + "0.0 = %s*%s\n" % (float(norm),m) equality += equality3 + "0.0 = %s*%s\n" % (float(norm),v) penalties = generate_penalty(generate_conditions(inequality)) constrain = generate_constraint(generate_solvers(solve(equality))) def cost(x): return sum(x) results = solver(cost, weights, constraints=constrain, \ penalty=penalties, disp=False, full_output=True) wts = list(results[0]) _norm = results[1] # should have _norm == norm warn = results[4] # nonzero if didn't converge #XXX: better to fail immediately if xlo < m < xhi... or the below? if warn or not almostEqual(_norm, norm): print "Warning: could not impose mean through reweighting" return None #impose_variance(v, samples, weights), weights return wts #samples, wts # "mean-preserving"
def mysticCompute(): #for b in range(10**5, budget, ba): results = [] for b in range(10**5, ba, (ba-10**5)/20): equations = "{} <= {}".format("+".join((["x{}".format(i) for i in range(len(componentIndices))])), b) print(equations) cf = generate_constraint(generate_solvers(simplify(equations))) pf = generate_penalty(generate_conditions(equations)) x0 = [b/len(componentIndices) for i in componentIndices] bounds = [(0,b) for i in componentIndices] result = diffev(sys, x0=x0, bounds=bounds, constraints=cf, penalty=pf, npop=10, disp=True) print(result) results.append(result) avail_.append(-sys(result)) budget_.append(b) print(eta(result)) eta_.append(eta(result)) return results
-3*x0 + x1 - 6.0 <= 0.0 x0 + 2*x1 - 4.0 <= 0.0 """ bounds = [(None, None),(-3.0, None)] # with penalty='penalty' applied, solution is: xs = [10.0, -3.0] ys = -22.0 # alternately, if solving for the maximum, the solution is: _xs = [-1.14285714, 2.57142857] _ys = 11.428571428571429 from mystic.symbolic import generate_conditions, generate_penalty pf = generate_penalty(generate_conditions(equations)) from mystic.symbolic import generate_constraint, generate_solvers, simplify 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)
# generate some sparse data xtrain = np.random.uniform(0, 100, size=(10, 4)) target = model(xtrain.T).T xtest = np.random.uniform(0, 100, size=(10, 4)) test = model(xtest.T).T # define some model constraints equations = """ 3*b + c > -0.75 4.5*b - d > 11.0 """ var = list('abcd') equations = simplify(equations, variables=var) cf = generate_constraint(generate_solvers(equations, variables=var)) if __name__ == '__main__': # build a kernel-transformed regressor ta = pre.FunctionTransformer(func=vectorize(cf, axis=1)) tp = pre.PolynomialFeatures(degree=3) e = lin.LinearRegression() # train and score, then test and score xtrain_ = tp.fit_transform(ta.fit_transform(xtrain)) assert 1.0 == e.fit(xtrain_, target).score(xtrain_, target) xtest_ = tp.fit_transform(ta.fit_transform(xtest)) assert 1 - e.score(xtest_, test) <= 1e-2 # EOF
import mystic.symbolic as ms import scipy.optimize as so equations = """ A*B + C >= 1 B <= A A >= -5 B >= -5 C >= -5 5 >= A 5 >= B 5 >= C """ var = list('ABC') eqns = ms.simplify(equations, variables=var, all=True) constrain = ms.generate_constraint(ms.generate_solvers(eqns, var), join=my.constraints.and_) def objective(x): return 3 * x[0] + 1.e-6 * x[0]**3 + 2 * x[1] + 2.e-6 / 3 * x[1]**3 + x[2]**2 mon = my.monitors.Monitor() def cost(x): kx = constrain(x) y = objective(kx) mon(kx, y) return y
x0, x1 = x return x0**2 + (x1 - 1)**2 bounds = [(-1, 1)] * 2 # with penalty='penalty' applied, solution is: xs, xs_ = [(0.5)**.5, 0.5], [-(0.5)**.5, 0.5] ys = 0.75 from mystic.symbolic import generate_constraint, generate_solvers, solve from mystic.symbolic import generate_penalty, generate_conditions equations = """ x1 - x0**2 = 0.0 """ cf = generate_constraint(generate_solvers(solve(equations, target='x1'))) pf = generate_penalty(generate_conditions(equations), k=1e12) if __name__ == '__main__': from mystic.solvers import diffev2 from mystic.math import almostEqual result = diffev2(objective, x0=bounds, bounds=bounds, constraints=cf, npop=40, disp=False, full_output=True)
Subject to: x[0] + 1*x[1] <= 7 -x[0] + 2*x[1] <= 4 where: 0 <= x[0] <= inf 0 <= x[1] <= 4 """ import numpy as np import mystic.symbolic as ms import mystic.solvers as my import mystic.math as mm # generate constraints and penalty for a linear system of equations A = np.array([[1, 1],[-1, 2]]) b = np.array([7,4]) eqns = ms.linear_symbolic(G=A, h=b) cons = ms.generate_constraint(ms.generate_solvers(ms.simplify(eqns))) 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)
def objective(x): x0,x1 = x return x0**2 + (x1 - 1)**2 bounds = [(-1,1)]*2 # with penalty='penalty' applied, solution is: xs, xs_ = [(0.5)**.5, 0.5], [-(0.5)**.5, 0.5] ys = 0.75 from mystic.symbolic import generate_constraint, generate_solvers, solve from mystic.symbolic import generate_penalty, generate_conditions equations = """ x1 - x0**2 = 0.0 """ cf = generate_constraint(generate_solvers(solve(equations, target='x1'))) pf = generate_penalty(generate_conditions(equations), k=1e12) if __name__ == '__main__': from mystic.solvers import diffev2 from mystic.math import almostEqual result = diffev2(objective, x0=bounds, bounds=bounds, constraints=cf, npop=40, disp=False, full_output=True) assert almostEqual(result[0], xs, tol=1e-2) \ or almostEqual(result[0], xs_, tol=1e-2) assert almostEqual(result[1], ys, rel=1e-2)
def cf(len=3): return generate_constraint(generate_solvers(solve(equations(len))))
98527*x0 + 34588*x1 + 5872*x2 + 59422*x4 + 65159*x6 - 1547604 - 30704*x3 - 29649*x5 == 0.0 98957*x1 + 83634*x2 + 69966*x3 + 62038*x4 + 37164*x5 + 85413*x6 - 1823553 - 93989*x0 == 0.0 900032 + 10949*x0 + 77761*x1 + 67052*x4 - 80197*x2 - 61944*x3 - 92964*x5 - 44550*x6 == 0.0 73947*x0 + 84391*x2 + 81310*x4 - 1164380 - 96253*x1 - 44247*x3 - 70582*x5 - 33054*x6 == 0.0 13057*x2 + 42253*x3 + 77527*x4 + 96552*x6 - 1185471 - 60152*x0 - 21103*x1 - 97932*x5 == 0.0 1394152 + 66920*x0 + 55679*x3 - 64234*x1 - 65337*x2 - 45581*x4 - 67707*x5 - 98038*x6 == 0.0 68550*x0 + 27886*x1 + 31716*x2 + 73597*x3 + 38835*x6 - 279091 - 88963*x4 - 76391*x5 == 0.0 76132*x1 + 71860*x2 + 22770*x3 + 68211*x4 + 78587*x5 - 480923 - 48224*x0 - 82817*x6 == 0.0 519878 + 94198*x1 + 87234*x2 + 37498*x3 - 71583*x0 - 25728*x4 - 25495*x5 - 70023*x6 == 0.0 361921 + 78693*x0 + 38592*x4 + 38478*x5 - 94129*x1 - 43188*x2 - 82528*x3 - 69025*x6 == 0.0 """ from mystic.symbolic import generate_penalty, generate_conditions pf = generate_penalty(generate_conditions(equations)) from mystic.symbolic import generate_constraint, generate_solvers, solve cf = generate_constraint(generate_solvers(solve(equations))) from numpy import round as npround if __name__ == '__main__': from mystic.solvers import diffev2 from mystic.math import almostEqual #result = diffev2(objective, x0=bounds, bounds=bounds, penalty=pf, npop=20, gtol=50, disp=True, full_output=True) #result = diffev2(objective, x0=bounds, bounds=bounds, penalty=pf, constraints=npround, npop=40, gtol=50, disp=True, full_output=True) result = diffev2(objective, x0=bounds, bounds=bounds, constraints=cf, npop=4, gtol=1, disp=True, full_output=True) print(result[0]) assert almostEqual(result[0], xs, tol=1e-8) #XXX: fails b/c rel & zero?
i=0 while i<= len(list): if list[i]==1: return i i=i+1 return len(list) # Define constraints cminconstr="x"+str(firstone(cvec))+" >= " for i in list(range(0,len(cvec))): if cvec[i]==1 and i!=firstone(cvec): cminconstr=cminconstr+"x"+str(i)+" + " cminconstr=cminconstr+"2" solv = generate_solvers(cminconstr,nvars=len(pointvec)) constraintfunc = generate_constraint(solv) #Run optimization if __name__ == '__main__': print("Powell's Method") print("===============") # dimensional information from mystic.tools import random_seed random_seed(12) ndim = len(pointvec) nbins = 1 #[2,1,2,1,2,1,2,1,1]
B < 0 """ print(equations) var = list('ABC') eqns = ms.simplify(equations, variables=var, all=True) if isinstance(eqns, str): _join = join_ = None print(eqns) else: _join, join_ = _or, or_ for eqn in eqns: print(eqn + '\n----------------------') constrain = ms.generate_constraint(ms.generate_solvers( eqns, var, locals=dict(e_=eps)), join=_join) solution = constrain([1, -2, 1]) print('solved: %s' % dict(zip(var, solution))) penalty = ms.generate_penalty(ms.generate_conditions(eqns, var, locals=dict(e_=eps)), join=join_) print('penalty: %s' % penalty(solution)) equations = """ A*(B-C) + 2*C > 1 B + C = 2*D D < 0
ys = 24.3062091 from mystic.symbolic import generate_constraint, generate_solvers, simplify from mystic.symbolic import generate_penalty, generate_conditions equations = """ 4.0*x0 + 5.0*x1 - 3.0*x6 + 9.0*x7 - 105.0 <= 0.0 10.0*x0 - 8.0*x1 - 17.0*x6 + 2.0*x7 <= 0.0 -8.0*x0 + 2.0*x1 + 5.0*x8 - 2.0*x9 - 12.0 <= 0.0 3.0*(x0-2)**2 + 4.0*(x1-3)**2 + 2.0*x2**2 - 7.0*x3 - 120.0 <= 0.0 5.0*x0**2 + 8.0*x1 + (x2-6)**2 - 2.0*x3 - 40.0 <= 0.0 0.5*(x0-8)**2 + 2.0*(x1-4)**2 + 3.0*x4**2 - x5 - 30.0 <= 0.0 x0**2 + 2.0*(x1-2)**2 - 2.0*x0*x1 + 14.0*x4 - 6.0*x5 <= 0.0 -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)
""" import numpy as np import mystic.symbolic as ms import mystic.solvers as my import mystic.math as mm # generate constraints and penalty for a nonlinear system of equations ieqn = ''' -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) x0 = np.random.rand(2) # compare against the exact minimum xs = np.array([2., 1.]) ys = objective(xs, -1)
import mystic.symbolic as ms import mystic.constraints as mc import mystic.models as mm from mystic.solvers import PowellDirectionalSolver, NelderMeadSimplexSolver, \ DifferentialEvolutionSolver, \ DifferentialEvolutionSolver2, \ LatticeSolver, BuckshotSolver, SparsitySolver almostEqual = my.math.almostEqual rosen = mm.rosen bounds = [(0.0001, 0.0002)] * 3 guess = [0.001, 0.001, 0.001] # generate the constraints _eqn = 'x0 + x1 + x2 <= .0005' _cons = ms.generate_constraint(ms.generate_solvers(ms.simplify(_eqn)), join=mc.and_) # explicitly include bounds in constraints eqn_ = '\n'.join([_eqn, ms.symbolic_bounds(*zip(*bounds))]) cons_ = ms.generate_constraint(ms.generate_solvers(ms.simplify(eqn_)), join=mc.and_) def test_constrained(TheSolver, tight, clip): ndim = len(bounds) solver = TheSolver(ndim) if TheSolver in [PowellDirectionalSolver, NelderMeadSimplexSolver]: solver.SetInitialPoints(guess) elif TheSolver in [ DifferentialEvolutionSolver, DifferentialEvolutionSolver2 ]:
eq_ = ''' abs(x0) - x1 > 0 x2/(x0 - x1) > 1 ''' res = ms.simplify(eq_, all=True) #print(res[0]) #print('') res0 = ms.simplify(eq_, all=False) #print(res0) #print('') assert res0 in res assert len(res) == 3 _eq = ''' x0**.5 == abs(x1 + x2) abs(x0) - x1 > 0 x2**2 > 1 ''' res = ms.simplify(_eq, all=True) import mystic.constraints as mc c = ms.generate_constraint(ms.generate_solvers(res), join=mc.or_) kx = c([1, 2, 3]) #print(kx) x0, x1, x2 = kx assert x0**.5 == abs(x1 + x2) assert abs(x0) - x1 > 0 assert x2**2 > 1
ys = 24.3062091 from mystic.symbolic import generate_constraint, generate_solvers, simplify from mystic.symbolic import generate_penalty, generate_conditions equations = """ 4.0*x0 + 5.0*x1 - 3.0*x6 + 9.0*x7 - 105.0 <= 0.0 10.0*x0 - 8.0*x1 - 17.0*x6 + 2.0*x7 <= 0.0 -8.0*x0 + 2.0*x1 + 5.0*x8 - 2.0*x9 - 12.0 <= 0.0 3.0*(x0-2)**2 + 4.0*(x1-3)**2 + 2.0*x2**2 - 7.0*x3 - 120.0 <= 0.0 5.0*x0**2 + 8.0*x1 + (x2-6)**2 - 2.0*x3 - 40.0 <= 0.0 0.5*(x0-8)**2 + 2.0*(x1-4)**2 + 3.0*x4**2 - x5 - 30.0 <= 0.0 x0**2 + 2.0*(x1-2)**2 - 2.0*x0*x1 + 14.0*x4 - 6.0*x5 <= 0.0 -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,
load = [i.mass for i in items] invest = [i.buy for i in items] constraints = ms.linear_symbolic(G=[load, invest], h=[max_load, max_spend]) # bounds (on x) bounds = [(0, i.limit) for i in items] # bounds constraints lo = 'x%s >= %s' lo = '\n'.join(lo % (i, str(float(j[0])).lstrip('0')) for (i, j) in enumerate(bounds)) hi = 'x%s <= %s' hi = '\n'.join(hi % (i, str(float(j[1])).lstrip('0')) for (i, j) in enumerate(bounds)) constraints = '\n'.join([lo, hi]).strip() + '\n' + constraints cf = ms.generate_constraint(ms.generate_solvers(ms.simplify(constraints)), join=mc.and_) pf = ms.generate_penalty(ms.generate_conditions(ms.simplify(constraints))) # integer constraints #constrain = mc.and_(mc.integers(float)(lambda x:x), cf) constrain = mc.integers(float)(lambda x: x) # solve mon = my.monitors.VerboseMonitor(10) result = my.solvers.diffev2(lambda x: -profit(x), bounds, npop=400, bounds=bounds, ftol=1e-6, gtol=100, itermon=mon,