forked from UnyieldingOrca/dl4j-rl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DeepQNetwork.java
234 lines (201 loc) · 6.94 KB
/
DeepQNetwork.java
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
package com.dap.dl4j;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import org.apache.commons.io.FileUtils;
import org.deeplearning4j.nn.conf.MultiLayerConfiguration;
import org.deeplearning4j.nn.multilayer.MultiLayerNetwork;
import org.nd4j.linalg.api.ndarray.INDArray;
import org.nd4j.linalg.factory.Nd4j;
public class DeepQNetwork {
int ReplayMemoryCapacity;
List<Replay> ReplayMemory;
double Epsilon;
float Discount;
MultiLayerNetwork DeepQ;
MultiLayerNetwork TargetDeepQ;
int BatchSize;
int UpdateFreq;
int UpdateCounter;
int ReplayStartSize;
Random r;
int InputLength;
int NumActions;
INDArray LastInput;
int LastAction;
DeepQNetwork(MultiLayerConfiguration conf , int replayMemoryCapacity , float discount ,
double epsilon , int batchSize , int updateFreq , int replayStartSize , int inputLength , int numActions){
DeepQ = new MultiLayerNetwork(conf);
DeepQ.init();
TargetDeepQ = new MultiLayerNetwork(conf);
TargetDeepQ.init();
TargetDeepQ.setParams(DeepQ.params());
ReplayMemoryCapacity = replayMemoryCapacity;
Epsilon = epsilon;
Discount = discount;
r = new Random();
BatchSize = batchSize;
UpdateFreq = updateFreq;
UpdateCounter = 0;
ReplayMemory = new ArrayList<Replay>();
ReplayStartSize = replayStartSize;
InputLength = inputLength;
NumActions = numActions;
}
void SetEpsilon(double e){
Epsilon = e;
}
void AddReplay(float reward , INDArray NextInput , int NextActionMask[]){
if( ReplayMemory.size() >= ReplayMemoryCapacity )
ReplayMemory.remove( r.nextInt(ReplayMemory.size()) );
ReplayMemory.add(new Replay(LastInput , LastAction , reward , NextInput , NextActionMask));
}
Replay[] GetMiniBatch(int BatchSize){
int size = ReplayMemory.size() < BatchSize ? ReplayMemory.size() : BatchSize ;
Replay[] retVal = new Replay[size];
for(int i = 0 ; i < size ; i++){
retVal[i] = ReplayMemory.get(r.nextInt(ReplayMemory.size()));
}
return retVal;
}
float FindMax(INDArray NetOutputs , int ActionMask[]){
int i = 0;
while(ActionMask[i] == 0) i++;
float maxVal = NetOutputs.getFloat(i);
for(; i < NetOutputs.size(1) ; i++){
if(NetOutputs.getFloat(i) > maxVal && ActionMask[i] == 1){
maxVal = NetOutputs.getFloat(i);
}
}
return maxVal;
}
int FindActionMax(INDArray NetOutputs , int ActionMask[]){
int i = 0;
while(ActionMask[i] == 0) i++;
float maxVal = NetOutputs.getFloat(i);
int maxValI = i;
for(; i < NetOutputs.size(1) ; i++){
if(NetOutputs.getFloat(i) > maxVal && ActionMask[i] == 1){
maxVal = NetOutputs.getFloat(i);
maxValI = i;
}
}
return maxValI;
}
int GetAction(INDArray Inputs , int ActionMask[]){
LastInput = Inputs;
INDArray outputs = DeepQ.output(Inputs);
System.out.print(outputs + " ");
if(Epsilon > r.nextDouble()) {
LastAction = r.nextInt(outputs.size(1));
while(ActionMask[LastAction] == 0)
LastAction = r.nextInt(outputs.size(1));
System.out.println(LastAction);
return LastAction;
}
LastAction = FindActionMax(outputs , ActionMask);
System.out.println(LastAction);
return LastAction;
}
void ObserveReward(float Reward , INDArray NextInputs , int NextActionMask[]){
AddReplay(Reward , NextInputs , NextActionMask);
if(ReplayStartSize < ReplayMemory.size())
TrainNetwork(BatchSize);
UpdateCounter++;
if(UpdateCounter == UpdateFreq){
UpdateCounter = 0;
System.out.println("Reconciling Networks");
ReconcileNetworks();
}
}
INDArray CombineInputs(Replay replays[]){
INDArray retVal = Nd4j.create(replays.length , InputLength);
for(int i = 0; i < replays.length ; i++){
retVal.putRow(i, replays[i].Input);
}
return retVal;
}
INDArray CombineNextInputs(Replay replays[]){
INDArray retVal = Nd4j.create(replays.length , InputLength);
for(int i = 0; i < replays.length ; i++){
if(replays[i].NextInput != null)
retVal.putRow(i, replays[i].NextInput);
}
return retVal;
}
void TrainNetwork(int BatchSize){
Replay replays[] = GetMiniBatch(BatchSize);
INDArray CurrInputs = CombineInputs(replays);
INDArray TargetInputs = CombineNextInputs(replays);
float TotalError = 0;
INDArray CurrOutputs = DeepQ.output(CurrInputs);
INDArray TargetOutputs = TargetDeepQ.output(TargetInputs);
float y[] = new float[replays.length];
for(int i = 0 ; i < y.length ; i++){
int ind[] = { i , replays[i].Action };
float FutureReward = 0 ;
if(replays[i].NextInput != null)
FutureReward = FindMax(TargetOutputs.getRow(i) , replays[i].NextActionMask);
float TargetReward = replays[i].Reward + Discount * FutureReward ;
TotalError += (TargetReward - CurrOutputs.getFloat(ind)) * (TargetReward - CurrOutputs.getFloat(ind));
CurrOutputs.putScalar(ind , TargetReward ) ;
}
//System.out.println("Avgerage Error: " + (TotalError / y.length) );
DeepQ.fit(CurrInputs, CurrOutputs);
}
void ReconcileNetworks(){
TargetDeepQ.setParams(DeepQ.params());
}
public boolean SaveNetwork(String ParamFileName , String JSONFileName){
//Write the network parameters:
try(DataOutputStream dos = new DataOutputStream(Files.newOutputStream(Paths.get(ParamFileName)))){
Nd4j.write(DeepQ.params(),dos);
} catch (IOException e) {
System.out.println("Failed to write params");
return false;
}
//Write the network configuration:
try {
FileUtils.write(new File(JSONFileName), DeepQ.getLayerWiseConfigurations().toJson());
} catch (IOException e) {
System.out.println("Failed to write json");
return false;
}
return true;
}
public boolean LoadNetwork(String ParamFileName , String JSONFileName){
//Load network configuration from disk:
MultiLayerConfiguration confFromJson;
try {
confFromJson = MultiLayerConfiguration.fromJson(FileUtils.readFileToString(new File(JSONFileName)));
} catch (IOException e1) {
System.out.println("Failed to load json");
return false;
}
//Load parameters from disk:
INDArray newParams;
try(DataInputStream dis = new DataInputStream(new FileInputStream(ParamFileName))){
newParams = Nd4j.read(dis);
} catch (FileNotFoundException e) {
System.out.println("Failed to load parems");
return false;
} catch (IOException e) {
System.out.println("Failed to load parems");
return false;
}
//Create a MultiLayerNetwork from the saved configuration and parameters
DeepQ = new MultiLayerNetwork(confFromJson);
DeepQ.init();
DeepQ.setParameters(newParams);
ReconcileNetworks();
return true;
}
}