ke qi
ke qi

Reputation: 44

Gurobi: there is a "max(x, 0)" in the objective function

In my multi-objective model, there is a "max(x ,0)" like formation in the objectives:

enter image description here

The objective function contains a part of "piecewise" function.

Codes are as bellow:

F6 = gp.quicksum(max((1 - (gp.quicksum(
    uncertainty_prob_regular[(i, j, t)] * proportion_goods_supplier_regular_scenario[(i, j, t)] + uncertainty_prob_contract[(i, j, t)] *
    proportion_goods_supplier_contract_scenario[(i, j, t)] + uncertainty_prob_emergency[(i, j, t)] * proportion_goods_supplier_emergency_scenario[
        (i, j, t)] for j in supplier))), 0) * demand_list[i] * alpha_price * sales_prices_list[i] for i, t in assign_list2)

The error is :

File "src\gurobipy\gurobi.pxi", line 3349, in gurobipy.quicksum
File "src\gurobipy\linexpr.pxi", line 434, in gurobipy.LinExpr.__richcmp__

I guess maybe the "max" method in python and the "quicksum" method in Gurobi does not compatible.

Upvotes: 0

Views: 1497

Answers (2)

ke qi
ke qi

Reputation: 44

At the beginning, I thought of expressing "max(x ,0)" through the constraint relationship of two additional variables. like: if x1>=0 then x2=1; else if x1<0, then x2=0.

But I failed, then I find the "Model.addGenConstrMax()" method in Gurobi REFERENCE MANUAL.

# two extra variables:
continue_check_origin = model.addVars(check_origin, vtype=GRB.CONTINUOUS, name='continue_check_origin')
continue_check_mature = model.addVars(check_mature, vtype=GRB.CONTINUOUS, name='continue_check_mature')

# constraints of check_mature and check_origin
for i in goods:
    for t in scenario:
        model.addGenConstrMax(continue_check_mature[(i, t)], [continue_check_origin[(i, t)]], 0, name='origin_mature_equal_max')

Upvotes: 0

Greg Glockner
Greg Glockner

Reputation: 5653

The objective function must be in a standard form. However, you can add additional variables to represent the quantity max(0, expr[t,i]). While you can use a max general constraint, it's much easier just to define the new variable pos[t,i] as nonnegative (LB=0, which is the default) and add the constraint pos[t,i] >= expr[t,i].

Upvotes: 1

Related Questions