Optimization - big M approach ompr

99 Views Asked by At

I'm trying to implement the big M approach to a simple problem but I'm struggling a lot as I'm new on optimization tasks.

I'm using the ompr package to solve the folowing problem:

  • Trying to minimise the sum of puchase cost defined as: sum("needed" - "already produced") * cost
  • I have a constraints on the minimum, the maximum "needed" value and on the needed sum

But when ("needed" - "already produced") < 0, the cost must be equal to 0.

Here is what I'be already done:

rm(list=ls(all=TRUE))

library(tidyverse)
library(ompr)
library(ompr.roi)
library(ROI.plugin.glpk)

# create fake data
df = tibble(
  price = c(3, -5, -1, 8),
  value = c(100, 10, 80, 90)
) %>%
  mutate(id = row_number())

df %>%
  glimpse()

# constraints
tot <- 380 
e_min <- 20
e_max <- 120   


# model
model <- MIPModel() %>%
  add_variable(x[i], i = df$id, type = "binary") %>% # for later big M approach

# y[i] is the needed quantity

  add_variable(y[i], i = df$id, type = "continuous", 
               lb = e_min,
               ub = e_max) |> 
  add_constraint(sum_expr(y[i], i=df$id) == tot) %>% 
  
  # big M 
  
  # ?? 
  
  # objective
  
  set_objective(sum_expr(y[i] * df$price[i] - df$value[i] * df$price[i], i = df$id),"min") %>%
  
  # objective with big M: ??

  solve_model(with_ROI(solver = "glpk"))

get_solution(model, y[i])

I can't implement the last constraint.

I plan to add a binary variable (x) that will multiply the cost: sum("needed" - "already produced") * x * cost

x == 1 if ("needed" - "already produced") > 0, else x == 0.

If M is large, M * x > ("needed" - "already produced") so:

  • if ("needed" - "already produced") > 0, x == 1, and,
  • if ("needed" - "already produced") < 0, x == 0

I'm correct ?

Thank you.

1

There are 1 best solutions below

3
On BEST ANSWER

I think you can do something along these lines:

min amount*cost
    amount >= 0
    amount >= "needed" - "already produced"

No need for binary variables or big-Ms.