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

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

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

问题

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

这是我的R代码:

 library(nloptr)
# 定义目标函数
    objective <- function(x) {
      profit <- c(12, 8, 5)  # 苹果派、可颂和甜甜圈的利润
      total_profit <- sum(profit * x)
      return(total_profit) 
    }

# 定义约束条件函数
    constraint <- function(x) {
      labor_mins <- c(30, 15, 10)  # 苹果派、可颂和甜甜圈的劳动时间
      eggs <- c(3, 2, 1)  # 苹果派、可颂和甜甜圈的鸡蛋数量
  
  # 劳动小时约束
# 可用劳动小时 - sum(labor_mins * x) >= 0
      labor_hours_constraint <- 500 - sum(labor_mins * x)  
  
  # 鸡蛋数量约束
# 可用鸡蛋 - sum(eggs * x) >= 0
      egg_units_constraint <- 60 - sum(eggs * x)  
      return(c(labor_hours_constraint, egg_units_constraint))
    }

# 设置优化选项
    opts <- list(
      "algorithm" = "NLOPT_GN_ISRES",
      "xtol_rel" = 1e-8
    )

# 定义优化问题
    res <- nloptr(
      x0 = rep(0, 3),  # 初始值(假设有3种产品)
      eval_f = objective,  # 目标函数
      lb = rep(0, 3),  # 下界(非负约束)
      ub = rep(Inf, 3),  # 上界
      eval_g_ineq = constraint,# 不等式约束函数
      opts = opts
    )

# 打印结果
    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:

 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

答案1

得分: 1

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

library(nloptr)

objective <- function(x)
{
  profit <- c(12, 8, 5)  
  total_profit <- -sum(profit * x)
  return(total_profit) 
}

constraint <- function(x)
{
  labor_mins <- c(30, 15, 10) 
  eggs <- c(3, 2, 1) 
  labor_hours_constraint <- sum(labor_mins * x) - 500   
  egg_units_constraint <- sum(eggs * x) - 60 
  return(c(labor_hours_constraint, egg_units_constraint))
}

opts <- list(
  algorithm = "NLOPT_LN_COBYLA",
  xtol_rel = 1e-8
)

res <- nloptr(
  x0 = rep(0, 3),
  eval_f = objective,  
  lb = rep(0, 3),  
  ub = rep(1000, 3),  
  eval_g_ineq = constraint,
  opts = opts)

print(res)

Call:
nloptr(x0 = rep(0, 3), eval_f = objective, lb = rep(0, 3), ub = rep(1000, 
    3), eval_g_ineq = constraint, opts = opts)


Minimization using NLopt version 2.7.1 

NLopt solver status: 4 ( NLOPT_XTOL_REACHED: Optimization stopped because xtol_rel or xtol_abs (above) was 
reached. )

Number of Iterations....: 70 
Termination conditions:  xtol_rel: 1e-08 
Number of inequality constraints:  2 
Number of equality constraints:    0 
Optimal value of objective function:  -260 
Optimal value of controls: 0 20 20
英文:

I have been able with the following code :

library(nloptr)

objective &lt;- function(x)
{
  profit &lt;- c(12, 8, 5)  
  total_profit &lt;- -sum(profit * x)
  return(total_profit) 
}

constraint &lt;- function(x)
{
  labor_mins &lt;- c(30, 15, 10) 
  eggs &lt;- c(3, 2, 1) 
  labor_hours_constraint &lt;- sum(labor_mins * x) - 500   
  egg_units_constraint &lt;- sum(eggs * x) - 60 
  return(c(labor_hours_constraint, egg_units_constraint))
}

opts &lt;- list(
  algorithm = &quot;NLOPT_LN_COBYLA&quot;,
  xtol_rel = 1e-8
)

res &lt;- nloptr(
  x0 = rep(0, 3),
  eval_f = objective,  
  lb = rep(0, 3),  
  ub = rep(1000, 3),  
  eval_g_ineq = constraint,
  opts = opts)

print(res)

Call:
nloptr(x0 = rep(0, 3), eval_f = objective, lb = rep(0, 3), ub = rep(1000, 
    3), eval_g_ineq = constraint, opts = opts)


Minimization using NLopt version 2.7.1 

NLopt solver status: 4 ( NLOPT_XTOL_REACHED: Optimization stopped because xtol_rel or xtol_abs (above) was 
reached. )

Number of Iterations....: 70 
Termination conditions:  xtol_rel: 1e-08 
Number of inequality constraints:  2 
Number of equality constraints:    0 
Optimal value of objective function:  -260 
Optimal value of controls: 0 20 20

答案2

得分: 0

以下是您要翻译的内容:

# 可以考虑以下方法,不使用nloptr包:

library(DEoptim)

objective_Function <- function(param)
{
  param <- round(param)
  nb_Pie <- param[1]
  nb_Croissant <- param[2]
  nb_Donut <- param[3]
  time <- nb_Pie * 30 + nb_Croissant * 15 + nb_Donut * 10
  eggs <- nb_Pie * 3 + nb_Croissant * 2 + nb_Donut * 1
  
  if((time > 500) | (eggs > 60) | (nb_Pie < 0) | (nb_Croissant < 0) | (nb_Donut < 0))
  {
    return(10 ^ 10)
    
  }else
  {
    profit <- nb_Pie * 12 + nb_Croissant * 8 + nb_Donut * 5
    return(-profit)
  }  
}

obj_DEoptim <- DEoptim(fn = objective_Function, 
                       lower = c(0, 0, 0),
                       upper = c(100, 100, 100),
                       control = list(itermax = 1000))


round(obj_DEoptim$optim$bestmem)

par1 par2 par3 
   0   20   20 

-obj_DEoptim$optim$bestval
260
英文:

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

library(DEoptim)

objective_Function &lt;- function(param)
{
  param &lt;- round(param)
  nb_Pie &lt;- param[1]
  nb_Croissant &lt;- param[2]
  nb_Donut &lt;- param[3]
  time &lt;- nb_Pie * 30 + nb_Croissant * 15 + nb_Donut * 10
  eggs &lt;- nb_Pie * 3 + nb_Croissant * 2 + nb_Donut * 1
  
  if((time &gt; 500) | (eggs &gt; 60) | (nb_Pie &lt; 0) | (nb_Croissant &lt; 0) | (nb_Donut &lt; 0))
  {
    return(10 ^ 10)
    
  }else
  {
    profit &lt;- nb_Pie * 12 + nb_Croissant * 8 + nb_Donut * 5
    return(-profit)
  }  
}

obj_DEoptim &lt;- DEoptim(fn = objective_Function, 
                       lower = c(0, 0, 0),
                       upper = c(100, 100, 100),
                       control = list(itermax = 1000))


round(obj_DEoptim$optim$bestmem)

par1 par2 par3 
   0   20   20 

-obj_DEoptim$optim$bestval
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:

确定