报错Traceback (most recent call last): File "C:\Users\70940\PycharmProjects\untitled5\test.py", line 13, in <module> res = linprog(c=c, A_ub=A, b_ub=b, A_eq=Aeq, b_eq=beq, bounds=list(zip(vlb, vub))) File "C:\Users\70940\anaconda3\lib\site-packages\scipy\optimize\_linprog.py", line 592, in linprog lp, solver_options = _parse_linprog(lp, options, meth) File "C:\Users\70940\anaconda3\lib\site-packages\scipy\optimize\_linprog_util.py", line 1003, in _parse_linprog lp = _clean_inputs(lp._replace(A_ub=A_ub, A_eq=A_eq)) File "C:\Users\70940\anaconda3\lib\site-packages\scipy\optimize\_linprog_util.py", line 335, in _clean_inputs raise ValueError( ValueError: Invalid input for linprog: A_eq must have exactly two dimensions, and the number of columns in A_eq must be equal to the size of c
时间: 2023-07-21 09:09:21 浏览: 197
Mac下anaconda中Spyder打开报错Traceback(most recent call last)…问题
5星 · 资源好评率100%
根据报错信息,问题出在参数`A_eq`的维度不正确。`A_eq`应该是一个二维数组,其中每一行表示一个等式约束条件。在您的代码中,`A_eq`只是一个一维数组,因此需要将其改为二维数组。
以下是修改后的代码:
```python
from scipy.optimize import linprog
import matplotlib.pyplot as plt
a = 0
while (1.1 - a) > 1:
c = [-0.05, -0.27, -0.19, -0.185, -0.185]
Aeq = [[1, 1.01, 1.02, 1.045, 1.065]]
beq = [1]
A = [[0, 0.025, 0, 0, 0], [0, 0, 0.015, 0, 0], [0, 0, 0, 0.055, 0], [0, 0, 0, 0, 0.026]]
b = [a, a, a, a]
vlb = [0, 0, 0, 0, 0]
vub = []
res = linprog(c=c, A_ub=A, b_ub=b, A_eq=Aeq, b_eq=beq, bounds=list(zip(vlb, vub)))
x = res.x
a += 0.001
print(a)
print(x)
Q = -res.fun
plt.plot(a, Q, '.'), plt.axis([0, 0.1, 0, 0.5]), plt.show()
plt.xlabel('a')
plt.ylabel('Q')
```
请尝试运行修改后的代码,看看是否解决了报错问题。
阅读全文