Skip to main content

gurobipy.Model has no attribute addGenConstrPWL [solved]

Answered

Comments

6 comments

  • Jaromił Najman
    Gurobi Staff Gurobi Staff

    Hi Rune,

    Could you post a minimal working example to make the issue reproducible? In particular, could you please post your \(\texttt{import}\) statements?

    Best regards,
    Jaromił

    0
  • Rune Delbecque
    Gurobi-versary
    First Comment
    First Question

    Dear Jaromił

    The following code should reproduce the issue with a small example. I included all the import statements I need in the complete code, even though they are not used in this example.

    import pandas as pd
    from gurobipy import *
    import numpy as np
    import math

    #inputs
    Cities = ['ab', 'bc', 'cd', 'de', 'ef']
    Citizens = [1000, 2000, 800, 1500, 350]
    Distances = [[0, 18, 20, 5, 37], [27, 0, 3, 19, 26], [16, 2, 0, 39, 24], [22, 4, 15, 0, 17], [36, 14, 7, 8, 0]]

    #variables
    DistanceToClosestDC = {}
    WeightCoefficients = {}
    DC = {}
    DistanceHelpDC = {}

    RK = Model("RK")

    # Create variables
    for i in range(0, len(Cities)):
    DC[i] = RK.addVar(vtype=GRB.BINARY, name="DC"+str(i))
    DistanceToClosestDC[i] = RK.addVar(vtype=GRB.CONTINUOUS, name="DistanceToClosestDC"+str(i))
    WeightCoefficients[i] = RK.addVar(vtype=GRB.CONTINUOUS, name="WeightCoefficients"+str(i))
    for j in range(0, len(Cities)):
    DistanceHelpDC[i,j] = RK.addVar(vtype=GRB.CONTINUOUS, name="DistanceHelpDC"+str(i)+"."+str(j))

    # Set objective
    RK.setObjective(quicksum(DC[i]*Citizens[i] + (1-DC[i])*WeightCoefficients[i]*Citizens[i] for i in range(0, len(Cities))), GRB.MAXIMIZE)

    # Constraints
    RK.addConstr(quicksum(DC[i] for i in range(0, len(Cities))) == 1, "c1")
    for i in range(0, len(Cities)):
    for j in range(0, len(Cities)):
    RK.addConstr(DistanceHelpDC[i,j] == (1-DC[j])*1000 + DC[j]*Distances[i][j], "c2."+str(i)+"."+str(j))
    RK.addGenConstrMin(DistanceToClosestDC[i], [DistanceHelpDC[i,j] for j in range(0,len(Cities))], 1000, "c3."+str(i))
    RK.addGenConstrPWL(DistanceToClosestDC[i], WeightCoefficients[i], [0, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5, 11.5, 12.5, 13.5, 14.5, 15.5, 16.5, 17.5, 18.5, 19.5, 20.5, 21.5, 22.5, 23.5, 24.5, 25, 26], [1, 0.979995816,0.855306584,0.746482118,0.651503874,0.568610134,0.496263335,0.43312154,0.378013558,0.329917209,0.287940374,0.251304437,0.219329855,0.19142354,0.167067871,0.145811082,0.127258888,0.111067173,0.096935602,0.084602054,0.073837758,0.064443051,0.056243675,0,0], "c4."+str(i))

    # Optimize model
    RK.optimize()

    Thank you in advance. Let me know if this suffices.

    Kind regards,

    Rune

    0
  • Jaromił Najman
    Gurobi Staff Gurobi Staff

    Hi Rune,

    When I execute your Python code, I don't get any error. The output is:

    Gurobi Optimizer version 9.1.1 build v9.1.1rc0 (mac64)
    Thread count: 4 physical cores, 8 logical processors, using up to 8 threads
    Optimize a model with 26 rows, 40 columns and 55 nonzeros
    Model fingerprint: 0x7cd2e8c9
    Model has 5 quadratic objective terms
    Model has 10 general constraints
    Variable types: 35 continuous, 5 integer (5 binary)
    Coefficient statistics:
    Matrix range [1e+00, 1e+03]
    Objective range [4e+02, 2e+03]
    QObjective range [7e+02, 4e+03]
    Bounds range [1e+00, 1e+00]
    RHS range [1e+00, 1e+03]
    Presolve added 0 rows and 100 columns
    Presolve time: 0.01s
    Presolved: 31 rows, 145 columns, 440 nonzeros
    Presolved model has 5 SOS constraint(s)
    Variable types: 140 continuous, 5 integer (5 binary)

    Root relaxation: objective 5.650000e+03, 23 iterations, 0.00 seconds

    Nodes | Current Node | Objective Bounds | Work
    Expl Unexpl | Obj Depth IntInf | Incumbent BestBd Gap | It/Node Time

    0 0 5650.00000 0 10 - 5650.00000 - - 0s
    H 0 0 4386.2280163 5650.00000 28.8% - 0s
    0 0 5514.11595 0 8 4386.22802 5514.11595 25.7% - 0s

    Cutting planes:
    Implied bound: 7
    MIR: 7
    Flow cover: 4
    Inf proof: 1
    RLT: 2
    Relax-and-lift: 13

    Explored 1 nodes (69 simplex iterations) in 0.04 seconds
    Thread count was 8 (of 8 available processors)

    Solution count 1: 4386.23

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

    How did you install Gurobi? What is your operating system? Are you using any specific setup on your machine?

    Best regards,
    Jaromił

     

    0
  • Rune Delbecque
    Gurobi-versary
    First Comment
    First Question

    Dear Jaromił

    I installed Gurobi via the Download Center of Gurobi (https://www.gurobi.com/downloads/). This should have worked, as I get the following results on the command-line tool:

    I'm using Windows 10 on my laptop.

    More details (since I have no clue what the problem could be):

    * I have an academic license that might not allow me to use all Gurobi tools?

    * I code in the Spyder environment (version 4.1.5) with Python version 3.6.9

    Kind regards,

    Rune

     

    0
  • Jaromił Najman
    Gurobi Staff Gurobi Staff

    Dear Rune,

    The academic license provides you access to all features of the Gurobi package. The code also works in a Spyder environment. Could you post the whole error output for the code snippet you provided?

    Could you also try printing the Gurobi version used by Spyder via

    print(gurobi.version())

    Best regards,
    Jaromił

    0
  • Rune Delbecque
    Gurobi-versary
    First Comment
    First Question

    Dear Jaromił

    The Gurobi version used was Gurobi 9.1.1.

    I re-installed Anaconda and Gurobi and now it seems to work. I still have no idea why it didn't work before.

    Thanks for your responses. It showed me that the error was not in the code but in the installation.

    Kind regards,

    Rune

    0

Please sign in to leave a comment.