Skip to main content

How to get the result of the objective function while using a slack variable?

Answered

Comments

2 comments

  • Riley Clement
    Gurobi Staff Gurobi Staff

    Hi Luis,

    First let's fix an error in your code. This:

    model.getObjective()

    only works properly after model.update() or model.write() or model.optimize().  In your code it will just return 0.

    I would not even use the getObjective() method.  You can just do something like this:

    orig_obj = 2 * x**2 + y
    penalty_weight = 0.0001
    model.setObjective(orig_obj + (penalty_weight * slack), GRB.MINIMIZE)

    After optimization you can use QuadExpr,getValue(), i.e.

    print(orig_obj.getValue())

    - Riley

    1
  • Luis Luengo
    First Comment
    First Question

    Thank you very much Riley. This solved my issue.

    0

Please sign in to leave a comment.