-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExample1.java
370 lines (278 loc) · 11.3 KB
/
Example1.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
package applications.rl;
import rl.*;
import static base.CommonConstants.getTol;
import java.util.ArrayList;
/** Category: Reinforcement Learning
* ID: Example1
* Description: Iterative Policy Evaluation applied on GridWorld
* Taken From:
* Details:
* TODO
*/
public class Example1 {
class Action implements IAction
{
public Action(String actionType, IState state){
this.actionType = actionType;
this.state = state;
}
/**
* The state that the action leads to
*/
@Override
public void addTransitionToState(IState state){
this.state = state;
}
@Override
public String toString(){
return this.actionType;
}
/**
* Returns the state to transition to
*/
public IState getTransitionToState(){
return this.state;
}
private String actionType;
private IState state;
}
class State implements IState
{
public State(int id){
this.id = id;
this.actions = new ArrayList<IAction>();
}
/**
* Returns the id of the state
*/
@Override
public int getId(){return this.id;}
/**
* Add an action to be executed when at this state
*/
@Override
public void addAction(IAction action){
actions.add(action);
}
/**
* Returns the i-th action that is possible when at this state
* @param i
* @return IAction The i-th action when at this state
*/
@Override
public IAction getAction(int i){
return this.actions.get(i);
}
/**
* Number of actions for this IState
*/
@Override
public int nActions(){
return this.actions.size();
}
@Override
public boolean equals(Object other){
if(this == other){
return true;
}
if(!(other instanceof State)){
return false;
}
return this.getId() == ((State)other).getId();
}
@Override
public int hashCode(){
return this.getId();
}
/**
* Returns the state that this state transitions to when the action with the given local id is apllied
* @param actionId Action local id
* @return IState
*/
public IState applyAction(int actionId){
return this.actions.get(actionId).getTransitionToState();
}
/**
* Return the probability of taking the action with the given name
* whilst at this state
*/
@Override
public double getActionProbability(int actionIdx){
return 0.25;
}
@Override
public double getReward(){
return -1.0;
}
/**
* Returns true if the state is a terminal state
*/
@Override
public boolean isTerminal(){
return this.isTerminalState;
}
/**
* Set a state that this state can transition to
*/
public void makeTerminalState(){
this.isTerminalState = true;
}
int id;
ArrayList<IAction> actions;
boolean isTerminalState = false;
}
/**
* The state space
*/
private StateSpaceImpl<State> stateSpace;
/**
* An object that performs value function evaluation using iterative policy
*/
private ValueFunctionIterativePolicyEvaluation valueFunction;
/**
* The object that holds the optimal policy
*/
ValueFunctionOptimalPolicyBuilder<StateSpaceImpl<State>, PolicyImpl> policyBuilder;
public void createStateSpace(){
stateSpace = new StateSpaceImpl<>(16, new ConstantTransitionDynamics(1.0));
// populate with states
for(int s=0; s<16; s++) {
State state = new State(s);
stateSpace.addState(state);
}
for(int s=0; s<16; s++) {
IState state = stateSpace.getState(s);
if(s == 0){
state.addAction(new Action("RIGHT", stateSpace.getState(1)) );
state.addAction(new Action("TOP", stateSpace.getState(4)));
state.addAction(new Action("LEFT", stateSpace.getState(0)));
state.addAction(new Action("BOTTOM", stateSpace.getState(0)));
}
else if(s == 1){
state.addAction(new Action("RIGHT", stateSpace.getState(2)) );
state.addAction(new Action("TOP", stateSpace.getState(5)));
state.addAction(new Action("LEFT", stateSpace.getState(0)));
state.addAction(new Action("BOTTOM", stateSpace.getState(1)));
}
else if(s == 2){
state.addAction(new Action("RIGHT", stateSpace.getState(3)));
state.addAction(new Action("TOP", stateSpace.getState(6)));
state.addAction(new Action("LEFT", stateSpace.getState(1)));
state.addAction(new Action("BOTTOM", stateSpace.getState(2)));
}
/*else if( s == 3){
state.addAction(new Action("TOP", stateSpace.getState(7)));
state.addAction(new Action("LEFT", stateSpace.getState(2)));
state.addAction(new Action("BOTTOM", stateSpace.getState(3)));
state.addAction(new Action("RIGHT", stateSpace.getState(3)));
}*/
else if(s == 4){
state.addAction(new Action("BOTTOM",stateSpace.getState(0) ));
state.addAction(new Action("RIGHT" ,stateSpace.getState(5) ));
state.addAction(new Action("TOP" ,stateSpace.getState(8) ));
state.addAction(new Action("LEFT", stateSpace.getState(4)));
}
else if(s == 5){
state.addAction(new Action("BOTTOM",stateSpace.getState(1) ));
state.addAction(new Action("RIGHT" ,stateSpace.getState(6) ));
state.addAction(new Action("TOP" ,stateSpace.getState(9) ));
state.addAction(new Action("LEFT" ,stateSpace.getState(4) ));
}
else if(s == 6){
state.addAction(new Action("BOTTOM", stateSpace.getState(2) ));
state.addAction(new Action("RIGHT" , stateSpace.getState(7) ));
state.addAction(new Action("TOP" , stateSpace.getState(10)));
state.addAction(new Action("LEFT" , stateSpace.getState(5) ));
}
else if(s == 7){
state.addAction(new Action("BOTTOM",stateSpace.getState(3) ));
state.addAction(new Action("TOP" ,stateSpace.getState(11) ));
state.addAction(new Action("LEFT" ,stateSpace.getState(6) ));
state.addAction(new Action("RIGHT", stateSpace.getState(7)));
}
else if( s == 8){
state.addAction(new Action("BOTTOM",stateSpace.getState(4) ));
state.addAction(new Action("RIGHT" ,stateSpace.getState(9) ));
state.addAction(new Action("TOP" ,stateSpace.getState(12) ));
state.addAction(new Action("LEFT", stateSpace.getState(8)));
}
else if(s == 9){
state.addAction(new Action("BOTTOM",stateSpace.getState(5) ));
state.addAction(new Action("RIGHT" ,stateSpace.getState(10) ));
state.addAction(new Action("TOP" ,stateSpace.getState(13) ));
state.addAction(new Action("LEFT" ,stateSpace.getState(8) ));
}
else if(s == 10){
state.addAction(new Action("BOTTOM",stateSpace.getState(6) ));
state.addAction(new Action("RIGHT" ,stateSpace.getState(11) ));
state.addAction(new Action("TOP" ,stateSpace.getState(14) ));
state.addAction(new Action("LEFT" ,stateSpace.getState(9) ));
}
else if(s == 11){
state.addAction(new Action("BOTTOM",stateSpace.getState(7) ));
state.addAction(new Action("TOP" ,stateSpace.getState(15) ));
state.addAction(new Action("LEFT" ,stateSpace.getState(10) ));
state.addAction(new Action("RIGHT", stateSpace.getState(11)));
}
/*else if(s == 12){
state.addAction(new Action("BOTTOM",stateSpace.getState(8) ));
state.addAction(new Action("RIGHT" ,stateSpace.getState(13) ));
}*/
else if( s == 13){
state.addAction(new Action("BOTTOM",stateSpace.getState(9) ));
state.addAction(new Action("RIGHT" ,stateSpace.getState(14) ));
state.addAction(new Action("LEFT" ,stateSpace.getState(12) ));
state.addAction(new Action("TOP", stateSpace.getState(13)));
}
else if(s == 14){
state.addAction(new Action("BOTTOM", stateSpace.getState(10) ));
state.addAction(new Action("RIGHT" , stateSpace.getState(15) ));
state.addAction(new Action("LEFT" , stateSpace.getState(13) ));
state.addAction(new Action("TOP", stateSpace.getState(14)));
}
else if(s == 15){
state.addAction(new Action("BOTTOM", stateSpace.getState(11) ));
state.addAction(new Action("LEFT" , stateSpace.getState(14) ));
state.addAction(new Action("TOP", stateSpace.getState(15)));
state.addAction(new Action("RIGHT", stateSpace.getState(15)));
}
if(state.getId() == 3 || state.getId() == 12){
((State) state).makeTerminalState();
}
}
}
public void createGame(IterativePolicyEvaluationParams params){
createStateSpace();
valueFunction = new ValueFunctionIterativePolicyEvaluation(params);
IPolicyBuilder<PolicyImpl> builder = ()->{return new PolicyImpl();};
policyBuilder = new ValueFunctionOptimalPolicyBuilder<>(builder, -(Double.MAX_VALUE - 1));
}
public void play(){
valueFunction.evaluate(this.stateSpace, new UniformPolicyValue(0.25));
double[] values = valueFunction.getValues();
// we now have Vstar available. We want to calculate the
// optimal policy pi star from it
policyBuilder.buildFrom(this.stateSpace, valueFunction);
// let's check the policy
IPolicy policy = policyBuilder.getPolicy();
for(int s=0; s<this.stateSpace.nStates(); ++s){
IState state = this.stateSpace.getState(s);
if(!state.isTerminal()){
IAction action = policy.getAction(state);
System.out.println("For state: "+state.getId()+" Action is: "+action);
}
}
}
public static void main(String[] args){
System.out.println("Playing GridWorld...");
IterativePolicyEvaluationParams params = new IterativePolicyEvaluationParams();
params.tol = getTol();
params.gamma = 1.0;
params.reward = -1.0;
params.showItrs = false;
Example1 game = new Example1();
game.createGame(params);
game.play();
System.out.println("Done...");
}
}