Using value of a decision variable as index of another decision variable
AnsweredHi everyone,
I am having trouble on using value of a decision variable as index of another decision variable. Now I have an integer variable i_d[m] for each m, which means the location of vehicle m. Now I have another binary variable x[i,t,j,t',m] which indicate whether vehicle m traverse space-time link (i,t,j,t'). Now I want to use i_d[m] as index of x with x[i,t,i_d[m],t',m] for further modeling, however, even though I have used the function of model.update() at the very beginning, I still get error of " KeyError: (0, 1, <gurobi.Var i_d_1>, 10, 1) ". If someone have ideas on how to successfully use decision variable as index of another decision variable.
Thanks in advanced !
Best regards
-
The issue you're encountering arises from attempting to use the value of a decision variable (
i_d[m]
) as an index for another decision variable (x[i,t,j,t',m]
). In Gurobi, decision variables are not evaluated until the model is solved, so using them directly as indices can lead to errors, such as the "KeyError" you've mentioned.To address this, you can consider the following approaches:
-
Big-M Formulation: Instead of directly indexing
x
withi_d[m]
, introduce additional binary variables and constraints to linki_d[m]
with the relevant entries ofx
. For instance, you could define a set of binary variablesz[i, m]
wherez[i, m] = 1
ifi_d[m] = i
and0
otherwise. You can then model the selection ofx
variables based on the values ofz[i, m]
. -
Reformulate the Problem: If possible, reformulate the problem to avoid the need for decision variables as indices. This might involve rethinking how the problem is represented and how constraints are applied.
-
Piecewise Linear Approximations: If the mapping between
i_d[m]
and the relevant indices ofx
can be approximated linearly, you could use piecewise linear approximations to model the relationship.
Here's a conceptual example using the Big-M approach:
from gurobipy import Model, GRB
# Define model
model = Model()
# Example data
M = 5 # Number of vehicles
N = 10 # Number of locations
T = 15 # Number of time periods
# Decision variables
i_d = model.addVars(M, vtype=GRB.INTEGER, name="i_d") # Location indices
x = model.addVars(N, T, N, T, M, vtype=GRB.BINARY, name="x") # Binary decision variables
z = model.addVars(N, M, vtype=GRB.BINARY, name="z") # Auxiliary binary variables
# Constraints to link i_d[m] and z[i, m]
for m in range(M):
model.addConstr(sum(z[i, m] for i in range(N)) == 1, name=f"link_z_{m}")
for i in range(N):
model.addConstr(i_d[m] == i * z[i, m], name=f"link_id_z_{i}_{m}")
# Additional constraints using z to select x
for m in range(M):
for t in range(T):
for t_prime in range(T):
for i in range(N):
model.addConstr(x[i, t, i_d[m], t_prime, m] <= z[i, m], name=f"select_x_{i}_{t}_{t_prime}_{m}")
# Objective and model optimization
model.setObjective(0, GRB.MINIMIZE) # Dummy objective
model.optimize()In this example:
-
z[i, m]
indicates whether the vehiclem
is at locationi
. - The constraints ensure that only one
z[i, m]
can be 1 for eachm
, effectively selecting the location. - The
x[i, t, i_d[m], t', m]
variables are linked throughz[i, m]
.
This approach ensures that the value of
i_d[m]
is properly accounted for in the constraints and decision-making process, avoiding the direct use of decision variables as indices.If you encounter further issues or need a more specific example, feel free to provide additional details about your problem.
0 -
-
Hi,
Thanks for your reply. But the error " KeyError: (0, 1, <gurobi.Var i_d_1>, 10, 1) " still shown up when the code as below is performed:
model.addConstr(x[i, t, i_d[m], t_prime, m] <= z[i, m], name=f"select_x_{i}_{t}_{t_prime}_{m}")
Thanks
Best regards
0 -
You are absolutely right! Gurobot has been giving the wrong example code here.
The rest is still fine, though, and you need to find a way to reformulate your model. You simply cannot have decision variables as indices because they are not static. You need to define auxiliary variables that model that connection.
If you want to use Gurobot yourself, you can check it out here: ChatGPT - Gurobot
0
Please sign in to leave a comment.
Comments
3 comments