Using the value of a decision variable as an argument in Python function
AnsweredHello,
I am new to Python and Gurobi and I am currently facing an issue with my objective function.
I have a multi-objective linear program in which my second objective function consists of a term, in which I would like to multiply my continuous decision variable (here: w_FTL[s, c]) with a certain distance value (here: dist[s, c]) and a parameter that is defined as a dictionary with some hundred key-value pairs (here: alpha_truck[normal_round(a_LTL[s, c], 2)]). The parameter “alpha_truck” depends on another continuous decision variable (here: a_LTL[s, c]) which shall be determined by the model during the optimization process. Once its value has been computed, the respective value should be rounded to two decimals using a custom function “normal_round” and then given as the key to the dictionary to get the corresponding value.
For reference, the dictionary, the objective function and the constraint to determine the decision variable value of a_LTL[s, c] look like this:
alpha_truck = {0.0: 100, 0.01: 99, 0.02: 98, 0.03: 95, 0.04: 90, 0.05: 87, 0.06: 82, ..., 1.0: 1}
obj2 = (w_FTL[s, c] * dist[s, c] * alpha_truck[normal_round(n_grossweight/n_chargeableweight, 2)]
+ w_LTL[s, c] * dist[s, c] * alpha_truck[normal_round(a_LTL[s, c], 2)] for s, c in SET)
# constraint to determine a_LTL[s, c]
m.addConstrs(
(a_LTL[s, c] <= w_LTL[s, c] / cap_trailer_WU for s, c in SET), name='capacity_utilization'
)
Now, my issue is that if I run the whole code, I get the following error message:
TypeError: int() argument must be a string, a bytes-like object or a number, not 'gurobipy.LinExpr'
Apparently, it seems as if it is not possible to give the value of the decision variable as an argument to a function. So, I would like to ask if someone could perhaps provide an answer on how I can give the actual value of a decision variable as an argument to a function? Is this even possible? If not, I would appreciate if someone would perhaps have an idea on how I can achieve what I am trying to do.
Thanks in advance!
-
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 Niklas,
the thing you want to achieve can be done, although it won't be easy.
First of all, rounding can be achieved, although it will require scaling and additional variables.
Further, I am afraid you won't be able to use constants the way you wish. I would suggest coming up with a (linear) function that will convert the value of a_LTL into the desired value (which would replace looking up the value in alpha_truck dict).
In terms of pseudocode, this would look something like this:
aux1 = m.addVar(vtype="C")
m.addConstr(aux1 == a * a_LTL + b) # it would be up to you to figure out the appropriate a and bYou would obviously need to include aux1 in the objective function.
Hope this helps.
Best regards
Jonasz0 -
Hi Jonasz,
Thank you very much for your helpful feedback! I think I understood your approach.
Just to make sure that I got it right: Basically, your suggestion would be to determine a function in dependency of "a_LTL" that will give me the corresponding values that I am currently trying to retrieve from the dictionary, right?
Kind regards,
Niklas
0 -
Hi Niklas,
Just to make sure that I got it right: Basically, your suggestion would be to determine a function in dependency of "a_LTL" that will give me the corresponding values that I am currently trying to retrieve from the dictionary, right?
That's correct.Best regardsJonasz0 -
Hi Jonasz,
Ok, thanks for the confirmation!
Best regards,
Niklas
0
Post is closed for comments.
Comments
5 comments