0/1 index function on constraints
AnsweredHello guys, I want to write a constraint that has a sum. In the sum, a variable is multiplied by an index parameter that is either 1 or 0. I think there are 2 ways to make this happen. The first is to create a dictionary that will have 0 or 1 according to the parameters it gets on its keys. The second way is to create a function that is going to determine the outcome 0/1 according to the input parameters each time. Do you think any way is better than the other in terms of execution? If I go the function way do I need to pass this function in the model in some way or just the definition of the function in a code above the optimization model is enough?
-
Hi Iason,
To make it clear what you are wanting to do, can you post code showing one of the two approaches you suggest?
- Riley
0 -

I want to model this constraint. δ is a binary index (parameter not variable) that takes 1/0 depending on the subscripts and superscripts. Can I create a function to emulate this index and multiply the function with the variable f? the input to the function will be the subscripts and superscripts. All superscripts and subscripts in δ are parameters.
0 -
0 -
I want the input of the function to be: a,k,i,r,s and the output 0/1. If I define this function somewhere in my code in the initial stages can I later use it in the constraint of the optimization model? If yes do I need to clarify anything to the optimization model or it will understand that this is a predefined function from previously in my code?
Kind regards
Iason0 -
Hi Iason,
Either the function or dictionary approach will work. If the delta values are only calculated once then the function will be faster.As far as the optimization model is concerned it doesn't know where the coefficients of variables come from - a dictionary, a function, randomly generated, or hardcoded - ultimately it's just a number so there is no special handling needed.
- Riley
0 -
Hi Riley,
By the same logic if I have a non-linear function like the below:
where x_a is variable and P_a and t_{0,a} are parameters. Can I define it not as a model constraint but as a function and use it in the constraints and in the objective function such as: 
and in the constraints as:

without violating the linear capabilities of Gurobi?
Kind regards
Iason0 -
What I mean is instead of writing the first equation as a non-linear constraint in the optimization model(that I suppose is impossible), I am wondering if I could make it a function and then use this non-linear function in the model objective function as well as the model constraints.
Kind regards
Iason0 -
Hi Riley,
Another example to make myself clear is: suppose I have the constraint:
Can I create a function f that takes as input R_i, R_w and returns their log difference and then set this function = U_i-U_w as a constraint? (If im not clear on something please let me know)
Kind regards
Iason0 -
Hi Riley, I have found a way to go around all these, my problem is how to set a logarithmic equation with parameterized variables. I cannot find this. Do you know how I could make the below code work?
I'm using python by the way.
Rirs = model.addVars(I, drs.keys(), lb=0, ub=1, vtype=GRB.CONTINUOUS, name='R_i^rs')
lRirs = model.addVars(I, drs.keys(), lb=0, ub=1, vtype=GRB.CONTINUOUS, name='lR_i^rs')
model.addGenConstrLog(Rirs[i,r,s] , lRirs[i,r,s] for i in I for r,s in drs.keys())
The first two lines define the variables and the third is establishing the log relationship but it gives error. Can you help me with this? That is the only help I need. Ignore the previous messages.
Kind regards
Iason0 -
Hi Riley, my code is in this folder in dropbox:
https://www.dropbox.com/scl/fo/0w8bt6jue8dn5cykewa94/h?dl=0&rlkey=2h1vvq0plpt8b6rl8pepehtosThere are 2 excel files that it reads. You do not have to go through the whole code. You can just run it up to line 370. Just change the initial link to the folder that you put the excel files. Im saying this to help you. If you know the answer of how to form this type of logarithmic constraint you can just let me know without looking at my code to speed up.
Kind regards
Iason0 -
Hi Riley, I think I have found the solution. I have put a one note with the explanation and my question in the Dropbox link. And the new version of my code named Benders new. The modeling explained in one note is on the code line 350. Could you take a look to confirm if this is correct? Basically, I'm wondering if the variable parameters of a constraint pass through the log constraint since we cannot have parametrized variables in the log constraint. I explain my question better in the one-note file. If you have any questions feel free to ask me. You do not need to go through my whole code. If you want you can run it up to the constraints on line below 350.
https://www.dropbox.com/scl/fo/0w8bt6jue8dn5cykewa94/h?dl=0&rlkey=2h1vvq0plpt8b6rl8pepehtos
Kind regards
Iason0 -
Hi Iason,
I'm not a dropbox user so the link doesn't work for me but I can make some comments based off the code snippet you posted.
With reference to the docs for Model.addGenConstrLog, note that it takes two arguments and each of these arguments must be a variable. I suspect you have a typo in your code as on this linemodel.addGenConstrLog(Rirs[i,r,s], lRirs[i,r,s] for i in I for r,s in drs.keys())
the first argument is a variable, but the second is a generator expression
lRirs[i,r,s] for i in I for r,s in drs.keys()
Perhaps you are wanting to do the following?:
for key in Rirs.keys():
model.addGenConstrLog(Rirs[key], lRirs[key])- Riley
0 -
Thanks for the help, Riley. So basically I have to put the for loop outside of the logarithm constraint function. that is the difference between this type of constraint with the rest of the constraints is that in the typical constraints we can put the generator inside the constraint. I got it, great.
Kind regards
Iason0
Please sign in to leave a comment.
Comments
13 comments