1 Week2 - Unconstrained & Constrained Optimization
1.1 Recitation
1.1.1 Unconstrained
Straigthforward exercises in modeling optimization without index vectors.
\[ \textbf{Equation 1:} \ \ Minimze \ \ y = x^2+2*x-3 \]
Since is an unconstrained quadratic optimization with a single variable, base R package optimizer can solve it
## $minimum
## [1] -1
##
## $objective
## [1] -4
Let’s check the result in the plot
#Data created for plot, using mutate on the function
x <- seq(-10,10,1)
data <- data <- data.frame(x) %>%
mutate(y = f(x))
#Ploting with a marker on the optimal solution
plot_ly(data, x = ~x, y = ~y, type = 'scatter', mode = 'lines') %>%
add_markers(y = result$objective, x = result$minimum)
\[ \textbf{Equation 2:} \ \ Maximize \ \ z =-x^2+2*x-y^2 \]
In this equation there are 2 variables, for this case it can be used the base R package optim.
## # A tibble: 1 x 4
## value function.count gradient.count convergence
## <dbl> <int> <int> <int>
## 1 -7.45e110 501 NA 1
1.2 Practice Problems
1.2.1 Handmade Baskets
\[ P = (x/73)^2-(x/95)+0.92 \]
P <- function(x){(x/73)^2-(x/95)+0.92}
#Data created for plot, using mutate on the function
x <- seq(-100,200,1)
data <- data <- data.frame(x) %>%
mutate(y = P(x))
min = data[data$y == min(data$y),] #cheating :P
#Ploting with a marker on the optimal solution
plot_ly(data, x = ~x, y = ~y, type = 'scatter', mode = 'lines') %>%
add_markers(y = min$y, x = min$x)
#Part 1: Price Functio
# #Question1: How much will it charge with 5 basket's order ?
# P(5)
#
# #Question2: What is the first derivative of the price function?
# library(Ryacas)
# x <- Sym("x")
# s <- expression((x/73)^2-(x/95)+0.92)
# deriv(s,x) #Didn't work TO DO
#
# #Question3: Second Derivative
# x <- Sym("x")
# s <- expression(-1/95 + (2*x)/5329)
# deriv(s,x) #worked
#
# #Question4: Lowest Unit Price
# result <- optimize(P,interval = c(-100,100), maximum = F)
# print(result)
1.2.2 Maximizing Storage
\[ \begin{align*} \textbf{Minimize} & \ Z = x \cdot y \\ \textbf{Subject to} \\ & 2 \cdot x+2 \cdot y = 26.7 \end{align*} \]
\[ \begin{align*} \textbf{Minimize} & \ Z\ =x\cdot\ \left(\frac{\left(26.7-2\cdot x\right)}{2}\right)\\ \end{align*} \]
Solving:
f <- function(x){x*((26.7 - 2*x)/2)}
result <- optimize(f,interval = c(-100,100), maximum = T)
print(result)
## $maximum
## [1] 6.7
##
## $objective
## [1] 45
Let’s check the result in the plot
#Data created for plot, using mutate on the function
x <- seq(-100,100,0.1)
data <- data <- data.frame(x) %>%
mutate(y = f(x))
#Ploting with a marker on the optimal solution
plot_ly(data, x = ~x, y = ~y, type = 'scatter', mode = 'lines') %>%
add_markers(y = result$objective, x = result$maximum)
1.2.3 Marketing MagicPuppy
\[ N = 1700 \left ( \frac{x}{90} - \left ( \frac{x}{90} \right ) ^2 \right ) \]
f <- function(x){1700*(x/90-(x/90)^2)}
#Question1: First derivative
# x <- Sym("x")
# s <- expression(1700*(x/90-(x/90)^2))
# deriv(s,x) #Didn't work TO DO
#Question2: How many units should be given away in the campaign in order to maximize its positive impact?
result <- optimize(f,interval = c(-100,200), maximum = T)
print(result)
## $maximum
## [1] 45
##
## $objective
## [1] 425
x <- seq(-50,100,1)
data <- data <- data.frame(x) %>%
mutate(y = f(x))
#Ploting with a marker on the optimal solution
plot_ly(data, x = ~x, y = ~y, type = 'scatter', mode = 'lines') %>%
add_markers(y = result$objective, x = result$maximum)
1.2.4 CureBase Cancer Institute
\[ W = \left ( m^{x^{2}} \right )\left ( n^{-x} \right )\left ( z^7 \right ) \]
#Question what is the value of x at the maximum value of W , given that m=0.06 , n=0.14 , z=0.15 ?)
f <- function(x){0.06^(x^2)*(0.14^(-x))*0.15^7}
result <- optimize(f,interval = c(-1,1), maximum = T)
print(result)
## $maximum
## [1] 0.35
##
## $objective
## [1] 0.0000024
x <- seq(-1,1,0.1)
data <- data <- data.frame(x) %>%
mutate(y = f(x))
#Ploting with a marker on the optimal solution
plot_ly(data, x = ~x, y = ~y, type = 'scatter', mode = 'lines') %>%
add_markers(y = result$objective, x = result$maximum)
1.2.5 Santa’s Molding Company
data <- tibble(Type = c("Profit","Time","Cubic Area", "index capacity"),
Toy = c(25,13,33,75), doll = c(25,15,25,70),
Capacity = c(NA,1440, 3000, NA))
kable(data, caption = "Santa’s Molding Company")
Type | Toy | doll | Capacity |
---|---|---|---|
Profit | 25 | 25 | |
Time | 13 | 15 | 1440 |
Cubic Area | 33 | 25 | 3000 |
index capacity | 75 | 70 |
$$ \[\begin{align*} \textbf{Maximize} & \ y = \sum_{i=1}^{N} p_i\cdot x_i \\ \textbf{subject to}\\ & x_i \le\ c_i \ \ \forall i \in N \\ & \sum_{i=1}^{N} t_{i} \cdot x_i \le\ 1440 \\ & \sum_{i=1}^{N} a_{i} \cdot x_i \le\ 3000 \\ & x_i \ge 0 \\ \\ \textbf{Where} \\ & i = \text{Products in N} \\ & p_i = \text{Profit margin for product i} \\ & c_i = \text{Product daily capacity} \\ & t_i = \text{Time to mold product i} \\ & a_i = \text{Cubic inches of product i} \\ & x_i = \text{Quantity of product i to mold} \\ \end{align*}\] $$
If we look closer on the dataframe, we can set constraints 2 and 3 as one with a matrix including capacity constraints, but for math notation I’ve set them differently since they are 2 different types of entities.
rm(x)
n = 2 #Number of products Toy and doll
c = filter(data, Type == "index capacity") %>% select(Toy,doll) %>% gather() %>% .$value #index_capacity
a = as.matrix(filter(data, Type %in% c("Time","Cubic Area")) %>% select(Toy,doll)) #constraints left hand
C = filter(data, Type %in% c("Time","Cubic Area")) %>% select(Capacity) %>% .$Capacity #constraints right hand
p = filter(data, Type == "Profit") %>% select(Toy,doll) %>% gather() %>% .$value #profit
model <- MIPModel() %>%
#Variable of profit
add_variable(x[i], i = 1:n, type = "integer", lb = 0) %>%
#minimize travel distance
set_objective(sum_expr(p[i]*x[i], i = 1:n), "max") %>%
#you cannot exceed the Plant Capacity
add_constraint(x[i] <= c[i], i= 1:n) %>%
#You cannot exceed aditivie capacity
add_constraint(sum_expr(x[i] * a[j,i], i = 1:n) <= C[j], j = 1:n)
#Solve
result <- solve_model(model, with_ROI(solver = "glpk", verbose = FALSE))
#Optimal Value
result$objective_value
## [1] 2575
#result solution
result$solution
## x[1] x[2]
## 53 50
Let’s solve the questions
P <- function(x,y){25*x+25*y}
#Question 1: Graphical selection
#Question 2: What is the value of x = 70 and y= 30
P(70,30)
## [1] 2500
#Question 3: What is the value of x = 50 and y= 53
P(50,53)
## [1] 2575
#Question 3: What is the value of x = 50 and y= 53
P(20,75)
## [1] 2375
1.2.6 Crazy Cereal
data <- tibble(Type = c("Sugary","Regular","Capacity"), Sugar = c(0.66,0.21,2000),
Corn_Flake = c(0.34,0.79,4000), Profit = c(0.94,0.82,NA))
kable(data, caption = "Crazy Cereal")
Type | Sugar | Corn_Flake | Profit |
---|---|---|---|
Sugary | 0.66 | 0.34 | 0.94 |
Regular | 0.21 | 0.79 | 0.82 |
Capacity | 2000.00 | 4000.00 |
$$ \[\begin{align*} \textbf{Maximize} & \ y = \sum_{i=1}^{N} p_i \cdot x_{i} \\ \textbf{subject to}\\ & \sum_{i=1}^{N} x_{i} \cdot a_{i,j} \le C_j, \ j \in M \\ & x_{i} \ge 0, \ x \in \mathbb{Z} \\ \\ \textbf{Where} \\ & i = \text{Products in N} \\ & j = \text{Components of N = M} \\ & p_i = \text{Profit margin for product i} \\ & a_{i,j} = \text{components for product i} \\ & C_j = \text{Component capacity} \\ & x_{i} = \text{Quantity of product} \\ \end{align*}\] $$
rm(x)
## Warning in rm(x): objeto 'x' não encontrado
n = 2 #Number of products Sugary and Regular
m = 2 #Number of components Sugar and Corn_Flake
#Let's do some roots R instead of dplyr :)
C = as.matrix(data[data$Type == "Capacity",c("Sugar", "Corn_Flake")])
a = as.matrix(data[data$Type %in% c("Sugary","Regular"),c("Sugar", "Corn_Flake")])
p = as.matrix(data$Profit[1:2])
model <- MIPModel() %>%
#Variable of profit
add_variable(x[i], i = 1:n, type = "integer", lb = 0) %>%
#maximize profit
set_objective(sum_expr(p[i] * x[i], i = 1:n), "max") %>%
#You cannot exceed component capacity
add_constraint(sum_expr(x[i] * a[i,j], i = 1:m) <= C[j], j = 1:n)
#Solve
result <- solve_model(model, with_ROI(solver = "glpk", verbose = FALSE))
#Optimal Value
result$objective_value
## [1] 5116
#result solution
result$solution
## x[1] x[2]
## 1644 4355
1.2.7 Jim’s Meat Packing Company
data <- tibble(Cut = c("Chuck","Sirloin","Restriction"), Lean_Meat = c(0.09,0.6,0.3),
Fat_Meat = c(0.02,0.06,0.05), Cost = c(9.3,8.4,NA))
kable(data, caption = "Jim's Meat")
Cut | Lean_Meat | Fat_Meat | Cost |
---|---|---|---|
Chuck | 0.09 | 0.02 | 9.3 |
Sirloin | 0.60 | 0.06 | 8.4 |
Restriction | 0.30 | 0.05 |
$$ \[\begin{align*} \textbf{Minimize} & \ y = \sum_{i=1}^{N} c_i \cdot x_{i} \\ \textbf{subject to}\\ & \sum_{i=1}^{N} x_{i} \cdot a_{i,j} \ge \sum_{i=1}^{N} R_j \cdot x_{i}, \ \ j \in N=1 \\ & \sum_{i=1}^{N} x_{i} \cdot a_{i,j} \le \sum_{i=1}^{N} R_j \cdot x_{i}, \ \ j \in N=2 \\ & \sum_{i=1}^{N} x_{i} \ge 50 \\ & x_{i} \ge 0, \ x \in \mathbb{Z} \\ \\ \textbf{Where} \\ & i = \text{Type of cut in N} \\ & j = \text{Type of meat} \\ & c_i = \text{Cost for product i} \\ & a_{i,j} = \text{components for product i} \\ & R_j = \text{Restriction capacity} \\ & x_{i} = \text{Quantity of product} \\ \end{align*}\] $$
rm(x)
## Warning in rm(x): objeto 'x' não encontrado
n = 2 #Number of products Sugary and Regular
m = 2 #Number of components Sugar and Corn_Flake
#Another way of subsetting data in R
R = as.matrix(data[3,2:3])
a = as.matrix(data[1:2,2:3])
c = as.matrix(data[1:2,4])
model <- MIPModel() %>%
#Variable of profit
add_variable(x[i], i = 1:n, type = "integer", lb = 0) %>%
#maximize profit
set_objective(sum_expr(c[i] * x[i], i = 1:n), "min") %>%
#yay
add_constraint(sum_expr(x[i], i = 1:n) >= 50) %>%
#You cannot exceed component capacity
add_constraint(sum_expr(x[i] * a[i,j], i = 1:n) >= sum_expr(x[i], i = 1:n)*R[j], j = 1) %>%
add_constraint(sum_expr(x[i] * a[i,j], i = 1:n) <= sum_expr(x[i], i = 1:n)*R[j], j = 2)
#Solve
result <- solve_model(model, with_ROI(solver = "glpk", verbose = F))
#Optimal Value
result$objective_value
## [1] 432
#result solution
result$solution
## x[1] x[2]
## 13 37
#Duals
get_column_duals(result)
## [1] NA
I couldn’t find a proper function on shadow price calculation on ompr package, so lets try to calculate it ourselves!
ModelIter <- function(z){
model <- MIPModel() %>%
#Variable of profit
add_variable(x[i], i = 1:n, type = "integer", lb = 0) %>%
#maximize profit
set_objective(sum_expr(c[i] * x[i], i = 1:n), "min") %>%
#yay
add_constraint(sum_expr(x[i], i = 1:n) >= z) %>%
#You cannot exceed component capacity
add_constraint(sum_expr(x[i] * a[i,j], i = 1:n) >= sum_expr(x[i], i = 1:n)*R[j], j = 1) %>%
add_constraint(sum_expr(x[i] * a[i,j], i = 1:n) <= sum_expr(x[i], i = 1:n)*R[j], j = 2) %>%
solve_model(with_ROI(solver = "glpk", verbose = F))
return(model$objective_value)
}
Iter_Results <- tibble(constraint = seq(48,55,1)) %>%
mutate(Obj_value = as.numeric(map(constraint, ModelIter)),
Marginal_value = round((Obj_value-lag(Obj_value,1)/(constraint-lag(constraint,1))),2))
kable(Iter_Results, caption = "Result")
constraint | Obj_value | Marginal_value |
---|---|---|
48 | 414 | |
49 | 423 | 9.3 |
50 | 432 | 8.4 |
51 | 440 | 8.4 |
52 | 448 | 8.4 |
53 | 458 | 9.3 |
54 | 466 | 8.4 |
55 | 475 | 8.4 |
## [1] 8.9
Marginal Value set as 8.85 got us an error, the answer was 8.63, would be good to understand what was made wrong in here.
1.2.8 John’s Shipping Company
data <- tibble(Fuel_Type = c("O_Type","D_Type","Restriction"), Hydrogen_Conc = c(45,90,60),
Oxygen_Conc = c(15,6,9), Cost = c(1.05,1.34,NA))
kable(data, caption = "John's Shipping Company")
Fuel_Type | Hydrogen_Conc | Oxygen_Conc | Cost |
---|---|---|---|
O_Type | 45 | 15 | 1.0 |
D_Type | 90 | 6 | 1.3 |
Restriction | 60 | 9 |
$$ \[\begin{align*} \textbf{Minimize} & \ y = \sum_{i=1}^{N} c_i \cdot x_{i} \\ \textbf{subject to}\\ & \sum_{i=1}^{N} x_{i} \cdot a_{i,j} \ge \sum_{i=1}^{N} R_j \cdot x_{i}, \ \ j \in N=1 \\ & \sum_{i=1}^{N} x_{i} \cdot a_{i,j} \le \sum_{i=1}^{N} R_j \cdot x_{i}, \ \ j \in N=2 \\ & \sum_{i=1}^{N} x_{i} \ge G \\ & x_{i} \ge 0, \ x \in \mathbb{Z} \\ \\ \textbf{Where} \\ & i = \text{Type of cut in N} \\ & j = \text{Type of meat} \\ & c_i = \text{Cost for product i} \\ & a_{i,j} = \text{components for product i} \\ & R_j = \text{Restriction capacity} \\ & x_{i} = \text{Quantity of product} \\ & G = \text{Minimum to be produced} \\ \end{align*}\] $$
rm(x)
## Warning in rm(x): objeto 'x' não encontrado
n = 2 #Number of products Sugary and Regular
m = 2 #Number of components Sugar and Corn_Flake
z = 10000 #minimum to be produced
#Another way of subsetting data in R
R = as.matrix(data[3,2:3])
a = as.matrix(data[1:2,2:3])
c = as.matrix(data[1:2,4])
model <- MIPModel() %>%
#Variable of profit
add_variable(x[i], i = 1:n, type = "integer", lb = 0) %>%
#maximize profit
set_objective(sum_expr(c[i] * x[i], i = 1:n), "min") %>%
#Produce at least the minimum required
add_constraint(sum_expr(x[i], i = 1:n) >= z) %>%
#Minimum component required
add_constraint(sum_expr(x[i] * a[i,j], i = 1:n) >= sum_expr(x[i], i = 1:n)*R[j], j = 1) %>%
#You cannot exceed component capacity
add_constraint(sum_expr(x[i] * a[i,j], i = 1:n) <= sum_expr(x[i], i = 1:n)*R[j], j = 2)
#Solve
result <- solve_model(model, with_ROI(solver = "glpk", verbose = F))
#Optimal Value
result$objective_value
## [1] 12433
#result solution
result$solution
## x[1] x[2]
## 3333 6667
1.2.9 A New Office Plant
data <- tibble(Type = c("Product_1","Product_2","Product_3", "Min", "Max"),
Nitrogen = c(360,380,310,1800,2200), Potassium = c(30,20,20,100,100),
cost = c(1.59,2.19,2.99,NA,NA))
kable(data, caption = "A New Office Plant")
Type | Nitrogen | Potassium | cost |
---|---|---|---|
Product_1 | 360 | 30 | 1.6 |
Product_2 | 380 | 20 | 2.2 |
Product_3 | 310 | 20 | 3.0 |
Min | 1800 | 100 | |
Max | 2200 | 100 |
$$ \[\begin{align*} \textbf{Minimize} & \ y = \sum_{i=1}^{N} c_i \cdot x_{i} \\ \textbf{subject to}\\ & \sum_{i=1}^{N} x_{i} \cdot a_{i,j} \ge Min_j, \ \ j \in M \\ & \sum_{i=1}^{N} x_{i} \cdot a_{i,j} \le Max_j, \ \ j \in M \\ & x_{i} \ge 0 \\ \textbf{Where} \\ & i = \text{Type of product in N} \\ & j = \text{Type of component in M} \\ & c_i = \text{Cost for product i} \\ & a_{i,j} = \text{components for product i} \\ & Min_j = \text{Restriction of component} \\ & Max_j = \text{Restriction of component} \\ & x_{i} = \text{Quantity of product} \\ \end{align*}\] $$
rm(x)
## Warning in rm(x): objeto 'x' não encontrado
n = 3 #Number of products
m = 2 #Number of components
#subsetting data to R
min = as.matrix(data[4,2:3])
max = as.matrix(data[5,2:3])
a = as.matrix(data[1:3,2:3])
c = as.matrix(data[1:3,4])
model <- MIPModel() %>%
# Variable of profit
add_variable(x[i], i = 1:n, type = "continuous", lb = 0) %>%
# maximize profit
set_objective(sum_expr(c[i] * x[i], i = 1:n), "min") %>%
#Produce at least the minimum required
add_constraint(sum_expr(x[i]*a[i,j], i = 1:n) >= min[j], j = 1:m) %>%
#Dont exceed maximum required
add_constraint(sum_expr(x[i]*a[i,j], i = 1:n) <= max[j], j = 1:m)
#Solve
result <- solve_model(model, with_ROI(solver = "glpk", verbose = F))
#Optimal Value
result$objective_value
## [1] 10
#result solution
result$solution
## x[1] x[2] x[3]
## 0.48 4.29 0.00
#Question: How many mg of Nitrogen would your solution provide to the plant every day? (Please round your answer to integer number)
result$solution[1]*a[1,1]+result$solution[2]*a[2,1]
## x[1]
## 1800
1.2.10 Rochak’s Ink Company
data <- tibble(Type = c("P_Cyan","P_Magenta","P_Yellow","Max"),
Stage1 = c(0.44,0.51,0.5,17*60^2), Stage2 = c(0.55,0.6,0.59,23*60^2),
Demand = c(84000,72000,93000,NA), Profit = c(0.6,0.72,0.62,NA))
kable(data, caption = "Rochak's Ink Company")
Type | Stage1 | Stage2 | Demand | Profit |
---|---|---|---|---|
P_Cyan | 0.44 | 0.55 | 84000 | 0.60 |
P_Magenta | 0.51 | 0.60 | 72000 | 0.72 |
P_Yellow | 0.50 | 0.59 | 93000 | 0.62 |
Max | 61200.00 | 82800.00 |
$$ \[\begin{align*} \textbf{Maximize} & \ y = \sum_{i=1}^{N} p_i \cdot x_{i} \\ \textbf{subject to}\\ & \sum_{i=1}^{N} x_{i} \cdot a_{i,j} \le Max_j, \ \ j \in M \\ & \sum_{i=1}^{N} x_{i} \le D_i \\ & x_{i} \ge 0, \ x \in \mathbb{Z} \\ \textbf{Where} \\ & i = \text{Type of product in N} \\ & j = \text{Production stage in M} \\ & p_i = \text{Profit for product i} \\ & a_{i,j} = \text{Time requirement for product i in stage j} \\ & Max_j = \text{Restriction of production time} \\ & D_i = \text{Demand for product} \\ & x_{i} = \text{Quantity of product} \\ \end{align*}\] $$
In this exercise, you’re willing to maximize profit given the demand. You don’t have to fulfill all of the demand, that’s why the $ {i=1}^{N} x{i} D_i$ constraint.
rm(x)
## Warning in rm(x): objeto 'x' não encontrado
n = 3 #Number of products
m = 2 #Number of components
#subsetting data to R
D = as.matrix(data[1:3,4])
max = as.matrix(data[4,2:3])
a = as.matrix(data[1:3,2:3])
p = as.matrix(data[1:3,5])
model <- MIPModel() %>%
# Variable of profit
add_variable(x[i], i = 1:n, type = "integer", lb = 0) %>%
# maximize profit
set_objective(sum_expr(p[i] * x[i], i = 1:n), "max") %>%
#Don't produce something you don't have demand for
add_constraint(x[i] <= D[i], i = 1:n) %>%
#Dont exceed maximum required
add_constraint(sum_expr(x[i]*a[i,j], i = 1:n) <= max[j], j = 1:m)
#Solve
result <- solve_model(model, with_ROI(solver = "glpk", verbose = F))
#Optimal Value
result$objective_value
## [1] 85222
#result solution
result$solution
## x[1] x[2] x[3]
## 55641 71996 0
1.2.11 Pasteur Cheese Factory
data <- tibble(Type = c("P_Original","P_Cheddar","P_Wasabi","Max","Cost"),
X_Ray = c(8.6,2.2,5.1,8*60,0.11), Checkweigher = c(7.0,6.8,9.8,9*60,0.22),
Profit = c(20,15,17,NA,NA))
kable(data, caption = "Pasteur Cheese Factory")
Type | X_Ray | Checkweigher | Profit |
---|---|---|---|
P_Original | 8.60 | 7.00 | 20 |
P_Cheddar | 2.20 | 6.80 | 15 |
P_Wasabi | 5.10 | 9.80 | 17 |
Max | 480.00 | 540.00 | |
Cost | 0.11 | 0.22 |
$$ \[\begin{align*} \textbf{Maximize} & \ y = \sum_{i=1}^{N} x_{i} \cdot \left( p_i - \sum_{j=1}^{M} c_{i,j} \right) \\ \textbf{subject to}\\ & \sum_{i=1}^{N} x_{i} \cdot a_{i,j} \le Max_j, \ \ j \in M \\ & x_{i} \ge 0, \ x \in \mathbb{Z} \\ \textbf{Where} \\ & i = \text{Type of product in N} \\ & j = \text{Production stage in M} \\ & p_i = \text{Profit for product i} \\ & c_{i,j} = \text{Cost for product i in stage j} \\ & a_{i,j} = \text{Time requirement for product i in stage j} \\ & Max_j = \text{Restriction of production time} \\ & D_i = \text{Demand for product} \\ & x_{i} = \text{Quantity of product} \\ \end{align*}\] $$
In this exercise, you’re willing to maximize profit given the cost of production.
rm(x)
## Warning in rm(x): objeto 'x' não encontrado
n = 3 #Number of products
m = 2 #Number of components
#subsetting data to R
c = as.vector(data[5,2:3]) #This cost will be changed
max = as.matrix(data[4,2:3])
a = as.matrix(data[1:3,2:3])
p = as.matrix(data[1:3,4])
#Multiply all rows of j column of a[i,j] to c[i], in r for this to happen you have to change the vector to a square matrix
kable(diag(c), caption = "As is")
0.11 | 0.00 |
0.00 | 0.22 |
0.95 | 1.5 |
0.24 | 1.5 |
0.56 | 2.2 |
#Get back to model
model <- MIPModel() %>%
#Variable of productW
add_variable(x[i], i = 1:n, type = "integer", lb = 0) %>%
#maximize profit
set_objective(sum_expr(x[i]*(p[i] - sum_expr(c[i,j],j = 1:m)), i = 1:n), "max") %>%
#Dont exceed maximum required
add_constraint(sum_expr(x[i]*a[i,j], i = 1:n) <= max[j], j = 1:m)
#Solve
result <- solve_model(model, with_ROI(solver = "glpk", verbose = F))
#Optimal Value
result$objective_value
## [1] 1239
#result solution
result$solution
## x[1] x[2] x[3]
## 48 30 0