Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct way to use a matrix in MILP objective function #267

Open
Zuumx opened this issue Jun 17, 2019 · 1 comment
Open

Correct way to use a matrix in MILP objective function #267

Zuumx opened this issue Jun 17, 2019 · 1 comment

Comments

@Zuumx
Copy link

Zuumx commented Jun 17, 2019

Hi again, I'm having some trouble formatting the following as a MILP problem. I have 980 products and 4 groups. Each product has a attraction rate AR that differs for each group. Each product also has total sales. I want to maximize the sum of AR + Sales (normalized). I have the latest dev version.

The following MIPModel works just fine:

model <- MIPModel() %>%
  add_variable(x[p, g], p = 1:n, g = 1:m, type = "binary") %>%
  set_objective(sum_expr(ar[p, g] * x[p, g] +
                         total_sales[p] * x[p, g], p = 1:n, g = 1:m), "max") %>%
  # each product can be used once
  add_constraint(sum_expr(x[p, g], g = 1:m) <= 1, p = 1:n) %>% 
  # we want n_products selected for each group
  add_constraint(sum_expr(x[p, g], p = 1:n) == n_products, g = 1:m)

I'm bit confused how to model the ar part as MILPModel. The following works but yields different (wrong) results: (#238)

ar_vector <- ar[1:nrow(ar), 1:ncol(ar)] %>% unlist()

milpmodel <- MILPModel() %>%
  add_variable(x[p, g], p = 1:n, g = 1:m, type = "binary") %>%
  set_objective(sum_expr(ar_vector * x[p, g]  +
                         colwise(total_sales[p]) * x[p, g] , p = 1:n, g = 1:m), "max")
  # each product can be used once
  add_constraint(sum_expr(x[p, g], g = 1:m) <= 1, p = 1:n) %>% 
  # we want n_products selected for each group
  add_constraint(sum_expr(x[p, g], p = 1:n) == n_products, g = 1:m) 

I also tried the following:

arfunc <- function(p,g){
  ar_sub <- ar[p, g] 
  ar_vec <- as.vector(ar_sub) 
  return(ar_vec)
}

set_objective(sum_expr(w_ar * colwise(arfunc(p, g)) * x[p, g] +
                           w_sales * colwise(total_sales[p]) * x[p, g], p = 1:n, g = 1:m), "max") 

Error in as.data.frame.default(x[[i]], optional = TRUE) : 
  cannot coerce class ‘structure("LinearVariableCollection", package = "ompr")’ to a data.frame

What is the correct way to format this problem?

@Zuumx Zuumx changed the title Correct way to use a matrix in MILP Correct way to use a matrix in MILP objective function Jun 17, 2019
@Zuumx
Copy link
Author

Zuumx commented Jun 17, 2019

Nevermind, I changed the function to:

afunc <- function(p, g) {
  vapply(seq_along(p), function(k) ar[p[k], g[k]], numeric(1L))
}

And the objective function is now correct. Both functions give same results but I guess MILPModel handles them differently.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant