向神秘主义者提供不等式/约束的向量

时间:2018-11-13 04:38:21

标签: python optimization scipy mystic

我正在尝试通过使用scipy(scipy.optimize.fmin_l_bfgs_b())提供的无约束算法来为迄今为止一直成功执行的功能最小化提供约束。

通过阅读(例如,Python constrained non-linear optimization),我发现了一个最小化的压缩包mystic,这似乎是我所需要的。我的情况如下。我有一个3N变量的函数(代表N节点的xyz位置坐标),我想提供一个约束列表,使得每个节点z/x = const.。总共有N个约束。如何为mystic()最有效地定义/提供这些约束?同样的约束对象也可以与scipy.optimize.slsqp()一起使用吗?由于我的约束是线性的,因此这也是一个可行的选择。

我尝试了以下操作,但它使我的计算机崩溃了:

import mystic.symbolic as ms
ieqns = ''
for p in range(N):
    ieqns += 'x'+str(p+2) +'/x'+str(p) +" <= 2"

cf = ms.generate_constraint(ms.generate_solvers(ms.simplify(ieqns)))
pf = ms.generate_penalty(ms.generate_conditions(ieqns), k=1e12)

1 个答案:

答案 0 :(得分:1)

我是<?php $dom = new DOMDocument(); $xml = <<<XML <?xml version="1.0" encoding="UTF-8"?> <A xmlns="xmlns://www.abc.com"><B/></A> XML; $dom->loadXML($xml); $new_dom = new DOMDocument(); $new_root = $new_dom->createElement("root"); $new_node = $new_dom->importNode($dom->documentElement->childNodes->item(0), TRUE); //order is important $new_dom->appendChild($new_root); //1 $new_root->appendChild($new_node); //2 $new_node->removeAttributeNS("xmlns://www.abc.com", ""); //3 // echo $new_dom->saveXML() ?> 123 <?xml version="1.0"?> <root><B/></root> 132,231,321,312 <?xml version="1.0"?> <root><default:B/></root> 213 <?xml version="1.0"?> <root xmlns:default="xmlns://www.abc.com"><default:B/></root> 的作者。我相信您想要做的事情是这样的:

mystic

然后我们可以在应用约束时将其最小化(但是,在以下情况下,约束基本上是不相关的):

>>> import mystic.symbolic as ms
>>> ieqns = ''
>>> for p in range(10):
...   ieqns += 'x{0} <= 2*x{1}\n'.format(p+2,p)
... 
>>> cf = ms.generate_constraint(ms.generate_solvers(ieqns))
>>>
>>> # test that it applies the constraints
>>> cf([1.,3.,5.,7.,9.,11.,13.,15.,17.,19.,21.,23.,25.])
[1.0, 3.0, 2.0, 6.0, 4.0, 11.0, 8.0, 15.0, 16.0, 19.0, 21.0, 23.0, 25.0]