Changeset 831 for mystic


Ignore:
Timestamp:
09/28/15 20:45:46 (8 months ago)
Author:
mmckerns
Message:

add simplify to simplify equations for a single variable on the LHS;
use simplify in many examples where symbolic equations are given;
add comparator function, to determine the comparator ('<=', '=', etc);
add test_simplify to test_symbolic

Location:
mystic
Files:
23 edited

Legend:

Unmodified
Added
Removed
  • mystic/examples/test_svc2.py

    r830 r831  
    5858 
    5959# build the constraints operator 
    60 from mystic.symbolic import linear_symbolic, solve, \ 
     60from mystic.symbolic import linear_symbolic, solve, simplify, \ 
    6161     generate_solvers as solvers, generate_constraint as constraint 
    6262constrain = 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' 
    6464x0,rhs = constrain.strip().split(' = ') 
    6565x0,xN = x0.split(' + ', 1)  
    6666N,x0 = x0.split("*") 
    6767constrain = "{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__) 
    6969constrain = constraint(solvers(constrain)) 
     70#constrain = constraint(solvers(solve(constrain))) 
    7071 
    7172from mystic import supressed 
  • mystic/examples2/crypta.py

    r817 r831  
    5757from mystic.constraints import unique, near_integers 
    5858 
     59from mystic.symbolic import generate_constraint, generate_solvers, solve 
    5960from mystic.symbolic import generate_penalty, generate_conditions 
    6061pf = generate_penalty(generate_conditions(equations,var),k=1e-6) 
    61 from mystic.constraints import as_constraint 
    62 cf = as_constraint(pf) 
     62cf = generate_constraint(generate_solvers(solve(equations,var))) 
    6363 
    6464from mystic.penalty import quadratic_inequality, quadratic_equality 
  • mystic/examples2/cvxlp.py

    r811 r831  
    3737from mystic.symbolic import generate_conditions, generate_penalty 
    3838pf = generate_penalty(generate_conditions(equations), k=1e3) 
     39from mystic.symbolic import generate_constraint, generate_solvers, simplify 
     40cf = generate_constraint(generate_solvers(simplify(equations))) 
     41 
    3942 
    4043 
     
    4447  from mystic.math import almostEqual 
    4548 
    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) 
    4750  assert almostEqual(result[0], xs, rel=1e-2) 
    4851  assert almostEqual(result[1], ys, rel=1e-2) 
    4952 
    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) 
    5154  assert almostEqual(result[0], xs, rel=1e-2) 
    5255  assert almostEqual(result[1], ys, rel=1e-2) 
  • mystic/examples2/cvxqp.py

    r811 r831  
    3232from mystic.symbolic import generate_conditions, generate_penalty 
    3333pf = generate_penalty(generate_conditions(equations), k=1e4) 
     34from mystic.symbolic import generate_constraint, generate_solvers, solve 
     35cf = generate_constraint(generate_solvers(solve(equations))) 
    3436 
    3537 
     
    3941  from mystic.math import almostEqual 
    4042 
    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) 
    4244  assert almostEqual(result[0], xs, rel=2e-2) 
    4345  assert almostEqual(result[1], ys, rel=2e-2) 
    4446 
    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) 
    4648  assert almostEqual(result[0], xs, rel=2e-2) 
    4749  assert almostEqual(result[1], ys, rel=2e-2) 
  • mystic/examples2/eq10.py

    r817 r831  
    4141from mystic.symbolic import generate_penalty, generate_conditions 
    4242pf = generate_penalty(generate_conditions(equations)) 
    43 #from mystic.constraints import as_constraint, discrete 
    44 #cf = as_constraint(pf) 
     43from mystic.symbolic import generate_constraint, generate_solvers, solve 
     44cf = generate_constraint(generate_solvers(solve(equations))) 
    4545 
    4646from numpy import round as npround 
     
    5353 
    5454   #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) 
    5657 
    5758    print result[0] 
  • mystic/examples2/eq20.py

    r817 r831  
    5151from mystic.symbolic import generate_penalty, generate_conditions 
    5252pf = generate_penalty(generate_conditions(equations)) 
    53 #from mystic.constraints import as_constraint, discrete 
    54 #cf = as_constraint(pf) 
     53from mystic.symbolic import generate_constraint, generate_solvers, solve 
     54cf = generate_constraint(generate_solvers(solve(equations))) 
    5555 
    5656from numpy import round as npround 
     
    6363 
    6464   #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) 
    6667 
    6768    print result[0] 
  • mystic/examples2/g01.py

    r776 r831  
    2323ys = -15.0 
    2424 
    25 from mystic.symbolic import generate_constraint, generate_solvers, solve 
     25from mystic.symbolic import generate_constraint, generate_solvers, simplify 
    2626from mystic.symbolic import generate_penalty, generate_conditions 
    2727 
    2828equations = """ 
    29 2*x0 + 2*x1 + x9 + x10 - 10.0 <= 0.0 
    30 2*x0 + 2*x2 + x9 + x11 - 10.0 <= 0.0 
    31 2*x1 + 2*x2 + x10 + x11 - 10.0 <= 0.0 
    32 -8*x0 + x9 <= 0.0 
    33 -8*x1 + x10 <= 0.0 
    34 -8*x2 + x11 <= 0.0 
    35 -2*x3 - x4 + x9 <= 0.0 
    36 -2*x5 - x6 + x10 <= 0.0 
    37 -2*x7 - x8 + x11 <= 0.0 
     292.0*x0 + 2.0*x1 + x9 + x10 - 10.0 <= 0.0 
     302.0*x0 + 2.0*x2 + x9 + x11 - 10.0 <= 0.0 
     312.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 
    3838""" 
    39 #cf = generate_constraint(generate_solvers(solve(equations))) #XXX: inequalities 
     39cf = generate_constraint(generate_solvers(simplify(equations))) 
    4040pf = generate_penalty(generate_conditions(equations)) 
    4141 
    42 from mystic.constraints import as_constraint 
    43  
    44 cf = as_constraint(pf) 
    4542 
    4643 
     
    5148    from mystic.math import almostEqual 
    5249 
    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) 
    5451 
    5552    assert almostEqual(result[0], xs, tol=1e-2) 
  • mystic/examples2/g02.py

    r776 r831  
    3535""" 
    3636 
    37 from mystic.symbolic import generate_constraint, generate_solvers, solve 
     37from mystic.symbolic import generate_constraint, generate_solvers, simplify 
    3838from mystic.symbolic import generate_penalty, generate_conditions 
    3939 
     
    4242sum([x0, x1, x2]) - 7.5*3 <= 0.0 
    4343""" 
    44 #cf = generate_constraint(generate_solvers(solve(equations))) #XXX: inequalities 
     44cf = generate_constraint(generate_solvers(simplify(equations))) 
    4545pf = generate_penalty(generate_conditions(equations)) 
    46  
    47 from mystic.constraints import as_constraint 
    48  
    49 cf = as_constraint(pf) 
    5046 
    5147 
     
    5753    from mystic.math import almostEqual 
    5854 
    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) 
    6056 
    6157    assert almostEqual(result[0], xs, rel=1e-2) 
  • mystic/examples2/g05.py

    r776 r831  
    2424ys = 5126.49810960 
    2525 
    26 from mystic.symbolic import generate_constraint, generate_solvers, solve 
     26from mystic.symbolic import generate_constraint, generate_solvers, simplify 
    2727from mystic.symbolic import generate_penalty, generate_conditions 
    2828 
     
    3434abs(1000*(sin(x3-.25) + sin(x3-x2-0.25)) + 1294.8) = 0.0 
    3535""" 
    36 #cf = generate_constraint(generate_solvers(solve(equations))) #XXX: inequalities 
     36#cf = generate_constraint(generate_solvers(simplify(equations))) 
    3737pf = generate_penalty(generate_conditions(equations), k=1e12) 
    38  
    39 from mystic.constraints import as_constraint 
    40  
    41 cf = as_constraint(pf) 
    4238 
    4339 
  • mystic/examples2/g06.py

    r776 r831  
    2424ys = -6961.81387628 
    2525 
    26 from mystic.symbolic import generate_constraint, generate_solvers, solve 
     26from mystic.symbolic import generate_constraint, generate_solvers, simplify 
    2727from mystic.symbolic import generate_penalty, generate_conditions 
    2828 
     
    3131(x0 - 6)**2 + (x1 - 5)**2 - 82.81 <= 0.0 
    3232""" 
    33 #cf = generate_constraint(generate_solvers(solve(equations))) #XXX: inequalities 
     33cf = generate_constraint(generate_solvers(simplify(equations))) 
    3434pf = generate_penalty(generate_conditions(equations), k=1e12) 
    35  
    36 from mystic.constraints import as_constraint 
    37  
    38 cf = as_constraint(pf) 
    3935 
    4036 
     
    4642    from mystic.math import almostEqual 
    4743 
    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) 
    4945 
    5046    assert almostEqual(result[0], xs, tol=1e-2) 
  • mystic/examples2/g07.py

    r776 r831  
    2727ys = 24.3062091 
    2828 
    29 from mystic.symbolic import generate_constraint, generate_solvers, solve 
     29from mystic.symbolic import generate_constraint, generate_solvers, simplify 
    3030from mystic.symbolic import generate_penalty, generate_conditions 
    3131 
    3232equations = """ 
    33 4*x0 + 5*x1 - 3*x6 + 9*x7 - 105.0 <= 0.0 
    34 10*x0 - 8*x1 - 17*x6 + 2*x7 <= 0.0 
    35 -8*x0 + 2*x1 + 5*x8 - 2*x9 - 12.0 <= 0.0 
    36 3*(x0-2)**2 + 4*(x1-3)**2 + 2*x2**2 - 7*x3 - 120.0 <= 0.0 
    37 5*x0**2 + 8*x1 + (x2-6)**2 - 2*x3 - 40.0 <= 0.0 
    38 0.5*(x0-8)**2 + 2*(x1-4)**2 + 3*x4**2 - x5 - 30.0 <= 0.0 
    39 x0**2 + 2*(x1-2)**2 - 2*x0*x1 + 14*x4 - 6*x5 <= 0.0 
    40 -3*x0 + 6*x1 + 12*(x8-8)**2 - 7*x9 <= 0.0 
     334.0*x0 + 5.0*x1 - 3.0*x6 + 9.0*x7 - 105.0 <= 0.0 
     3410.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 
     363.0*(x0-2)**2 + 4.0*(x1-3)**2 + 2.0*x2**2 - 7.0*x3 - 120.0 <= 0.0 
     375.0*x0**2 + 8.0*x1 + (x2-6)**2 - 2.0*x3 - 40.0 <= 0.0 
     380.5*(x0-8)**2 + 2.0*(x1-4)**2 + 3.0*x4**2 - x5 - 30.0 <= 0.0 
     39x0**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 
    4141""" 
    42 #cf = generate_constraint(generate_solvers(solve(equations))) #XXX: inequalities 
     42cf = generate_constraint(generate_solvers(simplify(equations, target=['x5','x3']))) 
    4343pf = generate_penalty(generate_conditions(equations)) 
    44  
    45 from mystic.constraints import as_constraint 
    46  
    47 cf = as_constraint(pf) 
    4844 
    4945 
     
    5450    from mystic.solvers import fmin_powell 
    5551    from mystic.math import almostEqual 
     52    from mystic.monitors import VerboseMonitor 
     53    mon = VerboseMonitor(10) 
    5654 
    5755    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  
    2525ys = -0.09582504 
    2626 
    27 from mystic.symbolic import generate_constraint, generate_solvers, solve 
     27from mystic.symbolic import generate_constraint, generate_solvers, simplify 
    2828from mystic.symbolic import generate_penalty, generate_conditions 
    2929 
     
    32321.0 - x0 + (x1 - 4)**2 <= 0.0 
    3333""" 
    34 #cf = generate_constraint(generate_solvers(solve(equations))) #XXX: inequalities 
     34cf = generate_constraint(generate_solvers(simplify(equations))) 
    3535pf = generate_penalty(generate_conditions(equations), k=1e12) 
    36  
    37 from mystic.constraints import as_constraint 
    38  
    39 cf = as_constraint(pf) 
    4036 
    4137 
  • mystic/examples2/g09.py

    r776 r831  
    2525ys = 680.6300573 
    2626 
    27 from mystic.symbolic import generate_constraint, generate_solvers, solve 
     27from mystic.symbolic import generate_constraint, generate_solvers, simplify 
    2828from mystic.symbolic import generate_penalty, generate_conditions 
    2929 
    3030equations = """ 
    31 2*x0**2 + 3*x1**4 + x2 + 4*x3**2 + 5*x4 - 127.0 <= 0.0 
    32 7*x0 + 3*x1 + 10*x2**2 + x3 - x4 - 282.0 <= 0.0 
    33 23*x0 + x1**2 + 6*x5**2 - 8*x6 - 196.0 <= 0.0 
    34 4*x0**2 + x1**2 - 3*x0*x1 + 2*x2**2 + 5*x5 - 11*x6 <= 0.0 
     312.0*x0**2 + 3.0*x1**4 + x2 + 4.0*x3**2 + 5.0*x4 - 127.0 <= 0.0 
     327.0*x0 + 3.0*x1 + 10.0*x2**2 + x3 - x4 - 282.0 <= 0.0 
     3323.0*x0 + x1**2 + 6.0*x5**2 - 8.0*x6 - 196.0 <= 0.0 
     344.0*x0**2 + x1**2 - 3.0*x0*x1 + 2.0*x2**2 + 5.0*x5 - 11.0*x6 <= 0.0 
    3535""" 
    36 #cf = generate_constraint(generate_solvers(solve(equations))) #XXX: inequalities 
     36cf = generate_constraint(generate_solvers(simplify(equations))) 
    3737pf = generate_penalty(generate_conditions(equations), k=1e12) 
    38  
    39 from mystic.constraints import as_constraint 
    40  
    41 cf = as_constraint(pf) 
    4238 
    4339 
     
    4844    from mystic.math import almostEqual 
    4945 
    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) 
    5147 
    5248    assert almostEqual(result[0], xs, rel=1e-2) 
  • mystic/examples2/g10.py

    r776 r831  
    2525ys = 7049.3307 
    2626 
    27 from mystic.symbolic import generate_constraint, generate_solvers, solve 
     27from mystic.symbolic import generate_constraint, generate_solvers, simplify 
    2828from mystic.symbolic import generate_penalty, generate_conditions 
    2929 
    3030equations = """ 
    31 -1 + 0.0025*(x3 + x5) <= 0.0 
    32 -1 + 0.0025*(-x3 + x4 + x6) <= 0.0 
    33 -1 + 0.01*(-x4 + x7) <= 0.0 
    34 100*x0 - x0*x5 + 833.33252*x3 - 83333.333 <= 0.0 
    35 x1*x3 - x1*x6 - 1250*x3 + 1250*x4 <= 0.0 
    36 x2*x4 - x2*x7 - 2500*x4 + 1250000 <= 0.0 
     31-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 
     34100.0*x0 - x0*x5 + 833.33252*x3 - 83333.333 <= 0.0 
     35x1*x3 - x1*x6 - 1250.0*x3 + 1250.0*x4 <= 0.0 
     36x2*x4 - x2*x7 - 2500.0*x4 + 1250000.0 <= 0.0 
    3737""" 
    38 #cf = generate_constraint(generate_solvers(solve(equations))) #XXX: inequalities 
     38cf = generate_constraint(generate_solvers(simplify(equations))) 
    3939pf = generate_penalty(generate_conditions(equations), k=1e12) 
    40  
    41 from mystic.constraints import as_constraint 
    42  
    43 cf = as_constraint(pf) 
    4440 
    4541 
  • mystic/examples2/g13.py

    r776 r831  
    2727ys = 0.05394983 
    2828 
    29 from mystic.symbolic import generate_constraint, generate_solvers, solve 
     29from mystic.symbolic import generate_constraint, generate_solvers, simplify 
    3030from mystic.symbolic import generate_penalty, generate_conditions 
    3131 
    3232equations = """ 
    3333x0**2 + x1**2 + x2**2 + x3**2 + x4**2 - 10.0 = 0.0 
    34 x1*x2 - 5*x3*x4 = 0.0 
     34x1*x2 - 5.0*x3*x4 = 0.0 
    3535x0**3 + x1**3 + 1.0 = 0.0 
    3636""" 
    37 #cf = generate_constraint(generate_solvers(solve(equations))) #XXX: solve slow 
     37cf = generate_constraint(generate_solvers(simplify(equations))) # slow solve 
    3838pf = generate_penalty(generate_conditions(equations)) 
    39  
    40 from mystic.constraints import as_constraint 
    41  
    42 cf = as_constraint(pf) 
    4339 
    4440 
  • mystic/examples2/integer_programming.py

    r819 r831  
    3838from mystic.symbolic import generate_penalty, generate_conditions 
    3939pf = generate_penalty(generate_conditions(equations)) 
     40from mystic.symbolic import generate_constraint, generate_solvers, simplify 
     41cf = generate_constraint(generate_solvers(simplify(equations))) 
    4042 
    4143from mystic.constraints import integers 
  • mystic/examples2/lp.py

    r788 r831  
    3838from mystic.symbolic import generate_conditions, generate_penalty 
    3939pf = generate_penalty(generate_conditions(equations)) 
     40from mystic.symbolic import generate_constraint, generate_solvers, simplify 
     41cf = generate_constraint(generate_solvers(simplify(equations))) 
    4042 
    4143# inverted objective, used in solving for the maximum 
     
    4850  from mystic.math import almostEqual 
    4951 
    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) 
    5153  assert almostEqual(result[0], xs, rel=1e-2) 
    5254  assert almostEqual(result[1], ys, rel=1e-2) 
    5355 
    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) 
    5557  assert almostEqual(result[0], xs, rel=1e-2) 
    5658  assert almostEqual(result[1], ys, rel=1e-2) 
    5759 
    5860  # 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) 
    6062  assert almostEqual( result[0], _xs, rel=1e-2) 
    6163  assert almostEqual(-result[1], _ys, rel=1e-2) 
    6264 
    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) 
    6466  assert almostEqual( result[0], _xs, rel=1e-2) 
    6567  assert almostEqual(-result[1], _ys, rel=1e-2) 
  • mystic/examples2/optqp.py

    r816 r831  
    3030from mystic.symbolic import generate_conditions, generate_penalty 
    3131pf = generate_penalty(generate_conditions(equations), k=1e4) 
     32from mystic.symbolic import generate_constraint, generate_solvers, solve 
     33cf = generate_constraint(generate_solvers(solve(equations))) 
    3234 
    3335# inverted objective, used in solving for the maximum 
     
    4042  from mystic.math import almostEqual 
    4143 
    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) 
    4345  assert almostEqual(result[0], xs, rel=2e-2) 
    4446  assert almostEqual(result[1], ys, rel=2e-2) 
    4547 
    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) 
    4749  assert almostEqual(result[0], xs, rel=2e-2) 
    4850  assert almostEqual(result[1], ys, rel=2e-2) 
    4951 
    5052 
    51 # EOD 
     53# EOF 
  • mystic/examples2/spring.py

    r776 r831  
    2525ys = 0.01266523 
    2626 
    27 from mystic.symbolic import generate_constraint, generate_solvers, solve 
     27from mystic.symbolic import generate_constraint, generate_solvers, simplify 
    2828from mystic.symbolic import generate_penalty, generate_conditions 
    2929 
     
    3434(x0 + x1)/1.5 - 1.0 <= 0.0 
    3535""" 
    36 #cf = generate_constraint(generate_solvers(solve(equations))) #XXX: inequalities 
     36# cf = generate_constraint(generate_solvers(simplify(equations))) #XXX: slow 
    3737pf = generate_penalty(generate_conditions(equations), k=1e12) 
    38  
    39 from mystic.constraints import as_constraint 
    40  
    41 cf = as_constraint(pf) 
    4238 
    4339 
  • mystic/examples2/vessel.py

    r776 r831  
    2525ys = 5804.3762083 
    2626 
    27 from mystic.symbolic import generate_constraint, generate_solvers, solve 
     27from mystic.symbolic import generate_constraint, generate_solvers, simplify 
    2828from mystic.symbolic import generate_penalty, generate_conditions 
    2929 
     
    3434x3 - 240.0 <= 0.0 
    3535""" 
    36 #cf = generate_constraint(generate_solvers(solve(equations))) #XXX: inequalities 
     36cf = generate_constraint(generate_solvers(simplify(equations))) 
    3737pf = generate_penalty(generate_conditions(equations), k=1e12) 
    38  
    39 from mystic.constraints import as_constraint 
    40  
    41 cf = as_constraint(pf) 
    4238 
    4339 
     
    4743    from mystic.solvers import diffev2 
    4844    from mystic.math import almostEqual 
     45    from mystic.monitors import VerboseMonitor 
     46    mon = VerboseMonitor(10) 
    4947 
    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) 
    5149 
    5250    assert almostEqual(result[0], xs, rel=1e-2) 
  • mystic/mystic/_symbolic.py

    r792 r831  
    356356        code += '_xlist = %s\n' % ','.join(targeted) 
    357357        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' 
    359359        code += 'soln = {}\n' 
    360360        code += '[soln.update(i) for i in _elist if i]\n' 
     
    362362        code += 'soln = symsol([' + eqlist + '], [' + target[0] + '])\n' 
    363363       #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' 
    365365    ######################################################################## 
    366366 
  • mystic/mystic/symbolic.py

    r802 r831  
    1313from __future__ import division 
    1414 
    15 __all__ = ['linear_symbolic','replace_variables','get_variables','solve', 
     15__all__ = ['linear_symbolic','replace_variables','get_variables', 
     16           'solve','simplify','comparator', 
    1617           'penalty_parser','constraints_parser','generate_conditions', 
    1718           'generate_solvers','generate_penalty','generate_constraint'] 
     
    9798    totalconstraints = ineqstring + eqstring 
    9899    return totalconstraints  
     100 
     101 
     102def 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 
     111def simplify(constraints, variables='x', target=None, **kwds): 
     112    """simplify a system of symbolic constraints equations. 
     113 
     114Returns a system of equations where a single variable has been isolated on 
     115the left-hand side of each constraints equation, thus all constraints are 
     116of the form "x_i = f(x)". 
     117 
     118Inputs: 
     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 
     137Additional 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 
     147Further 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) 
    99236 
    100237 
  • mystic/tests/test_symbolic.py

    r781 r831  
    7676  assert (x[1] - x[0]) - 1.0 == mean(x[:-1]) 
    7777 
     78def 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 
    7892 
    7993if __name__ == '__main__': 
     
    8296  test_generate_constraint() 
    8397  test_solve_constraint() 
     98  test_simplify() 
    8499 
Note: See TracChangeset for help on using the changeset viewer.