-
Notifications
You must be signed in to change notification settings - Fork 0
/
MDPSolver.java
288 lines (282 loc) · 10.9 KB
/
MDPSolver.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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import java.util.ArrayList;
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
public class MDPSolver {
static Model baseModel;
static State[] stateArr;
static Action ptrAct;
public static void main(String[] args) {
baseModel = new Model();
MDPSolver.assembleFigure17_1();
baseModel.infiniteValueIteration(.99f, .1f);
/* This infinite value iteration is only done to evaluate convergence of qLearning to optimal policy
* The values derived from it are in no other ways used to calculate q
* values or generate exploration policies/ policy factors.
*/
runQLearning_and_graphQVals();// Runs q learning and sends the information to a visualizer.
}
// Runs Q-learning and graphs the values derived.
static void runQLearning_and_graphQVals(){
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GraphingProcess gPross = new GraphingProcess();
gPross.takeInNames(baseModel.exportNames());
gPross.takeInArray(baseModel.Q_Learn_To_Beyond_Convergence_And_Record_Q_Val_Results());
f.add(gPross);
f.setSize(1000,700);
f.setLocation(0,0);
f.setVisible(true);
}
// Runs Q-learning and graphs the objective valuations (rather than standard q valuations)
static void runQLearning_and_graphObjectiveValuations(){
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GraphingProcess gPross = new GraphingProcess();
gPross.takeInNames(baseModel.exportNames());
gPross.takeInArray(baseModel.Q_Learn_To_Beyond_Convergence_And_Record_Objective_Valuations());
f.add(gPross);
f.setSize(1000,700);
f.setLocation(0,0);
f.setVisible(true);
}
// Assembles one of the possible stochastic environments.
// While the other similar methods have been removed it is generalizable to k-dimensional environements.
static void assembleFigure17_1() {
double r = -0.04f;
stateArr = new State[11];
stateArr[0] = new State(r, "State 1,3");
stateArr[1] = new State(r, "State 2,3");
stateArr[2] = new State(r, "State 3,3");
stateArr[3] = new State(1, "State 4,3");
stateArr[4] = new State(r, "State 1,2");
stateArr[5] = new State(r, "State 3,2");
stateArr[6] = new State(-1, "State 4,2");
stateArr[7] = new State(r, "State 1,1");
stateArr[8] = new State(r, "State 2,1");
stateArr[9] = new State(r, "State 3,1");
stateArr[10] = new State(r, "State 4,1");
//
ptrAct = new Action("Down");
ptrAct.addTransition(.8f, stateArr[4]);
ptrAct.addTransition(.1f, stateArr[1]);
ptrAct.addTransition(.1f, stateArr[0]);
stateArr[0].addAction(ptrAct);
//
ptrAct = new Action("Up");
ptrAct.addTransition(.1f, stateArr[1]);
ptrAct.addTransition(.9f, stateArr[0]);
stateArr[0].addAction(ptrAct);
//
ptrAct = new Action("Right");
ptrAct.addTransition(.8f, stateArr[1]);
ptrAct.addTransition(.1f, stateArr[0]);
ptrAct.addTransition(.1f, stateArr[4]);
stateArr[0].addAction(ptrAct);
//
ptrAct = new Action("Left");
ptrAct.addTransition(.9f, stateArr[0]);
ptrAct.addTransition(.1f, stateArr[4]);
stateArr[0].addAction(ptrAct);
//
ptrAct = new Action("Up");
ptrAct.addTransition(.8f, stateArr[1]);
ptrAct.addTransition(.1f, stateArr[0]);
ptrAct.addTransition(.1f, stateArr[2]);
stateArr[1].addAction(ptrAct);
//
ptrAct = new Action("Down");
ptrAct.addTransition(.8f, stateArr[1]);
ptrAct.addTransition(.1f, stateArr[0]);
ptrAct.addTransition(.1f, stateArr[2]);
stateArr[1].addAction(ptrAct);
//
ptrAct = new Action("Right");
ptrAct.addTransition(.8f, stateArr[2]);
ptrAct.addTransition(.1f, stateArr[1]);
ptrAct.addTransition(.1f, stateArr[1]);
stateArr[1].addAction(ptrAct);
//
ptrAct = new Action("Left");
ptrAct.addTransition(.8f, stateArr[0]);
ptrAct.addTransition(.1f, stateArr[1]);
ptrAct.addTransition(.1f, stateArr[1]);
stateArr[1].addAction(ptrAct);
//
ptrAct = new Action("Up");
ptrAct.addTransition(.8f, stateArr[2]);
ptrAct.addTransition(.1f, stateArr[1]);
ptrAct.addTransition(.1f, stateArr[3]);
stateArr[2].addAction(ptrAct);
//
ptrAct = new Action("Down");
ptrAct.addTransition(.8f, stateArr[5]);
ptrAct.addTransition(.1f, stateArr[1]);
ptrAct.addTransition(.1f, stateArr[3]);
stateArr[2].addAction(ptrAct);
//
ptrAct = new Action("Right");
ptrAct.addTransition(.8f, stateArr[3]);
ptrAct.addTransition(.1f, stateArr[2]);
ptrAct.addTransition(.1f, stateArr[5]);
stateArr[2].addAction(ptrAct);
//
ptrAct = new Action("Left");
ptrAct.addTransition(.8f, stateArr[1]);
ptrAct.addTransition(.1f, stateArr[2]);
ptrAct.addTransition(.1f, stateArr[5]);
stateArr[2].addAction(ptrAct);
//
//skip [3] because its an absorbing state
//
ptrAct = new Action("Up");
ptrAct.addTransition(.8f, stateArr[0]);
ptrAct.addTransition(.1f, stateArr[4]);
ptrAct.addTransition(.1f, stateArr[4]);
stateArr[4].addAction(ptrAct);
//
ptrAct = new Action("Down");
ptrAct.addTransition(.8f, stateArr[7]);
ptrAct.addTransition(.1f, stateArr[4]);
ptrAct.addTransition(.1f, stateArr[4]);
stateArr[4].addAction(ptrAct);
//
ptrAct = new Action("Right");
ptrAct.addTransition(.8f, stateArr[4]);
ptrAct.addTransition(.1f, stateArr[0]);
ptrAct.addTransition(.1f, stateArr[7]);
stateArr[4].addAction(ptrAct);
//
ptrAct = new Action("Left");
ptrAct.addTransition(.8f, stateArr[4]);
ptrAct.addTransition(.1f, stateArr[0]);
ptrAct.addTransition(.1f, stateArr[7]);
stateArr[4].addAction(ptrAct);
//
ptrAct = new Action("Down");
ptrAct.addTransition(.8f, stateArr[9]);
ptrAct.addTransition(.1f, stateArr[6]);
ptrAct.addTransition(.1f, stateArr[5]);
stateArr[5].addAction(ptrAct);
//
ptrAct = new Action("Up");
ptrAct.addTransition(.8f, stateArr[2]);
ptrAct.addTransition(.1f, stateArr[6]);
ptrAct.addTransition(.1f, stateArr[5]);
stateArr[5].addAction(ptrAct);
//
ptrAct = new Action("Right");
ptrAct.addTransition(.8f, stateArr[6]);
ptrAct.addTransition(.1f, stateArr[2]);
ptrAct.addTransition(.1f, stateArr[9]);
stateArr[5].addAction(ptrAct);
//
ptrAct = new Action("Left");
ptrAct.addTransition(.8f, stateArr[5]);
ptrAct.addTransition(.1f, stateArr[2]);
ptrAct.addTransition(.1f, stateArr[9]);
stateArr[5].addAction(ptrAct);
//
// skip 6 because its an absorbing state
//
ptrAct = new Action("Up");
ptrAct.addTransition(.8f, stateArr[4]);
ptrAct.addTransition(.1f, stateArr[7]);
ptrAct.addTransition(.1f, stateArr[8]);
stateArr[7].addAction(ptrAct);
//
ptrAct = new Action("Down");
ptrAct.addTransition(.8f, stateArr[7]);
ptrAct.addTransition(.1f, stateArr[7]);
ptrAct.addTransition(.1f, stateArr[8]);
stateArr[7].addAction(ptrAct);
//
ptrAct = new Action("Right");
ptrAct.addTransition(.8f, stateArr[8]);
ptrAct.addTransition(.1f, stateArr[4]);
ptrAct.addTransition(.1f, stateArr[7]);
stateArr[7].addAction(ptrAct);
//
ptrAct = new Action("Left");
ptrAct.addTransition(.8f, stateArr[7]);
ptrAct.addTransition(.1f, stateArr[4]);
ptrAct.addTransition(.1f, stateArr[7]);
stateArr[7].addAction(ptrAct);
//
ptrAct = new Action("Up");
ptrAct.addTransition(.8f, stateArr[8]);
ptrAct.addTransition(.1f, stateArr[7]);
ptrAct.addTransition(.1f, stateArr[9]);
stateArr[8].addAction(ptrAct);
//
ptrAct = new Action("Down");
ptrAct.addTransition(.8f, stateArr[8]);
ptrAct.addTransition(.1f, stateArr[7]);
ptrAct.addTransition(.1f, stateArr[9]);
stateArr[8].addAction(ptrAct);
//
ptrAct = new Action("Right");
ptrAct.addTransition(.8f, stateArr[9]);
ptrAct.addTransition(.1f, stateArr[8]);
ptrAct.addTransition(.1f, stateArr[8]);
stateArr[8].addAction(ptrAct);
//
ptrAct = new Action("Left");
ptrAct.addTransition(.8f, stateArr[7]);
ptrAct.addTransition(.1f, stateArr[8]);
ptrAct.addTransition(.1f, stateArr[8]);
stateArr[8].addAction(ptrAct);
//
ptrAct = new Action("Up");
ptrAct.addTransition(.8f, stateArr[5]);
ptrAct.addTransition(.1f, stateArr[8]);
ptrAct.addTransition(.1f, stateArr[10]);
stateArr[9].addAction(ptrAct);
//
ptrAct = new Action("Down");
ptrAct.addTransition(.8f, stateArr[9]);
ptrAct.addTransition(.1f, stateArr[8]);
ptrAct.addTransition(.1f, stateArr[10]);
stateArr[9].addAction(ptrAct);
//
ptrAct = new Action("Right");
ptrAct.addTransition(.8f, stateArr[10]);
ptrAct.addTransition(.1f, stateArr[5]);
ptrAct.addTransition(.1f, stateArr[9]);
stateArr[9].addAction(ptrAct);
//
ptrAct = new Action("Left");
ptrAct.addTransition(.8f, stateArr[8]);
ptrAct.addTransition(.1f, stateArr[9]);
ptrAct.addTransition(.1f, stateArr[5]);
stateArr[9].addAction(ptrAct);
//
ptrAct = new Action("Up");
ptrAct.addTransition(.8f, stateArr[6]);
ptrAct.addTransition(.1f, stateArr[10]);
ptrAct.addTransition(.1f, stateArr[9]);
stateArr[10].addAction(ptrAct);
//
ptrAct = new Action("Down");
ptrAct.addTransition(.8f, stateArr[10]);
ptrAct.addTransition(.1f, stateArr[10]);
ptrAct.addTransition(.1f, stateArr[9]);
stateArr[10].addAction(ptrAct);
//
ptrAct = new Action("Right");
ptrAct.addTransition(.8f, stateArr[10]);
ptrAct.addTransition(.1f, stateArr[10]);
ptrAct.addTransition(.1f, stateArr[6]);
stateArr[10].addAction(ptrAct);
//
ptrAct = new Action("Left");
ptrAct.addTransition(.8f, stateArr[9]);
ptrAct.addTransition(.1f, stateArr[6]);
ptrAct.addTransition(.1f, stateArr[10]);
stateArr[10].addAction(ptrAct);
//
for (int k = 0; k < stateArr.length; k++) {
baseModel.addState(stateArr[k]);
}
}
}