2

我一直在尝试使用 constrOptim() (我的第一次)解决 R 中的约束优化问题,但我正在努力为我的问题设置约束。

问题很简单,我可以设置函数,但是对于传递约束有点不知所措。

例如我定义的问题是(我将从固定在 1000 的 N 开始说所以我只想解决 X 最终我想选择 N 和 X 的最大利润):

所以我可以将功能设置为:

fun <- function(x, N, a, c, s) {   ## a profit function
    x1 <- x[1]
    x2 <- x[2]
    x3 <- x[3]    
    a1 <- a[1]
    a2 <- a[2]
    a3 <- a[3]
    c1 <- c[1]
    c2 <- c[2]
    c3 <- c[3]
    s1 <- s[1]
    s2 <- s[2]
    s3 <- s[3]  
    ((N*x1*a1*s1)-(N*x1*c1))+((N*x2*a2*s2)-(N*x2*c2))+((N*x3*a3*s3)-(N*x3*c3))
}

我需要实施的约束是:

x1>=0.03
x1<=0.7
x2>=0.03
x2<=0.7
x3>=0.03
x2<=0.7
x1+x2+x3=1

这里的 X 表示我需要优化分配 N 的存储桶,因此 x1=N 的百分比以放置在存储桶 1 中,以此类推,每个存储桶至少有 3% 但不超过 70%。

非常感谢任何帮助...

例如,这是我用来测试该功能是否符合我要求的示例:

    fun <- function(x, N, a, c, s) {   ## profit function
        x1 <- x[1]
        x2 <- x[2]
        x3 <- x[3]    
        a1 <- a[1]
        a2 <- a[2]
        a3 <- a[3]
        c1 <- c[1]
        c2 <- c[2]
        c3 <- c[3]
        s1 <- s[1]
        s2 <- s[2]
        s3 <- s[3]  
        ((N*x1*a1*s1)-(N*x1*c1))+((N*x2*a2*s2)-(N*x2*c2))+((N*x3*a3*s3)-(N*x3*c3))
    };

    x <-matrix(c(0.5,0.25,0.25));

    a <-matrix(c(0.2,0.15,0.1));

    s <-matrix(c(100,75,50));

    c <-matrix(c(10,8,7));

    N <- 1000;

fun(x,N,a,c,s);
4

2 回答 2

2

您可以使用 lpSolveAPI 包。

## problem constants
a <- c(0.2, 0.15, 0.1)
s <- c(100, 75, 50)
c <- c(10, 8, 7)
N <- 1000


## Problem formulation
# x1          >= 0.03
# x1          <= 0.7
#     x2      >= 0.03
#     x2      <= 0.7
#          x3 >= 0.03
# x1 +x2 + x3  = 1
#N*(c1- a1*s1)* x1 + (a2*s2 - c2)* x2 + (a3*s3-  c3)* x3

library(lpSolveAPI)
my.lp <- make.lp(6, 3)

在 lp solve 中构建模型的最佳方法是按列;

#constraints by columns
set.column(my.lp, 1, c(1, 1, 0, 0, 1, 1))
set.column(my.lp, 2, c(0, 0, 1, 1, 0, 1))
set.column(my.lp, 3, c(0, 0, 0, 0, 1, 1))
#the objective function ,since we need to max I set negtive max(f) = -min(f)
set.objfn (my.lp, -N*c(c[1]- a[1]*s[1], a[2]*s[2] - c[2],a[3]*s[3]-  c[3]))
set.rhs(my.lp, c(rep(c(0.03,0.7),2),0.03,1))
#constraint types 
set.constr.type(my.lp, c(rep(c(">=","<="), 2),">=","="))

看看我的模型

my.lp
Model name: 

 Model name: 
             C1     C2     C3          
Minimize  10000  -3250   2000          
R1            1      0      0  >=  0.03
R2            1      0      0  <=   0.7
R3            0      1      0  >=  0.03
R4            0      1      0  <=   0.7
R5            1      0      1  >=  0.03
R6            1      1      1   =     1
Kind        Std    Std    Std          
Type       Real   Real   Real          
Upper       Inf    Inf    Inf          
Lower         0      0      0      
 solve(my.lp)

[1] 0    ## sucess :)

get.objective(my.lp)
[1] -1435
get.constraints(my.lp)
[1] 0.70 0.70 0.03 0.03 0.97 1.00
## the decisions variables
get.variables(my.lp)
[1] 0.03 0.70 0.27
于 2012-12-20T17:12:34.487 回答
1

嗨,以防万一,我还找到了如下答案:

首先,您的目标函数可以使用向量运算更简洁地编写:

> my_obj_coeffs <- function(N,a,c,s) N*(a*s-c)


> fun <- function(x,N,a,c,s) sum(my_obj_coeffs(N,a,c,s) * x)

您正在尝试求解线性程序,因此您可以使用单纯形算法来求解它。在'boot'包中有一个轻量级的实现。

> library(boot)

> solution <- function(obj) simplex(obj, diag(3), rep(0.7,3), diag(3), rep(0.03,3), rep(1,3), 1, maxi=TRUE)

Then for the example parameters you used, you can call that solution function:

> a <- c(0.2,0.15,0.1)
> s <- c(100,75,50)
> c <- c(10,8,7)
> N <- 1000
> solution(my_obj_coeffs(N,a,c,s))

Linear Programming Results

Call : simplex(a = obj(N, a, s, c), A1 = diag(3), b1 = rep(0.7, 3), 
    A2 = diag(3), b2 = rep(0.03, 3), A3 = matrix(1, 1, 3), b3 = 1, 
    maxi = TRUE)

Maximization Problem with Objective Function Coefficients
      [,1]
[1,] 10000
[2,]  3250
[3,] -2000
attr(,"names")
[1] "x1" "x2" "x3"


Optimal solution has the following values
  x1   x2   x3 
0.70 0.27 0.03 
The optimal value of the objective  function is 7817.5.
于 2012-12-21T11:11:39.267 回答