-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetaheuristic_gpu.m
85 lines (75 loc) · 3.3 KB
/
metaheuristic_gpu.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
classdef metaheuristic_gpu < metaheuristic
% 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.
methods
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 = gpuArray.zeros(self.maxNoIterations, self.noDimensions);
self.historicBestFitness = gpuArray.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;
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)
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 = gpuArray.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)
if nargin == 1
population = self.population;
end
fit = self.fitnessFunction(population);
if nargin == 1
self.fitness = fit;
end
self.numberOfFunctionCalls=self.numberOfFunctionCalls+size(population,1);
end
function rp = getShuffledPopulation(self)
randIndexing = gpuArray.randperm(self.sizePopulation);
rp = self.population(randIndexing, :);
end
end
end