-
Notifications
You must be signed in to change notification settings - Fork 0
/
bga.m
147 lines (135 loc) · 5.07 KB
/
bga.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
% Binary Genetic Algorithm
%
% minimizes the objective function designated in ff
% Before beginning, set all the parameters in parts I, II, and III
% Haupt & Haupt
% 2003
clear
%_______________________________________________________
% I. Setup the GA
ff='make_presents'; % objective function
n_friends=8;
npar=2*n_friends-1; % number of optimization variables
varhi=.5; varlo=n_friends+0.4999; % variable limits
%_______________________________________________________
% II. Stopping criteria
maxit=5000; % max number of iterations
mincost=-9999999; % minimum cost
%_______________________________________________________
% III. GA parameters
popsize=12; % set population size
mutrate=0.05; % set mutation rate
selection=0.5; % fraction of population kept
nbits=4; % number of bits in each parameter
Nt=nbits*npar; % total number of bits in a chormosome
keep=floor(selection*popsize); % #population members that survive
best_costs=zeros(10,1);
global neighbours
load('neighbours.mat')
if length(neighbours)~=n_friends
error('There is a mismathc between the topology of social interaction and the problem: Please recreate the social interactions')
end
%--------------------here iteration starts---------
for iteration=1:10
tic
%_______________________________________________________
% Create the initial population
iga=0; % generation counter initialized
pop=round(rand(popsize,Nt)); % random population of 1s and 0s
par=gadecode(pop,varlo,varhi,nbits); % convert binary to continuous values
%cost=feval(ff,par); % calculates population
[rr, cc] =size(par);
for ii=1:rr
cost(ii) = feval(ff,par(ii,:));
end
% cost using ff
[cost,ind]=sort(cost); % min cost in element 1
par=par(ind,:);pop=pop(ind,:); % sorts population with lowest cost first
minc(1)=min(cost); % minc contains min of population
meanc(1)=mean(cost); % meanc contains mean of population
%_______________________________________________________
% Iterate through generations
while iga<maxit
iga=iga+1; % increments generation counter
%_______________________________________________________
% Pair and mate
M=ceil((popsize-keep)/2); % number of matings
prob=flipud([1:keep]'/sum([1:keep]));% weights chromosomes based upon position in list
odds=[0 cumsum(prob(1:keep))']; % probability distribution function
pick1=rand(1,M); % mate #1
pick2=rand(1,M); % mate #2
% ma and pa contain the indicies of the chromosomes that will mate
ic=1;
while ic<=M
for id=2:keep+1
if pick1(ic)<=odds(id) & pick1(ic)>odds(id-1)
ma(ic)=id-1;
end % if
if pick2(ic)<=odds(id) & pick2(ic)>odds(id-1)
pa(ic)=id-1;
end % if
end % id
ic=ic+1;
end % while
%_______________________________________________________
% Performs mating using single point crossover
ix=1:2:keep; % index of mate #1
xp=ceil(rand(1,M)*(Nt-1)); % crossover point
pop(keep+ix,:)=[pop(ma,1:xp) pop(pa,xp+1:Nt)];
% first offspring
pop(keep+ix+1,:)=[pop(pa,1:xp) pop(ma,xp+1:Nt)];
% second offspring
%_______________________________________________________
% Mutate the population
nmut=ceil((popsize-1)*Nt*mutrate); % total number
% of mutations
mrow=ceil(rand(1,nmut)*(popsize-1))+1; % row to mutate
mcol=ceil(rand(1,nmut)*Nt); % column to mutate
for ii=1:nmut
pop(mrow(ii),mcol(ii))=abs(pop(mrow(ii),mcol(ii))-1);
% toggles bits
end % ii
%_______________________________________________________
% The population is re-evaluated for cost
par(2:popsize,:)=gadecode(pop(2:popsize,:),varlo,varhi,nbits); % decode
% cost(2:popsize)=feval(ff,par(2:popsize,:));
[rr, cc] =size(par);
for ii=2:rr
cost(ii) = feval(ff,par(ii,:));
end
%_______________________________________________________
% Sort the costs and associated parameters
[cost,ind]=sort(cost); % min cost in element 1
par=par(ind,:);pop=pop(ind,:); % sorts population with
% Do statistics for a single nonaveraging run
minc(iga+1)=min(cost);
meanc(iga+1)=mean(cost);
%_______________________________________________________
% Stopping criteria
if iga>maxit | cost(1)<mincost
break
end
[iga cost(1)];
end %iga
best_costs(iteration)=cost(1);
toc
end %-------iteration ends-------
%_______________________________________________________
% Displays the output
day=clock;
disp(datestr(datenum(day(1),day(2),day(3),day(4),day(5), day(6)),0))
disp(['optimized function is ' ff])
format short g
disp(['popsize = ' num2str(popsize) ' mutrate = ' num2str(mutrate) ' # par = ' num2str(npar)])
disp(['#generations=' num2str(iga) ' best cost=' num2str(cost(1))])
disp(['best solution'])
disp([num2str(par(1,:))])
disp('binary genetic algorithm')
disp(['each parameter represented by ' num2str(nbits) ' bits'])
figure(24)
iters=0:length(minc)-1;
plot(iters,minc,iters,meanc,'-');
xlabel('generation');ylabel('cost');
title(['Binary Genetic Algorithm; Function: ' ff]);
%text(0,minc(1),'best');text(1,minc(2),'population average')
legend('best', 'population average');