⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 genealg.m

📁 一种标准的遗传算法实例
💻 M
字号:
function [stats,pop,elitechrome] = genealg(configuration,prnlevel,ffunc);
% GENEALG  implements simple genetic algorithm
% Version 1.0  	1/24/96 Ron Shaffer 
%		Naval Research Laboratory
% Version 1.1  	3/13/96 Ron Shaffer
%		Modified to return the best chromosome
%		Modifications for compatibility with Bohav. function
% Version 1.2	5/2/96 Ron Shaffer
%		Fixed bug in conversion of binary to integer numbers
%		Modified to include gray-coded chromosomes
%		DECODE renamed DECODEB for regular binary decoding
%		DECODEG--decode the chromosome using gray decoding
%
% [stats,pop] = genealg(configuration)
% stats:	fitness statistics for each generation
% pop:		final population of chromosomes
% elitechrome:	chromosome in current population with highest fitness score
% configuration:GA configuration in the following order
%		length of chromsome, population size, number of generations,
%		mutation rate, crossover rate, crossover type, elistist, gray 

%
% set GA configuration
%
clc
home
fprintf('** Starting GA Optimization ** \n');
fprintf('# \t   Mean \t     Stdev \t    Best \n');
ndim = configuration(1);
popsize = configuration(2);
numgen = configuration(3);
pm = configuration(4);
px = configuration(5);
xtype = configuration(6);
elite = configuration(7);
gray = configuration(8);
numxover = 0;
nummut = 0;
nstat = 7;
%
%	For bohachevsky function set minimum and maximum values
%
minboh = -1;
maxboh = 1;
%
% To use elitist strategy popsize must a odd integer
% If elitist strategy off popsize must be an even integer
%
test = rem(popsize,2);
if elite == 1
	if (test == 0)
		popsize = popsize + 1;
	end
else
	if (test ~= 0)
		popsize = popsize + 1;
	end
end
%
% initialize population randomly
%
pop = [(rand(popsize,ndim)<0.5)];
%
% Optimize for a given number of generations
%
tnumgen = numgen + 1;
for cgn = 1:tnumgen
%
% 	compute fitness function for each member of population
%
	scores = fitness(pop,ffunc,maxboh,minboh,gray);
%
	if prnlevel == 1
		for j = 1:popsize
			fprintf('%d',pop(j,1:ndim));
			fprintf('\t %8.2f',scores(j));
			fprintf('\n');
		end
	end
%	
	[topfit,topi] = max(scores);
	ms = mean(scores);
	sd = std(scores);
	fprintf('%i \t %8.2f \t %8.2f \t %8.2f \n',cgn,ms,sd,topfit);
	stats(cgn,1:nstat) = [cgn ms sd topfit pm nummut numxover];
%
%	find best chromosome based on fitness score
%
	if elite == 1
		elitechrome = pop(topi,:);
	end
%
%
%	Stop optimization after fixed number of generations
%
	if (cgn == numgen) 
		break;
	end
%
%	select most fit members of population for mating subset
%	using tournament selection and eliteist strategy (if turned on)
%	
	mate = tselect(pop,scores,elite);

%
%	perform crossover if required
%
	[child,txnum] = xover(mate,px,xtype);
	numxover = numxover + txnum;
%
%	perform mutation
%
	[child,tnmut] = mutate(child,pm);
	nummut = nummut + tnmut;
%
% 	Children + elite chromosome (if elitism turned on) form the new generation
%
	pop = child;
	if elite == 1
		pop(popsize,:) = elitechrome;
	end
%
end

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -