zoukankan      html  css  js  c++  java
  • 数学建模之路----遗传算法

    遗传操作:

    遗传操作是优选,强势个体的“选择”,个体间交换基因产生新个体的“交叉”,个体基因信息突变产新个体的“变异”;遗传算法中的搜索最优解过程就是模仿生物的这个进化过程,使用遗传算子来实现的;

    即有选择算子,交换算子,变异算子。

    有空再写‘’‘’‘’‘’‘’‘’‘’

    初始化:

    % %%初始化
    % %输入:chromes_size,dim维数,lb下界,ub上界
    % %输出 chromes
    function chromes = init_chromes(chromes_size,dim,lb,ub)
        chromes = rand(chromes_size,dim)*(ub-lb)+lb;
    end

    选择:

    function [newchromes,newfitness] = selection(chromes,fitness)
        weights = 1./fitness;
        totalfit = sum(weights);
        totalf = weights./totalfit;
        index = [];
        for i = 1:size(chromes,1)
            pick = rand;
            while pick == 0
                pick = rand;
            end
            for j = 1:size(chromes,1)
                pick = pick - totalf(j);
                if pick < 0
                    index = [index j];
                    break
                end
            end
        end
        newchromes = chromes(index,:);
        newfitness = fitness(index);
    end

     变异

    function newchromes = muatation(chromes,pm,lb,ub)
        for i = 1:size(chromes,1)
            newchromes(i,:) = chromes(i,:);
            if (rand < pm)
                pos = ceil(rand * size(chromes,2));
                newchromes(i,pos) = rand*(ub-lb)+lb;
            end
        end
    end
    

     交换

    function newchromes = crossover(chromes, pc)
        newchromes = ones(size(chromes));
        for i = 1 : size(chromes, 1)
            parents = randperm(10,2);
            pos = round(rand*size(chromes,2));
            if (rand < pc)
                newchromes(i,:) = [chromes(parents(1),1:pos),chromes(parents(2),pos+1:size(chromes,2))];
            else
                newchromes(i,:) = chromes(i,:);
            end
        end
    end
    

     主函数

    clear
    clc
    chromes_size = 20;
    dim = 2;
    pc = 0.9;
    pm = 0.2;
    lb = -1;
    ub = 1;
    maxiter = 1000;
    %目标方程
    namefunc = 'sphere';
    fd = str2func(namefunc);
    chromes = init_chromes(chromes_size,dim,lb,ub);
    
    for i = 1:chromes_size
        % fitness(i) = feval(fd,chromes);
        x = chromes(i, 1);
        y = chromes(i, 2);
        fitness(i) = sin(x)+cos(y)+0.1*x+0.1*y;
    end
    [bestfitness,bestindex] = min(fitness);
    bestchrome = chromes(bestindex,:);
    for iter = 1:maxiter
        [chromes,newfitness] = selection(chromes,fitness);
        chromes = crossover(chromes,pc);
        chromes = muatation(chromes,pm,lb,ub);
        for i = 1:chromes_size
            x = chromes(i, 1);
            y = chromes(i, 2);
            fitness(i) = sin(x)+cos(y)+0.1*x+0.1*y;
            if bestfitness >fitness(i)
                bestfitness = fitness(i);
                bestchrome = chromes(i,:);
            end
        end
        trace(iter) = bestfitness;
    end
    plot(trace)
    title('fitness curve')
    

      

  • 相关阅读:
    8.24
    8.23
    今日拔牙牙疼暂时不评论了,明天展示的时候老师也会给出建议
    8.22
    8.21
    8.20
    8.19随笔
    助教培训第四次作业
    助教培训第三次作业-墨刀的练习
    同时安装多个jdk设置及切换
  • 原文地址:https://www.cnblogs.com/freeyouth/p/10877696.html
Copyright © 2011-2022 走看看