【发布时间】:2010-12-03 10:31:53
【问题描述】:
import numpy
from numpy import asarray
Initial = numpy.asarray [2.0, 4.0, 5.0, 3.0, 5.0, 6.0] # Initial values to start with
bounds = [(1, 5000), (1, 6000), (2, 100000), (1, 50000), (1.0, 5000), (2, 1000000)]
# actual passed bounds
b1 = lambda x: numpy.asarray([1.4*x[0] - x[0]])
b2 = lambda x: numpy.asarray([1.4*x[1] - x[1]])
b3 = lambda x: numpy.asarray([x[2] - x[3]])
constraints = numpy.asarray([b1, b2, b3])
opt= optimize.fmin_slsqp(func,Initial,ieqcons=constraints,bounds=bounds, full_output=True,iter=200,iprint=2, acc=0.01)
问题: 我想传递不等式约束。考虑到我有 6 个参数
[ a, b, c, d, e, f]
在Initial 值中,我的约束是:
a<=e<=1.4*a ('e' varies from a to 1.4*a)
b<=f<=1.4*b ('f' varies from b to 1.4*b)
c>d ('c' must always be greater than d)
但这不能正常工作。我不知道错误是什么。 有没有更好的方法将我的约束作为函数传递? 请帮帮我。
【问题讨论】:
-
我不知道numpy,但是a和b是负数吗?否则,我看不出 e 和 f 的任何值如何满足 1.4*a
-
如果你清楚地说明你正在做什么,你想要发生什么,以及实际发生什么,而不是仅仅粘贴一个代码片段,这将有所帮助。
-
@pear,我试图回答你的问题,但正如 hughdbrown 所说,上面代码中的约束似乎不适用于正数。也许前两个的标志是倒退的?
-
对不起,都是大于0的正值。我改了,希望是正确的。