-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSED_code_LS_2.txt
More file actions
100 lines (87 loc) · 2.59 KB
/
SED_code_LS_2.txt
File metadata and controls
100 lines (87 loc) · 2.59 KB
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
% Initialization
%model = 'katup_zn.slx';
model = 'fullmodel_IPC_A_bintang_ps_regulated.slx';
% Simulation configurations
dt = 0.001;
% !!! Define LOG bounds for Kp, Ki, Kd
%
Kp_min = log10(0.01); Kp_max = log10(3);
Ki_min = -6; Ki_max = log10(0.1);
Kd_min = -6; Kd_max = log10(0.1);
%}
% !!! Define step size Kg and exploration probability
Kg = 0.05;
E = 0.8;
% Initialize LOG params w random values within bounds
Kp_log = log10(0.03);
Ki_log = log10(0.00001);
Kd_log = log10(0.0044);
% Define best params and best obj func value
Kp_best = 10^Kp_log;
Ki_best = 10^Ki_log;
Kd_best = 10^Kd_log;
% Arrays to store data
iterations = [];
current_f = [];
best_f = [];
r1_k = [];
% SED optimization
Kp = 10^Kp_log;
Ki = 10^Ki_log;
Kd = 10^Kd_log;
f_best = objectiveFunction(model, Kp, Ki, Kd);
% Optimization loop
k_max = 100; % !!!
for k = 1:k_max
% Evaluate obj func w current params
f_current = objectiveFunction(model, Kp, Ki, Kd)
current_f = [current_f; f_current];
% Check if the current params provide better solution
if f_current < f_best
Kp_best = Kp;
Ki_best = Ki;
Kd_best = Kd;
f_best = f_current;
end
% Generate random number r1 to decide exploration or exploitation
r1 = rand() % Between 0 and 1
r1_k = [r1_k; r1];
if r1 < E
% Exploration: generate new random params
r2_Kp = Kp_min + (Kp_max - Kp_min)*rand();
r2_Ki = Ki_min + (Ki_max - Ki_min)*rand();
r2_Kd = Kd_min + (Kd_max - Kd_min)*rand();
% Update params w random exploration
Kp_log = h(log10(Kp) - Kg*r2_Kp, Kp_min, Kp_max);
Ki_log = h(log10(Ki) - Kg*r2_Ki, Ki_min, Ki_max);
Kd_log = h(log10(Kd) - Kg*r2_Kd, Kd_min, Kd_max);
else
% Exploitation using best known params
Kp_log = log10(Kp_best);
Ki_log = log10(Ki_best);
Kd_log = log10(Kd_best);
end
% Evaluate obj func w new params
Kp = 10^Kp_log;
Ki = 10^Ki_log;
Kd = 10^Kd_log;
f_new = objectiveFunction(model, Kp, Ki, Kd);
% Update best solution if the new one is better
if f_new < f_best
Kp_best = Kp;
Ki_best = Ki;
Kd_best = Kd;
f_best = f_new;
end
best_f = [best_f; f_best];
iterations = [iterations; k];
end
% Output the best found params
fprintf('Optimized PID parameters:\n');
fprintf('Kp: %f\n', Kp_best);
fprintf('Ki: %f\n', Ki_best);
fprintf('Kd: %f\n', Kd_best);
% Tabled data
dataSED = table(iterations, current_f, best_f, r1_k)
%writetable(dataSED,'HORE_katup_step_SED_100.xlsx','Sheet',1)
writetable(dataSED,'HORE_IPC_step_SED_100.xlsx','Sheet',6)