My problem is that a bakery is selling three different products (apple pie, croissant, and donut). The profits from selling them are $12, $8, and $5, respectively. An apple pie costs 30 minutes labour_mins and 3 eggs. A croissant costs 15 minutes labour_mins and 2 eggs. And a donut costs 10 minutes labour_mins and an egg. The total available labour minutes and eggs are 500 mins and 60 eggs. The production volumme of each item needs to be greater than or equal to 0.
Here are my R codes:
library(nloptr)
# Define the objective function
objective <- function(x) {
profit <- c(12, 8, 5) # Profits for apple pie, croissant, and donut
total_profit <- sum(profit * x)
return(total_profit)
}
# Define the constraint function
constraint <- function(x) {
labor_mins <- c(30, 15, 10) # Labor minutes for apple pie, croissant, and donut
eggs <- c(3, 2, 1) # Eggs for apple pie, croissant, and donut
# Labor hours constraint
# Available Labor Hours - sum(labor_mins * x) >= 0
labor_hours_constraint <- 500 - sum(labor_mins * x)
# Egg units constraint
# Available Eggs - sum(eggs * x) >= 0
egg_units_constraint <- 60 - sum(eggs * x)
return(c(labor_hours_constraint, egg_units_constraint))
}
# Set optimization options
opts <- list(
"algorithm" = "NLOPT_GN_ISRES",
"xtol_rel" = 1e-8
)
# Define the optimization problem
res <- nloptr(
x0 = rep(0, 3), # Initial values (assuming 3 products)
eval_f = objective, # Objective function
lb = rep(0, 3), # Lower bounds (non-negative constraint)
ub = rep(Inf, 3), # Upper bounds
eval_g_ineq = constraint,# Inequality constraint function
opts = opts
)
#Print the result
print(res)
But the result I got is (0,0,0), it seems wrong. Does anyone know how to solve it and get a reasonable answer? Or I should redefine the problem?
Thank you
You can consider the following approach which is not with the nloptr package :