首页 » 技术分享 » 遗传算法初始,解决DEJONG 1 function

遗传算法初始,解决DEJONG 1 function

 

这几天上了遗传算法的课,来做个小作业,在做作业的时候,体会真的很大。这里展示下解决DEJONG1 function的代码。



clear all;
%% plot dejong function 1
% initialize x1 and x2 in the range of [-5.12, 5.12]
[x1, x2] = meshgrid(-5.12:0.1:5.12);
z = x1.^2+x2.^2;
mesh(x1, x2, z);


%% in the solution, we adapt real value representation to encode the chromosome.
population_size = 20;
selection_size = population_size - 2;
individuals = rand(population_size, 2)*5.12*2-5.12;
max_iter = 1000;
fitness_history = zeros(1, max_iter);
iter = 1;
while iter < max_iter+1
    % evalute the fitness of every individual, record the best fitness and
    % the index of corresponding chromosome in the population, for
    % visualization the evolutionary history, the best fitness should be
    % saved
    fitness_result = individuals(:, 1).^2 + individuals(:, 2).^2;
    [best_fitness, best_index] = min(fitness_result);
    best_x1_x2 = individuals(best_index,:);
    fitness_history(iter) =  best_fitness;
    [~, i] = sort(fitness_result);
    individuals = individuals(i, :);
    fitness_result = fitness_result(i);
%     %section from the population using roulettle wheel, the step is:a)sum
%     %all the fitness and divide by every fitness to caculate proportion of
%     %every chromosome. and the rand a number between[0,1] adopted to select
%     roulettle_proportion = fitness_result./sum(fitness_result);
%     roulettle_proportion = sort(roulettle_proportion, 'descend');
%     select_proportion = zeros(1, population_size);
%     for j = 1:population_size
%         select_proportion(j) = sum(roulettle_proportion(1:j));
%     end
%     selection_result = [];
%     for j = 1:selection_size
%         r = rand(1);
%         for k = 1:population_size
%             if r < select_proportion(k)
%                 selection_result = [selection_result; individuals(k, :)];
%                 break;
%             end
%         end
%     end
%     individuals = selection_result;
    % elitism principle to do selection
    individuals = individuals(1:selection_size, :);
    % crossover, randomly select parents from individuals to create
    % offspring
    offsprings = [];
    for i = 1:(population_size - selection_size)
        s = randperm(selection_size);
        offsprings = [offsprings; 0.5*(individuals(s(1), :)+individuals(s(2), :))];
    end
    % mutation
    
    miu = 0.01;
    individuals = individuals + rand(size(individuals, 1), 2)*miu*2 - miu;
    individuals = [individuals; offsprings];
    iter = iter + 1;
end
plot(fitness_history);
title(['best x:[', num2str(best_x1_x2(1)), ',',num2str(best_x1_x2(2)), ']', ',best_fitness:', num2str(best_fitness)]);

这里面初始化的种群数量可以改变的,里面也有按照roulette进行selection的代码,但是我给注释掉了,因为我最开始种群数量是5,但是使用roulette的时候,出来的结果不怎么好,改成elitism的话就好很多。。

PS:最开始我还以为一个chromosome中只是包含一个决策变量,实现过程中才知道,不是。。而是全部决策变量在一条chromosome中。我这里使用的real value based representation。我问过老师,老师说在numerical问题中一般都是real value的编码方法。。

转载自原文链接, 如需删除请联系管理员。

原文链接:遗传算法初始,解决DEJONG 1 function,转载请注明来源!

0