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

27 篇文章 83 订阅
4 篇文章 8 订阅

介绍

前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站
目前的多目标优化算法有很多, 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

运行过程

请添加图片描述

运行结果

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

目标优化遗传算法(MOGA)是一种用于解决多目标优化问题的进化算法。下面是一个简单的MOGA的完整代码示例: ```matlab % 设置参数 popSize = 100; % 种群大小 maxGen = 100; % 最大迭代次数 numObj = 2; % 目标函数数量 % 初始化种群 pop = rand(popSize, numObj); % 随机生成种群 % 迭代进化 for gen = 1:maxGen % 计算适应度 fitness = calculateFitness(pop); % 选择操作 selectedPop = selection(pop, fitness); % 交叉操作 crossedPop = crossover(selectedPop); % 变异操作 mutatedPop = mutation(crossedPop); % 合并新种群和原种群 combinedPop = [pop; mutatedPop]; % 非支配排序 [fronts, ranks] = nonDominatedSort(combinedPop); % 拥挤度计算 crowdingDistances = crowdingDistance(combinedPop, fronts); % 选择下一代种群 pop = nextGeneration(combinedPop, fronts, ranks, crowdingDistances, popSize); end % 输出最终结果 paretoFront = pop(ranks == 1, :); % 获取帕累托前沿解集合 disp(paretoFront); % 计算适应度函数(根据具体问题进行定义) function fitness = calculateFitness(population) % 这里假设有两个目标函数,可以根据具体问题进行修改 fitness(:, 1) = population(:, 1).^2 + population(:, 2).^2; fitness(:, 2) = (population(:, 1)-1).^2 + population(:, 2).^2; end % 选择操作(这里使用锦标赛选择) function selectedPop = selection(population, fitness) tournamentSize = 2; % 锦标赛大小 popSize = size(population, 1); selectedPop = zeros(size(population)); for i = 1:popSize % 随机选择锦标赛参与者 participants = randperm(popSize, tournamentSize); % 选择最优个体 [~, winnerIndex] = min(fitness(participants, :)); selectedPop(i, :) = population(participants(winnerIndex), :); end end % 交叉操作(这里使用单点交叉) function crossedPop = crossover(selectedPop) crossoverRate = 0.8; % 交叉概率 popSize = size(selectedPop, 1); numObj = size(selectedPop, 2); crossedPop = zeros(size(selectedPop)); for i = 1:2:popSize % 随机选择两个个体进行交叉 if rand < crossoverRate crossoverPoint = randi(numObj-1); crossedPop(i, :) = [selectedPop(i, 1:crossoverPoint), selectedPop(i+1, crossoverPoint+1:end)]; crossedPop(i+1, :) = [selectedPop(i+1, 1:crossoverPoint), selectedPop(i, crossoverPoint+1:end)]; else crossedPop(i, :) = selectedPop(i, :); crossedPop(i+1, :) = selectedPop(i+1, :); end end end % 变异操作(这里使用高斯变异) function mutatedPop = mutation(crossedPop) mutationRate = 0.1; % 变异概率 popSize = size(crossedPop, 1); numObj = size(crossedPop, 2); mutatedPop = crossedPop; for i = 1:popSize % 对每个个体的每个基因进行变异 for j = 1:numObj if rand < mutationRate mutatedPop(i, j) = crossedPop(i, j) + randn; end end end end % 非支配排序 function [fronts, ranks] = nonDominatedSort(population) popSize = size(population, 1); numObj = size(population, 2); fronts = cell(popSize, 1); ranks = zeros(popSize, 1); dominatedCount = zeros(popSize, 1); dominatedSet = cell(popSize, 1); for i = 1:popSize fronts{i} = []; dominatedSet{i} = []; for j = 1:popSize if i == j continue; end if all(population(j,:) <= population(i,:)) && any(population(j,:) < population(i,:)) fronts{i} = [fronts{i}, j]; elseif all(population(i,:) <= population(j,:)) && any(population(i,:) < population(j,:)) dominatedCount(i) = dominatedCount(i) + 1; dominatedSet{i} = [dominatedSet{i}, j]; end end if dominatedCount(i) == 0 ranks(i) = 1; end end frontIndex = 1; while ~isempty(fronts{frontIndex}) nextFront = []; for i = fronts{frontIndex} for j = dominatedSet{i} dominatedCount(j) = dominatedCount(j) - 1; if dominatedCount(j) == 0 ranks(j) = frontIndex + 1; nextFront = [nextFront, j]; end end end frontIndex = frontIndex + 1; fronts{frontIndex} = nextFront; end end % 拥挤度计算 function crowdingDistances = crowdingDistance(population, fronts) popSize = size(population, 1); numObj = size(population, 2); crowdingDistances = zeros(popSize, 1); for i = 1:length(fronts) front = fronts{i}; for j = 1:numObj [~, sortedIndices] = sort(population(front, j)); crowdingDistances(front(sortedIndices(1))) = inf; crowdingDistances(front(sortedIndices(end))) = inf; for k = 2:length(front)-1 crowdingDistances(front(sortedIndices(k))) = crowdingDistances(front(sortedIndices(k))) + ... (population(front(sortedIndices(k+1)), j) -1)), j)) / ... (max(population(front, j)) - min(population(front, j))); end end end end % 选择下一代种群 function nextGenPop = nextGeneration(population, fronts, ranks, crowdingDistances, popSize) popSize = size(population, 1); numObj = size(population, 2); nextGenPop = zeros(popSize, numObj); nextGenIndex = 1; frontIndex = 1; while nextGenIndex <= popSize front = fronts{frontIndex}; if nextGenIndex + length(front) <= popSize nextGenPop(nextGenIndex:nextGenIndex+length(front)-1, :) = population(front, :); nextGenIndex = nextGenIndex + length(front); else [~, sortedIndices] = sort(crowdingDistances(front), 'descend'); nextGenPop(nextGenIndex:popSize, :) = population(front(sortedIndices(1:popSize-nextGenIndex+1)), :); break; end frontIndex = frontIndex + 1; end end ``` 这段代码实现了一个简单的MOGA,其中包括初始化种群、迭代进化、适应度计算、选择操作、交叉操作、变异操作、非支配排序、拥挤度计算和选择下一代种群等步骤。你可以根据具体问题进行适当的修改和扩展。 希望对你有帮助!如果你有任何问题,请随时提问。
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

张叔zhangshu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值