需要帮助解决 R 中的优化问题

问题描述 投票:0回答:3

enter image description here

这是我的代码:

install.packages("stats")
install.packages("RMySQL")
install.packages("lpSolveAPI")
library(lpSolveAPI)
library(readr)
library(tidyverse)
library(lubridate)
library(RMySQL)


# Decision variables:
x = number of bushels A
y = number of bushels B

# Constraints:
x + 2*y ≤ 50 (Land)
2*x + 3*y ≤ 75 (Water)
3*x + 2*y ≤ 60 (Fertilizer)
x + 2*y ≤ 40  (Labor)

# Objective Function:
# Maximize profits 

lp_crop <- make.lp(0,2)
set.objfn(lp_crop, c(20, 30))
add.constraint(lp_crop, c(1, 2), "<=", 50)
add.constraint(lp_crop, c(2, 3), "<=", 75)
add.constraint(lp_crop, c(3, 2), "<=", 60)
add.constraint(lp_crop, c(1, 2), "<=", 40)

invisible(lp.control(lp_crop, sense = 'max'))
print(lp_crop)

solve(lp_crop)

# optimal amount of bushels to produce for each crop

v <- get.variables(lp_crop)
paste0('# A bushels', v[1])
paste0('# B bushels', v[2])

我的问题是关于最后一部分。我期待得到以下信息:

## [1] "# A bushels 10"
## [1] "# B bushels 15"

相反,我得到以下内容:

paste0('# B bushels', v[2])
# [1] "# B bushels0"
paste0('# A bushels', v[1])
# [1] "# A bushels0"
paste0('# B bushels', v[2])
# 1] "# B bushels0"

为什么我的代码没有解决优化问题?

r optimization analytics linear-programming
3个回答
1
投票

我无法复制错误的印刷品。相反,当我运行以下代码时,我看到的唯一问题是需要使用

paste()
而不是
paste0()
.

library(lpSolveAPI)

lp_crop <- make.lp(0,2)
set.objfn(lp_crop, c(20, 30))
add.constraint(lp_crop, c(1, 2), "<=", 50)
add.constraint(lp_crop, c(2, 3), "<=", 75)
add.constraint(lp_crop, c(3, 2), "<=", 60)
add.constraint(lp_crop, c(1, 2), "<=", 40)

invisible(lp.control(lp_crop, sense = 'max'))
print(lp_crop)



> print(lp_crop)
Model name: 
            C1    C2        
Maximize    20    30        
R1           1     2  <=  50
R2           2     3  <=  75
R3           3     2  <=  60
R4           1     2  <=  40
Kind       Std   Std        
Type      Real  Real        
Upper      Inf   Inf        
Lower        0     0  

solve(lp_crop)

v <- get.variables(lp_crop)

v

向量

v
包含两个值,10和15:

> v
[1] 10 15

paste0()
函数不会在函数的参数之间添加空格。要在参数之间生成空格,请使用
paste()
:

原码:

> paste0('# A bushels', v[1])
[1] "# A bushels10"
> paste0('# B bushels', v[2])
[1] "# B bushels15"

现在,使用

paste()

> paste('# A bushels', v[1])
[1] "# A bushels 10"
> paste('# B bushels', v[2])
[1] "# B bushels 15"
> 

1
投票

在 base R 中你可以使用

constrOptim
函数:

req <- matrix(c(1, 2,
                2, 3,
                3, 2,
                1, 2), 4, byrow =TRUE)

units <- c(50, 75, 60, 40)

v <- constrOptim(c(0,0), \(x)-x%*%c(20,30), NULL,-req,-units)$par
[1]  9.999983 15.000008

注意你正在处理整数问题。因此,解决方案应该是整数。但这里我们的值完全不是整数。请注意,优化假设值是连续的,这就是为什么给我们上面的结果,我们可以清楚地看到是

10
15


0
投票

这是可以考虑的另一种方法:

fn_Objective <- function(param)
{
  x <- param[1]
  y <- param[2]
  
  if((x + 2 * y) > 50)
  {
    return(10 ^ 30)
    
  }else if(2  * x + 3 * y > 75)
  {
    return(10 ^ 30)
    
  }else if(3 * x + 2 * y > 60)
  {
    return(10 ^ 30)
    
  }else if(x + 2 * y > 40)
  { 
    return(10 ^ 30)
    
  }else
  {
    return(-(x + y) ^ 2)
  }
}

optim(par = c(5, 5), fn = fn_Objective)
$par
[1] 10 15

$value
[1] -625

$counts
function gradient 
     253       NA 

$convergence
[1] 0

$message
NULL
© www.soinside.com 2019 - 2024. All rights reserved.