-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathoptimization_dqn.m
More file actions
266 lines (228 loc) · 9.27 KB
/
optimization_dqn.m
File metadata and controls
266 lines (228 loc) · 9.27 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
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
262
263
264
265
266
%% DQN优化集成权重系数(动态)
%% Clear All
clear;clc;close all;
%% Load Dataset
% Data for getting pareto optimal solutions
results_on_valid = xlsread('results_on_valid.xlsx');
save('.\myResults\results_on_valid','results_on_valid');
pValue = results_on_valid(:,2:4)';
tValue = results_on_valid(:,1)';
% Data for train and deploy agent
load dataset
validXn = dataset.validXn;
validYn = dataset.validYn;
validY = dataset.validY;
testXn = dataset.testXn;
testY = dataset.testY;
outputps = dataset.outputps;
%%
load dbn;load bilstm;load lstm;load gru;
pValue1 = myPredict('dbn',dbn,testXn,outputps);
pValue2 = myPredict('lstm',lstm,testXn,outputps);
pValue3 = myPredict('gru',gru,testXn,outputps);
pValueTest = [pValue1;pValue2;pValue3];
%% NSGA-II
load mooResults;
load population_size;
%% Compromise
load nsga2_weights;
compromiseSolution = nsga2_weights;
%% Pareto Front
load compromiseParetoFront;
% figure('Name','Pareto front of NSGA-II');
% plot(mooResults.paretoFront(:,1),mooResults.paretoFront(:,2),'o');
% xlabel('MSE');
% ylabel('SDE');
% hold on;
% plot(compromiseParetoFront(1),compromiseParetoFront(2),'r*','MarkerSize',30);
% legend('Pareto front','Selected solution');
%% DQN
%% Environments
num_obs = dataset.inputNum; %状态数
num_action = population_size; %动作数
ObservationInfo = rlNumericSpec([num_obs 1]); %创建连续状态空间所需的INFO(维度)
ObservationInfo.Name = 'Historical values'; %INFO名称
ObservationInfo.Description = 'x1, x2, x3, x4, x5'; %INFO描述
actions = eye(num_action);
ActionInfo = rlFiniteSetSpec(num2cell(actions,2)); %创建连续动作空间所需的INFO(元素)
ActionInfo.Name = 'Combination weights'; %INFO名称
ActionInfo.Description = 'w1, w2, w3, ..., w50'; %INFO描述
% Parameters
envConstants.Dataset = dataset;
envConstants.MooSolutions = mooResults.solutions;
envConstants.CompromiseSolution = compromiseSolution;
envConstants.BasePredValueValid = pValue;
envConstants.BasePredValueTest = pValueTest;
envConstants.RewardValue = 1.0;
% Training Environment
ResetHandleTrain = @() myResetFunctionTrainDQN(envConstants);
StepHandleTrain = @(Action,LoggedSignals) myStepFunctionTrainDQN(Action,LoggedSignals,envConstants);
envTrain = rlFunctionEnv(ObservationInfo,ActionInfo,StepHandleTrain,ResetHandleTrain);
% Deployment Environment
ResetHandleDeploy = @() myResetFunctionDeployDQN(envConstants);
StepHandleDeploy = @(Action,LoggedSignals) myStepFunctionDeployDQN(Action,LoggedSignals,envConstants);
envDeploy = rlFunctionEnv(ObservationInfo,ActionInfo,StepHandleDeploy,ResetHandleDeploy);
%% Policy and Representation
%% Critic Network
statePath = [
imageInputLayer([num_obs 1 1], 'Normalization', 'none', 'Name', 'state')
fullyConnectedLayer(48, 'Name', 'CriticStateFC1')
reluLayer("Name","state_relu1")
fullyConnectedLayer(48, 'Name', 'CriticStateFC2')
reluLayer("Name","state_relu2")
fullyConnectedLayer(48,"Name","CriticStateFC3")
];
actionPath = [
imageInputLayer([1 num_action 1], 'Normalization', 'none', 'Name', 'action')
fullyConnectedLayer(48, 'Name', 'CriticActionFC1')
];
commonPath = [
additionLayer(2,'Name', 'add')
reluLayer('Name','CriticCommonRelu1')
fullyConnectedLayer(1, 'Name', 'output')
];
criticNetwork = layerGraph(statePath);
criticNetwork = addLayers(criticNetwork, actionPath);
criticNetwork = addLayers(criticNetwork, commonPath);
criticNetwork = connectLayers(criticNetwork,'CriticStateFC3','add/in1');
criticNetwork = connectLayers(criticNetwork,'CriticActionFC1','add/in2');
% Configuration options
criticOpts = rlRepresentationOptions(...
'LearnRate', 1e-3,...
'GradientThreshold', 1,...
'UseDevice', "cpu");
% Representation
critic = rlQValueRepresentation(...
criticNetwork,...
ObservationInfo,...
ActionInfo,...
'Observation',{'state'},...
'Action',{'action'},...
criticOpts);
%% Actor Network
% No actor for DQN
%% Agent
% Configuration options
agentOpts = rlDQNAgentOptions(...
'UseDoubleDQN', false, ...
'ExperienceBufferLength', 100000, ...
'TargetUpdateMethod', 'smoothing',...
'TargetSmoothFactor', 1e-3, ...
'DiscountFactor', 0.99, ...
'MiniBatchSize', 64, ...
'NumStepsToLookAhead', 1);
agentOpts.EpsilonGreedyExploration.Epsilon = 0.999;
agentOpts.EpsilonGreedyExploration.EpsilonDecay = 0.0001;
agentOpts.EpsilonGreedyExploration.EpsilonMin = 0.1;
% DQN agent
agent = rlDQNAgent(critic,agentOpts);
%% Train Agent
% Configuration options
trainOpts = rlTrainingOptions(...
'MaxEpisodes', 2000, ...
'MaxStepsPerEpisode', dataset.validNum, ...
'ScoreAveragingWindowLength', 20, ...
'StopTrainingCriteria', "AverageReward", ...
'StopTrainingValue', 1000, ...
'Verbose', true, .... %显示训练过程 command window
'Plots', "none", ... %显示训练过程 episode manager none
'UseParallel', true);
trainingInfo = train(agent,envTrain,trainOpts); %在envTrain中依据trainOpts训练agent agent中已包含了critic和actor两部分
%% Simulate DQN Agent
% Simulation On envTrain
simOptsTrain = rlSimulationOptions('MaxSteps',dataset.validNum);
experience_train = sim(envTrain,agent,simOptsTrain);
[action_train,~] = find(squeeze(experience_train.Action.CombinationWeights.Data)==1);
% Simulation On envDeploy
simOptsDeploy = rlSimulationOptions('MaxSteps',dataset.testNum);
experience_deploy = sim(envDeploy,agent,simOptsDeploy);
[action_deploy,~] = find(squeeze(experience_deploy.Action.CombinationWeights.Data)==1);
%% Get dynamic weights
nsga2_dqn_weights = mooResults.solutions(action_deploy,:)';
%% Figure
fileName = 'DQN';
root = ['C:\Users\liuhui116\Desktop\铁路风工程\图片\',fileName,'\'];
fileType = '-djpeg';
resolution = '-r600';
% Plot reward train
figureName='Episode reward of DQN during training';
figure('Name',figureName);
plot(trainingInfo.EpisodeReward, ...
'LineWidth', 1);
hold on;
plot(trainingInfo.AverageReward, ...
'LineWidth', 1);
legend('Episode reward','Average reward', ...
'Location', 'SouthEast', ...
'Orientation', 'vertical', ...
'FontSize', 11, ...
'FontName', 'Times New Roman');
xlabel('Episode', ...
'FontSize', 12, ...
'FontName', 'Times New Roman', ...
'FontWeight', 'bold');
ylabel('Reward', ...
'FontSize', 12, ...
'FontName', 'Times New Roman', ...
'FontWeight', 'bold');
set(gcf,'Position',[200,200,400,300]);
print(gcf,[root,figureName],fileType,resolution);
% Plot selection results
load compromiseIndex;
figureName='Selection results of the Pareto optimal solutions in the testing set';
figure('Name',figureName);
plot(action_deploy,'o');
hold on;
line([1,dataset.testNum],[compromiseIndex,compromiseIndex], ...
'Color','red', ...
'LineWidth', 1);
legend('Dynamic','Static','Location','bestoutside','Orientation','vertical')
xlabel('Times (3-min)', ...
'FontSize', 12, ...
'FontName', 'Times New Roman', ...
'FontWeight', 'bold');
ylabel('Selected solution', ...
'FontSize', 12, ...
'FontName', 'Times New Roman', ...
'FontWeight', 'bold');
set(gcf,'Position',[200,200,500,300]);
print(gcf,[root,figureName],fileType,resolution);
% Plot reward during sim in envTrain
figureName='Reward for each step of the reinforcement learning agent in the training environment';
figure('Name',figureName);
plot(experience_train.Reward.Data, ...
'LineWidth', 1);
xlabel('Times (3-min)', ...
'FontSize', 12, ...
'FontName', 'Times New Roman', ...
'FontWeight', 'bold');
ylabel('Instant reward', ...
'FontSize', 12, ...
'FontName', 'Times New Roman', ...
'FontWeight', 'bold');
% yticks([-envConstants.RewardValue, +envConstants.RewardValue]);
% yticklabels({'Punishment','Reward'});
set(gcf,'Position',[200,200,400,300]);
print(gcf,[root,figureName],fileType,resolution);
% Plot reward during sim in envDeploy
figureName='Reward for each step of the reinforcement learning agent in the deployment environment';
figure('Name',figureName);
plot(experience_deploy.Reward.Data, ...
'LineWidth', 1);
xlabel('Times (3-min)', ...
'FontSize', 12, ...
'FontName', 'Times New Roman', ...
'FontWeight', 'bold');
ylabel('Instant reward', ...
'FontSize', 12, ...
'FontName', 'Times New Roman', ...
'FontWeight', 'bold');
set(gcf,'Position',[200,200,400,300]);
print(gcf,[root,figureName],fileType,resolution);
%% Save variables
% Environments
fullpath = mfilename('fullpath');
[path,name]=fileparts(fullpath);
save(['.\myStatus\',name,'_',datestr(datetime,'yyyy-mm-dd_HH-MM-SS')]);
% Key parameters
save('.\myModel\nsga2_dqn_weights','nsga2_dqn_weights');