我已经使用Pulp为python中的优化问题编写了代码。 当我运行下面的代码时,收到以下错误:
TypeError:列表索引必须是整数或切片,而不是LpVariable。
如何将LpVariable用作列表或数组的索引?
代码是:
import numpy as np
from pulp import *
power=[0,601,610,635,650]
amb = [[1,4,8],[3,6,5]]
#define the optimisation function
prob=LpProblem("Test",LpMaximize)
# define decision variables
x=LpVariable.matrix("x",(range(len(amb)),range(len(amb[0]))),0,4,LpInteger)
cost=[[0 for X in range(len(amb[0]))] for y in range(len(amb))]
for i in range(len(amb)):
for j in range(len(amb[0])):
cost[i][j]=(amb[i][j]*power[x[i][j]])
objective_function = pulp.LpAffineExpression(lpSum(cost))
prob += objective_function
#write the problem
prob.writeLP("Test.lp")
#solve the problem
prob.solve()
print(prob.objective)