-
Notifications
You must be signed in to change notification settings - Fork 15
/
main_script.m
261 lines (199 loc) · 7.77 KB
/
main_script.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
%% Multi-Objective Optimal Operation (M3O) Toolbox
%
% This toolbox purpose is to design the optimal operations of multipurpose
% water reservoir system, in an attempt to close the gap between research
% studies and real-world applications. The toolbox, called Multi-Objective
% Optimal Operations (M3O), allows users to design Pareto optimal (or
% approximate) operating policies through several alternative state-of-the-art
% methods. The application of all these techniques on the same case study
% contributes a step-forward with respect to traditional literature review
% papers as the availability of the source code, along with the possibility
% of cross-comparing the results on the same problem, allows a better
% understanding of pros and cons of each approach. At the same time, the
% modular structure of M3O allows experienced users to easily customize, and
% possibly further develop, the implemented code according to their specific
% requirements.
clear all;
clc
global sys_param;
addpath(genpath('sim'))
addpath(genpath('lib'))
%% Configure general system parameters
sys_param.simulation.q = load('inflow.txt','-ascii') ;
sys_param.simulation.h_in = 0.6 ;
sys_param.simulation.w = 370 ; % irrigation demand
sys_param.simulation.hFLO = 0.8 ; % m the flooding threshold
sys_param.simulation.h0 = -0.5;
sys_param.simulation.A = 145900000;
sys_param.simulation.r_min = 0;
sys_param.simulation.r_max = 518;
sys_param.simulation.delta = 60*60*24;
[sys_param.simulation.vv, sys_param.simulation.VV] = deal(0);
%% --- Run DDP (Deterministic Dynamic Programming) ---
clc
addpath('./DDP')
% Configure the parameters
load 'grids.mat';
sys_param.algorithm = grids;
sys_param.algorithm.name = 'ddp';
sys_param.algorithm.Hend = 0 ; % penalty set to 0
[vv, VV] = construct_rel_matrices(grids); % compute daily minimum/maximum release matrixes
sys_param.algorithm.min_rel = vv;
sys_param.algorithm.max_rel = VV;
% weights for aggregation of objectives
lambda = [1 0; .75 .25; .5 .5 ; .35 .65; .2 .8; .1 .9; 0 1];
Nalt = size(lambda,1);
JJ_ddp = nan(Nalt,2);
Hddp = cell(Nalt,1);
for i = 1: Nalt
sys_param.algorithm.weights = lambda(i,:);
[JJ_ddp(i,:), Hddp{i}] = run_ddp() ;
end
figure; plot( JJ_ddp(:,1), JJ_ddp(:,2), 'o' );
xlabel('flooding'); ylabel('irrigation');
%% --- Run SDP (Stochastic Dynamic Programming) ---
clc
addpath('./SDP')
% Configure the parameters
load 'grids.mat';
sys_param.algorithm = grids;
sys_param.algorithm.name = 'sdp';
sys_param.algorithm.Hend = 0 ; % penalty set to 0
sys_param.algorithm.T = 1 ; % the period is equal 1 as we assume stationary conditions
[vv, VV] = construct_rel_matrices(grids); % compute daily minimum/maximum release matrixes
sys_param.algorithm.min_rel = vv;
sys_param.algorithm.max_rel = VV;
% Estimate inflow probability density function assuming log-normal
% distribution fitting
sys_param.algorithm.q_stat = lognfit(sys_param.simulation.q);
sys_param.algorithm.gamma = 1; % set future discount factor
lambda = [1 0; .75 .25; .5 .5 ; .35 .65; .2 .8; .1 .9; 0 1];
Nalt = size(lambda,1);
JJ_sdp = nan(Nalt,2);
Hsdp = cell(Nalt, 1);
for i = 1: Nalt
sys_param.algorithm.weights = lambda(i,:);
[JJ_sdp(i,:), Hsdp{i}] = run_sdp();
end
figure; plot( JJ_sdp(:,1), JJ_sdp(:,2), 'o' );
xlabel('flooding'); ylabel('irrigation');
%% --- Run EMODPS (Evolutionary Multi-Objective Direct Policy Search) ---
clc
addpath('./EMODPS')
% Define the parameterized class for the policy (i.e., standard operating
% policy)
sys_param.algorithm.name = 'emodps' ;
pClass = 'stdOP';
% Define MOEA and its setting (i.e., NSGAII)
moea_param.name = 'NSGAII';
moea_param.pop = 40; % number of individuals
moea_param.gen = 50; % number of generation
[JJ_emodps, Popt] = run_emodps(pClass, moea_param) ;
figure; plot( JJ_emodps(:,1), JJ_emodps(:,2), 'o' );
xlabel('flooding'); ylabel('irrigation');
%% --- Run FQI (Fitted Q-Iteration) ---
clc
addpath('./FQI')
load 'grids.mat';
sys_param.algorithm = grids;
% Construction of sample dataset by Monte Carlo simulation
sys_param.algorithm.name = 'doe';
[F, G] = run_doe(50) ; % create 50 samples of tuples
% Aggregation of the immediate costs
lambda = [1 0; .75 .25; .5 .5 ; .35 .65; .2 .8; .1 .9; 0 1];
Nalt = size(lambda,1);
JJ_fqi = nan(Nalt, 2);
Qfqi = cell(Nalt, 1);
% Define regressor and its parameters (i.e. extra-trees).
reg_param.name = 'ET';
reg_param.M = 200; % number of trees
reg_param.nmin = 25; % minimum number of points per leaf
reg_param.maxIter = 40;
sys_param.algorithm.name = 'fqi';
sys_param.algorithm.gamma = 0.99;
for i = 1: Nalt
sys_param.algorithm.weights = lambda(i,:);
[JJ_fqi(i,:), Qfqi{i}] = run_fqi(F, G, reg_param);
end
figure; plot( JJ_fqi(:,1), JJ_fqi(:,2), 'o' );
xlabel('flooding'); ylabel('irrigation');
%% --- Run MPC (Model Predictive Control) ---
clc;
addpath('./MPC')
% MPC settings
sys_param.algorithm.name = 'mpc';
sys_param.algorithm.P = 3; % Length of the moving prediction horizon
sys_param.algorithm.mi_e = mean(sys_param.simulation.q);
sys_param.algorithm.sigma_e = std(sys_param.simulation.q);
% mpc_input = sys_param.simulation.q; % Candidate disturbance variable to be predicted for MPC
errorLevel = 0; % Disturbance prediction error [%]
% Weights value for aggregation of objectives
lambda = [1 0; .75 .25; .5 .5 ; .35 .65; .2 .8; .1 .9; 0 1];
Nalt = size(lambda,1);
JJ_mpc = nan(Nalt, 2);
Ompc = cell(Nalt, 1);
for i = 1: Nalt
sys_param.algorithm.weights = lambda(i,:);
[JJ_mpc(i,:), Ompc{i}] = run_mpc(errorLevel);
end
figure; plot( JJ_mpc(:,1), JJ_mpc(:,2), 'o' );
xlabel('flooding'); ylabel('irrigation');
%% --- Run ISO ( Implicit Stochastic Optimization) ---
clc;
addpath('./DDP')
% Configure the parameters
load 'grids.mat';
sys_param.algorithm = grids;
sys_param.algorithm.name = 'iso';
[vv, VV] = construct_rel_matrices(grids); % compute daily minimum/maximum release matrixes
sys_param.algorithm.min_rel = vv;
sys_param.algorithm.max_rel = VV;
sys_param.algorithm.Hend = 0 ; % penalty set to 0
% Define regression method
regressor = 'linear_spline';
sys_param.algorithm.regressorName = regressor;
% weights value for aggregation of objectives
lambda = [1 0; .75 .25; .5 .5 ; .35 .65; .2 .8; .1 .9; 0 1];
Nalt = size(lambda,1);
[JJ_iso, err_perc] = deal(nan(Nalt,2));
policy = cell(Nalt,1);
for i = 1: Nalt
sys_param.algorithm.weights = lambda(i,:);
[JJ_iso(i,:), policy{i}, err_perc(i,:)] = run_iso(regressor);
end
% plot
figure; plot( JJ_iso(:,1), JJ_iso(:,2), 'o' );
xlabel('flooding'); ylabel('irrigation');
%% --- Run SSDP (Sampling Stochastic Dynamic Programming) ---
clc;
addpath('./SSDP')
% Configure the parameters
load 'grids.mat';
sys_param.algorithm = grids;
sys_param.algorithm.name = 'ssdp';
sys_param.algorithm.Hend = 0 ; % penalty set to 0
[vv, VV] = construct_rel_matrices(grids); % compute daily minimum/maximum release matrixes
sys_param.algorithm.min_rel = vv;
sys_param.algorithm.max_rel = VV;
sys_param.algorithm.T = 365;
sys_param.algorithm.interp_foo = @interp1qr; % default interpolator
sys_param.algorithm.gamma = 1;
sys_param.algorithm.Pr_mode = 2;
sys_param.algorithm.cycle_T = 300;
sys_param.algorithm.forecast_T = 60;
% estimate inflow pdf (log-normal distribution) and create the test samples
% of total 25 ensembles with 365 days each
q_stat = lognfit(sys_param.simulation.q) ;
esp_sample = lognrnd( q_stat(1), q_stat(2), 365, 25 );
sys_param.algorithm.esp_sample = esp_sample ;
% weights value for aggregation of objectives
lambda = [1 0; .75 .25; .5 .5 ; .35 .65; .2 .8; .1 .9; 0 1];
Nalt = size(lambda,1);
JJ_ssdp = nan(Nalt, 2);
H_policy = cell(Nalt, 1);
for i = 1: Nalt
sys_param.algorithm.weights = lambda(i,:);
[JJ_ssdp(i,:), H_policy{i}] = run_ssdp() ;
end
figure; plot( JJ_ssdp(:,1), JJ_ssdp(:,2), 'o' );
xlabel('flooding'); ylabel('irrigation');