-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask_2.m
84 lines (67 loc) · 2.95 KB
/
task_2.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
function task_2()
% Implement Task -- Vehicle Flow Rate = 100 +- 15% (veh/min) = const.
clc
addpath("src/");
% Load Genetic Algorithm paramters
[f, ~, chromosome_length, initial_population_size, max_generations, elitism_percentage, ...
roulette_wheel_percentage, mutation_probability, upper_x, lower_x, v_mean, v_deviation, h_const, g] = load_data();
% Execute the Genetic Algorithm and store the solutions
executions_num = 7;
solutions = NaN(executions_num, chromosome_length);
for i=1 : executions_num
[solutions(i,:), stats] = genetic_algorithm_adaptive(f, chromosome_length, initial_population_size, max_generations, ...
elitism_percentage, roulette_wheel_percentage, mutation_probability, upper_x, lower_x, v_mean, v_deviation, h_const, g);
tmp = stats{2, 1};
solutions_stats(i, 1) = tmp(end);
% Plot the fitness scores as the population evolves
figure(1);
title('Fitness Scores as Generations Evolve');
x_axis = linspace(0, max_generations, max_generations);
y_axis = transpose(tmp(:,:));
plot(x_axis, y_axis);
legend_str{i} = sprintf('Execution %d', i);
legend(legend_str);
hold all;
drawnow;
tmp = stats{3, 1};
solutions_stats(i, 2) = tmp(end);
% Plot the constraints violations as the population evolves
figure(2);
title('Constraints Violations as Generations Evolve');
y_axis = transpose(tmp(:,:));
plot(x_axis, y_axis);
legend_str{i} = sprintf('Execution %d', i);
legend(legend_str);
hold all;
drawnow;
tmp = stats{4, 1};
solutions_stats(i, 3) = tmp(end);
% Plot the minima calculations as the population evolves
figure(3);
title('Minima of Objective Function as Generations Evolve');
y_axis = transpose(tmp(:,:));
plot(x_axis, y_axis);
legend_str{i} = sprintf('Execution %d', i);
legend(legend_str);
hold all;
drawnow;
end
% Save the plots
current_project = pwd;
file_dest = strcat(current_project, '/plots/v_changing_fitness_scores', '.png');
saveas(figure(1), file_dest);
file_dest = strcat(current_project, '/plots/v_changing_constraints_violations', '.png');
saveas(figure(2), file_dest);
file_dest = strcat(current_project, '/plots/v_changing_minima', '.png');
saveas(figure(3), file_dest);
% Display the solutions of all of the algorithm executions
fprintf("Solutions:\n");
disp(solutions);
for i=1 : executions_num
fprintf("Solution %d|\tFitness Score: %.6f\tMSE of Constraints Violation: %.6f\tMinimum: %.6f\n", i, solutions_stats(i, 1), solutions_stats(i, 2), solutions_stats(i, 3));
end
% Calculate the mean of all of the solutions to give a final answer
mean_solution = mean(solutions, 1);
fprintf("A mean value solution:\n");
disp(mean_solution);
end