GA-带有轮盘赌轮的自定义整数选择

时间:2019-06-20 15:16:42

标签: matlab genetic-algorithm roulette-wheel-selection

我需要做什么:

获取 n 个染色体的种群,计算每个染色体的优/优,通过权重随机选择,然后从种群中返回最佳染色体。 在我的情况下, n 是10,染色体是一个从1到3的整数数组。

示例:[1,3,2,1,1,1,2,3,3,2,1 ... k]。 (k为81)

轮盘赌选的工作原理link

根据父母的健康状况选择父母。染色体越好,被选择的机会就越多。想象一下一个轮盘赌轮,在轮盘赌中所有种群的染色体都被放置了,每个轮盘的位置都因其健身功能而大。

enter image description here

然后将大理石扔到那里并选择染色体。具有更大适应性的染色体将被选择更多次。

这可以通过以下算法进行模拟。

  1. [总和] 计算种群中所有染色体适应度的总和-S。
  2. [选择] 从时间间隔(0,S)-r生成随机数。
  3. [循环] 遍历总体,将适应度从0和s相加。当总和s大于r时,停止并返回您所在的染色体。

如何编写带有整数的染色体的自定义轮盘选择?


我发现此功能仅适用于人口中的一条染色体 link

% ---------------------------------------------------------
% Roulette Wheel Selection Algorithm. A set of weights
% represents the probability of selection of each
% individual in a group of choices. It returns the index
% of the chosen individual.
% Usage example:
% fortune_wheel ([1 5 3 15 8 1])
%    most probable result is 4 (weights 15)
% ---------------------------------------------------------
function choice = fortune_wheel(weights)
  accumulation = cumsum(weights);
  p = rand() * accumulation(end);
  chosen_index = -1;
  for index = 1 : length(accumulation)
    if (accumulation(index) > p)
      chosen_index = index;
      break;
    end
  end
  choice = chosen_index;

我不知道如何编写用于评估整个染色体的良性/健康函数,然后迭代哪个是最佳解决方案?

我需要这样的东西,但是要有整数:

enter image description here

请欢迎提出任何想法。

编辑: 我想出了什么:

function [ selected_chromosome ] = RouletteWheelSelection( population )
% ---------------------------------------------------------
% Roulette Wheel Selection Algorithm. A set of weights
% represents the probability of selection of each
% individual in a group of choices. It returns the chosen chromosome.
% ---------------------------------------------------------
  struct_size = size(population.chromosomes);
  num_of_chromosomes = struct_size(1);

  for cnt = 1:num_of_chromosomes
      qk = cumsum(population.chromosomes(cnt,:));
      goodness(cnt) = sum(qk);
  end

  total_goodness = sum(goodness);
  for cnt2 = 1:num_of_chromosomes
      probabilities(cnt2) = goodness(cnt2) / total_goodness;
  end

  index = sum(rand >= cumsum([0, probabilities]));

  selected_chromosome = population.chromosomes(index, :);
end

0 个答案:

没有答案
相关问题