Skip to content

Instantly share code, notes, and snippets.

@njtierney
Created August 18, 2016 15:50
Show Gist options
  • Save njtierney/f5e003bda327899f4eb621b26e0f2da5 to your computer and use it in GitHub Desktop.
Save njtierney/f5e003bda327899f4eb621b26e0f2da5 to your computer and use it in GitHub Desktop.
library(dplyr)
library(ROI)
library(ROI.plugin.glpk)
library(ompr)
library(ompr.roi)
rb_mat <- function(r,c,prob) matrix(rbinom(r*c,1,prob),r,c)
my_A <- rb_mat(r = 1000, c = 200) # 1000 heart attacks, 200 potential locations
# number of OHCA incidents
J <- nrow(my_A)
I <- ncol(my_A)
# number of AEDs
N <- 5
MIPModel() %>%
add_variable(x[j], j = 1:J, type = "binary") %>%
add_variable(y[i], i = 1:I, type = "binary") %>%
# add_variable(a[i,j], i = 1:I, j = 1:J, type = "binary") %>%
add_variable(my_A[i,j], i = 1:I, j = 1:J, type = "binary") %>%
set_objective(sum_exp(x[j], j = 1:J), "max") %>%
add_constraint(sum_exp(y[i], i = 1:I), "==", N) %>%
add_constraint(sum_exp(a[i,j] * y[i], i = 1:I, j = 1:J), ">=", x[j])
# > Error in on_element(push, inplace_update_ast, get_ast_value, element) :
# > The expression contains a variable, that is not part of the model.
@ShuchitaShukla
Copy link

Hi, I am now facing issue in only last line, can anyone please tell if this is making it a quadratic constraint.

model <- MIPModel() %>%
add_variable(Qtysold[i],i=1:n,type = "integer" ,lb=0 ) %>%
add_variable(Total_mat[i,j],i=1:n,j=1:68, type = "continuous", lb = 0) %>%
set_objective(sum_expr(Qtysold[i]*Revenueperunit[i],i=1:n), "max")%>%
add_constraint(Qtysold[i] <= qty_req[i], i = 1:n)%>%
add_constraint(Qtysold[i]*Raw_mat[i,j]==Total_mat[i,j],i=1:n,j=1:68)%>%
add_constraint(colSums(Total_mat[i,j],i=1:n)<=Inv_Stock[j],j=1:68)

Error:rror in check_for_unknown_vars_impl(model, the_ast) :
The expression contains a variable that is not part of the model.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment