Gurobi / modeling-examples

Gurobi modeling examples
https://gurobi.github.io/modeling-examples/
Apache License 2.0
615 stars 272 forks source link

[Question] Why x.sum() on constraint takes so long? #4

Closed fillipefeitosa closed 3 years ago

fillipefeitosa commented 3 years ago

Hello there! Using this as base example

I am probably doing something wrong here. I have a distance matrix with costs for traveling from point to point. Not using facility fixed cost, so I want to limit the number of facilities just to fulfill the total demand, such as:

open = m.addVars(list(dict_capacity.keys()), vtype=GRB.BINARY, name="open") #dict capacity has the capacities as values

open_max = m.addVar(vtype=GRB.INTEGER, name="max_open_opportunities", lb=1, ub=32)
m.addConstr(open_max == open.sum())

Assuming that 32 is the max number of facilities I want open, the model takes too long. The way I understood, open.sum() is supposed to give me the sum of the Binary variables on that solution, ie. the number of open facilities. Is it not supposed to have this quicksum quite fast?

m.addConstrs(
            (
                open.sum() * dict_capacity[capacity] <= total_demand + 100
                for capacity in list(dict_capacity.keys())
            )
        )

This is a less hardcoded version I made to test. It works (opens 31 facilities), only because all facilities have 80 units to offer. But it takes so long that I need to Control+C to get objetive value and opened facilities.

open has 628 items

Thanks for your time!

hosseinsadeghi commented 3 years ago

From your description, it sounds like it's the optimization that takes long (which probably makes sense if you have a large number of variables and you're solving a hard problem).

To figure out if the .sum() method takes longer, you can track the execution of the code up to and pass the relevant part in the code. But it really sounds like you're even starting the optimization.

orojuan commented 3 years ago

Thank you for your interest in Gurobi Optimizer!

If you're an academic and your problem goes beyond installation, licensing, and bug reports please visit our Gurobi Support Portal (https://support.gurobi.com/) where you may be able to get answers to your questions via:

If you are a commercial user or an academic having installation and/or licensing issues or would like to report a bug, please submit a request through our Gurobi Support Portal. To do this, simply follow these instructions:

  1. Login to https://support.gurobi.com.
  2. Click on the "Submit a request" link in the top right corner of the webpage.
  3. Complete the form.

Kind regards,