Inncorrect output?
回答済みHi!
I am working on a quite simple model, but do not seem to obtain the correct result. For the model presented in the following I have set the costCCS cost to 1 000 000 (extremely high) in order for the model to choose all x[i] to be the value of 1, as this is to then be, in practice the cheapest outcome. But the model give the following result:
x[0]=1
x[1]=1
x[2]=0
x[3]=1
x[4]=1
Any suggestions of what could be wrong in the model? Unsure if it is my coding or the model itself that is an issue...

import gurobipy as gp
from gurobipy import GRB
import numpy as np
P = [0,1,2,3,4]
distance = np.array([[0,50,70,110,170],
[50,0,20,60,120],
[70,20,0,40,100],
[110,60,40,0,60],
[170,120,100,60,0]])
costRP = [100,90,140,200,70]
costCCS = 150
# Parameter
d_max = 140
# Arcs
A = [(i,j) for i in P for j in P if i!=j] # arcs where vessel travels between different ports
D = {(i,j):distance[i,j] for i,j in A if i<j and j!=0 and distance[i,j]<=d_max} # reducing the set even further
data = []
for i,j in D:
data.append((i,j,D[i,j]))
print(data)
# Arcs beginning at source node
startres=[]
for i in data:
if i[0] == 0:
startres.append(i)
print(startres)
# Arcs ending in sink node
ends=[]
for i in data:
if i[1] == len(P)-1:
ends.append(i)
print(ends)
# Remaining arcs
rest=[]
for i in data:
if i[0]!=0 and i[1]!=len(P)-1:
rest.append(i)
print(rest)
# Modelling
f = gp.Model(name = "RegularSimpleModel")
# Variables
x = f.addVars(P, name = 'x', vtype = GRB.BINARY)
y = f.addVars(D, name = 'y', vtype = GRB.BINARY)
# Variables
aux = f.addVar(name = 'aux', vtype = GRB.CONTINUOUS)
# Constraints
# Two constraints that define that if an arc (eks: from port 1 to port 2) is chosen, there has to be an established RP in port 1. And vice versa.
c1 = f.addConstrs((y[i,j]<=x[i] for i,j in D), name='RP_i')
c2 = f.addConstrs((y[i,j]<=x[j] for i,j in D), name='RP_j')
# Constraints that force vessel to travel from port 1 and end in port 5
c3 = f.addConstr((sum(y[i[0],i[1]] for i in startres)==1), name='Start')
c4 = f.addConstr((sum(y[i[0],i[1]] for i in ends)==1), name='End')
# Flow conservation constraint, excluding arcs with start node in port 1 and end node in port 5
c5 = f.addConstrs((y.sum('*',j) - y.sum(j,'*') >= 0 for j in P if j != 0 and j != len(P)-1), name = 'FlowConservation')
# Max constraint that holds the maximum distance we want to minimize.
c6 = f.addConstrs((y[i]*D[i] <= aux for i in D), name='MaxConstraint')
obj = sum(x[i] * costRP[i] for i in P) + (costCCS * aux)
f.setObjective(obj, GRB.MINIMIZE)
f.optimize()
-
正式なコメント
This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum, or try Gurobot, our chatbot interface offering instant, expert-level support. -
I am also struggling to retrieve the reduced costs of the different variables. How would I proceed with this? I have tried the following:
f.getAttr(x.RC)
But then I get the error: 'tupledict' object has no attribute 'RC'
0 -
Variable \(x_2\) is set to \(0\) because your aux variable is forced to take the value of \(60\), because \(y_{3,4}=1\).
As stated in the documentation, reduced costs are only available for convex continuous models.
Best regards,
Jaromił0
投稿コメントは受け付けていません。
コメント
3件のコメント