• 多目标遗传算法 ------ NSGA-II (部分源码解析) 实数、二进制编码的变异操作 mutation.c


    遗传算法的变异操作

     1 /* Mutation routines */
     2 
     3 # include <stdio.h>
     4 # include <stdlib.h>
     5 # include <math.h>
     6 
     7 # include "global.h"
     8 # include "rand.h"
     9 
    10 /* Function to perform mutation in a population */
    11 void mutation_pop (population *pop)
    12 {
    13     int i;
    14     for (i=0; i<popsize; i++)
    15     {
    16         mutation_ind(&(pop->ind[i]));
    17     }
    18     return;
    19 }

    一次进化过程中的 变异操作,  需要调用   变异函数 mutation_ind  种群个数popsize 次。

    函数包装,判断是实数编码还是二进制编码并调用不同的变异函数

     1 /* Function to perform mutation of an individual */
     2 void mutation_ind (individual *ind)
     3 {
     4     if (nreal!=0)
     5     {
     6         real_mutate_ind(ind);
     7     }
     8     if (nbin!=0)
     9     {
    10         bin_mutate_ind(ind);
    11     }
    12     return;
    13 }

    二进制编码 的  变异操作:

    每个个体  的   每个变量  的  二进制编码段   的  每个比特位,   以变异概率  pmut_bin  进行变异。

    (单点变异)

     1 /* Routine for binary mutation of an individual */
     2 void bin_mutate_ind (individual *ind)
     3 {
     4     int j, k;
     5     double prob;
     6     for (j=0; j<nbin; j++)
     7     {
     8         for (k=0; k<nbits[j]; k++)
     9         {
    10             prob = randomperc();
    11             if (prob <=pmut_bin)
    12             {
    13                 if (ind->gene[j][k] == 0)
    14                 {
    15                     ind->gene[j][k] = 1;
    16                 }
    17                 else
    18                 {
    19                     ind->gene[j][k] = 0;
    20                 }
    21                 nbinmut+=1;
    22             }
    23         }
    24     }
    25     return;
    26 }

    实数编码  情况下的   变异操作:

    (多项式变异)

     1 /* Routine for real polynomial mutation of an individual */
     2 void real_mutate_ind (individual *ind)
     3 {
     4     int j;
     5     double rnd, delta1, delta2, mut_pow, deltaq;
     6     double delta;
     7     double y, yl, yu, val, xy;
     8     for (j=0; j<nreal; j++)
     9     {
    10         rnd = randomperc();
    11         if (rnd <= pmut_real)
    12         {
    13             y = ind->xreal[j];
    14             yl = min_realvar[j];
    15             yu = max_realvar[j];
    16             delta1 = (y-yl)/(yu-yl);
    17             delta2 = (yu-y)/(yu-yl);
    18             delta = minimum (delta1,delta2);
    19             rnd = randomperc();
    20             mut_pow = 1.0/(eta_m+1.0);
    21             if (rnd <= 0.5)
    22             {
    23                 xy = 1.0-delta1;
    24                 val = 2.0*rnd+(1.0-2.0*rnd)*(pow(xy,(eta_m+1.0)));
    25                 deltaq =  pow(val,mut_pow) - 1.0;
    26             }
    27             else
    28             {
    29                 xy = 1.0-delta2;
    30                 val = 2.0*(1.0-rnd)+2.0*(rnd-0.5)*(pow(xy,(eta_m+1.0)));
    31                 deltaq = 1.0 - (pow(val,mut_pow));
    32             }
    33             y = y + deltaq*(yu-yl);
    34             if (y<yl)
    35             {
    36                 y = yl;
    37             }
    38             if (y>yu)
    39             {
    40                 y = yu;
    41             }
    42             ind->xreal[j] = y;
    43             nrealmut+=1;
    44         }
    45     }
    46     return;
    47 }
    eta_m 为 实数编码的 多项式变异的 参数, 为全局设定。
    (该变异方式是为了模拟二进制编码的单点变异,逻辑比较简单,数学含义不明)

    每个个体  的   每个变量  的  实数表示,   以变异概率  pmut_real  进行变异
  • 相关阅读:
    LeetCode 121. Best Time to Buy and Sell Stock
    LeetCode 221. Maximal Square
    LeetCode 152. Maximum Product Subarray
    LeetCode 53. Maximum Subarray
    LeetCode 91. Decode Ways
    LeetCode 64. Minimum Path Sum
    LeetCode 264. Ugly Number II
    LeetCode 263. Ugly Number
    LeetCode 50. Pow(x, n)
    LeetCode 279. Perfect Squares
  • 原文地址:https://www.cnblogs.com/devilmaycry812839668/p/6265115.html
Copyright © 2020-2023  润新知