Skip to main content

Adding a logarithmic constraint on a decision variable in Gurobi

Answered

Comments

8 comments

  • Maliheh Aramon
    • Gurobi Staff

    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,
    Maliheh
    1
  • Matthias Miltenberger
    • Gurobi Staff

    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
  • Matthias Miltenberger
    • Gurobi Staff

    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
  • Riley Clement
    • Gurobi Staff

    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
  • RUPKATHA GHOSH
    • Gurobi-versary
    • First Comment
    • First Question

    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
  • Riley Clement
    • Gurobi Staff
    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
  • Riley Clement
    • Gurobi Staff

    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
  • RUPKATHA GHOSH
    • Gurobi-versary
    • First Comment
    • First Question

    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.