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 @dirkschumacher, @njtierney,
I am solving an optimization problem using the followoing code.
library(Rglpk)
library(magrittr)
library(dplyr)
library(ROI)
library(ROI.plugin.glpk)
library(ompr)
library(ompr.roi)
data<-read.csv('input.csv',header=TRUE,sep=",")
Inv_Stock<-read.csv('INV_DATA.csv',header = TRUE,sep = ",")
Inv_Stock<- sapply(Inv_Stock,as.matrix)
qty_req<- data[,5]
qty_req<- sapply(qty_req,as.double)

Revenueperunit<-data[,4]
Revenueperunit<-sapply(Revenueperunit,as.double)

Raw_mat<-data[,-c(1,2,3,4,5)]
Raw_mat <- sapply(Raw_mat,as.matrix)

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(rawmat[j,i]*Qtysold[i]==Total_mat[j,i])%>%
add_constraint(sum_expr(Total_mat[i,j],i=1:n)<=Inv_Stock[1,j],j=1:68)
result <- solve_model(model, with_ROI(solver = "glpk"))

Context of the problem:
A vector holding inventory of total raw materials available by colums containing the raw material name and a single row with total qty available per column.
A matrix raw material with column names of raw material and each row indicating per order number, the quantity if raw material req for unit item to be manufactured.
A vector qty req indicating per order number the number of quantity asked by a customer to manufacture.
A vector revenue/unit indicating the revenue from each unit of material per order.
Problem statement:
We want to maximize total revenue
we want to determine qtysold such that atleast 80% of the qty req per order is delivered.
when we multiply qtysold with req raw material matrix , total req raw material columnwise should not exceed the total available in inventory stock.

Error: while adding last two constraints, its not giving answer, it says'(' unexpected operator found.
Error in acc[[key]] : no such index at level 1

@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