• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python constraints.as_constraint函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了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))
开发者ID:cdeil,项目名称:mystic,代码行数:25,代码来源:test_symbolic_basic.py


示例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)
开发者ID:uqfoundation,项目名称:mystic,代码行数:16,代码来源:test_symbolic.py


示例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)
开发者ID:uqfoundation,项目名称:mystic,代码行数:17,代码来源:test_symbolic.py


示例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)


开发者ID:jcfr,项目名称:mystic,代码行数:28,代码来源:g08.py


示例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)

开发者ID:uqfoundation,项目名称:mystic,代码行数:29,代码来源:g11_alt.py


示例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)


开发者ID:Magellen,项目名称:mystic,代码行数:27,代码来源:g12.py


示例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
开发者ID:uqfoundation,项目名称:mystic,代码行数:31,代码来源:test_combined.py



注:本文中的mystic.constraints.as_constraint函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python math.almostEqual函数代码示例发布时间:2022-05-27
下一篇:
Python mysqlstmt.Select类代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap