Adding a logarithmic constraint on a decision variable in Gurobi
AnsweredHi,
So, I wanted to add this constraint to my quadratic model i.e.,
ln (w1) - ln (w2) + d12 = ln 2, where all these are decision variables.
Since, logarithm does not take variable values, so I introduced another auxiliary variable named u, and then wrote the following constraints:
quadratic_model.addQConstr(u*w1 - 2*w2 == 0)
quadratic_model.addGenConstrLog(u,d12)
However, I am not getting the correct optimal solution, as compared to the results from LINGO Software. Kindly help!
-
Hi,
Please have a look at the script attached assuming that you would like to implement the constraint below with \(w_1\), \(w_2\), and \(d_{12}\) being decision variables:
\[\ln(w_1) - \ln(w_2) + d_{12} = \ln(2)\]
import gurobipy as gp
from gurobipy import GRB
import math
model = gp.Model()
d12 = model.addVar(name="d12")
# Make sure to have the tightest possible lower and upper bounds for
# variables appearing in the general constraints
w1 = model.addVar(lb=1, ub=3, name="w1")
w2 = model.addVar(lb=1, ub=3, name="w2")
# u1 = ln(w1)
u1 = model.addVar(name="u1")
# u2 = ln(w2)
u2 = model.addVar(name="u2")
model.addGenConstrLog(w1, u1, name="gen0")
model.addGenConstrLog(w2, u2, name="gen1")
model.addConstr(u1 - u2 + d12 == math.log(2), name="c0")Best regards,Maliheh1 -
You cannot retrieve the objective function value because the model is infeasible. You should enable the output from Gurobi (don't use OutputFlag=False) to see this:
Solution count 0
Model is infeasible or unbounded
Best objective -, best bound -, gap -1 -
Ah, I see! You need to define your continuous variables as free variables. Gurobi will always assume a lower bound of 0 if no other lower bound is specified.
d12 = quadratic_model.addVar(vtype=GRB.CONTINUOUS, name='d12', lb=-GRB.INFINITY)
Then, the model is solved to optimality with an objective value of 13.9459. There might be something else that is different from the LINGO model. Please compare the two models and make sure all bounds are set up correctly.
0 -
The lingo code does not seem to have the lb and ub values for w variables that you have defined in your gurobi model.
- Riley
0 -
No, it has. It has declared all the 'd' variables as free variables at the end. Also, for the W variables, LINGO, being an optimization software automatically imposes non-negativity restrictions. So, it does not need to be mentioned, Sir.
0 -
w1 = quadratic_model.addVar(vtype = GRB.CONTINUOUS, lb = 1, ub = 3, name = 'w1') w2 = quadratic_model.addVar(vtype = GRB.CONTINUOUS, lb = 1, ub = 3, name = 'w2') w3 = quadratic_model.addVar(vtype = GRB.CONTINUOUS, lb = 1, ub = 3, name = 'w3') w4 = quadratic_model.addVar(vtype = GRB.CONTINUOUS, lb = 1, ub = 3, name = 'w4') w5 = quadratic_model.addVar(vtype = GRB.CONTINUOUS, lb = 1, ub = 3, name = 'w5')
Remove the bounds and you will have your solution of 1.02778...
- Riley
1 -
You need to remove the lower bounds on the u variables as well
# u1 = ln(w1)
u1 = quadratic_model.addVar(lb = -GRB.INFINITY, name="u1")
# u2 = ln(w2)
u2 = quadratic_model.addVar(lb = -GRB.INFINITY, name="u2")
# u3 = ln(w3)
u3 = quadratic_model.addVar(lb = -GRB.INFINITY, name="u3")
# u2 = ln(w4)
u4 = quadratic_model.addVar(lb = -GRB.INFINITY, name="u4")
# u1 = ln(w5)
u5 = quadratic_model.addVar(lb = -GRB.INFINITY, name="u5")1 -
I just have one small concern. Why do we need to define the upper bound of the variables so tightly? While using LINGO, the w variables are defined as >= 0. But, for Gurobi, I have to put the ub as 3.
Ex:
w1 = quadratic_model.addVar(vtype = GRB.CONTINUOUS, lb = 1, ub = 3, name = 'w1'
Only then am I getting the desired result. Otherwise, the solution for the w variables are coming all haphazard. Any explanation for the same?
0
Please sign in to leave a comment.
Comments
8 comments