-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.m
70 lines (66 loc) · 1.87 KB
/
main.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
clc;
% initial parameter
global N_INDIVIDUAL;
global N_VAR;
global REAL_VALUE;
global LB;
global UB;
global I_GENERATION;
global GLOBAL_BEST_FITNESS;
global BEST_FITNESS;
global BEST_X;
global AVG_FITNESS;
global MAX_FITNESS;
global MIN_FITNESS;
BEST_FITNESS = [];
BEST_X = [];
AVG_FITNESS = [];
MAX_FITNESS = [];
MIN_FITNESS = [];
global MUTATION_PROBABILITY;
global CROSSOVER_PROBABILITY;
global SEARCH_METHOD;
SEARCH_METHOD = 'MAX';
if SEARCH_METHOD == 'MAX'
GLOBAL_BEST_FITNESS = -9999999;
elseif SEARCH_METHOD == 'MIN'
GLOBAL_BEST_FITNESS = 9999999;
end
N_INDIVIDUAL = 16; % individu per populasi
REAL_VALUE = 4; % 4 angka di belakang koma. semakin kecil jumlah gen pada kromosom juga akan semakin kecil
MUTATION_PROBABILITY = 0.1;
CROSSOVER_PROBABILITY = 0.8;
% diketahui untuk mencari nilai maksimum f(x1, x2) = 10.4+ x1 *
% sin(3*pi*x1) + x2 * sin(13*pi*x2) dimana
% -2 <= x1 <= 15.5 dan 3.27 <= x2 <= 8.75
UB = [15.5, 8.75]; % upper bound
LB = [-2 3.27]; % lower bound
N_VAR = length(UB); % 2 variabel, x1 dan x2
N_GENERATION = 100;
individual = init_population();
for i=1 : N_GENERATION
I_GENERATION = i;
fitness(individual);
parent = selection(individual);
%individual
new_individual = crossover(individual, parent);
%new_individual
individual = mutation(new_individual);
end
plot(BEST_FITNESS, 'Color', 'red', 'linewidth', 2);
hold on;
plot(MAX_FITNESS, 'Color', 'green');
plot(AVG_FITNESS, 'Color', 'blue');
plot(MIN_FITNESS, 'Color', 'black');
title('Best, Max, Mean, Min Fitness', 'fontweight', 'bold');
xlabel('Generation');
ylabel('f(x(i))');
legend('Best', 'Maximum', 'Mean', 'Minimum', 'Location', 'Southoutside', 'Orientation', 'Horizontal');
hold off;
disp('Best x(i)');
disp(BEST_X);
disp('Best Fitness Value');
disp(BEST_FITNESS(I_GENERATION));
%disp(MAX_FITNESS(I_GENERATION));
disp(AVG_FITNESS(I_GENERATION));
%disp(MIN_FITNESS(I_GENERATION));