nsga2 R中多目标函数的约束设置

nsga2 R中多目标函数的约束设置,r,optimization,constraints,R,Optimization,Constraints,我想使用库mco中的函数nsga2来解决多目标问题并找到帕累托边界,但我无法正确设置约束 目标函数如下。问题的背景是项目选择,即我有五个项目,分别由x[1]、x[2]、。。。x[5],并且只能选择一些。例如,如果选择了项目编号1,则如果未选择x[1]=0,则x[1]=1,这对于所有项目都是正确的(x[n]的值是离散的,为1或0)。我的另一个限制是所选项目的总预算应该少于100。运行nsga2功能后,解决方案中的参数似乎不正确,因为参数不是1或0。我的约束条件有错吗?如何找到x[1]到x[5]的最

我想使用库
mco
中的函数
nsga2
来解决多目标问题并找到帕累托边界,但我无法正确设置约束

目标函数如下。问题的背景是项目选择,即我有五个项目,分别由x[1]、x[2]、。。。x[5],并且只能选择一些。例如,如果选择了项目编号1,则如果未选择x[1]=0,则x[1]=1,这对于所有项目都是正确的(x[n]的值是离散的,为1或0)。我的另一个限制是所选项目的总预算应该少于100。运行
nsga2
功能后,
解决方案中的参数似乎不正确,因为参数不是1或0。我的约束条件有错吗?如何找到x[1]到x[5]的最佳值?谢谢

# objective functions to minimize
 ObjFun <- function (x){
   f1 <- -0.02*x[1] + 0.01*x[2] + 0.02*x[3] + -0.01*x[4] + 0.02*x[5]  
   f2 <- 0.17*x[1] + -0.08*x[2] + 0.10*x[3] + 0.09*x[4] + 0.07*x[5] 
   c(f1, f2)      }

 # The constraints 
 Constr <- function(x){ 
   100 >= 20*x[1] + 30*x[2] + 20*x[3] + 33*x[4] + 60*x[5] # Total budget >= total project costs
   x[1:5] == 1
   x[1:5] == 0      }

 library(mco)
 Solution <- nsga2(ObjFun, 5, 2, lower.bounds=c(0,0,0,0,0), upper.bounds=c(1,1,1,1,1), constraints = Constr)
 # plot(Solution)
 Solution$par
#最小化的目标函数

ObjFun由于
x[i]
只能是1或0,因此您正在处理一个组合优化问题,其中您必须优化的空间是离散的:

一般来说,数值优化程序是在连续空间(R^n的子集)上构造的。然而,在您的例子中,离散空间很小,问题本身适合于一种简单的暴力方法,即在所有32个可能点上计算ObjFunc。这里的帕累托边界也是离散的

## objective functions to minimize
ObjFun <- function (x){
    f1 <- -0.02*x[1] + 0.01*x[2] + 0.02*x[3] + -0.01*x[4] + 0.02*x[5]  
    f2 <- 0.17*x[1] + -0.08*x[2] + 0.10*x[3] + 0.09*x[4] + 0.07*x[5] 
    c(f1=f1, f2=f2)
}

## space of all 32 feasible solutions 
space <- expand.grid(data.frame(matrix(0:1, nrow=2, ncol=5)))
## brute force evaluation of ObjFun on all the 32 feasible solutions
val <- sapply(data.frame(t(space)),  ObjFun)
tmp <- sol <- cbind(space, t(val))

## returns indices of all rows which are Pareto dominated
## by the i-th row
which.are.dominated <- function(i, tmp){
    s1 <- tmp$f1[i]
    s2 <- tmp$f2[i]
    with(tmp,
         which( (s1 <= f1) &
                (s2 <= f2) &
                ( (s1 < f1) |
                  (s2 < f2) )
               ))
}
## For each feasible solution i, remove all feasible solutions which are Pareto dominated by feasible solutions i
i <- 1
repeat{
    remove <- which.are.dominated(i, tmp)
    if(length(remove)>0) tmp <- tmp[-remove, ]
    if(i>=nrow(tmp)) break
    i <- i+1
}
with(sol, plot(f1, f2))
points(tmp$f1, tmp$f2, pch=20, col=2)
legend("topright", col=2, pch=20, "Pareto frontier")

您使用蛮力提供的解决方案非常适合发布的示例,因为它只有5个项目。我用50个项目尝试了你的方法,它在计算上变得昂贵,并且产生了大数据帧。可以使用一些遗传算法或投资组合优化技术来节省时间和空间吗?谢谢!@x[n]的值应该是二进制的。您认为将nsga2返回的最佳x[n]值四舍五入为0或1是将其变为二进制的好方法吗?谢谢你说的“好”到底是什么意思?1.函数
nsga2
返回超立方体中的点(其坐标通常不同于0或1);2.应用
圆形
将使所有坐标等于0或1;您可以使用
all(在%0:1中选择的selected.project.combines%)来检查这一点
;3.目视检查
圆形
ed点,确认其成本接近近似点的成本(因此近似最优)。你还需要什么?你担心什么?
##n.projects <- 12
n.projects <- 50
if(n.projects>25) generations=600


set.seed(1)
vecf1 <- rnorm(n.projects)
vecf2 <- rnorm(n.projects)
vcost <- rnorm(n.projects)
n.solutions <- 200

library(mco)

ObjFun <- function (x){
    f1 <- sum(vecf1*x)
    f2 <- sum(vecf2*x)
    c(f1=f1, f2=f2)
}
Constr <- function(x){ 
    c(100 - sum(vcost*x)) # Total budget >= total project costs
}

Solution <- nsga2(ObjFun, n.projects, 2,
                  lower.bounds=rep(0,n.projects), upper.bounds=rep(1,n.projects),
                  popsize=n.solutions,  constraints = Constr, cdim=1,
                  generations=generations)

selected.project.combinations <- unique(round(Solution$par))
selected.project.combinations.costs <- sapply(data.frame(t(selected.project.combinations)),  ObjFun)

## final plotting of results
max.n.proj.plot <- 12
if(n.projects <= max.n.proj.plot){
    xsamp <- expand.grid(data.frame(matrix(0:1, nrow=2, ncol=n.projects)))
}else{
    xsamp <- matrix(sample(0:1, n.projects*2^max.n.proj.plot, replace=TRUE), ncol=n.projects)
}
fsamp <- sapply(data.frame(t(xsamp)), ObjFun)

par(mfrow=c(1,2))
plot(Solution)
points(fsamp[1, ], fsamp[2, ])
points(t(selected.project.combinations.costs), col=3, pch=20)
legend("bottomleft", bty="n", pch=c(20,1), col=c(3,1),
       c("Costs of optimal\nproject combinations",
         "Costs of discarded\nproject combinations"),
       y.intersp=1.8
       )

plot(t(fsamp), xlim=range(Solution$value[ ,1], fsamp[1, ]),
     ylim=range(Solution$value[ ,2], fsamp[2, ]))
points(Solution$value, col=2, pch=".")
points(t(selected.project.combinations.costs), col=3, pch=20)