how to get time value from multidict to my objective function?
AnsweredHello developer,
I need to create a variable with truck, location, time. I use multidict() for my parameter.
set_truck = ["trucks1","trucks2","trucks3"]
location, cost, time= gp.multidict({("A", "B"):[100,123],
("A","C"): [70,134],
("B","D"): [82,345],
("C","D"): [95,234],
("C","E"): [100,453],
("E","F"): [77,345],
("E","B"): [85,643],
("F","C"): [50,332]})
and I create a variable like this ,
x = mdl.addVars(location,set_truck, time, vtype=GRB.BINARY, name="locate_"+str(k)+"_")
when I print x I got the wrong output. why?
{('A', 'B', 'trucks1', 'A', 'B'): <gurobi.Var *Awaiting Model Update*>,
('A', 'B', 'trucks1', 'A', 'C'): <gurobi.Var *Awaiting Model Update*>,
('A', 'B', 'trucks1', 'B', 'D'): <gurobi.Var *Awaiting Model Update*>,
('A', 'B', 'trucks1', 'C', 'D'): <gurobi.Var *Awaiting Model Update*>,
why my time parameter is showing location inputs?
why I didn't get output like this?
{('A', 'B', 'trucks1', 123): <gurobi.Var *Awaiting Model Update*>,
('A', 'B', 'trucks1', 134): <gurobi.Var *Awaiting Model Update*>,
('A', 'B', 'trucks1', 345): <gurobi.Var *Awaiting Model Update*>,
('A', 'B', 'trucks2', 234): <gurobi.Var *Awaiting Model Update*>,
my objective function something like this
mdl.modelSense = GRB.MINIMIZE
for k in set_truck:
mdl.setObjective(quicksum(x[i,j,k,t]* cost[i,j] for i,j in location for t in time))
Thank you
-
Official comment
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 why not try our AI Gurobot?. -
The \( \texttt{time} \) returned by multidict() is a \( \texttt{dict} \), not a \( \texttt{list} \):
>>> time
{('A', 'B'): 123, ('A', 'C'): 134, ('B', 'D'): 345, ('C', 'D'): 234, ('C', 'E'): 453, ('E', 'F'): 345, ('E', 'B'): 643, ('F', 'C'): 332}When you pass \( \texttt{time} \) to Model.addVars(), Gurobi uses the dictionary keys as indices. This isn't desirable here, since \( \texttt{time.keys() == location} \).
Can you post the full list of variable indices that you want to use? You won't be able to use the \( \texttt{time} \) values as indices, since \( 345 \) is used twice and would result in duplicate variable keys:
>>> time.values()
[123, 134, 345, 234, 453, 345, 643, 332]Also, the code for the objective function doesn't quite make sense. You should not set the objective function in a \( \texttt{for} \) loop, since you can only have one objective function. Maybe the \( \texttt{for k in set_truck} \) should be in the quicksum() function?
0 -
Thank you for your reply.
value of time is just randomVariable :
\(x_{i,j,t}^k\) Binary variable=1, if truck k traverses edge e_{ij} at time t
Does that mean the code will be something like this?mdl.modelSense = GRB.MINIMIZE
mdl.setObjective(quicksum(x[i,j,k,t]* cost[i,j] for i,j in location for t in time for k in set_truck))0 -
I don't see a reason to use multidict() to generate a dictionary that maps locations to times. Such a dictionary makes sense for costs, but not times.
I suspect you want something like the following, assuming your time indices are \( \{0, \ldots, 4\} \):
trucks = ["trucks1", "trucks2", "trucks3"]
times = list(range(5))
cost = {("A", "B"): 100,
("A", "C"): 70,
("B", "D"): 82,
("C", "D"): 95,
("C", "E"): 100,
("E", "F"): 77,
("E", "B"): 85,
("F", "C"): 50}
locations = cost.keys()
# ...
x = mdl.addVars(locations, trucks, times, vtype=GRB.BINARY, name="x")This creates \( 120 \) variables (\( 8 \) locations \( \times \) \( 3 \) trucks \( \times \) \( 5 \) times) with the following indices:
(A, B, trucks1, 0)
(A, B, trucks1, 1)
(A, B, trucks1, 2)
(A, B, trucks1, 3)
(A, B, trucks1, 4)
(A, B, trucks2, 0)
(A, B, trucks2, 1)
(A, B, trucks2, 2)
(A, B, trucks2, 3)
(A, B, trucks2, 4)
(A, B, trucks3, 0)
(A, B, trucks3, 1)
(A, B, trucks3, 2)
(A, B, trucks3, 3)
(A, B, trucks3, 4)
(A, C, trucks1, 0)
(A, C, trucks1, 1)
...
(F, C, trucks2, 4)
(F, C, trucks3, 0)
(F, C, trucks3, 1)
(F, C, trucks3, 2)
(F, C, trucks3, 3)
(F, C, trucks3, 4)Is this what you're trying to do?
Yes, your modified objective function makes more sense.
0 -
Still I have syntax error with my objective function why?
set_time =[123, 134, 345, 234, 453, 346, 643, 332]
mdl.modelSense = GRB.MINIMIZE
mdl.setObjective(quicksum(x[i,j,k,t]* cost[i,j] for i,j in location for t in set_time for k in set_truck))0 -
The objective function and syntax discussion is continued in a separate community post.
0
Post is closed for comments.
Comments
6 comments