dirkschumacher / ompr

R package to model Mixed Integer Linear Programs
https://dirkschumacher.github.io/ompr/
Other
269 stars 35 forks source link

How write a constraint once there is a variable used as the index? #442

Closed Arash-72 closed 1 year ago

Arash-72 commented 1 year ago

Capture

I have a model in which I am deciding to send patients to ICU or not. The y_i variable is a binary variable which equals 1 if the patient i is sent to ICU and 0 otherwise. In the model I also have a capacity constraint for ICU (which has 10 beds) at each time period t. So the patients, whose arrival time (which is a parameter given to the model) to ICU is less than or equal to t and the time they leave ICU is greater than or equal to t, use one capacity unit of the ICU. The length of patient stay in ICU, for each patient i, is also a variable in my optimization problem. Does anyone know how I can formulate this constraint using OMPR?

Model <- Model %>% add_constraint(sum_expr(y_i[i] * as.numeric(T_ar[i] <= t & t <= T_ar[i] + ICU_LOS[i]), i=1:n) <= 10, t=1:100)

I have tried the above way, but it didn't work, because ICU_LOS[i] is a variable.

sbmack commented 1 year ago

What are you trying to optimize? Your problem description suggests using discrete event simulation. It appears simple enough to not need a DES app but rather could be coded up in R or perhaps Excel.

Arash-72 commented 1 year ago

In the optimization model, the objective is to minimize the cost of the system. I have the data for patient arrivals. Let's assume that the objective is min sum (y_i[i] * ICU_LOS[i], for all i).

For the capacity constraint, one way is to define a binary variable z[i,t] which will be one if the patient is in ICU and 0 otherwise. But the issue is the t index in z[i,t] is related to another variable in the optimization problem T^ar_i and l[i].