我解决了PySCIPOpt中的IP问题,并解决了Julia中的同样问题,发现解决方案时间截然不同。 Julia使用Cbc在25秒内解决了这个问题,而PySCIPOpt使用内置解算器花费了198秒。在逐行运行代码的过程中,我发现大部分时间花在了PySCIPOpt的问题表达部分,而不是实际解决它。我想知道这是否是预期的,或者是否有一些方法可以提高效率(或与Julia表现相当)。
编辑:以下是我的配方。
model=Model("Route_Selection")
start_time=time.clock()
x={}
for j in range(J):
x[j]=model.addVar(vtype = 'B', name = 'x (%s)' %j)
y={}
for i in range(I):
y[i]=model.addVar(vtype='C', name = 'y (%s)' %i)
model.setObjective(quicksum(C[j]*x[j] for j in range(J))+ M* quicksum(y[i] for i in range(I)), "minimize")
for i in range(I):
model.addCons(quicksum(A_mat[i,j]*x[j] for j in range(J))+y[i] ==1, name='coverage of (%s)' %i)
model.addCons(quicksum(x[j] for j in range(J))<= N, name = 'vehicle constraint')
model.optimize()
print (time.clock()-start_time, "seconds")
答案 0 :(得分:2)
事实证明,利用矩阵A的稀疏性可以使模型构建更快。以下对代码的编辑使其运行得更快。
model=Model("Route_Selection")
start_time=time.clock()
x={}
for j in range(J):
x[j]=model.addVar(vtype = 'B', name = 'x (%s)' %j)
y={}
for i in range(I):
y[i]=model.addVar(vtype='C', name = 'y (%s)' %i)
model.setObjective(quicksum(C[j]*x[j] for j in range(J))+ M* quicksum(y[i] for i in range(I)), "minimize")
from scipy.sparse import csr_matrix #added line 1
B=csr_matrix(A_mat) #added line 2
for i in range(I):
start=B.indptr[i] #added line 3
end=B.indptr[i+1] #added line 4
model.addCons(quicksum(x[j] for j in B.indices[start:end])+y[i] ==1, name='coverage of (%s)' %i) #edited line 5
model.addCons(quicksum(x[j] for j in range(J))<= N, name = 'vehicle constraint')
model.optimize()
print (time.clock()-start_time, "seconds")
补充:这是参考的Julia代码。 PySCIPOpt的求解时间比较约为17秒,Julia约为22秒。
tic()
Routing=Model(solver=CbcSolver(logLevel=0))
#Variables
@variable(Routing, X[j=1:J], Bin)
@variable(Routing, Y[i=1:I], Bin)
#Objective
@objective(Routing, Min, sum(C[j]*X[j] for j=1:J)+sum(M*Y[i] for i=1:I))
#Constraints
A=sparse(A_mat)
@constraint(Routing, consRef[i=1:I], sum(A[i,j]*X[j] for j=1:J)+Y[i]==1)
@constraint(Routing, sum(X[j] for j=1:J)<=N)
solve(Routing)
toc()