from mystic.constraints import as_penalty from mystic import random_seed random_seed(123) t = t1,t2,t3 = (impose_sum, impose_product, impose_mean) #c = [lambda x: ti(xi, x) for (xi,ti) in zip(x,t)] #XXX c1 = lambda x: t1(x1, x) c2 = lambda x: t2(x2, x) c3 = lambda x: t3(x3, x) c = (c1,c2,c3) k=1 solver = 'buckshot' #'diffev' ptype = linear_equality #quadratic_equality p = [as_penalty(ci, ptype) for ci in c] 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) # etc: more coupling of constraints
from mystic.constraints import as_penalty from mystic import random_seed random_seed(123) t = t1,t2,t3 = (impose_sum, impose_product, impose_mean) #c = [lambda x: ti(xi, x) for (xi,ti) in zip(x,t)] #XXX c1 = lambda x: t1(x1, x) c2 = lambda x: t2(x2, x) c3 = lambda x: t3(x3, x) c = (c1,c2,c3) k=1 solver = 'buckshot' #'diffev' ptype = linear_equality #quadratic_equality p = [as_penalty(ci, ptype) for ci in c] 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) # etc: more coupling of constraints