Skipping nonexisting decision variables in for loop
AnsweredHello,
I am trying to maximize the probability of matches p[(i,j)], but because we don't have p[(i,j)] for every z[I,j] value > the algorithm stops running when it finds an i and j that don't satisfy the p[(i,j)]. Is there a way to skip nonexisting variables so the algorithm can continue to run?
z[I,j]: the decision variable to have both women i and man j in the group, =1 if both are chosen =, 0 otherwise
Thank you,
0

You could define a list or a dictionary of indices that are not present and add an \(\texttt{if}\)clause to your quicksum call.
import gurobipy as gp
m = gp.Model()
xI = 1
xJ = 2
x = m.addVars(xI, xJ, name="x")
yI = 3
yJ = 4
y = m.addVars(yI, yJ, name="y")
present = {}
# loop over the bigger set yI x yJ
for i in range(yI):
for j in range(yJ):
if i < xI and j < xJ:
present[i,j] = True
else:
present[i,j] = False
m.setObjective(
gp.quicksum(x[i,j]*y[i,j]
for i in range(yI)
for j in range(yI)
if present[i,j]))Note that the above snippet works only if \(x_I \times x_J\) is a subset of \(y_I \times y_J\) and might require more work if you are using other indices.
0
Please sign in to leave a comment.
Comments
1 comment