Skip to main content

Model ignoring setPWLObj()

Answered

Comments

5 comments

  • Eli Towle
    Gurobi Staff Gurobi Staff

    What makes you think Gurobi is ignoring the PWL objective terms? Can you please post a complete, minimal reproducible code example that shows the behavior you describe?

    0
  • Fabio Ciccarelli
    Gurobi-versary
    First Comment
    First Question
    var_list = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'l', 'm', 'n']

    var_sublist = ['d', 'h', 'n']

    distance_dict = {('a', 'b') : 10,

                     ('b', 'c') : 7,

                     ('c', 'd') : -17,

                     ('e', 'f') : 8,

                     ('f', 'g') : 8,

                     ('g', 'h') : -16,

                     ('i', 'l') : 4,

                     ('l', 'm') : 7,

                     ('m', 'n') : -11,

                     ('l', 'e') : 0,

                     ('l', 'a') : 0,

                     ('f', 'a') : 0,}

    for var in var_list:

            distance_dict[('start', var)] = 0

    var_list.insert(0, 'start')

    model = gp.Model()

    model.Params.LogToConsole = 0

    s = model.addVars(var_list, lb = 0, vtype = GRB.CONTINUOUS, name = 's')

    for u in var_sublist:

            model.setPWLObj(s[u], [0,1,2], [0,1,1])

    for (u,v) in distance_dict:

            model.addConstr(s[v] - s[u] >= distance_dict[u, v])

    model.optimize()

    for var in var_sublist:

            print(var, s[var].X)

    print(f'The optimal o.f. value is equal to: {model.getObjective().getValue()}')

    # IT PRINTS 0 EVEN IF WE HAVE MORE THAN A VARIABLE BELONGING TO var_sublist WITH VALUES HIGHER THAN 0
    0
  • Fabio Ciccarelli
    Gurobi-versary
    First Comment
    First Question

    I think the problem can be related to the way I retrieve the objective value as well, but I did not manage to find on the web a way to retrieve optimal PWL Objective value after optimization. 

     

    Thanks for your help.

    0
  • Eli Towle
    Gurobi Staff Gurobi Staff

    If you leave LogToConsole at its default value, Gurobi shows the optimal objective value of the problem is \(2\):

    Gurobi Optimizer version 10.0.2 build v10.0.2rc0 (mac64[arm])

    CPU model: Apple M1 Pro
    Thread count: 8 physical cores, 8 logical processors, using up to 8 threads

    Optimize a model with 24 rows, 13 columns and 48 nonzeros
    Model fingerprint: 0xaacfb7ec
    Model has 3 piecewise-linear objective terms
    Variable types: 13 continuous, 0 integer (0 binary)
    Coefficient statistics:
      Matrix range     [1e+00, 1e+00]
      Objective range  [0e+00, 0e+00]
      Bounds range     [0e+00, 0e+00]
      RHS range        [4e+00, 2e+01]
      PWLObj x range   [1e+00, 1e+00]
      PWLObj obj range [1e+00, 1e+00]
    Presolve removed 24 rows and 13 columns
    Presolve time: 0.00s
    Presolve: All rows and columns removed

    Explored 0 nodes (0 simplex iterations) in 0.00 seconds (0.00 work units)
    Thread count was 1 (of 8 available processors)

    Solution count 1: 2 

    Optimal solution found (tolerance 1.00e-04)
    Best objective 2.000000000000e+00, best bound 2.000000000000e+00, gap 0.0000%

    Unfortunately, Model.getObjective() only includes the linear and quadratic objective terms, not the piecewise-linear objective terms. You can retrieve the optimal objective value of the model by querying the ObjVal model attribute:

    model.ObjVal
    0
  • Fabio Ciccarelli
    Gurobi-versary
    First Comment
    First Question

    Thank you so much for your time and kindness!

    0

Please sign in to leave a comment.