Skip to main content

Retrieving value of a second objective function in single-objective function optimization

Answered

Comments

2 comments

  • Eli Towle
    Gurobi Staff Gurobi Staff

    If you want to evaluate a function at the optimal solution of your single-objective solve, you could:

    Below is an example code snippet that does this, modified from the mip1.py example:

    import gurobipy as gp
    from gurobipy import GRB

    m = gp.Model()

    x = m.addVar(vtype=GRB.BINARY, name="x")
    y = m.addVar(vtype=GRB.BINARY, name="y")
    z = m.addVar(vtype=GRB.BINARY, name="z")

    m.addConstr(x + 2 * y + 3 * z <= 4, "c0")
    m.addConstr(x + y >= 1, "c1")

    # Set the objective function
    obj1 = x + y + 2 * z
    m.setObjective(obj1, GRB.MAXIMIZE)

    # Function we want to evaluate after solving
    obj2 = 3 * x + 2 * y + z

    m.optimize()

    print("Optimal objective value:", m.ObjVal)
    print("Value of obj2 at current solution:", obj2.getValue())

    The output printed at the end is:

    Optimal objective value: 3.0
    Value of obj2 at current solution: 4.0
    0
  • Niklas Heidingsfelder
    Gurobi-versary
    First Comment
    First Question

    Hello Eli,

    Thank you for the fast response. That's exactly what I was looking for!

    Best regards,

    Niklas

    0

Please sign in to leave a comment.