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' );
0 commit comments