当前位置:   article > 正文

matlab基于遗传算法的多目标优化算法(附代码获取方法)_多目标遗传算法matlab程序

多目标遗传算法matlab程序

介绍

前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站
目前的多目标优化算法有很多, Kalyanmoy Deb的带精英策略的快速非支配排序遗传算法( nondominated sorting genetic algorithm Il,NSGA-I)无疑是其中应用最为广泛也是最为成功的一种。


clc;clear;
tic;
%% 初始化
PopSize=200;%种群大小 
MaxIteration =300;%最大迭代次数
R=50;
location1=load('location1_100.txt');%优化100个城市
location2=load('A_location2_100.txt');
% location1=load('location1.txt');
% location2=load('location2.txt');
CityNum =size(location1,2);%城市数
V=CityNum;
M=2;
pc=0.8;pm=0.9;
for i=1:PopSize
    chromosome(i,1:CityNum)=randperm(CityNum);
    chromosome(i,CityNum+1:CityNum+2)=costfunction(chromosome(i,1:CityNum),location1,location2);
end
chromosome= non_domination_sort_mod(chromosome);%将解分  最后一列为拥挤度 倒数第二列为分级数
index=find(chromosome(:,103)==1);
costrep=chromosome(index,101:102);%第一级即非劣解

%% 主循环
pool = round(PopSize/2);  %突变池规模

for Iteration=1:MaxIteration
    if ~mod(Iteration,10)
        fprintf('current iter:%d\n',Iteration)
        disp([' Number of Repository Particles = ' num2str(size(costrep,1))]);
    end
    parent_chromosome = selection_individuals(chromosome,pool,2);
    parent_var=parent_chromosome(:,1:CityNum);%分离出解向量
    %% 交叉
    offspring_var=[];offspring_cost=[];
    for ic=1:pool/2
        
        m1=randi(pool);%选出交叉向量
        m2=randi(pool);
        while m1==m2
            m1=randi(pool);
        end
        scro(1,:)=parent_var(m1,:);
        scro(2,:)=parent_var(m2,:);
        if rand<pc
            c1=randi(CityNum);%选出交叉位置
            c2=randi(CityNum);
            while c1==c2
                c1=randi(CityNum);
            end
            chb1=min(c1,c2);
            chb2=max(c1,c2);
            
            middle=scro(1,chb1+1:chb2);
            scro(1,chb1+1:chb2)=scro(2,chb1+1:chb2);
            scro(2,chb1+1:chb2)=middle;
            for i=1:chb1
                while find(scro(1,chb1+1:chb2)==scro(1,i))
                    zhi=find(scro(1,chb1+1:chb2)==scro(1,i));
                    y=scro(2,chb1+zhi);
                    scro(1,i)=y;
                end
                while find(scro(2,chb1+1:chb2)==scro(2,i))
                    zhi=find(scro(2,chb1+1:chb2)==scro(2,i));
                    y=scro(1,chb1+zhi);
                    scro(2,i)=y;
                end
            end
            for i=chb2+1:CityNum
                while find(scro(1,1:chb2)==scro(1,i))
                    zhi=find(scro(1,1:chb2)==scro(1,i));
                    y=scro(2,zhi);
                    scro(1,i)=y;
                end
                while find(scro(2,1:chb2)==scro(2,i))
                    zhi=find(scro(2,1:chb2)==scro(2,i));
                    y=scro(1,zhi);
                    scro(2,i)=y;
                end
            end
        end
        if rand<pm%逆序变异
            m1=randi(CityNum);
            m2=randi(CityNum);
            while m1==m2
                m1=randi(CityNum);
            end
            loc1=min(m1,m2);loc2=max(m1,m2);
            scro(1,loc1:loc2)=fliplr(scro(1,loc1:loc2));
            %             tt=scro(1,m2);
            %             scro(1,m2)=scro(1,m1);
            %             scro(1,m1)=tt;
        end
        if rand<pm%对换变异
            m1=randi(CityNum);
            m2=randi(CityNum);
            while m1==m2
                m1=randi(CityNum);
            end
            tt=scro(2,m2);
            scro(2,m2)=scro(2,m1);
            scro(2,m1)=tt;
        end
        scro_cost(1,:)=costfunction(scro(1,:),location1,location2);
        scro_cost(2,:)=costfunction(scro(2,:),location1,location2);
        offspring_var=[offspring_var;scro];%解
        offspring_cost=[offspring_cost;scro_cost];%适应度
        
    end
    offspring_chromosome(:,1:V)=offspring_var;
    offspring_chromosome(:,V+1:V+M)=offspring_cost;
    
    main_pop = size(chromosome,1);
    offspring_pop = size(offspring_chromosome,1);
    intermediate_chromosome(1:main_pop,:) = chromosome;
    intermediate_chromosome(main_pop + 1 :main_pop + offspring_pop,1 : M+V) = ...
        offspring_chromosome;
    intermediate_chromosome = ...
        non_domination_sort_mod(intermediate_chromosome);
    %% 选择
    
    chromosome = replace_chromosome(intermediate_chromosome,PopSize);
    
    
    index=find(intermediate_chromosome(:,103)==1);
    costrep=intermediate_chromosome(index,101:102);
    cost=intermediate_chromosome(:,101:102);
        if ~mod(Iteration,1)
    
            figure (1)
            plot(costrep(:,1),costrep(:,2),'r*',cost(:,1),cost(:,2),'kx');
            xlabel('F1');ylabel('F2');
            title(strcat('Interaction ',num2str(Iteration), ' Pareto non-dominated solutions'));
            %          hold on
        end
    if ~mod(Iteration,MaxIteration)
        %             if ~mod(Iteration,1)
        fun_pf=costrep;
        [fun_pf,~]=sortrows(fun_pf,1);
        plot(fun_pf(:,1),fun_pf(:,2),'k*-');
        title(strcat('Interaction ',num2str(Iteration), ' Pareto non-dominated solutions'));
        hold on;
        grid on;
    end
end
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145

运行过程

请添加图片描述

运行结果

在这里插入图片描述
如需帮助VX:zhangshu2274
代码下载链接

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/53221
推荐阅读
相关标签
  

闽ICP备14008679号