How do i model nested conditional constraints in Gurobi?
OngoingIf D_k > a, then x_k=1, otherwise x_k=0.
when x_k=1, if D_k(i) > a, then x_k(i) = 1, otherwise x_k(i) = 0.
when x_k=1 and x_k(i) = 1, if D_k(ij) > a, then x_k(ij) = 1, otherwise x_k(ij) = 0.
I want to model such nest conditional constraints in Gurobi.
-
To model nested conditional constraints in Gurobi, an optimization solver, you will need to use binary decision variables and conditional constraints. Given your problem statement, it looks like you are trying to create a hierarchical or nested structure of binary decision variables where the value of one variable depends on the value of another.
Let's break down your requirements:
- Top-level condition: If Dk>a , then xk=1 ; otherwise, xk=0 .
- Second-level condition: When xk=1 , if Dk(i)>a , then xk(i)=1 ; otherwise, xk(i)=0 .
- Third-level condition: When xk=1 and xk(i)=1 , if Dk(ij)>a , then xk(ij)=1 ; otherwise, xk(ij)=0 .
Here is how you can model these conditions in Gurobi using Python:
from gurobipy import Model, GRB
# Create a new model
model = Model("NestedConditionalConstraints")
# Define the parameter 'a'
a = ... # assign the value of 'a'
# Define D_k, D_k(i), D_k(ij) based on your problem
# For example, D_k could be a list or a dictionary of values
# Define binary variables x_k, x_k(i), x_k(ij)
x_k = model.addVar(vtype=GRB.BINARY, name="x_k")
x_k_i = {} # a dictionary to hold x_k(i) variables
x_k_ij = {} # a dictionary to hold x_k(ij) variables
# Add constraints
# Top-level condition
for k in D_k:
model.addGenConstrIndicator(x_k, True, D_k[k] > a, name=f"constraint_top_{k}")
# Second-level condition
for i in some_index_set:
x_k_i[i] = model.addVar(vtype=GRB.BINARY, name=f"x_k({i})")
model.addGenConstrIndicator(x_k_i[i], True, D_k_i[i] > a, x_k == 1, name=f"constraint_level2_{k}_{i}")
# Third-level condition
for j in some_other_index_set:
x_k_ij[(i, j)] = model.addVar(vtype=GRB.BINARY, name=f"x_k({i},{j})")
model.addGenConstrIndicator(x_k_ij[(i, j)], True, D_k_ij[(i, j)] > a, and_(x_k == 1, x_k_i[i] == 1), name=f"constraint_level3_{k}_{i}_{j}")
# Update the model to integrate new variables
model.update()
# Set objective and solve the model as per your problem requirementsRemember to replace the
D_k
,D_k_i
,D_k_ij
, anda
with actual values or expressions relevant to your problem. Thesome_index_set
andsome_other_index_set
should be replaced with the actual index sets you are working with.This script creates a hierarchical set of binary decision variables where the activation of each variable depends on the conditions specified in your problem statement. The
addGenConstrIndicator
function in Gurobi is used to model these conditional constraints effectively.0 -
when i write " model.addGenConstrIndicator(x_k_i[i], True, D_k_i[i] > a, x_k == 1, name=f"constraint_level2_{k}_{i}")" in gurobi, i get a feedback about "gurobipy.GurobiError: Additional sense argument provided for general constraint of indicator type"
0
Please sign in to leave a comment.
Comments
2 comments