• 数学建模之路----遗传算法


    遗传操作:

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

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

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

    初始化:

    % %%初始化
    % %输入: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')
    

      

  • 相关阅读:
    Excel:大小写转换
    Qt Quick开发环境搭建
    如何集成QML与C++?
    第十八章:QML扩展
    如何编写整洁的Qml代码
    Linux环境,4个版本的微信对比
    Oracle 按照 IN的顺序排序结果集
    __VA_ARGS__的用法
    手写Web图片懒加载~
    Selenuim+Python之元素定位总结及实例说明
  • 原文地址:https://www.cnblogs.com/freeyouth/p/10877696.html
Copyright © 2020-2023  润新知