-
Notifications
You must be signed in to change notification settings - Fork 0
/
FunctionOptimization.m
78 lines (59 loc) · 2.07 KB
/
FunctionOptimization.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
clear;
clc;
populationSize = 100;
numberOfGenes = 50;
crossoverProbability = 0.8;
mutationProbability = 0.02;
tournamentSelectionParameter = 0.75;
variableRange = 10.0;
numberOfGenerations = 100;
fitness = zeros(populationSize,1);
tournamentSize = 2;
nVariables = 2;
nCopies = 1;
population = InitializePopulation(populationSize, numberOfGenes);
for iGeneration = 1:numberOfGenerations
maximumFitness = 0.0;
xBest = zeros(1,2);
bestIndividualIndex = 0;
for i = 1:populationSize
chromosome = population(i,:);
x = DecodeChromosome(chromosome,nVariables, variableRange);
fitness(i) = EvaluateIndividual(x);
if (fitness(i) > maximumFitness)
maximumFitness = fitness(i);
bestIndividualIndex = i;
xBest = x;
end
end
tempPopulation = population;
for i = 1:2:populationSize
i1 = TournamentSelect(fitness,tournamentSelectionParameter,tournamentSize);
i2 = TournamentSelect(fitness,tournamentSelectionParameter,tournamentSize);
chromosome1 = population(i1,:);
chromosome2 = population(i2,:);
r = rand;
if (r < crossoverProbability)
newChromosomePair = Cross(chromosome1,chromosome2);
tempPopulation(i,:) = newChromosomePair(1,:);
tempPopulation(i+1,:) = newChromosomePair(2,:);
else
tempPopulation(i,:) = chromosome1;
tempPopulation(i+1,:) = chromosome2;
end
end
for i = 1:populationSize
originalChromosome = tempPopulation(i,:);
mutatedChromosome = Mutate(originalChromosome,mutationProbability);
tempPopulation(i,:) = mutatedChromosome;
end
bestIndividual = population(bestIndividualIndex,:);
population = InsertBestIndividual(tempPopulation,bestIndividual,nCopies);
end
functionValue=1/maximumFitness;
% Print final result
format short;
disp('xBest');
disp(xBest);
disp('Function Value');
disp(functionValue);