Example #1
0
# Copyright (c) 1997-2016 California Institute of Technology.
# Copyright (c) 2016-2017 The Uncertainty Quantification Foundation.
# License: 3-clause BSD.  The full license text is available at:
#  - http://trac.mystic.cacr.caltech.edu/project/mystic/browser/mystic/LICENSE

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)
Example #2
0
# Copyright (c) 1997-2016 California Institute of Technology.
# Copyright (c) 2016-2017 The Uncertainty Quantification Foundation.
# License: 3-clause BSD.  The full license text is available at:
#  - http://trac.mystic.cacr.caltech.edu/project/mystic/browser/mystic/LICENSE

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)
Example #3
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 mystic.constraints import and_ as combined
from numpy import round as npround

c = combined(npround, cf)

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=c,
                     npop=4,
                     gtol=1,
                     disp=True,
Example #4
0
    k = 100
    solver = 'fmin_powell' #'diffev'
    ptype = quadratic_equality



    # case #1: couple penalties into a single constraint

#   p = [lambda x: abs(xi - fi(x)) for (xi,fi) in zip(x,f)] #XXX
    p1 = lambda x: abs(x1 - f1(x))
    p2 = lambda x: abs(x2 - f2(x))
    p3 = lambda x: abs(x3 - f3(x))
    p = (p1,p2,p3)
    p = [ptype(pi)(lambda x:0.) for pi in p]
    penalty = combined(*p, k=k)
    constraint = as_constraint(penalty, solver=solver)

    x = [1,2,3,4,5]
    x_ = constraint(x)

#   print "target: %s, %s, %s" % (x1, x2, x3)
#   print "solved: %s, %s, %s" % (f1(x_), f2(x_), f3(x_))
    assert round(f1(x_)) == round(x1)
    assert round(f2(x_)) == round(x2)
    assert round(f3(x_)) == round(x3)



   # case #2: couple constraints into a single constraint
Example #5
0
    k = 100
    solver = 'fmin_powell' #'diffev'
    ptype = quadratic_equality



    # case #1: couple penalties into a single constraint

#   p = [lambda x: abs(xi - fi(x)) for (xi,fi) in zip(x,f)] #XXX
    p1 = lambda x: abs(x1 - f1(x))
    p2 = lambda x: abs(x2 - f2(x))
    p3 = lambda x: abs(x3 - f3(x))
    p = (p1,p2,p3)
    p = [ptype(pi)(lambda x:0.) for pi in p]
    penalty = combined(*p, k=k)
    constraint = as_constraint(penalty, solver=solver)

    x = [1,2,3,4,5]
    x_ = constraint(x)

#   print "target: %s, %s, %s" % (x1, x2, x3)
#   print "solved: %s, %s, %s" % (f1(x_), f2(x_), f3(x_))
    assert round(f1(x_)) == round(x1)
    assert round(f2(x_)) == round(x2)
    assert round(f3(x_)) == round(x3)



   # case #2: couple constraints into a single constraint