Changeset 831
- Timestamp:
- 09/28/15 20:45:46 (8 months ago)
- Location:
- mystic
- Files:
-
- 23 edited
Legend:
- Unmodified
- Added
- Removed
-
mystic/examples/test_svc2.py
r830 r831 58 58 59 59 # build the constraints operator 60 from mystic.symbolic import linear_symbolic, solve, \60 from mystic.symbolic import linear_symbolic, solve, simplify, \ 61 61 generate_solvers as solvers, generate_constraint as constraint 62 62 constrain = linear_symbolic(Aeq,Beq) 63 #NOTE: assumes a single equation of the form: '1.0*x0 + ... = 0.0\n'63 #NOTE: HACK assumes a single equation of the form: '1.0*x0 + ... = 0.0\n' 64 64 x0,rhs = constrain.strip().split(' = ') 65 65 x0,xN = x0.split(' + ', 1) 66 66 N,x0 = x0.split("*") 67 67 constrain = "{x0} = ({rhs} - ({xN}))/{N}".format(x0=x0, xN=xN, N=N, rhs=rhs) 68 #NOTE: end slight hack(as mystic.symbolic.solve takes __forever__)68 #NOTE: end HACK (as mystic.symbolic.solve takes __forever__) 69 69 constrain = constraint(solvers(constrain)) 70 #constrain = constraint(solvers(solve(constrain))) 70 71 71 72 from mystic import supressed -
mystic/examples2/crypta.py
r817 r831 57 57 from mystic.constraints import unique, near_integers 58 58 59 from mystic.symbolic import generate_constraint, generate_solvers, solve 59 60 from mystic.symbolic import generate_penalty, generate_conditions 60 61 pf = generate_penalty(generate_conditions(equations,var),k=1e-6) 61 from mystic.constraints import as_constraint 62 cf = as_constraint(pf) 62 cf = generate_constraint(generate_solvers(solve(equations,var))) 63 63 64 64 from mystic.penalty import quadratic_inequality, quadratic_equality -
mystic/examples2/cvxlp.py
r811 r831 37 37 from mystic.symbolic import generate_conditions, generate_penalty 38 38 pf = generate_penalty(generate_conditions(equations), k=1e3) 39 from mystic.symbolic import generate_constraint, generate_solvers, simplify 40 cf = generate_constraint(generate_solvers(simplify(equations))) 41 39 42 40 43 … … 44 47 from mystic.math import almostEqual 45 48 46 result = diffev2(objective, x0=bounds, bounds=bounds, penalty=pf, npop=40, disp=False, full_output=True, ftol=1e-10, gtol=100)49 result = diffev2(objective, x0=bounds, bounds=bounds, penalty=pf, constraint=cf, npop=40, disp=False, full_output=True, ftol=1e-10, gtol=100) 47 50 assert almostEqual(result[0], xs, rel=1e-2) 48 51 assert almostEqual(result[1], ys, rel=1e-2) 49 52 50 result = fmin_powell(objective, x0=[0.0,0.0], bounds=bounds, penalty=pf, disp=False, full_output=True, gtol=3)53 result = fmin_powell(objective, x0=[0.0,0.0], bounds=bounds, penalty=pf, constraint=cf, disp=False, full_output=True, gtol=3) 51 54 assert almostEqual(result[0], xs, rel=1e-2) 52 55 assert almostEqual(result[1], ys, rel=1e-2) -
mystic/examples2/cvxqp.py
r811 r831 32 32 from mystic.symbolic import generate_conditions, generate_penalty 33 33 pf = generate_penalty(generate_conditions(equations), k=1e4) 34 from mystic.symbolic import generate_constraint, generate_solvers, solve 35 cf = generate_constraint(generate_solvers(solve(equations))) 34 36 35 37 … … 39 41 from mystic.math import almostEqual 40 42 41 result = diffev2(objective, x0=bounds, bounds=bounds, penalty=pf, npop=40, disp=False, full_output=True)43 result = diffev2(objective, x0=bounds, bounds=bounds, constraint=cf, penalty=pf, npop=40, disp=False, full_output=True) 42 44 assert almostEqual(result[0], xs, rel=2e-2) 43 45 assert almostEqual(result[1], ys, rel=2e-2) 44 46 45 result = fmin_powell(objective, x0=[0.0,0.0], bounds=bounds, penalty=pf, disp=False, full_output=True)47 result = fmin_powell(objective, x0=[0.0,0.0], bounds=bounds, constraint=cf, penalty=pf, disp=False, full_output=True) 46 48 assert almostEqual(result[0], xs, rel=2e-2) 47 49 assert almostEqual(result[1], ys, rel=2e-2) -
mystic/examples2/eq10.py
r817 r831 41 41 from mystic.symbolic import generate_penalty, generate_conditions 42 42 pf = generate_penalty(generate_conditions(equations)) 43 #from mystic.constraints import as_constraint, discrete44 #cf = as_constraint(pf)43 from mystic.symbolic import generate_constraint, generate_solvers, solve 44 cf = generate_constraint(generate_solvers(solve(equations))) 45 45 46 46 from numpy import round as npround … … 53 53 54 54 #result = diffev2(objective, x0=bounds, bounds=bounds, penalty=pf, npop=20, gtol=50, disp=True, full_output=True) 55 result = diffev2(objective, x0=bounds, bounds=bounds, penalty=pf, constraints=npround, npop=40, gtol=50, disp=True, full_output=True) 55 #result = diffev2(objective, x0=bounds, bounds=bounds, penalty=pf, constraints=npround, npop=40, gtol=50, disp=True, full_output=True) 56 result = diffev2(objective, x0=bounds, bounds=bounds, constraints=cf, npop=4, gtol=1, disp=True, full_output=True) 56 57 57 58 print result[0] -
mystic/examples2/eq20.py
r817 r831 51 51 from mystic.symbolic import generate_penalty, generate_conditions 52 52 pf = generate_penalty(generate_conditions(equations)) 53 #from mystic.constraints import as_constraint, discrete54 #cf = as_constraint(pf)53 from mystic.symbolic import generate_constraint, generate_solvers, solve 54 cf = generate_constraint(generate_solvers(solve(equations))) 55 55 56 56 from numpy import round as npround … … 63 63 64 64 #result = diffev2(objective, x0=bounds, bounds=bounds, penalty=pf, npop=20, gtol=50, disp=True, full_output=True) 65 result = diffev2(objective, x0=bounds, bounds=bounds, penalty=pf, constraints=npround, npop=40, gtol=50, disp=True, full_output=True) 65 #result = diffev2(objective, x0=bounds, bounds=bounds, penalty=pf, constraints=npround, npop=40, gtol=50, disp=True, full_output=True) 66 result = diffev2(objective, x0=bounds, bounds=bounds, constraints=cf, npop=4, gtol=1, disp=True, full_output=True) 66 67 67 68 print result[0] -
mystic/examples2/g01.py
r776 r831 23 23 ys = -15.0 24 24 25 from mystic.symbolic import generate_constraint, generate_solvers, s olve25 from mystic.symbolic import generate_constraint, generate_solvers, simplify 26 26 from mystic.symbolic import generate_penalty, generate_conditions 27 27 28 28 equations = """ 29 2 *x0 + 2*x1 + x9 + x10 - 10.0 <= 0.030 2 *x0 + 2*x2 + x9 + x11 - 10.0 <= 0.031 2 *x1 + 2*x2 + x10 + x11 - 10.0 <= 0.032 -8 *x0 + x9 <= 0.033 -8 *x1 + x10 <= 0.034 -8 *x2 + x11 <= 0.035 -2 *x3 - x4 + x9 <= 0.036 -2 *x5 - x6 + x10 <= 0.037 -2 *x7 - x8 + x11 <= 0.029 2.0*x0 + 2.0*x1 + x9 + x10 - 10.0 <= 0.0 30 2.0*x0 + 2.0*x2 + x9 + x11 - 10.0 <= 0.0 31 2.0*x1 + 2.0*x2 + x10 + x11 - 10.0 <= 0.0 32 -8.0*x0 + x9 <= 0.0 33 -8.0*x1 + x10 <= 0.0 34 -8.0*x2 + x11 <= 0.0 35 -2.0*x3 - x4 + x9 <= 0.0 36 -2.0*x5 - x6 + x10 <= 0.0 37 -2.0*x7 - x8 + x11 <= 0.0 38 38 """ 39 #cf = generate_constraint(generate_solvers(solve(equations))) #XXX: inequalities 39 cf = generate_constraint(generate_solvers(simplify(equations))) 40 40 pf = generate_penalty(generate_conditions(equations)) 41 41 42 from mystic.constraints import as_constraint43 44 cf = as_constraint(pf)45 42 46 43 … … 51 48 from mystic.math import almostEqual 52 49 53 result = fmin_powell(objective, x0=x, bounds=bounds, penalty=pf, disp=False, full_output=True)50 result = fmin_powell(objective, x0=x, bounds=bounds, constraints=cf, disp=False, full_output=True) 54 51 55 52 assert almostEqual(result[0], xs, tol=1e-2) -
mystic/examples2/g02.py
r776 r831 35 35 """ 36 36 37 from mystic.symbolic import generate_constraint, generate_solvers, s olve37 from mystic.symbolic import generate_constraint, generate_solvers, simplify 38 38 from mystic.symbolic import generate_penalty, generate_conditions 39 39 … … 42 42 sum([x0, x1, x2]) - 7.5*3 <= 0.0 43 43 """ 44 #cf = generate_constraint(generate_solvers(solve(equations))) #XXX: inequalities 44 cf = generate_constraint(generate_solvers(simplify(equations))) 45 45 pf = generate_penalty(generate_conditions(equations)) 46 47 from mystic.constraints import as_constraint48 49 cf = as_constraint(pf)50 46 51 47 … … 57 53 from mystic.math import almostEqual 58 54 59 result = diffev2(objective, x0=bounds, bounds=bounds, penalty=pf, npop=40, disp=False, full_output=True)55 result = diffev2(objective, x0=bounds, bounds=bounds, constraints=cf, penalty=pf, npop=40, disp=False, full_output=True) 60 56 61 57 assert almostEqual(result[0], xs, rel=1e-2) -
mystic/examples2/g05.py
r776 r831 24 24 ys = 5126.49810960 25 25 26 from mystic.symbolic import generate_constraint, generate_solvers, s olve26 from mystic.symbolic import generate_constraint, generate_solvers, simplify 27 27 from mystic.symbolic import generate_penalty, generate_conditions 28 28 … … 34 34 abs(1000*(sin(x3-.25) + sin(x3-x2-0.25)) + 1294.8) = 0.0 35 35 """ 36 #cf = generate_constraint(generate_solvers(s olve(equations))) #XXX: inequalities36 #cf = generate_constraint(generate_solvers(simplify(equations))) 37 37 pf = generate_penalty(generate_conditions(equations), k=1e12) 38 39 from mystic.constraints import as_constraint40 41 cf = as_constraint(pf)42 38 43 39 -
mystic/examples2/g06.py
r776 r831 24 24 ys = -6961.81387628 25 25 26 from mystic.symbolic import generate_constraint, generate_solvers, s olve26 from mystic.symbolic import generate_constraint, generate_solvers, simplify 27 27 from mystic.symbolic import generate_penalty, generate_conditions 28 28 … … 31 31 (x0 - 6)**2 + (x1 - 5)**2 - 82.81 <= 0.0 32 32 """ 33 #cf = generate_constraint(generate_solvers(solve(equations))) #XXX: inequalities 33 cf = generate_constraint(generate_solvers(simplify(equations))) 34 34 pf = generate_penalty(generate_conditions(equations), k=1e12) 35 36 from mystic.constraints import as_constraint37 38 cf = as_constraint(pf)39 35 40 36 … … 46 42 from mystic.math import almostEqual 47 43 48 result = fmin_powell(objective, x0=x, bounds=bounds, penalty=pf, disp=False, full_output=True)44 result = fmin_powell(objective, x0=x, bounds=bounds, constraints=cf, penalty=pf, disp=False, full_output=True) 49 45 50 46 assert almostEqual(result[0], xs, tol=1e-2) -
mystic/examples2/g07.py
r776 r831 27 27 ys = 24.3062091 28 28 29 from mystic.symbolic import generate_constraint, generate_solvers, s olve29 from mystic.symbolic import generate_constraint, generate_solvers, simplify 30 30 from mystic.symbolic import generate_penalty, generate_conditions 31 31 32 32 equations = """ 33 4 *x0 + 5*x1 - 3*x6 + 9*x7 - 105.0 <= 0.034 10 *x0 - 8*x1 - 17*x6 + 2*x7 <= 0.035 -8 *x0 + 2*x1 + 5*x8 - 2*x9 - 12.0 <= 0.036 3 *(x0-2)**2 + 4*(x1-3)**2 + 2*x2**2 - 7*x3 - 120.0 <= 0.037 5 *x0**2 + 8*x1 + (x2-6)**2 - 2*x3 - 40.0 <= 0.038 0.5*(x0-8)**2 + 2 *(x1-4)**2 + 3*x4**2 - x5 - 30.0 <= 0.039 x0**2 + 2 *(x1-2)**2 - 2*x0*x1 + 14*x4 - 6*x5 <= 0.040 -3 *x0 + 6*x1 + 12*(x8-8)**2 - 7*x9 <= 0.033 4.0*x0 + 5.0*x1 - 3.0*x6 + 9.0*x7 - 105.0 <= 0.0 34 10.0*x0 - 8.0*x1 - 17.0*x6 + 2.0*x7 <= 0.0 35 -8.0*x0 + 2.0*x1 + 5.0*x8 - 2.0*x9 - 12.0 <= 0.0 36 3.0*(x0-2)**2 + 4.0*(x1-3)**2 + 2.0*x2**2 - 7.0*x3 - 120.0 <= 0.0 37 5.0*x0**2 + 8.0*x1 + (x2-6)**2 - 2.0*x3 - 40.0 <= 0.0 38 0.5*(x0-8)**2 + 2.0*(x1-4)**2 + 3.0*x4**2 - x5 - 30.0 <= 0.0 39 x0**2 + 2.0*(x1-2)**2 - 2.0*x0*x1 + 14.0*x4 - 6.0*x5 <= 0.0 40 -3.0*x0 + 6.0*x1 + 12.0*(x8-8)**2 - 7.0*x9 <= 0.0 41 41 """ 42 #cf = generate_constraint(generate_solvers(solve(equations))) #XXX: inequalities 42 cf = generate_constraint(generate_solvers(simplify(equations, target=['x5','x3']))) 43 43 pf = generate_penalty(generate_conditions(equations)) 44 45 from mystic.constraints import as_constraint46 47 cf = as_constraint(pf)48 44 49 45 … … 54 50 from mystic.solvers import fmin_powell 55 51 from mystic.math import almostEqual 52 from mystic.monitors import VerboseMonitor 53 mon = VerboseMonitor(10) 56 54 57 55 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) -
mystic/examples2/g08.py
r776 r831 25 25 ys = -0.09582504 26 26 27 from mystic.symbolic import generate_constraint, generate_solvers, s olve27 from mystic.symbolic import generate_constraint, generate_solvers, simplify 28 28 from mystic.symbolic import generate_penalty, generate_conditions 29 29 … … 32 32 1.0 - x0 + (x1 - 4)**2 <= 0.0 33 33 """ 34 #cf = generate_constraint(generate_solvers(solve(equations))) #XXX: inequalities 34 cf = generate_constraint(generate_solvers(simplify(equations))) 35 35 pf = generate_penalty(generate_conditions(equations), k=1e12) 36 37 from mystic.constraints import as_constraint38 39 cf = as_constraint(pf)40 36 41 37 -
mystic/examples2/g09.py
r776 r831 25 25 ys = 680.6300573 26 26 27 from mystic.symbolic import generate_constraint, generate_solvers, s olve27 from mystic.symbolic import generate_constraint, generate_solvers, simplify 28 28 from mystic.symbolic import generate_penalty, generate_conditions 29 29 30 30 equations = """ 31 2 *x0**2 + 3*x1**4 + x2 + 4*x3**2 + 5*x4 - 127.0 <= 0.032 7 *x0 + 3*x1 + 10*x2**2 + x3 - x4 - 282.0 <= 0.033 23 *x0 + x1**2 + 6*x5**2 - 8*x6 - 196.0 <= 0.034 4 *x0**2 + x1**2 - 3*x0*x1 + 2*x2**2 + 5*x5 - 11*x6 <= 0.031 2.0*x0**2 + 3.0*x1**4 + x2 + 4.0*x3**2 + 5.0*x4 - 127.0 <= 0.0 32 7.0*x0 + 3.0*x1 + 10.0*x2**2 + x3 - x4 - 282.0 <= 0.0 33 23.0*x0 + x1**2 + 6.0*x5**2 - 8.0*x6 - 196.0 <= 0.0 34 4.0*x0**2 + x1**2 - 3.0*x0*x1 + 2.0*x2**2 + 5.0*x5 - 11.0*x6 <= 0.0 35 35 """ 36 #cf = generate_constraint(generate_solvers(solve(equations))) #XXX: inequalities 36 cf = generate_constraint(generate_solvers(simplify(equations))) 37 37 pf = generate_penalty(generate_conditions(equations), k=1e12) 38 39 from mystic.constraints import as_constraint40 41 cf = as_constraint(pf)42 38 43 39 … … 48 44 from mystic.math import almostEqual 49 45 50 result = diffev2(objective, x0=bounds, bounds=bounds, penalty=pf, npop=40, gtol=200, disp=False, full_output=True)46 result = diffev2(objective, x0=bounds, bounds=bounds, constraints=cf, penalty=pf, npop=40, gtol=200, disp=False, full_output=True) 51 47 52 48 assert almostEqual(result[0], xs, rel=1e-2) -
mystic/examples2/g10.py
r776 r831 25 25 ys = 7049.3307 26 26 27 from mystic.symbolic import generate_constraint, generate_solvers, s olve27 from mystic.symbolic import generate_constraint, generate_solvers, simplify 28 28 from mystic.symbolic import generate_penalty, generate_conditions 29 29 30 30 equations = """ 31 -1 + 0.0025*(x3 + x5) <= 0.032 -1 + 0.0025*(-x3 + x4 + x6) <= 0.033 -1 + 0.01*(-x4 + x7) <= 0.034 100 *x0 - x0*x5 + 833.33252*x3 - 83333.333 <= 0.035 x1*x3 - x1*x6 - 1250 *x3 + 1250*x4 <= 0.036 x2*x4 - x2*x7 - 2500 *x4 + 1250000 <= 0.031 -1.0 + 0.0025*(x3 + x5) <= 0.0 32 -1.0 + 0.0025*(-x3 + x4 + x6) <= 0.0 33 -1.0 + 0.01*(-x4 + x7) <= 0.0 34 100.0*x0 - x0*x5 + 833.33252*x3 - 83333.333 <= 0.0 35 x1*x3 - x1*x6 - 1250.0*x3 + 1250.0*x4 <= 0.0 36 x2*x4 - x2*x7 - 2500.0*x4 + 1250000.0 <= 0.0 37 37 """ 38 #cf = generate_constraint(generate_solvers(solve(equations))) #XXX: inequalities 38 cf = generate_constraint(generate_solvers(simplify(equations))) 39 39 pf = generate_penalty(generate_conditions(equations), k=1e12) 40 41 from mystic.constraints import as_constraint42 43 cf = as_constraint(pf)44 40 45 41 -
mystic/examples2/g13.py
r776 r831 27 27 ys = 0.05394983 28 28 29 from mystic.symbolic import generate_constraint, generate_solvers, s olve29 from mystic.symbolic import generate_constraint, generate_solvers, simplify 30 30 from mystic.symbolic import generate_penalty, generate_conditions 31 31 32 32 equations = """ 33 33 x0**2 + x1**2 + x2**2 + x3**2 + x4**2 - 10.0 = 0.0 34 x1*x2 - 5 *x3*x4 = 0.034 x1*x2 - 5.0*x3*x4 = 0.0 35 35 x0**3 + x1**3 + 1.0 = 0.0 36 36 """ 37 #cf = generate_constraint(generate_solvers(solve(equations))) #XXX: solve slow 37 cf = generate_constraint(generate_solvers(simplify(equations))) # slow solve 38 38 pf = generate_penalty(generate_conditions(equations)) 39 40 from mystic.constraints import as_constraint41 42 cf = as_constraint(pf)43 39 44 40 -
mystic/examples2/integer_programming.py
r819 r831 38 38 from mystic.symbolic import generate_penalty, generate_conditions 39 39 pf = generate_penalty(generate_conditions(equations)) 40 from mystic.symbolic import generate_constraint, generate_solvers, simplify 41 cf = generate_constraint(generate_solvers(simplify(equations))) 40 42 41 43 from mystic.constraints import integers -
mystic/examples2/lp.py
r788 r831 38 38 from mystic.symbolic import generate_conditions, generate_penalty 39 39 pf = generate_penalty(generate_conditions(equations)) 40 from mystic.symbolic import generate_constraint, generate_solvers, simplify 41 cf = generate_constraint(generate_solvers(simplify(equations))) 40 42 41 43 # inverted objective, used in solving for the maximum … … 48 50 from mystic.math import almostEqual 49 51 50 result = diffev2(objective, x0=bounds, bounds=bounds, penalty=pf, npop=40, disp=False, full_output=True)52 result = diffev2(objective, x0=bounds, bounds=bounds, constraint=cf, penalty=pf, npop=40, disp=False, full_output=True) 51 53 assert almostEqual(result[0], xs, rel=1e-2) 52 54 assert almostEqual(result[1], ys, rel=1e-2) 53 55 54 result = fmin_powell(objective, x0=[0.0,0.0], bounds=bounds, penalty=pf, disp=False, full_output=True)56 result = fmin_powell(objective, x0=[0.0,0.0], bounds=bounds, constraint=cf, penalty=pf, disp=False, full_output=True) 55 57 assert almostEqual(result[0], xs, rel=1e-2) 56 58 assert almostEqual(result[1], ys, rel=1e-2) 57 59 58 60 # alternately, solving for the maximum 59 result = diffev2(_objective, x0=bounds, bounds=bounds, penalty=pf, npop=40, disp=False, full_output=True)61 result = diffev2(_objective, x0=bounds, bounds=bounds, constraint=cf, penalty=pf, npop=40, disp=False, full_output=True) 60 62 assert almostEqual( result[0], _xs, rel=1e-2) 61 63 assert almostEqual(-result[1], _ys, rel=1e-2) 62 64 63 result = fmin_powell(_objective, x0=[0,0], bounds=bounds, penalty=pf, npop=40, disp=False, full_output=True)65 result = fmin_powell(_objective, x0=[0,0], bounds=bounds, constraint=cf, penalty=pf, npop=40, disp=False, full_output=True) 64 66 assert almostEqual( result[0], _xs, rel=1e-2) 65 67 assert almostEqual(-result[1], _ys, rel=1e-2) -
mystic/examples2/optqp.py
r816 r831 30 30 from mystic.symbolic import generate_conditions, generate_penalty 31 31 pf = generate_penalty(generate_conditions(equations), k=1e4) 32 from mystic.symbolic import generate_constraint, generate_solvers, solve 33 cf = generate_constraint(generate_solvers(solve(equations))) 32 34 33 35 # inverted objective, used in solving for the maximum … … 40 42 from mystic.math import almostEqual 41 43 42 result = diffev2(_objective, x0=bounds, bounds=bounds, penalty=pf, npop=40, ftol=1e-8, gtol=100, disp=False, full_output=True)44 result = diffev2(_objective, x0=bounds, bounds=bounds, constraint=cf, penalty=pf, npop=40, ftol=1e-8, gtol=100, disp=False, full_output=True) 43 45 assert almostEqual(result[0], xs, rel=2e-2) 44 46 assert almostEqual(result[1], ys, rel=2e-2) 45 47 46 result = fmin_powell(_objective, x0=[-1.0,1.0], bounds=bounds, penalty=pf, disp=False, full_output=True)48 result = fmin_powell(_objective, x0=[-1.0,1.0], bounds=bounds, constraint=cf, penalty=pf, disp=False, full_output=True) 47 49 assert almostEqual(result[0], xs, rel=2e-2) 48 50 assert almostEqual(result[1], ys, rel=2e-2) 49 51 50 52 51 # EO D53 # EOF -
mystic/examples2/spring.py
r776 r831 25 25 ys = 0.01266523 26 26 27 from mystic.symbolic import generate_constraint, generate_solvers, s olve27 from mystic.symbolic import generate_constraint, generate_solvers, simplify 28 28 from mystic.symbolic import generate_penalty, generate_conditions 29 29 … … 34 34 (x0 + x1)/1.5 - 1.0 <= 0.0 35 35 """ 36 # cf = generate_constraint(generate_solvers(solve(equations))) #XXX: inequalities36 # cf = generate_constraint(generate_solvers(simplify(equations))) #XXX: slow 37 37 pf = generate_penalty(generate_conditions(equations), k=1e12) 38 39 from mystic.constraints import as_constraint40 41 cf = as_constraint(pf)42 38 43 39 -
mystic/examples2/vessel.py
r776 r831 25 25 ys = 5804.3762083 26 26 27 from mystic.symbolic import generate_constraint, generate_solvers, s olve27 from mystic.symbolic import generate_constraint, generate_solvers, simplify 28 28 from mystic.symbolic import generate_penalty, generate_conditions 29 29 … … 34 34 x3 - 240.0 <= 0.0 35 35 """ 36 #cf = generate_constraint(generate_solvers(solve(equations))) #XXX: inequalities 36 cf = generate_constraint(generate_solvers(simplify(equations))) 37 37 pf = generate_penalty(generate_conditions(equations), k=1e12) 38 39 from mystic.constraints import as_constraint40 41 cf = as_constraint(pf)42 38 43 39 … … 47 43 from mystic.solvers import diffev2 48 44 from mystic.math import almostEqual 45 from mystic.monitors import VerboseMonitor 46 mon = VerboseMonitor(10) 49 47 50 result = diffev2(objective, x0=bounds, bounds=bounds, penalty=pf, npop=40, gtol=500, disp=False, full_output=True)48 result = diffev2(objective, x0=bounds, bounds=bounds, constraints=cf, penalty=pf, npop=40, gtol=50, disp=False, full_output=True, itermon=mon) 51 49 52 50 assert almostEqual(result[0], xs, rel=1e-2) -
mystic/mystic/_symbolic.py
r792 r831 356 356 code += '_xlist = %s\n' % ','.join(targeted) 357 357 code += '_elist = [symsol(['+eqlist+'], [i]) for i in _xlist]\n' 358 code += '_elist = [i if isinstance(i, dict) else {j:i[-1][-1]} for j,i in zip(_xlist,_elist) ]\n'358 code += '_elist = [i if isinstance(i, dict) else {j:i[-1][-1]} for j,i in zip(_xlist,_elist) if i]\n' 359 359 code += 'soln = {}\n' 360 360 code += '[soln.update(i) for i in _elist if i]\n' … … 362 362 code += 'soln = symsol([' + eqlist + '], [' + target[0] + '])\n' 363 363 #code += 'soln = symsol([' + eqlist + '], [' + targeted[0] + '])\n' 364 code += 'soln = soln if isinstance(soln, dict) else {' + target[0] + ': soln[-1][-1]} \n'364 code += 'soln = soln if isinstance(soln, dict) else {' + target[0] + ': soln[-1][-1]} if soln else ""\n' 365 365 ######################################################################## 366 366 -
mystic/mystic/symbolic.py
r802 r831 13 13 from __future__ import division 14 14 15 __all__ = ['linear_symbolic','replace_variables','get_variables','solve', 15 __all__ = ['linear_symbolic','replace_variables','get_variables', 16 'solve','simplify','comparator', 16 17 'penalty_parser','constraints_parser','generate_conditions', 17 18 'generate_solvers','generate_penalty','generate_constraint'] … … 97 98 totalconstraints = ineqstring + eqstring 98 99 return totalconstraints 100 101 102 def comparator(equation): 103 """identify the comparator (e.g. '<', '=', ...) in a constraints equation""" 104 if '\n' in equation.strip(): #XXX: failure throws error or returns ''? 105 return [comparator(eqn) for eqn in equation.strip().split('\n') if eqn] 106 return '<=' if equation.count('<=') else '<' if equation.count('<') else \ 107 '>=' if equation.count('>=') else '>' if equation.count('>') else \ 108 '!=' if equation.count('!=') else \ 109 '==' if equation.count('==') else '=' if equation.count('=') else '' 110 111 def simplify(constraints, variables='x', target=None, **kwds): 112 """simplify a system of symbolic constraints equations. 113 114 Returns a system of equations where a single variable has been isolated on 115 the left-hand side of each constraints equation, thus all constraints are 116 of the form "x_i = f(x)". 117 118 Inputs: 119 constraints -- a string of symbolic constraints, with one constraint 120 equation per line. Standard python syntax should be followed (with 121 the math and numpy modules already imported). 122 123 For example: 124 >>> constraints = ''' 125 ... x0 - x2 <= 2. 126 ... x2 = x3*2.''' 127 >>> print simplify(constraints) 128 x0 <= x2 + 2.0 129 x2 = 2.0*x3 130 >>> constraints = ''' 131 ... x0 - x1 - 1.0 = mean([x0,x1]) 132 ... mean([x0,x1,x2]) >= x2''' 133 >>> print simplify(constraints) 134 x0 = 3.0*x1 + 2.0 135 x0 >= -x1 + 2*x2 136 137 Additional Inputs: 138 variables -- desired variable name. Default is 'x'. A list of variable 139 name strings is also accepted for when desired variable names 140 don't have the same base, and can include variables that are not 141 found in the constraints equation string. 142 target -- list providing the order for which the variables will be solved. 143 If there are "N" constraint equations, the first "N" variables given 144 will be selected as the dependent variables. By default, increasing 145 order is used. 146 147 Further Inputs: 148 locals -- a dictionary of additional variables used in the symbolic 149 constraints equations, and their desired values. 150 cycle -- boolean to cycle the order for which the variables are solved. 151 If cycle is True, there should be more variety on the left-hand side 152 of the simplified equations. By default, the variables do not cycle. 153 """ 154 import random 155 _locals = {} 156 # default is _locals with numpy and math imported 157 # numpy throws an 'AttributeError', but math passes error to sympy 158 code = """from numpy import *; from math import *;""" # prefer math 159 code += """from numpy import mean as average;""" # use np.mean not average 160 code += """from numpy import var as variance;""" # look like mystic.math 161 code += """from numpy import ptp as spread;""" # look like mystic.math 162 code += """_sqrt = lambda x:x**.5;""" # 'domain error' to 'negative power' 163 code = compile(code, '<string>', 'exec') 164 exec code in _locals 165 166 def _flip(cmp): 167 "flip the comparator (i.e. '<' to '>', and '<=' to '>=')" 168 return '<=' if cmp == '>=' else '<' if cmp == '>' else \ 169 '>=' if cmp == '<=' else '>' if cmp == '<' else cmp 170 171 def _simplify(eqn, rand=random.random, target=None, **kwds): 172 'isolate one variable on the lhs' 173 verbose = kwds.get('verbose', False) 174 vars = kwds.get('variables', 'x') 175 cmp = comparator(eqn) 176 res = solve(eqn.replace(cmp,'='), target=target, **kwds) 177 _eqn = res.replace('=',cmp) 178 if verbose: print _eqn 179 if not cmp.count('<')+cmp.count('>'): 180 return _eqn 181 # evaluate expression to see if comparator needs to be flipped 182 locals = kwds['locals'] if 'locals' in kwds else None 183 if locals is None: locals = {} 184 locals.update(dict((var,rand()) for var in get_variables(res, vars))) 185 locals_ = _locals.copy() 186 locals_.update(locals) #XXX: allow this? 187 # make sure '=' is '==' so works in eval 188 _cmp = comparator(_eqn) 189 variants = [100000,-200000,100100,-200,110,-20,11,-2,1] #HACK 190 #HACK: avoid (rand-M)**(1/N) where (rand-M) negative; sqrt(x) to x**.5 191 before = eqn.replace(cmp, '==') if cmp == '=' else eqn 192 after = _eqn.replace(_cmp, '==') if _cmp == '=' else _eqn 193 before = before.replace('sqrt','_sqrt') 194 after = after.replace('sqrt','_sqrt') 195 while variants: 196 try: 197 after, before = eval(after, locals_), eval(before, locals_) 198 break 199 except ValueError as error: 200 if error.message.startswith('negative number') and \ 201 error.message.endswith('raised to a fractional power'): 202 val = variants.pop() 203 [locals_.update({k:v+val}) for k,v in locals_.items() if k in get_variables(_eqn, vars)] 204 else: 205 raise error 206 else: #END HACK 207 after, before = eval(after, locals_), eval(before, locals_) 208 if before == after: 209 return _eqn 210 # flip comparator, then return 211 cmp = _flip(cmp) 212 return res.replace('=',cmp) 213 cycle = kwds.get('cycle', False) 214 eqns = [] 215 used = [] 216 for eqn in constraints.strip().split('\n'): 217 # get least used, as they are likely to be simpler 218 vars = get_variables(eqn, variables) 219 vars.sort(key=eqn.count) #XXX: better to sort by count(var+'**')? 220 vars = target[:] if target else vars 221 if cycle: vars = [var for var in vars if var not in used] + used 222 while vars: 223 try: # cycle through variables trying 'simplest' first 224 res = _simplify(eqn, variables=variables, target=vars, **kwds) 225 eqns.append(res) 226 used.append(res.split(comparator(res),1)[0].strip()) 227 break 228 except ValueError: 229 if isinstance(vars, basestring): vars = [] 230 else: vars.pop(0) 231 else: # failure... so re-raise error 232 res = _simplify(eqn, variables=variables, target=target, **kwds) 233 eqns.append(res) 234 #eqns = (_simplify(eqn, **kwds) for eqn in constraints.strip().split('\n')) 235 return '\n'.join(eqns) 99 236 100 237 -
mystic/tests/test_symbolic.py
r781 r831 76 76 assert (x[1] - x[0]) - 1.0 == mean(x[:-1]) 77 77 78 def test_simplify(): 79 constraints = """ 80 mean([x0, x1, x2]) <= 5.0 81 x0 <= x1 + x2""" 82 83 from mystic.math.measures import mean 84 _constraints = simplify(constraints) 85 solv = generate_solvers(_constraints) 86 constraint = generate_constraint(solv) 87 x = constraint([1.0, -2.0, -3.0]) 88 assert all(x) == all([-5.0, -2.0, -3.0]) 89 assert mean(x) <= 5.0 90 assert x[0] <= x[1] + x[2] 91 78 92 79 93 if __name__ == '__main__': … … 82 96 test_generate_constraint() 83 97 test_solve_constraint() 98 test_simplify() 84 99
Note: See TracChangeset
for help on using the changeset viewer.