【优化求解】基于NSGA2算法求解多目标优化问题matlab代码
?1 模型簡(jiǎn)介
2 部分代碼
clc;
clear;
close all;
%% Problem Definition
data=load('mydata');
R=data.R;
model.R=R;
model.method='cvar';
model.alpha=0.95;
CostFunction=@(x) PortMOC(x,model);? ? ? % Cost Function
nVar=size(R,2);? ? ? ? ? ? ?% Number of Decision Variables
VarSize=[1 nVar];? ?% Size of Decision Variables Matrix
VarMin=0;? ? ? ? ? % Lower Bound of Variables
VarMax=1;? ? ? ? ? % Upper Bound of Variables
% Number of Objective Functions
nObj=numel(CostFunction(unifrnd(VarMin,VarMax,VarSize)));
%% NSGA-II Parameters
MaxIt=100;? ? ? % Maximum Number of Iterations
nPop=50;? ? ? ? % Population Size
pCrossover=0.7;? ? ? ? ? ? ? ? ? ? ? ? ?% Crossover Percentage
nCrossover=2*round(pCrossover*nPop/2);? % Number of Parnets (Offsprings)
pMutation=0.4;? ? ? ? ? ? ? ? ? ? ? ? ? % Mutation Percentage
nMutation=round(pMutation*nPop);? ? ? ? % Number of Mutants
mu=0.02;? ? ? ? ? ? ? ? ? ? % Mutation Rate
sigma=0.1*(VarMax-VarMin);? % Mutation Step Size
%% Initialization
empty_individual.Position=[];
empty_individual.Cost=[];
empty_individual.Out=[];
empty_individual.Rank=[];
empty_individual.DominationSet=[];
empty_individual.DominatedCount=[];
empty_individual.CrowdingDistance=[];
pop=repmat(empty_individual,nPop,1);
for i=1:nPop
? ??
? ? pop(i).Position=unifrnd(VarMin,VarMax,VarSize);
? ??
? ? [pop(i).Cost, pop(i).Out]=CostFunction(pop(i).Position);
? ??
end
% Non-Dominated Sorting
[pop, F]=NonDominatedSorting(pop);
% Calculate Crowding Distance
pop=CalcCrowdingDistance(pop,F);
% Sort Population
[pop, F]=SortPopulation(pop);
%% NSGA-II Main Loop
for it=1:MaxIt
? ??
? ? % Crossover
? ? popc=repmat(empty_individual,nCrossover/2,2);
? ? for k=1:nCrossover/2
? ? ? ??
? ? ? ? i1=randi([1 nPop]);
? ? ? ? p1=pop(i1);
? ? ? ??
? ? ? ? i2=randi([1 nPop]);
? ? ? ? p2=pop(i2);
? ? ? ??
? ? ? ? [popc(k,1).Position, popc(k,2).Position]=Crossover(p1.Position,p2.Position,VarMin,VarMax);
? ? ? ??
? ? ? ? [popc(k,1).Cost, popc(k,1).Out]=CostFunction(popc(k,1).Position);
? ? ? ? [popc(k,2).Cost, popc(k,2).Out]=CostFunction(popc(k,2).Position);
? ? ? ??
? ? end
? ? popc=popc(:);
? ??
? ? % Mutation
? ? popm=repmat(empty_individual,nMutation,1);
? ? for k=1:nMutation
? ? ? ??
? ? ? ? i=randi([1 nPop]);
? ? ? ? p=pop(i);
? ? ? ??
? ? ? ? popm(k).Position=Mutate(p.Position,mu,sigma,VarMin,VarMax);
? ? ? ??
? ? ? ? [popm(k).Cost, popm(k).Out]=CostFunction(popm(k).Position);
? ? ? ??
? ? end
? ??
? ? % Merge
? ? pop=[pop
? ? ? ? ?popc
? ? ? ? ?popm]; %#ok
? ? ?
? ? % Non-Dominated Sorting
? ? [pop, F]=NonDominatedSorting(pop);
? ? % Calculate Crowding Distance
? ? pop=CalcCrowdingDistance(pop,F);
? ? % Sort Population
? ? pop=SortPopulation(pop);
? ??
? ? % Truncate
? ? pop=pop(1:nPop);
? ??
? ? % Non-Dominated Sorting
? ? [pop, F]=NonDominatedSorting(pop);
? ? % Calculate Crowding Distance
? ? pop=CalcCrowdingDistance(pop,F);
? ? % Sort Population
? ? [pop, F]=SortPopulation(pop);
? ??
? ? % Store F1
? ? F1=pop(F{1});
? ??
? ? % Show Iteration Information
? ? disp(['Iteration ' num2str(it) ': Number of F1 Members = ' num2str(numel(F1))]);
? ??
? ? % Plot F1 Costs
? ? figure(1);
? ? PlotCosts(F1);
? ? pause(0.01);
? ??
end
%% Results
3 仿真結(jié)果
4 參考文獻(xiàn)
[1]張利. NSGA2算法及其在電力系統(tǒng)穩(wěn)定器參數(shù)優(yōu)化中的應(yīng)用[D]. 西南交通大學(xué), 2013.
**部分理論引用網(wǎng)絡(luò)文獻(xiàn),若有侵權(quán)聯(lián)系博主刪除。**
總結(jié)
以上是生活随笔為你收集整理的【优化求解】基于NSGA2算法求解多目标优化问题matlab代码的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: linux桌面旋转了180度,[多图]回
- 下一篇: [pytorch、学习] - 5.2 填