R中的全局多优化函数规范

时间:2016-06-24 09:05:00

标签: r function optimization genetic-algorithm

我想使用mco包的ngsa2来解决3个目标的优化问题。简而言之,我希望找到最佳的土地用途来解决环境问题。 这是我的实验: - 总共可以使用100个土地(下面的代码中的所有选项),每个土地使用的特点是三个表现(main.goal1,main.goal2和main.goal3)。 - 我有50个田地,其特征(田间土壤.Kq)是100个土地用途的子集(即,每个田地都不可能使用所有土地)=> options.soil1和options.soil2

我的目标是为我的50个字段中的每一个分配土地用途,以便最大限度地减少all.goal1,main.goal2和main.goal3。根据我的阅读,遗传算法对于此类问题非常有用。

所以这是我的虚拟数据。

set.seed(0)
all.options<-data.frame(num.option=1:100,main.goal1 = abs(rnorm(100)),
           main.goal2 = abs(rnorm(100)),
           main.goal3 = abs(rnorm(100))) # all possible combinations of the 3 goals
options.soil1<-subset(all.options, main.goal1>0.5) # possible combinations for soil1
options.soil2<-subset(all.options, main.goal3<0.5) # possible combinations for soil2

fields.Kq<-data.frame(num.field=1:50,soil=round(runif(50,0,1),0))

我猜我的目标函数看起来应该是

my.function<-function(x) {
  x[1]<-sum(A[,1) # main.goal1 for selected options for each of fields.Kq
  x[2]<-sum(A[,2) # main.goal2 for selected options for each of fields.Kq
  x[3]<-sum(A[,3) # main.goal3 for selected options for each of fields.Kq
} # where A should be a matrix of 50 lines with one line per field, and     #"choosen" land use option  

nsga2(my.function)

不幸的是我无法走得更远,因为我是R的优化新手。如何建立矩阵A,每个领域选择土地使用? 并使用,nga,如何归还这些土地用途? (以及main.goal1,main.goal2和main.goal3的优化(最小化)值?

提前感谢您提供给我的所有帮助,我非常期待建议/链接/书籍......以推进我的优化问题。

致以最诚挚的问候,

LH

1 个答案:

答案 0 :(得分:0)

以下是我解决问题的方法:

library("mco")
set.seed(0)
all.options<-data.frame(num.option=1:100,main.goal1 = abs(rnorm(100)),
                        main.goal2 = abs(rnorm(100)),
                        main.goal3 = abs(rnorm(100)),soil=c(rep("soilType1",50),rep("soilType2",50))) # all possible combinations of the 3 goals

fields.Kq<-data.frame(num.field=1:50,soil=rep(c("soilType1","soilType2"),25))

main.goal1=function(x)    # x - a vector 
{ 
  main.goal1=sum(all.options[x,1]) # compute main.goal1
  return(main.goal1) }

main.goal2=function(x)    # x - a vector 
{ 
  main.goal2=sum(all.options[x,2]) # compute main.goal2
  return(main.goal2) }

main.goal3=function(x)    # x - a vector 
{ 
  main.goal3=sum(all.options[x,3]) # compute main.goal3
  return(main.goal3) }

eval=function(x) c(main.goal1(x),main.goal2(x),main.goal3(x)) #objectivefunction

D<-length(fields.Kq[,1]) # number of fields
D2<-length(fields.Kq[,1])/2 # number of fields per type (simplified)
D.soil1<-max(which(all.options$soil=="soilType1")) # get boundary for bound soil1
D.soil2<-min(which(all.options$soil=="soilType2")) # get boundary for bound soil2

G=nsga2(fn=eval,idim=D,odim=3,
        lower.bounds=c(rep(1,D2),rep(D.soil2,D2)),upper.bounds=c(rep(D.soil1,D2),rep(100,D2)), # lower/upper bound: min/max num option
        popsize=20,generations=1:1000, cprob = 0.7, cdist = 5,
        mprob = 0.2, mdist = 10)

我通过这本非常有用且内容丰富的书中找到的例子来定义它&#34; R&#34;中的现代优化作者Paul Cortez。

LH