• Gurobi Staff

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

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.

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
Iason

• Gurobi Staff

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

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
Iason

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
Iason

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
Iason

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
Iason

Hi Riley, my code is in this folder in dropbox:
https://www.dropbox.com/scl/fo/0w8bt6jue8dn5cykewa94/h?dl=0&rlkey=2h1vvq0plpt8b6rl8pepehtos

There 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
Iason

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
Iason

• Gurobi Staff

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 line

model.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

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
Iason