Skip to main content

DeprecationWarning: Deprecated, pass a TempConstr or use Model.addLConstr

Answered

Comments

4 comments

  • Riley Clement
    Gurobi Staff Gurobi Staff

    Hi Junyang,

    It sounds like you are using "old code", i.e. functionality that is not intended to be used with recent versions of our solver.

    If you copy and paste the code that is causing the error then we will be able to give you further guidance.

    - Riley

    0
  • junyang cai
    Gurobi-versary
    First Comment
    First Question

    Hi Riley,

    Here is the old code:

    import numpy as np
    import gurobipy as grb
    import os
    import sys
    from collections import namedtuple

    script_path = os.path.dirname(sys.argv[0])

    def generate_uniform(n_instances: int = 500, n_facilities: int = 70, n_customers: int = 70,
                         max_pairwise_cost: float = 10 ** 4, max_startup_cost: float = 3 * 10 ** 3, seed: int = 0):
        """
        Generates a sequence of uniform facility location instances
        with pairwise cost dij in [0, max_pairwise_cost)
        and startup costs fj in [0, max_startup_cost)
        The defaults correspond to the generator used in balcan 2018
        :param n_instances: number of instances to generate
        :param n_facilities: number of facilities per instance
        :param n_customers: number of customers per instance
        :param max_pairwise_cost:
        :param max_startup_cost:
        :param seed: random seed
        :return: list of instances, where each instance is a tuple of numpy matrices
            representing d and f the pairwise cost and the startup cost
            dij is the cost of customer i to facility j
            fj is the startup cost of facility j
        """
        np.random.seed(seed)
        instances = []
        for i in range(n_instances):
            # create pairwise costs dij
            pairwise_cost_matrix = np.random.uniform(0, max_pairwise_cost, (n_customers, n_facilities))
            # create startup costs fj
            startup_costs = np.random.uniform(0, max_startup_cost, n_facilities)
            instances.append((pairwise_cost_matrix, startup_costs))
        return instances


    def generate_MIP_instance(pairwise_cost_matrix, startup_costs):
        """
        Generates gurobipy model of facility location problem with pairwise costs and startup costs
        this is of the form
        min     sum_j f_j x_j + sum_j,i d_ij y_ij
        s.t.    sum_j y_ij = 1 \forall i        (1)
                y_ij <= x_j  \forall i,j        (2)
                x_j \in {0,1}
                y_ij \in [0,1]
        :param pairwise_cost_matrix: unit supply costs dij
        :param startup_costs: cost fj of creating facility j
        :return: gurobipy instance of MIP
        """
        # extract number of customers and number of facilities from data
        n_customers, n_facilities = pairwise_cost_matrix.shape
        assert startup_costs.shape == (n_facilities,), \
            "cost matrices don't have same number of facilities, " \
            "pairwise cost shape: {}, " \
            "startup cost shape: {}".format(pairwise_cost_matrix.shape, startup_costs.shape)

        # initialize gurobi model
        m = grb.Model()
        # add variables yij \in [0,1] with coefficient dij
        y = {}
        for i in range(n_customers):
            for j in range(n_facilities):
                y[(i,j)] = m.addVar(0, 1, pairwise_cost_matrix[i,j], grb.GRB.CONTINUOUS, name="y_{%d,%d}"%(i,j))

        # add variables xj \in {0,1} with coefficient fj
        x = {}
        for j in range(n_facilities):
            x[j] = m.addVar(0, 1, startup_costs[j], grb.GRB.BINARY, name="x_%d"%(j))

        m.update()

        # add constraints (1)
        for i in range(n_customers):
            m.addConstr(grb.quicksum(y[(i,j)] for j in range(n_facilities)), grb.GRB.EQUAL, 1, "demand_%d"%(i))

        # add constraints (2)
        for i in range(n_customers):
            for j in range(n_facilities):
                m.addConstr(y[(i,j)], grb.GRB.LESS_EQUAL, x[j], "precedence_{%d,%d}"%(i,j))
        m.update()
        return m

    if __name__ == '__main__':

        FacilityLocationDistribution = namedtuple("FacilityLocationDesc", [
            "distribution_name",
            "num_of_instances",
            "n_facilities",
            "n_customers",
            "max_pairwise_cost",
            "max_startup_cost",
            "seed"
        ])

        distributions = [
            FacilityLocationDistribution(
                distribution_name="train_easy",
                num_of_instances=200,
                n_facilities=100,
                n_customers=200,
                max_pairwise_cost=10 ** 4,
                max_startup_cost=3 * 10 ** 3,
                seed=564
            ),

            FacilityLocationDistribution(
                distribution_name="train_hard",
                num_of_instances=200,
                n_facilities=100,
                n_customers=400,
                max_pairwise_cost=10 ** 4,
                max_startup_cost=3 * 10 ** 3,
                seed=763
            ),

            FacilityLocationDistribution(
                distribution_name="test_easy",
                num_of_instances=100,
                n_facilities=100,
                n_customers=200,
                max_pairwise_cost=10 ** 4,
                max_startup_cost=3 * 10 ** 3,
                seed=895
            ),

            FacilityLocationDistribution(
                distribution_name="test_hard",
                num_of_instances=100,
                n_facilities=100,
                n_customers=400,
                max_pairwise_cost=10 ** 4,
                max_startup_cost=3 * 10 ** 3,
                seed=459
            )
        ]

        instance_dir = os.path.join(script_path, "instances")
        if not os.path.exists(instance_dir):
            os.makedirs(instance_dir)

        for inst_desc in distributions:

            dist_desc = "facility_location_{}_nf_{}_nc_{}_maxd_{}_maxs_{}_seed_{}".format(
                inst_desc.distribution_name, inst_desc.n_facilities, inst_desc.n_customers,
                inst_desc.max_pairwise_cost, inst_desc.max_startup_cost, inst_desc.seed
            )
            distribution_dir = os.path.join(instance_dir,
                                            dist_desc)
            # generate random instances
            instances = generate_uniform(n_instances=inst_desc.num_of_instances, n_facilities=inst_desc.n_facilities,
                                         n_customers=inst_desc.n_customers, max_pairwise_cost=inst_desc.max_pairwise_cost,
                                         max_startup_cost=inst_desc.max_startup_cost, seed=inst_desc.seed)
            # for each generated instance, create MIP then write to file
            for i, (pairwise_cost_matrix, startup_costs) in enumerate(instances):
                model = generate_MIP_instance(pairwise_cost_matrix, startup_costs)

                outfile = os.path.join(distribution_dir, dist_desc+"_{}.mps".format(i))
                os.makedirs(os.path.dirname(outfile), exist_ok=True)

                model.write(outfile)
     
    Here is the message:
    generate.py:156: DeprecationWarning: Deprecated, pass a TempConstr or use Model.addLConstr
      model = generate_MIP_instance(pairwise_cost_matrix, startup_costs)
    0
  • Riley Clement
    Gurobi Staff Gurobi Staff

    Thanks Junyang,

    The signature for the addConstr method changed in Gurobi 9.1, and this is causing your issue:

    addConstr (9.0)     addConstr (9.1) 


    You can resolve your issue by replacing instances of addConstr in your code with addLConstr which uses the old addConstr format.

    - Riley

    0
  • junyang cai
    Gurobi-versary
    First Comment
    First Question

    Thank you!

    0

Please sign in to leave a comment.