Skip to content

Instantly share code, notes, and snippets.

@jschwinger233
Last active August 29, 2015 14:00
Show Gist options
  • Save jschwinger233/11165057 to your computer and use it in GitHub Desktop.
Save jschwinger233/11165057 to your computer and use it in GitHub Desktop.
function [fval x] = gaboundconstraint(fun, lb ,ub)
%lb = -10*ones(1,10);ub = 10*ones(1,10);
%Schaffer = '-0.5+(sin(sqrt(sum(x.^2,2))).^2-0.5)./(1+0.001*sum(x.^2,2)).^2';
%Schaffer在x = (0,0,…,0)处有全局极小点-1
%Ackley = '-20*exp(-0.2*sqrt(sum(x.^2,2)/size(x,2))) - exp(sum(cos(2*pi*x),2)/nGenome) + exp(1) + 20';
%Ackley在x = (0,0,…,0)处有全局极小点0
%Griewank = '1/4000*sum(x.^2,2) - prod(cos(bsxfun(@rdivide,x,sqrt(1:nGenome))),2) + 1';
%Griewank在x = (0,0,…,0)处有全局极小点0
%Rastrigin = 'sum(x.^2 - 10*cos(2*pi*x) + 10,2)';
%Rastrigin在x = (0,0,…,0)处有全局极小点0
%Rosenbrock = 'sum((1-x(:,1:end-1)).^2 + 100*(x(:,2:end)-x(:,1:end-1).^2).^2,2)';
%Rosenbrock(1,...,1) = 0
%Easom = '-prod(cos(x),2).*exp(-sum((x-pi).^2,2))';
%Easom(pi,...,pi) = -1;
%Bukin = '100*sqrt(abs(x2 - 0.01*x1.^2)) + 0.01*abs(x1 + 10)';
%lb = [-15 -3];ub = [5 3];
%Bukin(-10,1) = 0;
tic
nPop = 5;
popSize = 200;
xFraction = 0.8;
mFraction = 0.1;
mInterval = 50;
nElite = 2;
generation = 100000;
stopStall = 100;
nGenome = length(lb);
nXover = xFraction * popSize;
nMutate = popSize - nElite - nXover;
nParent = nXover + nMutate;
nMigrate = mFraction * popSize;
totalPop = popSize * nPop;
score = zeros(1, popSize);
xover = zeros(nXover, nGenome);
mutate = zeros(nMutate, nGenome);
fval = zeros(1,generation);
fmean = zeros(1,generation);
icomer = zeros(1, mFraction * totalPop);
irep = zeros(1, mFraction * totalPop);
ScalingRank = 1./ sqrt(1:popSize);
ScalingRank = ScalingRank / sum(ScalingRank);
indexVec = [1:nGenome 1:nGenome];
dirSign = [ones(1,nGenome) -ones(1,nGenome)];
iForward = [ones(1,nPop-1) 1-nPop];
pop = repmat(lb,totalPop,1) + rand(totalPop, nGenome).* repmat(ub-lb,totalPop,1);
fitness = fun(pop);
MeshSize = 1;
for g = 1:generation
idx = 1:popSize;
for i = 1:nPop
thisPop = pop(idx,:);
[~, fitsort] = sort(fitness(idx));
score(fitsort) = ScalingRank;
wheel = cumsum(score);
position = (rand + (0:nParent)) / nParent;
[~, iparent] = histc(position(1:end-1), wheel);
iparent = iparent(randperm(nParent));
parent = thisPop(iparent+1,:);
elite = thisPop(fitsort(1:nElite),:);
xparent = parent(1:nXover, :);
[~, ibest] = min(fitness(idx(iparent(1:nXover)+1)));
rd = rand(2, 3, nXover/2);
for imatch = 1:nXover/2
ri = rd(:, :, imatch);
temp = ri * xparent([2*imatch-1, 2*imatch, ibest], :);
xover([2*imatch-1, 2*imatch], :) = bsxfun(@rdivide, temp, sum(ri, 2));
end
mutate = repmat(lb,nMutate,1) + rand(nMutate, nGenome).* repmat(ub-lb,nMutate,1);
pop(idx,:) = [elite; xover; mutate];
fitness(idx) = fun(pop(idx, :));
idx = idx + popSize;
end
if (nPop > 1) && (rem(g, mInterval) == 0)
offset = 0;
idx = 1:nMigrate;
for i = 1:nPop
[~, isort] = sort(fitness(offset+1:offset+popSize));
icomer(idx) = isort(1:nMigrate) + offset;
irep(idx + iForward(i)*nMigrate) = isort(end-nMigrate+1:end) + offset ;
offset = offset + popSize;
idx = idx + nMigrate;
end
pop(irep, :) = pop(icomer, :);
fitness(irep) = fitness(icomer);
end
[CurrentMin, k] = min(fitness);
if g > 1
if fval(g-1) > CurrentMin
x = pop(k,:);
MeshSize = min(1,MeshSize*4);
stall = 0;
else
MeshSize = max(sqrt(eps), MeshSize/4);
stall = stall + 1;
end
fval(g) = CurrentMin;
else
fval(g) = CurrentMin;
x = pop(k,:);
stall = 0;
end
fmean(g) = mean(fitness);
if stall == stopStall
break
end
end
shg
clf reset
set(gcf, 'color','white', 'menubar','none', 'name','Genetic Algorithm')
plot(1:g, [fval(1:g); fmean(1:g)], '.')
fval = fval(g);
legend('Best Fitness','Mean Fitness')
title(['Min=',vectorize(vpa(fval))])
xlabel('Generation')
ylabel('Fitness Value')
shg
toc
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment