使用R语言和nloptr包解决非线性优化问题。

huangapple go评论187阅读模式
英文:

Solving a non-linear optimization problem by using R with the nloptr package

问题

我的问题是面包店在销售三种不同的产品(苹果派、可颂和甜甜圈)。销售它们的利润分别为12美元、8美元和5美元。
一个苹果派需要30分钟的劳动时间和3个鸡蛋。
一个可颂需要15分钟的劳动时间和2个鸡蛋。
而一个甜甜圈需要10分钟的劳动时间和一个鸡蛋。
总共可用的劳动时间和鸡蛋分别为500分钟和60个。

这是我的R代码:

  1. library(nloptr)
  2. # 定义目标函数
  3. objective <- function(x) {
  4. profit <- c(12, 8, 5) # 苹果派、可颂和甜甜圈的利润
  5. total_profit <- sum(profit * x)
  6. return(total_profit)
  7. }
  8. # 定义约束条件函数
  9. constraint <- function(x) {
  10. labor_mins <- c(30, 15, 10) # 苹果派、可颂和甜甜圈的劳动时间
  11. eggs <- c(3, 2, 1) # 苹果派、可颂和甜甜圈的鸡蛋数量
  12. # 劳动小时约束
  13. # 可用劳动小时 - sum(labor_mins * x) >= 0
  14. labor_hours_constraint <- 500 - sum(labor_mins * x)
  15. # 鸡蛋数量约束
  16. # 可用鸡蛋 - sum(eggs * x) >= 0
  17. egg_units_constraint <- 60 - sum(eggs * x)
  18. return(c(labor_hours_constraint, egg_units_constraint))
  19. }
  20. # 设置优化选项
  21. opts <- list(
  22. "algorithm" = "NLOPT_GN_ISRES",
  23. "xtol_rel" = 1e-8
  24. )
  25. # 定义优化问题
  26. res <- nloptr(
  27. x0 = rep(0, 3), # 初始值(假设有3种产品)
  28. eval_f = objective, # 目标函数
  29. lb = rep(0, 3), # 下界(非负约束)
  30. ub = rep(Inf, 3), # 上界
  31. eval_g_ineq = constraint,# 不等式约束函数
  32. opts = opts
  33. )
  34. # 打印结果
  35. print(res)

但我得到的结果是(0,0,0),看起来不对。有谁知道如何解决这个问题并得到合理的答案吗?还是我应该重新定义问题?

谢谢

英文:

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:

  1. library(nloptr)
  2. # Define the objective function
  3. objective <- function(x) {
  4. profit <- c(12, 8, 5) # Profits for apple pie, croissant, and donut
  5. total_profit <- sum(profit * x)
  6. return(total_profit)
  7. }
  8. # Define the constraint function
  9. constraint <- function(x) {
  10. labor_mins <- c(30, 15, 10) # Labor minutes for apple pie, croissant, and donut
  11. eggs <- c(3, 2, 1) # Eggs for apple pie, croissant, and donut
  12. # Labor hours constraint
  13. # Available Labor Hours - sum(labor_mins * x) >= 0
  14. labor_hours_constraint <- 500 - sum(labor_mins * x)
  15. # Egg units constraint
  16. # Available Eggs - sum(eggs * x) >= 0
  17. egg_units_constraint <- 60 - sum(eggs * x)
  18. return(c(labor_hours_constraint, egg_units_constraint))
  19. }
  20. # Set optimization options
  21. opts <- list(
  22. "algorithm" = "NLOPT_GN_ISRES",
  23. "xtol_rel" = 1e-8
  24. )
  25. # Define the optimization problem
  26. res <- nloptr(
  27. x0 = rep(0, 3), # Initial values (assuming 3 products)
  28. eval_f = objective, # Objective function
  29. lb = rep(0, 3), # Lower bounds (non-negative constraint)
  30. ub = rep(Inf, 3), # Upper bounds
  31. eval_g_ineq = constraint,# Inequality constraint function
  32. opts = opts
  33. )
  34. #Print the result
  35. 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

答案1

得分: 1

我已经能够使用以下代码:

  1. library(nloptr)
  2. objective <- function(x)
  3. {
  4. profit <- c(12, 8, 5)
  5. total_profit <- -sum(profit * x)
  6. return(total_profit)
  7. }
  8. constraint <- function(x)
  9. {
  10. labor_mins <- c(30, 15, 10)
  11. eggs <- c(3, 2, 1)
  12. labor_hours_constraint <- sum(labor_mins * x) - 500
  13. egg_units_constraint <- sum(eggs * x) - 60
  14. return(c(labor_hours_constraint, egg_units_constraint))
  15. }
  16. opts <- list(
  17. algorithm = "NLOPT_LN_COBYLA",
  18. xtol_rel = 1e-8
  19. )
  20. res <- nloptr(
  21. x0 = rep(0, 3),
  22. eval_f = objective,
  23. lb = rep(0, 3),
  24. ub = rep(1000, 3),
  25. eval_g_ineq = constraint,
  26. opts = opts)
  27. print(res)
  28. Call:
  29. nloptr(x0 = rep(0, 3), eval_f = objective, lb = rep(0, 3), ub = rep(1000,
  30. 3), eval_g_ineq = constraint, opts = opts)
  31. Minimization using NLopt version 2.7.1
  32. NLopt solver status: 4 ( NLOPT_XTOL_REACHED: Optimization stopped because xtol_rel or xtol_abs (above) was
  33. reached. )
  34. Number of Iterations....: 70
  35. Termination conditions: xtol_rel: 1e-08
  36. Number of inequality constraints: 2
  37. Number of equality constraints: 0
  38. Optimal value of objective function: -260
  39. Optimal value of controls: 0 20 20
