本文整理汇总了Python中mystic.constraints.as_constraint函数的典型用法代码示例。如果您正苦于以下问题:Python as_constraint函数的具体用法?Python as_constraint怎么用?Python as_constraint使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了as_constraint函数的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_matrix_interface
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))
示例2: test_numpy_penalty
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)
示例3: test_generate_penalty
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)
示例4: generate_constraint
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)
示例5: Copyright
# 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)
示例6: penalty
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)
示例7: abs
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