if constraint with sums
AnsweredHi,
I've checked some posts about if constraints, however, I haven't been able to solve my problem. I have a binary variable called X, with two sets of indices p and t. p represents if there is a production facility at location p, and t if the production facility is working at time t (represents the number of hours ina year, 8760). I would like to model a constraint stating that the number of hours that a facility is working is either zero or higher than 6132 (70% of the number of hours in a year). Why can't it be written like this:
m.addConstrs(X.sum(p, '*') == 0 or X.sum(p, '*') >= 6132 for p in range(num_ele))
And what is the correct way to do it?
Thank you!
-
Official comment
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 try Gurobot, our chatbot interface offering instant, expert-level support. -
Hi José,
You can use semi-continuous or semi-integer variables for this purpose. Please refer to this part in the documentation for further information: Variables (gurobi.com)
You just have to assign a semi-continuous variable to the value of the sum and set its lower bound to 6132:
s = m.addVars(num_ele, vtype="SEMICONT", lb=6132)
m.addConstrs(X.sum(p, '*') == s[p] for p in range(num_ele))Cheers,
Matthias0 -
Hello Matthias,
Thank you for your brief reply.
In that case, I will add a constraint that makes every possible production facility work at least 6132 hours? However, the model is supposed to choose which production locations will have a production facility. That's why I had the constraint to include X.sum(p, '*') == 0 or... If I only write that constraint, every possible production facility will be active for at least 6132, right?
0 -
Semi-continuous variables can be either at 0 or between the specified bounds. Otherwise, they would just be normal variables. So my suggestion should exactly do what you describe. You can assign those variables a high price in the objective so there is an incentive to keep them at zero if possible.
Cheers,
Matthias0 -
Dear Matthias,
Oh, that's perfect then! I described the wrong problem for the title of the post.
I have another question, this time regarding an if constraint. I have two binary variables, both with two indexes. Again variable X, denotes if there is a production facility at location p and working at time t, and variable Y, which denotes if there is transportation between production p and retailer i.
If there is no production at location p, then obviously there is no transportation of goods between production facility p and retailer i. I have been trying to model this constraint as follows:
m.addConstrs((X.sum(p, 'x') == 0) >> (Y.sum(p, '*') == 0) for p in range(num_ele))
There can be production at facility p at a certain time t or not, that's why I have done the sum, in order to add the constraint that there must be no production at all, regardless of the hour. Concerning the binary variable Y, this one doesn't have an index representing a time resolution, only if transportation between p and i has been established. So I guess I can write a constraint stating that the variable Y should be zero for every p where there is no production during the year (X.sum(p, 'x') == 0) regardless of the i index, rigth?
Either way, I have not been able to write this constraint. With the code above the model prompts the following error: gurobipy.GurobiError: Indicator constraints can only be triggered by a single binary variable at a given value
I have checked similar posts about this but I still haven't managed to get the constraint right.
I would really appreciate your help with this. Thank you in advance,
José Rodrigues
0 -
Hi José,
Yes, to add an indicator constraint, you need to work with a single binary variable that either activates or deactivates the constraint. In your case, you can add a new auxiliary variable and assign the sum to this variable. Then you can use it in your indicator constraints.
Cheers,
Matthias1 -
Appreciate your comment Matthias Miltenberger. Such thinking process of Indicator constraints is helping me building my model
0
Post is closed for comments.
Comments
7 comments