我正在用下面的模型测试CVXOpt
>>> from cvxopt.modeling import op
>>> x = variable()
>>> y = variable()
>>> c1 = ( 2*x+y >> c2 = ( x+2*y >> c3 = ( x >= 0 )
>>> c4 = (y >= 0 )
>>> lp1 = op(-4*x-5*y, [c1,c2,c3,c4])然而,我有两个问题:
发布于 2014-07-28 16:34:39
您使用的语法似乎有一些问题。此外,请始终确保您的代码片段允许运行代码。编写优化问题的正确方法是:
>>> from cvxopt.modeling import op
>>> from cvxopt.modeling import variable
>>> x = variable()
>>> y = variable()
>>> c1 = ( 2*x+y <= 7) # You forgot to add the (in-) equality here and below
>>> c2 = ( x+2*y >= 2)
>>> c3 = ( x >= 0 )
>>> c4 = (y >= 0 )
>>> lp1 = op(-4*x-5*y, [c1,c2,c3,c4])
>>> lp1.solve()
pcost dcost gap pres dres k/t
0: -1.0900e+01 -2.3900e+01 1e+01 0e+00 8e-01 1e+00
1: -1.3034e+01 -2.0322e+01 9e+00 1e-16 5e-01 9e-01
2: -2.4963e+01 -3.5363e+01 3e+01 3e-16 8e-01 3e+00
3: -3.3705e+01 -3.3938e+01 2e+00 3e-16 4e-02 4e-01
4: -3.4987e+01 -3.4989e+01 2e-02 5e-16 4e-04 5e-03
5: -3.5000e+01 -3.5000e+01 2e-04 3e-16 4e-06 5e-05
6: -3.5000e+01 -3.5000e+01 2e-06 4e-16 4e-08 5e-07
Optimal solution found.在第五行和第六行中,您忘记添加( In -)相等项。您必须明确说明您要比较的是什么,即不平等符号和您想要比较的值。
如果使用分号分隔所有(in-)等式,则可以将它们写在一行上。同样,请注意语法,您忘记了在示例代码中关闭一些括号。
>>> c1 = ( 2*x+y <= 7); c2 = ( x+2*y >= 2); c3 = ( x >= 0 ); c4 = (y >= 0 )https://stackoverflow.com/questions/22664398
复制相似问题