Python 使用Scipy的线性规划失败,但二次规划成功地找到了解决方案

Python 使用Scipy的线性规划失败,但二次规划成功地找到了解决方案,python,optimization,linear-programming,quadratic-programming,quadprog,Python,Optimization,Linear Programming,Quadratic Programming,Quadprog,我试图用Python解决一个线性规划问题。Ling程序未能找到解决方案。但四元程序是有效的。我不明白为什么,我也不确定我在linprog和quad程序中的程序公式是否等效 下面是线性编程问题、我的代码和来自linprog的错误消息 代码 来自linprog失败的错误消息 Optimization failed. Unable to find a feasible starting point. 您可以使用安装quad程序 sudo pip install quadprog 有关使用quad

我试图用Python解决一个线性规划问题。Ling程序未能找到解决方案。但四元程序是有效的。我不明白为什么,我也不确定我在linprog和quad程序中的程序公式是否等效

下面是线性编程问题、我的代码和来自linprog的错误消息

代码

来自linprog失败的错误消息

Optimization failed. Unable to find a feasible starting point.
您可以使用安装quad程序

sudo pip install quadprog
有关使用quadprog的示例,请参见

https://scaron.info/blog/quadratic-programming-in-python.html


你的问题没有解决办法。您有约束条件
[0,0,0.4,0,0,0]*x=0.8
x[2]您的问题没有解决方案。您有约束条件
[0,0,0.4,0,0,0,0]*x=0.8
x[2]您的程序需要人们安装
quadprog
。是的,sudo pip安装quadprog已知scipy中的单纯形线性解算器存在问题。较新版本有一个内部点解算器,它看起来更坚固。您的程序要求人们安装
quadprog
。是的,sudo pip安装quadprog已知scipy中的单纯形线性解算器存在问题。较新的版本有一个内部点解算器,它看起来更坚固。我在你的解决方案中遇到了一个问题。它主要起作用,但有时会给出一些负值作为解决方案,即,如果您尝试使用存在解决方案的差异W1和b1。您能提供一个示例吗?下面是一个示例,W1和b1优化后的解决方案成功终止。当前函数值:3.000000次迭代:9[2.19916938-1.19916938 8.7635795-7.7635795-0.5 1.5]W1[[0.35855621 0.0.0.0.85993925 0.][0.0.35855621 0.05538291 0.0.0.][0.0.0.0.0.05538291 0.0.85993925]]b1[0.35855621 0.05538291 0.85993925]我测试了您的输入,但我的输出为正值。我把它添加到我的答案中。谢谢。我一直在尝试几种W1,b1组合,它们都有解决方案。有时,解不在(0,1)范围内。但是当我向你的程序中添加options={“bland”:True}时,问题就消失了。它主要起作用,但有时会给出一些负值作为解决方案,即,如果您尝试使用存在解决方案的差异W1和b1。您能提供一个示例吗?下面是一个示例,W1和b1优化后的解决方案成功终止。当前函数值:3.000000次迭代:9[2.19916938-1.19916938 8.7635795-7.7635795-0.5 1.5]W1[[0.35855621 0.0.0.0.85993925 0.][0.0.35855621 0.05538291 0.0.0.][0.0.0.0.0.05538291 0.0.85993925]]b1[0.35855621 0.05538291 0.85993925]我测试了您的输入,但我的输出为正值。我把它添加到我的答案中。谢谢。我一直在尝试几种W1,b1组合,它们都有解决方案。有时,解不在(0,1)范围内。但是,当我向程序中添加options={“bland”:True}时,问题就消失了。
https://scaron.info/blog/quadratic-programming-in-python.html
'''
min [1, 1, 1, 1, 1, 1] * x

with [ 0   0   0   0   0   0   ]       [ 0    ]
     [ 0   0   0.4 0   0   0   ] * x = [ 0.4  ]
     [ 0   0   0   0.5 0   0   ]       [ 0.25 ]

and  [ 0   0   0   0   0   0   ]       [ 0     ]
     [ 0   0   0   0   0.4 0   ] * x = [ 0.04  ]
     [ 0   0   0   0   0   0.5 ]       [ 0.125 ]

and  0 <= x[i] <= 1, for 0 <= i < 6

'''

import scipy.optimize as opt
import numpy as np

k = 6
n = 3

W1 = np.zeros((n, k))
W1[1, 2] = 0.4
W1[2, 3] = 0.5
b1 = np.zeros([n, 1])
b1[1] = 0.4
b1[2] = 0.25

W3 = np.zeros((n, k))
W3[1, 4] = 0.4
W3[2, 5] = 0.5
b3 = np.zeros([n, 1])
b3[1] = 0.04
b3[2] = 0.125

c = np.array([1] * k)
A_eq = np.vstack([W1, W3])
b_eq = np.vstack([b1, b3])
bounds = np.array([(0, 1)] * k)
options = {"disp": True}
result = opt.linprog(c=c, A_eq=A_eq, b_eq=b_eq, bounds=bounds, options=options)

print(result.x)
Optimization terminated successfully.
         Current function value: 1.850000    
         Iterations: 4
[0.   0.   1.   0.5  0.1  0.25]
import scipy.optimize as opt
import numpy as np

k = 6
n = 3

W1 = np.array([[0.35855621, 0, 0, 0, 0.85993925, 0 ], 
               [0, 0.35855621, 0.05538291, 0, 0, 0 ], 
               [0, 0, 0, 0.05538291, 0, 0.85993925]])
b1 = np.array([[0.35855621], 
               [0.05538291], 
               [0.85993925]])

c = np.array([1] * k)
A_eq = W1
b_eq = b1
bounds = np.array([(0, 1)] * k)
options = {"disp": True}
result = opt.linprog(c=c, A_eq=A_eq, b_eq=b_eq, bounds=bounds, options=options)

print(result.x)
Optimization terminated successfully.
         Current function value: 1.571416    
         Iterations: 3
[0.         0.15446089 0.         0.         0.41695528 1.        ]