我是python complex的新手。我尝试建模VRP,需要消除可行的解决方案的子线路,但我不能用以下代码:
from docplex.mp.model import Model
import numpy as np
n = 10
Q = 20
N = [i for i in range(1, n+1)]
V = [0] + N
rnd = np.random
rnd.seed(0)
q = {i: rnd.randint(1, 10) for i in N}
loc_x = rnd.rand(len(V))*200
loc_y = rnd.rand(len(V))*100
A = [(i, j) for i in V for j in V if i != j]
c = {(i, j): np.hypot(loc_x[i]-loc_x[j], loc_y[i]-loc_y[j]) for i, j in A}
mdl = Model('CVRP')
x = mdl.binary_var_dict(A, name='x')
u = mdl.continuous_var_dict(N, lb=0, name='u')
mdl.minimize(mdl.sum(c[i, j]*x[i, j] for i, j in A))
mdl.add_constraints(mdl.sum(x[i, j] for j in V if j != i) == 1 for i in N)
mdl.add_constraints(mdl.sum(x[i, j] for i in V if i != j) == 1 for j in N)
mdl.add_constraints(mdl.add(u[i] - u[j] + n*(x[i,j]) <= n - 1 for i,j in N if i!=j))
mdl.parameters.timelimit = 15
solution = mdl.solve()
print(solution)
当我运行它时,我得到以下错误:
File "C:Usersuser.spyder-py3TSP complexexp .py",第25行mdl.add_constraints (mdl。add (u[我]- [j] + n * (x (i, j)) & lt;我= n - 1, j n如果我! = j))
TypeError: cannot unpack non-iterable int object
我真的很感谢你的帮助。谢谢! !谢谢大家,问题是N是一维数组,但我认为它是二维的,通过使用I,j在N中,我算出来了。顺便问一下,你知道什么好的资源来学习CPLEX Python吗?我是新来的,不知道从哪里开始。谢谢! !
看一下Docplex的例子:https://github.com/IBMDecisionOptimization/docplex-examples