-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetaheuristic.m
317 lines (292 loc) · 11.6 KB
/
metaheuristic.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
classdef metaheuristic < handle
% Metaheursitic Algorithms by aeroreyna
% This implementation always considers a minimization problems
% and that all the variables have a range [0-1], the proper
% adjustments has to be done in the fitness function.
properties
sizePopulation = 30;
noDimensions = 1;
population = []; %size = (sizePop, NoDim)
fitness = []; %size = (sizePop, 1)
bestSolution = [];
bestFitness = inf;
worstFitness = -inf;
worstSolution = [];
fitnessFunction;
numberOfFunctionCalls = 0;
maxNoIterations = 100;
actualIteration = 0;
initialSolutions = [];
stripFitnessCalls = false;
end
%this properties are for data visualization
properties
historicBestSolution = []; %Keeps track of best solutions
historicBestFitness = []; %Keeps track of best fitness
eachIterationFunction; %If defined, it's called at the end of each iteration
customPlotFunction; %If defined, it's called instead of default plot function
plotEachIterationB = false; %Refresh plot at each iteration
plotPopulationB = false; %Plot the actual population
plotBestSolutionB = false; %Plot the actual best solution
plotHistoricB = true; %Plot the historic record
X; %Stores the plot data X
Y; %Stores the plot data Y
Z; %Stores the plot data Z
handleHistoricPlot;
handlePopulationPlot;
improvementsCount = 0;
bestSolutionChanges = 0; %No of improvements
bestSolutionImprovers = []; %Record of improvers
saveRecordOnline = false;
algorithmName = 'Unregistered';
end
properties (Access = private)
onlineObj = 0
end
methods (Abstract)
operators(self)
end
methods
function self = metaheuristic(fitnessFunction, noDimensions)
if nargin < 1
return
end
self.fitnessFunction = fitnessFunction;
self.noDimensions = noDimensions;
self.numberOfFunctionCalls=0;
end
function bestSolution = start(self)
if size(self.fitnessFunction,1) == 0
error('There is no fitness function attached to this process');
end
if size(self.population,1) == 0
self.initialPopulation();
self.evalPopulation();
end
if size(self.fitness,1) == 0
self.evalPopulation();
end
self.historicBestSolution = zeros(self.maxNoIterations, self.noDimensions);
self.historicBestFitness = zeros(self.maxNoIterations, 1);
self.updateBest();
for i=1:self.maxNoIterations
self.actualIteration = i;
self.operators();
self.updateBest();
self.historicBestSolution(i,:) = self.bestSolution;
self.historicBestFitness(i,:) = self.bestFitness;
%data visualization
%disp(i)
if self.plotEachIterationB == true
if size(self.customPlotFunction,1)~=0
self.customPlotFunction(self);
else
self.plot();
end
end
if size(self.eachIterationFunction,1)~=0
self.eachIterationFunction(self);
end
end
%Returns the best solution with proper scale by the FitnessF.
[~, bestSolution] = self.fitnessFunction(self.bestSolution);
end
function initialPopulation(self, sizePopulation, noDimensions)
% Method that start a new random population, it could use the
% selfect propierties, or well assing new properties of
% population size and/or number of dimensions.
if nargin == 1
sizePopulation = self.sizePopulation;
noDimensions = self.noDimensions;
elseif nargin == 2
self.sizePopulation = sizePopulation;
noDimensions = self.noDimensions;
else
self.sizePopulation = sizePopulation;
self.noDimensions = noDimensions;
end
self.population = rand(sizePopulation, noDimensions);
if size(self.initialSolutions,1) ~= 0
if size(self.initialSolutions,2) ~= self.noDimensions || size(self.initialSolutions,1) > self.sizePopulation
error('Initial custom population do not have the right dimensions');
end
self.population(1:size(self.initialSolutions,1),:) = self.initialSolutions;
end
end
function fit = evalPopulation(self, population)
% This method evual the fitness of the population, or well the
% solution pass thorught the argument. If no argument it send
% then the fitness of all the population is store in the
% fitness propertie of this selfect.
if nargin == 1
population = self.population;
end
if self.stripFitnessCalls
fit = zeros(size(population,1),1);
for i=1:size(population,1)
fit(i) = self.fitnessFunction(population(i,:));
end
else
fit = self.fitnessFunction(population);
end
if nargin == 1
self.fitness = fit;
end
self.numberOfFunctionCalls=self.numberOfFunctionCalls+size(population,1);
end
function rp = getShuffledPopulation(self)
randIndexing = randperm(self.sizePopulation);
rp = self.population(randIndexing, :);
end
function bestArray = bestSolutionArray(self)
bestArray = repmat(self.bestSolution, self.sizePopulation, 1);
end
function sortPopulation(self)
% Sort the population using the fitness value, the order is
% assendent. Sort the fitness array as well.
temp = sortrows([self.fitness, self.population], 1);
self.fitness = temp(:,1);
self.population = temp(:,2:end);
end
function solutions = checkBounds(self, solutions)
if nargin == 1
solutions = self.population;
end
solutions(solutions>1)=1;
solutions(solutions<0)=0;
if nargin == 1
self.population = solutions;
end
end
function solutions = checkBoundsToroidal(self, solutions)
if nargin == 1
solutions = self.population;
end
solutions = solutions - floor(solutions);
if nargin == 1
self.population = solutions;
end
end
function updateBest(self)
% update the best know so far solution and it's fitness.
[bestFitTemp, bestIndex] = min(self.fitness);
if bestFitTemp < self.bestFitness
self.bestSolution = self.population(bestIndex,:);
self.bestFitness = bestFitTemp;
self.bestSolutionChanges = self.bestSolutionChanges + 1;
self.bestSolutionImprovers = [self.bestSolutionImprovers; self.bestSolution];
end
end
function updateWorst(self)
% update the best know so far solution and it's fitness.
[worstFitTemp, bestIndex] = max(self.fitness);
if worstFitTemp > self.worstFitness
self.worstFitness = worstFitTemp;
self.worstSolution = self.population(bestIndex,:);
end
end
function r = diversity(self, solutions)
%Function that messure the diversity of the population.
if nargin == 1
solutions = self.population;
end
n = size(solutions,1);
r = 0;
for i = 1:n-1
for j = i+1:n
r = r + norm(solutions(i,:)-solutions(j,:));
end
end
r = 2 * r / (n*(n-1));
end
function plot(self)
if self.plotHistoricB == true
if size(self.handleHistoricPlot,1) == 0
self.handleHistoricPlot = figure;
end
figure(self.handleHistoricPlot.Number);
hold off
plot(1:self.actualIteration, self.historicBestFitness(1:self.actualIteration));
end
if self.plotPopulationB == true
if size(self.handlePopulationPlot,1) == 0
self.handlePopulationPlot = figure;
end
figure(self.handlePopulationPlot.Number);
hold off
self.graph2d()
hold on
self.plotSolutions(self.population);
end
if self.plotBestSolutionB == true
if size(self.handlePopulationPlot,1) == 0
self.handlePopulationPlot = figure;
self.graph2d()
hold on
end
self.plotSolutions(self.bestSolution,'or');
end
drawnow
end
function plotSolutions(~, solutions, properties)
if size(solutions,2) == 1
error('only two dimensions are allow to plot')
end
if size(solutions,2) > 2
solutions = solutions(:,1:2);
end
if nargin == 2
properties='ob';
end
plot(solutions(:,1),solutions(:,2),properties);
end
function [X,Y,Z] = getGraphData(self, fitnessFunc)
if size(self.X,1) == 0 || size(self.Y,1) == 0 || size(self.Z,1) == 0
x=0:1/100:1;
y=x;
[X,Y]=meshgrid(x,y);
[row,col]=size(X);
Z = zeros(row,col);
for l=1:col
for h=1:row
Z(h,l)=fitnessFunc([X(h,l),Y(h,l)]);
end
end
self.X = X;
self.Y = Y;
self.Z = Z;
else
X = self.X;
Y = self.Y;
Z = self.Z;
end
end
function graph3d(self,fitnessFunc)
if nargin == 1
if size(self.fitnessFunction,1) == 0
error('Fitness function is empty');
end
fitnessFunc = self.fitnessFunction;
end
[x,y,z] = self.getGraphData(fitnessFunc);
mesh(x,y,z);
end
function graph2d(self,fitnessFunc)
if nargin == 1
if size(self.fitnessFunction,1) == 0
error('Fitness function is empty');
end
fitnessFunc = self.fitnessFunction;
end
[x,y,z] = self.getGraphData(fitnessFunc);
contour(x,y,z,4);
end
function plotHistoricSolutions(self)
figure
self.graph2d();
hold on;
self.plotSolutions(self.historicBestSolution)
self.plotSolutions(self.historicBestSolution, '')
end
end
end