Constraint Modelling
回答済みYes it worked, thank you!
I would like to add a constraint to my model: w_ij = min(1, sum over k of R_ijk) for every i, j
w = model.addVars(I, J, vtype=GRB.BINARY, name="w") #:if clinic i is assigned to timeslot j
Any idea how to fix this and correctly code this constraint?
-
Hi Salma,
These issues are related in that they can all be resolved by closely followed the API documentation.
The documentation for min_ indicates the arguments (except for the constant) must be "Var, or list of Var, or tupledict of Var values", but in your code you are trying to use R.sum(i, j, '*') which is a sum of variables. In order to resolve you will need to create another family of variables to equal R.sum(i, j, '*'), eg
w = model.addVars(I, J, vtype=GRB.BINARY, name="w") #:if clinic i is assigned to timeslot j
R = model.addVars(I, J, K, vtype=GRB.INTEGER, name="R") # Number of rooms assigned to clinic i assigned to a timeslot j and location K
R_ij_sum = model.addVars(I, J, name="R_ij_sum")
model.addConstrs((R_ij_sum[i, j] == R.sum(i, j, '*') for i in range(I) for j in range(J)), name="R_ij_sum_constraint")
model.addConstrs((w[i, j] == min_(R_ij_sum[i,j], constant=1) for i in range(I) for j in range(J)), name="w_ij_constraint")It is also worth considering whether the following would also be enough to model what you need to
w = model.addVars(I, J, vtype=GRB.BINARY, name="w") #:if clinic i is assigned to timeslot j
R = model.addVars(I, J, K, vtype=GRB.INTEGER, name="R") # Number of rooms assigned to clinic i assigned to a timeslot j and location K
model.addConstrs((w[i, j] <= R.sum(i, j, '*')) for i in range(I) for j in range(J)), name="w_ij_constraint")As for the indicator constraints the documentation states
An INDICATOR constraint \( z = f \rightarrow a^T x \leq b \) states that if the binary indicator variable \( z \) is equal to \( f \), where \( f \in \{0,1\} \), then the linear constraint \( a^T x \leq b \) should hold.
The following
If sum(R_ijk) > 0, then w_ij = 1
cannot be directly modeled with an indicator constraint because it does not fit the requirements:
* sum(R_ijk) is not a binary variable
* the relation is >, and not =In order to achieve what you want you will need to form the contrapositive of your expression, eg "if w = 0 then sum(R_ijk) = 0" and model that instead.
- Riley
0
サインインしてコメントを残してください。
コメント
1件のコメント