Skip to main content

Multi Objective Best Solution

Answered

Comments

1 comment

  • Chung-Kyun Han
    • Gurobi Staff

    Hi Joseph,

    Thank you for reaching out.

    We recommend you look through our page about Multiple Objectives first.
    Especially when you read Working With Multiple Objectives page, you can realize how Gurobi deals with multiple objectives.
    Briefly, we support two approaches: blended and hierarchical.
    The first one is to set weights for each objective, and another is to set priorities between objectives.
    Also, you can apply both approaches together.

    Here are codes for building a model for your described problem.

    import gurobipy as gp
    from gurobipy import GRB

    options, _cost, _performance = zip(
                [1, 10, 30],
                [2, 20, 20],
                [3, 20, 10],
                [4, 30, 10]
    )
    cost = {o: _cost[i] for i, o in enumerate(options)}
    performance = {o: _performance[i] for i, o in enumerate(options)}


    m = gp.Model()

    x = m.addVars(options, vtype=GRB.BINARY, name='x')
    m.addConstr(gp.quicksum(x) == 1, name='OneOption')

    m.ModelSense = GRB.MINIMIZE
    obj_cost = gp.quicksum([cost[o] * x[o] for o in options])
    obj_profit = gp.quicksum([performance[o] * x[o] for o in options])
    m.setObjectiveN(obj_cost, 0, priority=0, name="cost")
    m.setObjectiveN(obj_profit, 1, priority=1, name="performance")

    m.optimize()
    assert m.Status == GRB.OPTIMAL

    for o in options:
        if x[o].x > 0.5:
            print(f'Option {o} is the best choice')
            break

    There is a constraint, OneOption, which makes Gurobi choose only one solution.
    The optimal solution is Option 3, as you expected.
    You can play with this model by changing 'priority' or setting 'weight'.

    Additionally, you can have more concise codes if you utilize multidict and tupledict.prod.

    options, cost, performance = gp.multidict(
        {
            arr[0]: arr[1:] for arr in
            [
                [1, 10, 30],
                [2, 20, 20],
                [3, 20, 10],
                [4, 30, 10],
            ]
        }
    )
    ...

    obj_cost = x.prod(cost)
    obj_performance = x.prod(performance)
    ...


    You also can find a Python script about multi-objectives on our website or in your local drive (<installdir>/examples/python/multiobj.py).

    If you have any further questions, feel free to leave messages.

     

    Best regards,
    Chung-Kyun Han

    0

Please sign in to leave a comment.