首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在R中使用NSGA-II的二进制编码GA

在R中使用NSGA-II的二进制编码GA
EN

Stack Overflow用户
提问于 2017-06-07 20:52:41
回答 1查看 455关注 0票数 1

我有一个多目标最小化函数。我想在R中使用NSGA-II。有这样的软件包: nsga2R和mco。但这些软件包不支持二进制编码的染色体。在我的适应度函数中,由于我的问题的结构,我需要二进制染色体来获得最优解。有没有办法在nsga2中对R使用二进制编码的染色体(或者使用不同的算法)?谢谢。

EN

回答 1

Stack Overflow用户

发布于 2017-07-04 22:00:57

我也遇到了同样的问题,所以我决定自己解决它。但如果这是正确的方式就没有监护人。

以下部分用于软件包'mco'

我将官方的nsga2实现中的部分复制到'mco‘包中。此解决方法仅适用于二进制(0-1)变量。

1. /src/nsga2.c中的重写函数如下:

代码语言:javascript
复制
static void mutate_ind (nsga2_ctx *ctx, individual *ind) {
  int j;
  double prob;
  GetRNGstate();
  for (j = 0; j < ctx->input_dim; j++)
    {
    //for (k=0; k < ctx[j]->input_dim; k++)
    //{
      //prob = randomperc();
      prob = unif_rand();
      if (prob <= ctx->mutation_probability) {
        if (ind->input[j] == 0)
        {
          ind->input[j] = 1;
        }
        else
        {
          ind->input[j] = 0;
        }
        ctx->input_mutations+=1;
      }
    //}
  }
  PutRNGstate();

然后

代码语言:javascript
复制
static void crossover (nsga2_ctx *ctx,
                       individual *parent1, individual *parent2,
                       individual *child1, individual *child2) {

  
  int i;
  int nbits=1;
  double rand;
  int temp, site1, site2, temp2, temp3;
  
  GetRNGstate();
  
    rand=unif_rand();
    if (rand <= ctx->crossing_probability)
    {
      ctx->input_crossings++;
      //site1 = rnd(0,ctx->input_dim);
      //site2 = rnd(0,ctx->input_dim);
      if(unif_rand()<=0.5){
        temp2=0;
      }else{
        temp2=1;
      }
      
      if(unif_rand()<=0.5){
        temp3=0;
      }else{
        temp3=1;
      }
      
      site1=temp2;
      site2=temp3;
      
      if (site1 > site2)
      {
        temp = site1;
        site1 = site2;
        site2 = temp;
      }
      for (i=0; i<site1; i++)
      {
        child1->input[i] = parent1->input[i];
        child2->input[i] = parent2->input[i];
      }
      for (i=site1; i<site2; i++)
      {
        child1->input[i] = parent2->input[i];
        child2->input[i] = parent1->input[i];
      }
      for (i=site2; i<nbits; i++)
      {
        child1->input[i] = parent1->input[i];
        child2->input[i] = parent2->input[i];
      }
    }
    else
    {
      for (i=0; i<nbits; i++)
      {
        child1->input[i] = parent1->input[i];
        child2->input[i] = parent2->input[i];
      }
    }

  PutRNGstate();
}

代码语言:javascript
复制
static void population_initialize(nsga2_ctx *ctx, population *pop) {
  GetRNGstate();
  int i, j;
  for (i = 0; i < pop->size; ++i)  {
    for (j=0; j<ctx->input_dim; ++j) {
      /* Generate random value between lower and upper bound */
      //double delta = ctx->upper_input_bound[j] - ctx->lower_input_bound[j];
      //pop->ind[i].input[j] = ctx->lower_input_bound[j] + delta*unif_rand();
	  if(unif_rand() <= 0.5){
		  pop->ind[i].input[j] = 0;
	  }
	  else{
		  pop->ind[i].input[j] = 1;
	  }
    }
  }
  PutRNGstate();
}

2.定义函数()如下

代码语言:javascript
复制
double seed;
double oldrand[55];
int jrand;

/* Create next batch of 55 random numbers */
void advance_random ()
{
  int j1;
  double new_random;
  for(j1=0; j1<24; j1++)
  {
    new_random = oldrand[j1]-oldrand[j1+31];
    if(new_random<0.0)
    {
      new_random = new_random+1.0;
    }
    oldrand[j1] = new_random;
  }
  for(j1=24; j1<55; j1++)
  {
    new_random = oldrand[j1]-oldrand[j1-24];
    if(new_random<0.0)
    {
      new_random = new_random+1.0;
    }
    oldrand[j1] = new_random;
  }
}

/* Fetch a single random number between 0.0 and 1.0 */
double randomperc()
{
  jrand++;
  if(jrand>=55)
  {
    jrand = 1;
    advance_random();
  }
  return((double)oldrand[jrand]);
}

3.用'nsga2.c‘中的unif_rand()替换每个unif_rand()

4.在R中构建包

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44413213

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档