【问题标题】:Binary coded GA with NSGA-II in RR中带有NSGA-II的二进制编码GA
【发布时间】:2017-06-07 12:52:41
【问题描述】:

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

【问题讨论】:

    标签: r genetic-algorithm


    【解决方案1】:

    我遇到了同样的问题,所以我决定自己解决它。但不保证方法是否正确。

    以下部分是针对包'mco'
    我将官方 nsga2 实现中的部分内容复制到“mco”包中。 此解决方法仅适用于二进制 (0-1) 变量。

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

    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();

    然后

    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();
    }

    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.定义函数randomperc()如下

    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() 替换为 randomperc()

    4.在R中构建包

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-14
      • 1970-01-01
      • 2020-04-18
      • 2011-05-08
      • 1970-01-01
      相关资源
      最近更新 更多