英文:

I have been able with the following code :

  1. library(nloptr)
  2. objective &lt;- function(x)
  3. {
  4. profit &lt;- c(12, 8, 5)
  5. total_profit &lt;- -sum(profit * x)
  6. return(total_profit)
  7. }
  8. constraint &lt;- function(x)
  9. {
  10. labor_mins &lt;- c(30, 15, 10)
  11. eggs &lt;- c(3, 2, 1)
  12. labor_hours_constraint &lt;- sum(labor_mins * x) - 500
  13. egg_units_constraint &lt;- sum(eggs * x) - 60
  14. return(c(labor_hours_constraint, egg_units_constraint))
  15. }
  16. opts &lt;- list(
  17. algorithm = &quot;NLOPT_LN_COBYLA&quot;,
  18. xtol_rel = 1e-8
  19. )
  20. res &lt;- nloptr(
  21. x0 = rep(0, 3),
  22. eval_f = objective,
  23. lb = rep(0, 3),
  24. ub = rep(1000, 3),
  25. eval_g_ineq = constraint,
  26. opts = opts)
  27. print(res)
  28. Call:
  29. nloptr(x0 = rep(0, 3), eval_f = objective, lb = rep(0, 3), ub = rep(1000,
  30. 3), eval_g_ineq = constraint, opts = opts)
  31. Minimization using NLopt version 2.7.1
  32. NLopt solver status: 4 ( NLOPT_XTOL_REACHED: Optimization stopped because xtol_rel or xtol_abs (above) was
  33. reached. )
  34. Number of Iterations....: 70
  35. Termination conditions: xtol_rel: 1e-08
  36. Number of inequality constraints: 2
  37. Number of equality constraints: 0
  38. Optimal value of objective function: -260
  39. Optimal value of controls: 0 20 20

答案2

得分: 0

以下是您要翻译的内容:

  1. # 可以考虑以下方法,不使用nloptr包:
  2. library(DEoptim)
  3. objective_Function <- function(param)
  4. {
  5. param <- round(param)
  6. nb_Pie <- param[1]
  7. nb_Croissant <- param[2]
  8. nb_Donut <- param[3]
  9. time <- nb_Pie * 30 + nb_Croissant * 15 + nb_Donut * 10
  10. eggs <- nb_Pie * 3 + nb_Croissant * 2 + nb_Donut * 1
  11. if((time > 500) | (eggs > 60) | (nb_Pie < 0) | (nb_Croissant < 0) | (nb_Donut < 0))
  12. {
  13. return(10 ^ 10)
  14. }else
  15. {
  16. profit <- nb_Pie * 12 + nb_Croissant * 8 + nb_Donut * 5
  17. return(-profit)
  18. }
  19. }
  20. obj_DEoptim <- DEoptim(fn = objective_Function,
  21. lower = c(0, 0, 0),
  22. upper = c(100, 100, 100),
  23. control = list(itermax = 1000))
  24. round(obj_DEoptim$optim$bestmem)
  25. par1 par2 par3
  26. 0 20 20
  27. -obj_DEoptim$optim$bestval
  28. 260
英文:

You can consider the following approach which is not with the nloptr package :

  1. library(DEoptim)
  2. objective_Function &lt;- function(param)
  3. {
  4. param &lt;- round(param)
  5. nb_Pie &lt;- param[1]
  6. nb_Croissant &lt;- param[2]
  7. nb_Donut &lt;- param[3]
  8. time &lt;- nb_Pie * 30 + nb_Croissant * 15 + nb_Donut * 10
  9. eggs &lt;- nb_Pie * 3 + nb_Croissant * 2 + nb_Donut * 1
  10. if((time &gt; 500) | (eggs &gt; 60) | (nb_Pie &lt; 0) | (nb_Croissant &lt; 0) | (nb_Donut &lt; 0))
  11. {
  12. return(10 ^ 10)
  13. }else
  14. {
  15. profit &lt;- nb_Pie * 12 + nb_Croissant * 8 + nb_Donut * 5
  16. return(-profit)
  17. }
  18. }
  19. obj_DEoptim &lt;- DEoptim(fn = objective_Function,
  20. lower = c(0, 0, 0),
  21. upper = c(100, 100, 100),
  22. control = list(itermax = 1000))
  23. round(obj_DEoptim$optim$bestmem)
  24. par1 par2 par3
  25. 0 20 20
  26. -obj_DEoptim$optim$bestval
  27. 260

huangapple
  • 本文由 发表于 2023年6月5日 18:36:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76405592.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定