Skip to content

Commit 9369f2c

Browse files
authored
Add files via upload
Continuos Genetic algorithm.
1 parent 591a992 commit 9369f2c

File tree

4 files changed

+195
-0
lines changed

4 files changed

+195
-0
lines changed

cga.m

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
% Continuous Genetic Algorithm
2+
%
3+
% minimizes the objective function designated in ff
4+
% Before beginning, set all the parameters in parts
5+
% I, II, and III
6+
% Haupt & Haupt
7+
% 2003
8+
clear
9+
%_______________________________________________________
10+
% I Setup the GA
11+
ff='scales'; % objective function
12+
npar=10; % number of optimization variables
13+
varhi=pi; varlo=0; % variable limits
14+
%_______________________________________________________
15+
% II Stopping criteria
16+
maxit=10; % max number of iterations
17+
mincost=-9999999; % minimum cost
18+
%_______________________________________________________
19+
% III GA parameters
20+
popsize=100; % set population size
21+
mutrate=0.9; % set mutation rate
22+
selection=0.5; % fraction of population kept
23+
Nt=npar; % continuous parameter GA Nt=#variables
24+
keep=floor(selection*popsize); % #population members that survive
25+
nmut=ceil((popsize-1)*Nt*mutrate); % total number of mutations
26+
M=ceil((popsize-keep)/2); % number of matings
27+
%_______________________________________________________
28+
% Create the initial population
29+
iga=0; % generation counter initialized
30+
par=(varhi-varlo)*rand(popsize,npar)+varlo; % random
31+
32+
33+
34+
35+
cost=scales(par); % calculates population cost using ff
36+
[rr, cc] =size(par);
37+
for ii=1:rr
38+
cost(ii) = scales(par(ii,:));
39+
end
40+
41+
42+
43+
44+
[cost,ind]=sort(cost); % min cost in element 1
45+
par=par(ind,:); % sort continuous
46+
minc(1)=min(cost); % minc contains min of
47+
meanc(1)=mean(cost); % meanc contains mean of population
48+
%_______________________________________________________
49+
% Iterate through generations
50+
while iga<maxit
51+
iga=iga+1; % increments generation counter
52+
%_______________________________________________________
53+
% Pair and mate
54+
M=ceil((popsize-keep)/2); % number of matings
55+
prob=flipud([1:keep]'/sum([1:keep])); % weights chromosomes
56+
odds=[0 cumsum(prob(1:keep))']; % probability distribution function
57+
pick1=rand(1,M); % mate #1
58+
pick2=rand(1,M); % mate #2
59+
% ma and pa contain the indicies of the chromosomes that will mate
60+
ic=1;
61+
while ic<=M
62+
for id=2:keep+1
63+
if pick1(ic)<=odds(id) & pick1(ic)>odds(id-1)
64+
ma(ic)=id-1;
65+
end
66+
if pick2(ic)<=odds(id) & pick2(ic)>odds(id-1)
67+
pa(ic)=id-1;
68+
end
69+
end
70+
ic=ic+1;
71+
end
72+
%_______________________________________________________
73+
% Performs mating using single point crossover
74+
ix=1:2:keep; % index of mate #1
75+
xp=ceil(rand(1,M)*Nt); % crossover point
76+
r=rand(1,M); % mixing parameter
77+
for ic=1:M
78+
xy=par(ma(ic),xp(ic))-par(pa(ic),xp(ic)); % ma and pa
79+
% mate
80+
par(keep+ix(ic),:)=par(ma(ic),:); % 1st offspring
81+
par(keep+ix(ic)+1,:)=par(pa(ic),:); % 2nd offspring
82+
par(keep+ix(ic),xp(ic))=par(ma(ic),xp(ic))-r(ic).*xy;
83+
% 1st
84+
par(keep+ix(ic)+1,xp(ic))=par(pa(ic),xp(ic))+r(ic).*xy;
85+
% 2nd
86+
if xp(ic)<npar % crossover when last variable not selected
87+
par(keep+ix(ic),:)=[par(keep+ix(ic),1:xp(ic)) par(keep+ix(ic)+1,xp(ic)+1:npar)];
88+
par(keep+ix(ic)+1,:)=[par(keep+ix(ic)+1,1:xp(ic)) par(keep+ix(ic),xp(ic)+1:npar)];
89+
end % if
90+
end
91+
%_______________________________________________________
92+
% Mutate the population
93+
mrow=sort(ceil(rand(1,nmut)*(popsize-1))+1);
94+
mcol=ceil(rand(1,nmut)*Nt);
95+
for ii=1:nmut
96+
par(mrow(ii),mcol(ii))=(varhi-varlo)*rand+varlo;
97+
% mutation
98+
end % ii
99+
%_______________________________________________________
100+
% The new offspring and mutated chromosomes are evaluated
101+
%cost=feval(ff,par);
102+
[rr, cc] =size(par);
103+
for ii=2:rr
104+
cost(ii) = feval(ff,par(ii,:));
105+
end
106+
107+
108+
109+
110+
111+
%_______________________________________________________
112+
% Sort the costs and associated parameters
113+
[cost,ind]=sort(cost);
114+
par=par(ind,:);
115+
%_______________________________________________________
116+
% Do statistics for a single nonaveraging run
117+
minc(iga+1)=min(cost);
118+
meanc(iga+1)=mean(cost);
119+
%_______________________________________________________
120+
% Stopping criteria
121+
if iga>maxit | cost(1)<mincost
122+
break
123+
end
124+
[iga cost(1)]
125+
end %iga
126+
%_______________________________________________________
127+
% Displays the output
128+
day=clock;
129+
disp(datestr(datenum(day(1),day(2),day(3),day(4),day(5),day(6)),0))
130+
disp(['optimized function is ' ff])
131+
format short g
132+
disp(['popsize = ' num2str(popsize) ' mutrate = ' num2str(mutrate) ' # par = ' num2str(npar)])
133+
disp(['#generations=' num2str(iga) ' best cost=' num2str(cost(1))])
134+
disp(['best solution'])
135+
disp([num2str(par(1,:))])
136+
disp('continuous genetic algorithm')
137+
figure(24)
138+
iters=0:length(minc)-1;
139+
plot(iters,minc,iters,meanc,'-');
140+
xlabel('generation');ylabel('cost');
141+
title(['Continuous Genetic Algorithm; Function: ' ff]);
142+
%text(0,minc(1),'best');text(1,minc(2),'population average')
143+
legend('best', 'population average');

circlesinasquare.m

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
function f = circlesinasquare(xy)
2+
3+
x1=xy(1);
4+
y1=xy(2);
5+
6+
x2=xy(3);
7+
y2=xy(4);
8+
9+
x3=xy(5);
10+
y3=xy(6);
11+
12+
x4=xy(7);
13+
y4=xy(8);
14+
15+
d12 = sqrt((x1-x2)^2 + (y1-y2)^2);
16+
d13 = sqrt((x1-x3)^2 + (y1-y3)^2);
17+
d14 = sqrt((x1-x4)^2 + (y1-y4)^2);
18+
19+
d23 = sqrt((x2-x3)^2 + (y2-y3)^2);
20+
d24 = sqrt((x2-x4)^2 + (y2-y4)^2);
21+
22+
d34 = sqrt((x3-x4)^2 + (y3-y4)^2);
23+
24+
f = -(d12 + d13 + d14 + d23 + d24 + d34);
25+
26+
% f = 1/(d12 + d13 + d14 + d23 + d24 + d34);

eigenvalues.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
function f = eigenvalues(x)
2+
A = [1 2 3; 4 15 6; 7 8 19];
3+
[r, c] = size(A);
4+
f = abs(det(A - x*eye(r))); % + abs(1/(x + 0.20075));

scales.m

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
function cost=scales(s)
3+
w=(1:100);
4+
5+
cost=0;
6+
7+
lhs=0;
8+
9+
rhs=0;
10+
11+
for i=1:w
12+
if s(i) == 0;
13+
lhs=lhs+w(i);
14+
else
15+
rhs=rhs+w(i);
16+
end
17+
18+
cost=abs(lhs-rhs);
19+
end
20+
21+
22+

0 commit comments

Comments
 (0)