# Capacitated Facility Optimization in R using OMPR

I am trying to solve a MIP for a capacitated facility optimization problem using ompr package. Getting this "subscript out of bounds" error, when ever i run the model. Not sure, where i am getting wrong.

My code and the problem i am trying to solve is as follows - # Loading the necessary libraries library(devtools) library(dplyr) library(tidyr) library(ROI) library(ROI.plugin.symphony) library(ompr) library(ompr.roi) library(ROI.plugin.glpk)

```
# THE PROBLEM
df<-read.table(text="
Supply_Factory N.AMerica_Demand S.America_Demand Europe_Demand Asia_Demand
Africa_Demand Fixed_Cost Low_Capacity Fixed_Cost High_Capacity
N.AMerica 81 92 101 130 115 6000 10 9000 20
S.America 117 77 108 98 100 4500 10 6750 20
Europe 102 105 95 119 111 6500 10 9750 20
Asia 115 125 90 59 74 4100 10 6150 20
Africa 142 100 103 105 71 4000 10 6000 20
Demand 12 8 14 16 7 - - - -
",header=T)
df
# Data creations
cost_matrix <-
```

as.matrix(subset(df,select=c(Supply_Factory,N.AMerica_Demand,S.America_Demand,Eu rope_Demand,Asia_Demand,Africa_Demand))[1:5,-1]) cost_matrix

```
Lcapacity_matrix <-
as.matrix(subset(df,select=c(Low_Capacity,High_Capacity))[1:5,1])
Lcapacity_matrix
Hcapacity_matrix <-
as.matrix(subset(df,select=c(Low_Capacity,High_Capacity))[1:5,2])
Hcapacity_matrix
LFC_matrix <- as.matrix(subset(df,select=c(Fixed_Cost,Fixed_Cost.1))[1:5,1])
LFC_matrix
HFC_matrix <- as.matrix(subset(df,select=c(Fixed_Cost,Fixed_Cost.1))[1:5,2])
HFC_matrix
total_demand <- as.matrix(df[6,2:6])
total_demand
# Optimization Model
result <- MIPModel() %>%
# Create a variable matrix x which stores the demand allocations
add_variable(x[i,j],i=1:5,j=1:5,type="integer") %>%
# Create a variable matrix l,h which stores the capacity allocations
add_variable(l[i],i=1:5,type="binary") %>%
add_variable(h[i],i=1:5,type="binary") %>%
# Constraint 1: A factory can have either high capacity or low capacity
add_constraint(l[i]+h[i]<=1,i=1:5) %>%
# Constraint 2: All demand have to be allocated
add_constraint((sum_expr(x[i,j],i=1:5) - sum_expr(total_demand[j]))
==0,j=1:5) %>%
# Constraint 3: The demand allocations should not exceed the supply capacity
add_constraint((sum_expr(x[i,j],j=1:5) -
(Lcapacity_matrix[i]*l[i]+Hcapacity_matrix[i]*h[i])) <=0,i=1:5) %>%
# Objective function to minimize the cost
set_objective(sum_expr(cost_matrix[i,j]*x[i,j],i=1:5,j=1:5)+
sum_expr(LFC_matrix[i]*l[i],i=1:5)+
sum_expr(HFC_matrix[i]*h[i],i=1:5)
,"min") %>%
# The solver
solve_model(with_ROI(solver = "symphony"))
```