Multiple "or" statements within a constraint
回答済みHello everyone,
I'm working on an assignment problem and trying to make sure that summation over time of binary value "x" from start time (a) to end time (a+duration) is equal to the frequency (f) of the service (i). Second dimension of the binary array represents the the service provider and I want all service to be provided by only one provider at any time, therefore I tried using the "or" statement. It was working fine when I only had 2 providers but multiple "or" statements did not work and you can see the constraint that I'm having trouble with below.

I thought of having the sum(x[t,s,i,y] for t in range(a_yi[i,y],a_yi[i,y]+d_yi[i,y]) for s in range(S)) equal to the frequency and adding another constraint:
for t in range(T):
for i in range(N):
for y in range(Y):
m.addConstr(sum(x[t,s,i,y] for s in range(S)) <=1
to make sure no-one is receiving service from multiple service provider at the same time, but that didn't help me with my case. Because over time service is supposed to be received by only one service provider.
I was hoping that someone will be able to help me with this problem, any kind of help would be appreciated!
-
正式なコメント
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?. -
Hi Yaren,
The Python \(\texttt{or}\) operator does not work with Gurobi's Python syntax. You could use the addGenConstrOr function. However, it requires all input variables to be binary making it quite hard to use directly in your case.
I guess that you are trying to model that at most one of the 4 sum terms is equal to \(\texttt{f_yi}\), is this correct? For this, you could introduce additional binary variables \(p_{s,i,y}\) which equals 1 if service is provided by provider \(s\) and \(0\) otherwise. You can then introduce the indicator constraints for each \(s\)
(p[s,i,y] = 1) -> (sum(x[t,s,i,y] for t in range(...) == f_yi[i,y])
To make sure that no-one is receiving service from multiple providers, you can add
sum(p[s,i,y] for s in S) <= 1 # limit number of providers
In the case that \(\texttt{f_yi}\) is a scalar, you can use the equality constraint
sum(x[t,s,i,y] for t in range(...) == f_yi[i,y]*p[s,i,y]
instead of the indicator constraint. This additionally forces all \(\texttt{x[t,s,i,y]}\) values to 0.
Best regards,
Jaromił0 -
Hello Jaromił,
Thank you so much for your response, I really appreciate it! I'm fairly new to Gurobi, wasn't aware that the "or" statement wasn't working the way I wanted and thanks for pointing it out. And you're right, addGenConstrOr function doesn't work for my case.
I think the only problem I'm facing right now is that: p_s,i,y is a variable object and if I'm not mistaken, p_s,i,y couldn't be evaluated to a value in the "if" statement. Therefore unfortunately, I don't know how to write the following statement you recommended.
(p[s,i,y] = 1) -> (sum(x[t,s,i,y] for t in range(...) == f_yi[i,y])
And I'm receiving this following error:

Best regards,
Yaren
0 -
Hi Yaren,
Sorry for not being more clear from the beginning. You are correct and variable objects cannot be used in \(\texttt{if}\)-statements. You can model indicator constraints via the addGenConstrIndicator function as
model.addGenConstrIndicator(p[s,i,y], True, sum(x[t,s,i,y] for t in range(...) == f_yi[i,y], GRB.EQUAL, 1.0)or directly via addConstr
model.addConstr((p[s,i,y] == 1) >> (sum(x[t,s,i,y] for t in range(...) == f_yi[i,y]))Best regards,
Jaromił0 -
Thank you so much Jaromił, this was really helpful!
Best,
Yaren
0
投稿コメントは受け付けていません。
コメント
5件のコメント