function [x, endPop, bPop, traceInfo] = ga(bounds, evalFN, evalOps, startPop, opts, ...
termFN, termOps, selectFN, selectOps, xOverFNs, xOverOps, mutFNs, mutOps)
% Output Arguments:
% x - the best solution found during the course of the run
% endPop - the final population
% bPop - a trace of the best population
% traceInfo - a matrix of best and means of the ga for each generation
%
% Input Arguments:
% bounds - a matrix of upper and lower bounds on the variables
% evalFN - the name of the evaluation .m function
% evalOps - options to pass to the evaluation function ([NULL])
% startPop - a matrix of solutions that can be initialized
% from initialize.m
% opts - [epsilon prob_ops display] change required to consider two
% solutions different, prob_ops 0 if you want to apply the
% genetic operators probabilisticly to each solution, 1 if
% you are supplying a deterministic number of operator
% applications and display is 1 to output progress 0 for
% quiet. ([1e-6 1 0])
% termFN - name of the .m termination function (['maxGenTerm'])
% termOps - options string to be passed to the termination function
% ([100]).
% selectFN - name of the .m selection function (['normGeomSelect'])
% selectOpts - options string to be passed to select after
% select(pop,#,opts) ([0.08])
% xOverFNS - a string containing blank seperated names of Xover.m
% files (['arithXover heuristicXover simpleXover'])
% xOverOps - A matrix of options to pass to Xover.m files with the
% first column being the number of that xOver to perform
% similiarly for mutation ([2 0;2 3;2 0])
% mutFNs - a string containing blank seperated names of mutation.m
% files (['boundaryMutation multiNonUnifMutation ...
% nonUnifMutation unifMutation'])
% mutOps - A matrix of options to pass to Xover.m files with the
% first column being the number of that xOver to perform
% similiarly for mutation ([4 0 0;6 100 3;4 100 3;4 0 0])
%% 初始化参数
n = nargin;
if n < 2 || n == 6 || n == 10 || n == 12
disp('Insufficient arguements')
end
% 默认评估选项
if n < 3
evalOps = [];
end
% 默认参数
if n < 5
opts = [1e-6, 1, 0];
end
% 默认参数
if isempty(opts)
opts = [1e-6, 1, 0];
end
%% 判断是否为m文件
if any(evalFN < 48)
% 浮点数编码
if opts(2) == 1
e1str = ['x=c1; c1(xZomeLength)=', evalFN ';'];
e2str = ['x=c2; c2(xZomeLength)=', evalFN ';'];
% 二进制编码
else
e1str = ['x=b2f(endPop(j,:),bounds,bits); endPop(j,xZomeLength)=', evalFN ';'];
end
else
% 浮点数编码
if opts(2) == 1
e1str = ['[c1 c1(xZomeLength)]=' evalFN '(c1,[gen evalOps]);'];
e2str = ['[c2 c2(xZomeLength)]=' evalFN '(c2,[gen evalOps]);'];
% 二进制编码
else
e1str=['x=b2f(endPop(j,:),bounds,bits);[x v]=' evalFN ...
'(x,[gen evalOps]); endPop(j,:)=[f2b(x,bounds,bits) v];'];
end
end
%% 默认终止信息
if n < 6
termOps = 100;
termFN = 'maxGenTerm';
end
%% 默认变异信息
if n < 12
% 浮点数编码
if opts(2) == 1
mutFNs = 'boundaryMutation multiNonUnifMutation nonUnifMutation unifMutation';
mutOps = [4, 0, 0; 6, termOps(1), 3; 4, termOps(1), 3;4, 0, 0];
% 二进制编码
else
mutFNs = 'binaryMutation';
mutOps = 0.05;
end
end
%% 默认交叉信息
if n < 10
% 浮点数编码
if opts(2) == 1
xOverFNs = 'arithXover heuristicXover simpleXover';
xOverOps = [2, 0; 2, 3; 2, 0];
% 二进制编码
else
xOverFNs = 'simpleXover';
xOverOps = 0.6;
end
end
%% 仅默认选择选项,即轮盘赌。
if n < 9
selectOps = [];
end
%% 默认选择信息
if n < 8
selectFN = 'normGeomSelect';
selectOps = 0.08;
end
%% 默认终止信息
if n < 6
termOps = 100;
termFN = 'maxGenTerm';
end
%% 没有定的初始种群
if n < 4
startPop = [];
end
%% 随机生成种群
if isempty(startPop)
startPop = initializega(80, bounds, evalFN, evalOps, opts(1: 2));
end
%% 二进制编码
if opts(2) == 0
bits = calcbits(bounds, opts(1));
end
%% 参数设置
xOverFNs = parse(xOverFNs);
mutFNs = parse(mutFNs);
xZomeLength = size(startPop, 2); % xzome 的长度
numVar = xZomeLength - 1; % 变量数
popSize = size(startPop,1); % 种群人口个数
endPop = zeros(popSize, xZomeLength); % 第二种群矩阵
numXOvers = size(xOverFNs, 1); % Number of Crossover operators
numMuts = size(mutFNs, 1); % Number of Mutation operators
epsilon = opts(1); % Threshold for two fittness to differ
oval = max(startPop(:, xZomeLength)); % Best value in start pop
bFoundIn = 1; % Number of times best has changed
done = 0; % Done with simulated evolution
gen = 1; % Current Generation Number
collectTrace = (nargout > 3); % Should we collect info every gen
floatGA = opts(2) == 1; % Probabilistic application of ops
display = opts(3); % Display progress
%% 精英模型
while(~done)
[bval, bindx] = max(startPop(:, xZomeLength)); % Best of current pop
best = startPop(bindx, :);
if collectTrace
traceInfo(gen, 1) = gen; % current generation
traceInfo(gen, 2) = startPop(bindx, xZomeLength); % Best fittness
traceInfo(gen, 3) = mean(startPop(:, xZomeLength)); % Avg fittness
traceInfo(gen, 4) = std(startPop(:, xZomeLength));
end
%% 最佳解
if ( (abs(bval - oval) > epsilon) || (gen==1))
% 更新显示
if display
fprintf(1, '\n%d %f\n', gen, bval);
end
% 更新种群矩阵
if floatGA
bPop(bFoundIn, :) = [gen, startPop(bindx, :)];
else
bPop(bFoundIn, :) = [gen, b2f(startPop(bindx, 1 : numVar), bounds, bits)...
startPop(bindx, xZomeLength)];
end
bFoundIn = bFoundIn + 1; % Update number of changes
oval = bval; % Update the best val
else
if display
fprintf(1,'%d ',gen); % Otherwise just update num gen
end
end
%% 选择种群
endPop = feval(selectFN, startPop, [gen, selectOps]);
% 以参数为操作数的模型运行
if floatGA
for i = 1 : numXOvers
for j = 1 : xOverOps(i, 1)
a = round(rand * (popSize - 1) + 1); % Pick a parent
b = round(rand * (popSize - 1) + 1); % Pick another parent
xN = deblank(xOverFNs(i, :)); % Get the name of crossover function
[c1, c2] = feval(xN, endPop(a, :), endPop(b, :), bounds, [gen, xOverOps(i, :)]);
% Make sure we created a new
if c1(1 : numVar) == endPop(a, (1 : numVar))
c1(xZomeLength) = endPop(a, xZomeLength);
elseif c1(1:numVar) == endPop(b, (1 : numVar))
c1(xZomeLength) = endPop(b, xZomeLength);
else
eval(e1str);
end
if c2(1 : numVar) == endPop(a, (1 : numVar))
c2(xZomeLength) = endPop(a, xZomeLength);
elseif c2(1 : numVar) == endPop(b, (1 : numVar))
c2(xZomeLength) = endPop(b, xZomeLength);
else
eval(e2str);
end
endPop(a, :) = c1;
endPop(b, :) = c2;
end
end
for i = 1 : numMuts
for j = 1 : mutOps(i, 1)
a = round(rand * (popSize - 1) + 1);
c1 = feval(deblank(mutFNs(i, :)), endPop(a, :), bounds, [gen, mutOps(i, :)]);
if c1(1 : numVar) == endPop(a, (1 : numVar))

普通网友
- 粉丝: 243
- 资源: 45
最新资源
- 基于最大功率跟踪MPPT算法的光伏电池充电模型,稳定直流输出电压,精准控制电池充放电的双向电路技术(48V直流侧参考电压),基于最大功率跟踪MPPT算法的直流侧电压稳定控制光伏电池充电模型,光伏-电池
- 基于S7-200 PLC的组态王燃油锅炉控制系统设计与实施:详尽的梯形图接线图原理及IO分配解析,基于S7-200 PLC与组态王技术的燃油锅炉控制系统:梯形图原理图及IO分配详解,基于S7-200
- 基于知识蒸馏的Resnet改进轻量化模型实现高光谱图像分类(附数据集及高准确率代码),基于知识蒸馏学习的轻量级高光谱图像分类模型-教师Resnet助力深度学习突破边界代码套件(附数据集),基于知识蒸
- 《基于佳点集与多策略改进的麻雀搜索算法(GSSA)复现与实证分析》,基于佳点集的GSSA算法复现:策略优化与基准测试函数的实现分析,麻雀搜索算法(SSA)复现:《基于佳点集的改进麻雀搜索算法-闫少强》
- MATLAB中的语义分割技术探究与应用实践,MATLAB中的语义分割技术探讨与应用实践,matlab,语义分割 ,MATLAB; 语义分割; 图像处理; 深度学习,Matlab语义分割技术解析
- 基于S7-200 PLC和MCGS组态的电镀控制详解:梯形图程序、接线图与IO分配及组态画面全解析,基于S7-200 PLC和MCGS组态的电镀控制综合教程:梯形图程序、接线图与组态画面详解,基于S7
- 基于PLC的S7-200组态王游泳池水处理系统详解:梯形图程序、接线图与组态画面全攻略,基于PLC的S7-200组态王游泳池水处理系统:梯形图程序、接线图与组态画面详解,S7-200组态王基于PLC的
- 基于Matlab Simulink的虚拟同步控制VSG仿真模型:适应电网波动与指令突变的有功控制研究,高性能虚拟同步控制VSG仿真模型:适应电网复杂变化,离并网均稳定运行,实现电压电流双环控制与SVP
- 永磁直驱风力发电系统仿真分析与最佳尖速比控制策略探究:基于背靠背结构的控制研究文献附后,永磁直驱风力发电系统:基于背靠背结构的仿真模型与尖速比控制策略的研究文献,永磁直驱风力发电系统,永磁同步电机,风
- 光储系统并网仿真研究:光照变化下三相电压稳定与双闭环控制策略应用,基于Simulink的光储并网仿真模型研究:探究光照强度变化下三相电压的稳定与双闭环控制策略,光储、光伏并网,光储并网仿真模型,风光储
- 基于LabVIEW的智能多路压力数据采集系统设计与实现,Labview下的多路压力数据采集系统精细化设计,基于Labview的多路压力数据采集系统的设计 ,基于Labview;多路压力数据;采集系统
- 欧姆龙机器视觉软件系统FH系列:FH图像传感器与CCD视觉检测模拟软件的仿真应用,欧姆龙机器视觉软件系统FH系列:FH图像传感器与仿真软件结合,全面升级实现高精度视觉检测模拟 ,欧姆龙机器视觉软件系统
- iSecure Cnnter:海康威视专业安防解决方案的智能化综合管理云平台,iSecure Cnnter:海康威视领先的综合安防管理平台解决方案,iSecure Cnnter海康威视综合安防管理平
- Creo 6.0与Pro E5.0下的平衡车全结构设计图纸与三维模型,含零件及装配体,适合新手学习与软件技术进阶 ,Creo 6.0与Pro E5.0下的平衡车全结构设计图纸及三维模型,含零件与装配体
- XC7Z020 ZYNQ7000系列在线升级功能开发详解:全方位通信接口支持与上位机、下位机测试程序整合,XC7Z020 ZYNQ7000系列软件在线升级功能:涵盖上位机与下位机,支持多种通信接口测试
- 基于组态王与PLC技术的养殖场测控系统设计与实现:图纸详备、组态画面一览,基于组态王与PLC技术的养殖场测控系统:梯形图程序、接线图与组态画面详解,S7-200 组态王 基于组态王和PLC的养殖场测控
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈


