我正在使用SciPy进行优化,SLSQP方法似乎忽略了我的约束.
具体来说,我希望x [3]和x [4]在[0-1]的范围内
我收到的消息是:'不平等约束不兼容'
以下是执行结果,后跟示例代码(使用虚函数):
status: 4
success: False
njev: 2
nfev: 24
fun: 0.11923608071680103
x: array([-10993.4278558 , -19570.77080806, -23495.15914299, -26531.4862831 ,
4679.97660534])
message: 'Inequality constraints incompatible'
jac: array([ 12548372.4766904 , 12967696.88362279, 39928956.72239509,
-9224613.99092537, 3954696.30747453, 0. ])
nit: 2
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
from random import random
from scipy.optimize import minimize
def func(x):
""" dummy function to optimize """
print 'x'+str(x)
return random()
my_constraints = ({'type':'ineq', 'fun':lambda(x):1-x[3]-x[4]},
{'type':'ineq', 'fun':lambda(x):x[3]},
{'type':'ineq', 'fun':lambda(x):x[4]},
{'type':'ineq', 'fun':lambda(x):1-x[4]},
{'type':'ineq', 'fun':lambda(x):1-x[3]})
minimize(func, [57.9499 ,-18.2736,1.1664,0.0000,0.0765],
method='SLSQP',constraints=my_constraints)
Run Code Online (Sandbox Code Playgroud)
编辑 - 即使删除第一个约束,问题仍然存在. …
当实例化包含结构指针数组的结构时,我保证结构数组成员中的所有指针都将设置为NULL?
这是一个示例结构:
typedef struct mmNode {
int val;
int board[2][NUM_PITS+1];
int side;
struct mmNode* children[NUM_PITS+1];
} mmNode;
Run Code Online (Sandbox Code Playgroud)
IE:如果我创建一个mmNode结构的实例,那么mmNode.children总是将元素设置为NULL?
我有一个空*我进入一些实际上是二维int数组的函数.我想将它作为参数发送给需要二维数组的函数.正确施放它的最佳方法是什么?
void foo(void* val){
//How to cast val in order to send to bar??
bar()
}
void bar(int val[2][2]){
//Do something
}
Run Code Online (Sandbox Code Playgroud) 我有一个字典,我已经添加了10个空字符列表的值.然后,我尝试将添加单值在列表中类型的字典之一.但似乎我将它设置为列表中的所有dicts.有任何想法吗?我怎么能实现它这个不能工作呢?
bids={}
bids[1]=[{}]*10
bids[1][0][1]=0
Run Code Online (Sandbox Code Playgroud)
出价打印为
{1: [{1: 0}, {1: 0}, {1: 0}, {1: 0}, {1: 0}, {1: 0}, {1: 0}, {1: 0}, {1: 0}, {1: 0}]}
Run Code Online (Sandbox Code Playgroud)
虽然我期待:
{1: [{}, {1: 0}, {}, {}, {}, {}, {}, {}, {}, {}]}
Run Code Online (Sandbox Code Playgroud) 我根据以下示例使用 CVXOPT 进行线性编程:http ://abel.ee.ucla.edu/cvxopt/examples/tutorial/lp.html 我很确定我表达了一个约束
X1 >= 0
Run Code Online (Sandbox Code Playgroud)
但是得到一个负值。怎么来的?我收到“找到最佳解决方案”消息
A = matrix( [ [0.0, 0.0, 1.0, 1.0, -0.0, -0.0, -1.0, -1.0, -1.0, 0.0, 0.0],
[0.0, 1.0, 1.0, 0.0, -0.0, -1.0, -1.0, -0.0, 0.0, -1.0, 0.0],
[1.0, 0.0, 0.0, 1.0, -1.0, -0.0, -0.0, -1.0, 0.0, 0.0, -1.0]
]
)
Run Code Online (Sandbox Code Playgroud)
约束值(右侧)
b = matrix( [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0] )
Run Code Online (Sandbox Code Playgroud)
最小化功能:
c = matrix( [-1.0, -1.0, -1.0] )
Run Code Online (Sandbox Code Playgroud)
调用:
sol=solvers.lp(c,A,b)
Run Code Online (Sandbox Code Playgroud)
但:
print (sol['x']):
[-4.83e-09]
[ …Run Code Online (Sandbox Code Playgroud)