这是我的代码和结果。我使用Spyder
来解决问题,但这不起作用。
from scipy.optimize import linprog
c = [2, 3, 4, 6, 7, 5, 7, 8, 9, 9, 8, 9]
A = [[1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0],
[0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0],
[0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1],
[1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1],
[1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0],
[0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1],
[0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0],
[0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1]]
b = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
x0_bounds = (1, None)
x1_bounds = (1, None)
x2_bounds = (1, None)
x3_bounds = (1, None)
x4_bounds = (1, None)
x5_bounds = (1, None)
x6_bounds = (1, None)
x7_bounds = (1, None)
x8_bounds = (1, None)
x9_bounds = (1, None)
x10_bounds = (1, None)
x11_bounds = (1, None)
res = linprog(c,A,b,bounds=(x0_bounds, x1_bounds, x2_bounds, x3_bounds, x4_bounds, x5_bounds, x6_bounds, x7_bounds, x8_bounds, x9_bounds, x10_bounds, x11_bounds), method='simplex')
print(res)
runfile('C:/Users/Jo/Desktop/project/project1.py', wdir='C:/Users/Jo/Desktop/project')
输出:
fun: 9.0
message: 'Optimization failed. Unable to find a feasible starting point.'
nit: 3
status: 2
success: False
x: nan
答案 0 :(得分:1)
scipy中没有整数编程,只有连续线性编程。
你的问题是不可行的(method=simplex
对你的消息并不那么健壮。
(偶数)第一个约束:
[1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0] * x <= 1 # informal algebraic notation
不可行,因为每个变量都被约束为range(1,np.inf)
。
这意味着:row_0 * x >= 4
,永远不会是<= 1
。
method="interior-point"
(scipy&gt; = 1.0)将给出预期的输出:
con: array([], dtype=float64)
fun: 164.90023152478039
message: 'The algorithm terminated successfully and determined that the problem is infeasible.'
nit: 5
slack: array([ -7.14186342, -8.35704274, -8.90930795, -10.95194357,
-5.89531228, -7.50250265, -6.92825952, -11.33686301,
-4.52268237, -7.17603881, -8.66152866])
status: 2
success: False
x: array([ 1.04386377, 3.83436037, 3.51159834, 4.51186381, 2.09089519,
2.58762946, 1.20140415, 1.55269982, 1.89974364, 1.38473169,
1.87908735, 1.9103365 ])