メインコンテンツへスキップ

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

回答済み

コメント

3件のコメント

  • 正式なコメント
    Simranjit Kaur
    • Gurobi Staff
    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.
  • Eli Towle
    • 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

投稿コメントは受け付けていません。