def test_matrix_interface(): # Demonstrates linear_symbolic() A = asarray([[3., 4., 5.], [1., 6., -9.]]) b = asarray([0., 0.]) G = [1., 0., 0.] h = [5.] print "equality constraints" print "G: %s" % G print "h: %s" % h print "inequality constraints" print "A:\n%s" % A print "b: %s" % b constraints_string = linear_symbolic(A=A, b=b, G=G, h=h) print "symbolic string:\n", constraints_string.rstrip() pf = generate_penalty(generate_conditions(constraints_string)) @inner(as_constraint(pf)) def cf(x): return sum(x) #XXX: implement: wrap_constraint( as_constraint(pf), sum, ctype='inner') ? print "c = constraints wrapped around sum(x)" x0 = [1., 1., 1.] print "c(%s): %s\n" % (x0, cf(x0))
def test_numpy_penalty(): constraints = """ mean([x0, x1, x2]) = 5.0 x0 = x1 + x2""" ineq,eq = generate_conditions(constraints) assert eq[0]([7,5,3]) == 0.0 assert eq[1]([7,4,3]) == 0.0 penalty = generate_penalty((ineq,eq)) assert penalty([9.0,5,4.0]) == 100.0 assert penalty([7.5,4,3.5]) == 0.0 constraint = as_constraint(penalty, solver='fmin') assert almostEqual(penalty(constraint([3,4,5])), 0.0, 1e-10)
def test_numpy_penalty(): constraints = """ mean([x0, x1, x2]) = 5.0 x0 = x1 + x2""" ineq, eq = generate_conditions(constraints) assert eq[0]([7, 5, 3]) == 0.0 assert eq[1]([7, 4, 3]) == 0.0 penalty = generate_penalty((ineq, eq)) assert penalty([9.0, 5, 4.0]) == 100.0 assert penalty([7.5, 4, 3.5]) == 0.0 constraint = as_constraint(penalty, solver='fmin') assert almostEqual(penalty(constraint([3, 4, 5])), 0.0, 1e-10)
def test_generate_penalty(): constraints = """ x0**2 = 2.5*x3 - a exp(x2/x0) >= b""" ineq,eq = generate_conditions(constraints, nvars=4, locals={'a':5.0, 'b':7.0}) assert ineq[0]([4,0,0,1,0]) == 6.0 assert eq[0]([4,0,0,1,0]) == 18.5 penalty = generate_penalty((ineq,eq)) assert penalty([1,0,2,2.4]) == 0.0 assert penalty([1,0,0,2.4]) == 7200.0 assert penalty([1,0,2,2.8]) == 100.0 constraint = as_constraint(penalty, nvars=4, solver='fmin') assert almostEqual(penalty(constraint([1,0,0,2.4])), 0.0, 1e-10)
def test_matrix_interface(): # Demonstrates linear_symbolic() A = asarray([[3., 4., 5.], [1., 6., -9.]]) b = asarray([0., 0.]) G = [1., 0., 0.] h = [5.] # print("equality constraints") # print("G: %s" % G) # print("h: %s" % h) # print("inequality constraints") # print("A:\n%s" % A) # print("b: %s" % b) constraints_string = linear_symbolic(A=A, b=b, G=G, h=h) cs = constraints_string.split('\n') assert cs[0] == "1.0*x0 + 0.0*x1 + 0.0*x2 <= 5.0" assert cs[1] == "3.0*x0 + 4.0*x1 + 5.0*x2 = 0.0" assert cs[2] == "1.0*x0 + 6.0*x1 + -9.0*x2 = 0.0" # print("symbolic string:\n%s" % constraints_string.rstrip()) pf = generate_penalty(generate_conditions(constraints_string)) cn = as_constraint(pf) x0 = [1., 1., 1.] assert almostEqual(pf(cn(x0)), 0.0, tol=2e-2)
def test_matrix_interface(): # Demonstrates linear_symbolic() A = asarray([[3., 4., 5.], [1., 6., -9.]]) b = asarray([0., 0.]) G = [1., 0., 0.] h = [5.] # print "equality constraints" # print "G: %s" % G # print "h: %s" % h # print "inequality constraints" # print "A:\n%s" % A # print "b: %s" % b constraints_string = linear_symbolic(A=A, b=b, G=G, h=h) cs = constraints_string.split('\n') assert cs[0] == "1.0*x0 + 0.0*x1 + 0.0*x2 <= 5.0" assert cs[1] == "3.0*x0 + 4.0*x1 + 5.0*x2 = 0.0" assert cs[2] == "1.0*x0 + 6.0*x1 + -9.0*x2 = 0.0" # print "symbolic string:\n", constraints_string.rstrip() pf = generate_penalty(generate_conditions(constraints_string)) cn = as_constraint(pf) x0 = [1., 1., 1.] assert almostEqual(pf(cn(x0)), 0.0, tol=1e-2)
xs = [1.22797136, 4.24537337] ys = -0.09582504 from mystic.symbolic import generate_constraint, generate_solvers, solve from mystic.symbolic import generate_penalty, generate_conditions equations = """ x0**2 - x1 + 1.0 <= 0.0 1.0 - x0 + (x1 - 4)**2 <= 0.0 """ #cf = generate_constraint(generate_solvers(solve(equations))) #XXX: inequalities pf = generate_penalty(generate_conditions(equations), k=1e12) from mystic.constraints import as_constraint cf = as_constraint(pf) if __name__ == '__main__': from mystic.solvers import buckshot from mystic.math import almostEqual result = buckshot(objective, 2, 40, bounds=bounds, penalty=pf, disp=False, full_output=True) assert almostEqual(result[0], xs, tol=1e-2) assert almostEqual(result[1], ys, rel=1e-2)
return -pi * x[2]**2 * x[3] - (4 / 3.) * pi * x[2]**3 + 1296000.0 def penalty4(x): # <= 0.0 return x[3] - 240.0 @quadratic_inequality(penalty1, k=1e12) @quadratic_inequality(penalty2, k=1e12) @quadratic_inequality(penalty3, k=1e12) @quadratic_inequality(penalty4, k=1e12) def penalty(x): return 0.0 solver = as_constraint(penalty) if __name__ == '__main__': from mystic.solvers import diffev2 from mystic.math import almostEqual result = diffev2(objective, x0=bounds, bounds=bounds, penalty=penalty, npop=40, gtol=500, disp=False, full_output=True)
# 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 g11 import objective, bounds, xs, xs_, ys from mystic.penalty import quadratic_equality from mystic.constraints import with_penalty @with_penalty(quadratic_equality, k=1e12) def penalty(x): # == 0.0 return x[1] - x[0]**2 from mystic.constraints import as_constraint solver = as_constraint(penalty) if __name__ == '__main__': from mystic.solvers import diffev2 from mystic.math import almostEqual result = diffev2(objective, x0=bounds, bounds=bounds, constraints=solver, npop=40, xtol=1e-8, ftol=1e-8, 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)
W + A + L + T + Z - 34 == 0 """ var = list('ABCDEFGHIJKLMNOPQRSTUVWXYZ') #NOTE: FOR A MORE DIFFICULT PROBLEM, COMMENT OUT THE FOLLOWING 5 LINES bounds[0] = (5, 5) # A bounds[4] = (20, 20) # E bounds[8] = (25, 25) # I bounds[14] = (10, 10) # O bounds[20] = (1, 1) # U from mystic.constraints import unique, near_integers, has_unique from mystic.symbolic import generate_penalty, generate_conditions pf = generate_penalty(generate_conditions(equations, var), k=1) from mystic.constraints import as_constraint cf = as_constraint(pf) from mystic.penalty import quadratic_equality @quadratic_equality(near_integers) @quadratic_equality(has_unique) def penalty(x): return pf(x) from numpy import round, hstack, clip def constraint(x): x = round(x).astype(int) # force round and convert type to int x = clip(x, 1, nletters) #XXX: hack to impose bounds
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 = and_(*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 from mystic.math.measures import impose_product, impose_sum, impose_mean
from mystic.constraints import with_penalty def penalty(x): #NOTE: not built as a 'penalty function' (loses functionality) sum = 0.0 for i in range(1, 10): for j in range(1, 10): for k in range(1, 10): p = lambda v: ((v[0] - i)**2 + (v[1] - j)**2 + (v[2] - k)**2 - 48.0) p = with_penalty(quadratic_inequality, k=1e2)(p) sum += p(x) return sum solver = as_constraint(penalty) #XXX: may not work, as not a penalty function if __name__ == '__main__': from mystic.solvers import buckshot from mystic.math import almostEqual result = buckshot(objective, 3, 10, bounds=bounds, penalty=penalty, disp=False, full_output=True) assert almostEqual(result[0], xs, tol=1e-2)
from mystic.constraints import as_constraint from mystic.penalty import quadratic_inequality from mystic.constraints import with_penalty def penalty(x): #NOTE: not built as a 'penalty function' (loses functionality) sum = 0.0 for i in range(1,10): for j in range(1,10): for k in range(1,10): p = lambda v: ((v[0]-i)**2 + (v[1]-j)**2 + (v[2]-k)**2 - 48.0) p = with_penalty(quadratic_inequality, k=1e2)(p) sum += p(x) return sum solver = as_constraint(penalty) #XXX: may not work, as not a penalty function if __name__ == '__main__': from mystic.solvers import buckshot from mystic.math import almostEqual result = buckshot(objective, 3, 10, bounds=bounds, penalty=penalty, disp=False, full_output=True) assert almostEqual(result[0], xs, tol=1e-2) assert almostEqual(result[1], ys, rel=1e-2)