scipopt / PySCIPOpt

Python interface for the SCIP Optimization Suite
https://pyscipopt.readthedocs.io/en/latest/
MIT License
825 stars 255 forks source link

Set the limits of a variable from a set of intervals #919

Closed Grenby closed 2 weeks ago

Grenby commented 1 month ago

Dear support team,

I want to know how to set the limits of a variable from a set of intervals.

For my task, I want to set the working hours of the stores during the week (for example, from 9 to 18) and I want the car to arrive in the intervals (9 + 24k, 18 + 24k), where k is some number of days.

Can you tell me how to do this using pyscipopt without additional variable like days?

Joao-Dionisio commented 1 month ago

Hey @Grenby! You can use normal python syntax to impose bounds, meaning you can do things like

car_variable = {}
for k in range(n_days):
    car_variable[k] = model.addVar(lb=9 + 24*k, ub = 18 + 24*k)

Is this what you want? Or do you mean you want a single variable for all the days?

One possibility (I don't know if you can do this in your model or not), is just to assume a day has 9 hours. So you have 1,2,...,9 <- day 1 , 10, 11, 18 <- day2, 19, ..27 <- day 3

So if your car is arriving at time 200, then you know it's actually day 23 at 11h (because 229 + 2 = 200) Another example to make it easier. Say your car is arriving at 17h on day 3. Then, with this transformation you would say that the car is arriving at time 26, because it's after 2 working days (29) and after 8h of the 3rd day. So you get 2*9+8=26.

If I misunderstood your problem, can you please give a bit more context?

Grenby commented 1 month ago

I will describe the problem in more detail: I have set points where it is necessary to distribute cargo, sometimes the distance between the points is more than one day, and car should arrive during working hours (from 9 to 18 on any day). I think that for this purpose it is possible to limit the permissible values ​​of the arrival time to intervals (9,18), (9+24, 18+24)... I described a special case of introducing intervals for permissible values ​​of a variable. In my opinion, such a problem can be solved by introducing a new variable "day", but is it possible to set interval values ​​using the scipopt?

Joao-Dionisio commented 1 month ago

If you were to do that directly, the problem would be disjoint no? Meaning that you would need to solve an optimization problem for each day and then compare the optimal solutions. I'm almost sure there is no solver that supports modeling these things directly.

I think the other approach I suggested should work. Just ignore the non-working hours, and start thinking of days as only having 9 hours. So if the solution you get is 5, you know that it's actually 13h00 of the 1st day. If it is 21, you know it's 12h00 of the 3rd day. Here is a picture to help illustrate what I mean

image