-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValueIteration.java
188 lines (155 loc) · 5.41 KB
/
ValueIteration.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
import java.util.ArrayList;
import java.util.Vector;
public class ValueIteration {
public static void main(String[] args) {
double[][] V = new double[10][10];
char[][] Policy = new char[10][10];
double delta = 0;
do {
delta = 0;
for (int i=0; i<V.length; i++) {
for (int j=0; j<V[0].length; j++) {
double v = V[i][j];
if ( (i==9) && (j==9) )
continue;
V[i][j] = doUpdate(i, j, V, Policy);
delta = Math.max(delta, Math.abs( v - V[i][j] ) );
}
}
} while (delta > 0);
Policy[9][9] = 'G';
printResults(V, Policy);
}
public static double doUpdate(int i, int j, double[][] V, char[][] Policy) {
// Array that stores the values that each action (l, u, r, d) gives. Max obtained from here.
double[] actions = new double[4];
// left
actions[0] = calcValue(V, i, j, i, j-1);
// down
actions[1] = calcValue(V, i, j, i-1, j);
// right
actions[2] = calcValue(V, i, j, i, j+1);
// up
actions[3] = calcValue(V, i, j, i+1, j);
double max = Math.max( Math.max(actions[0], actions[1]), Math.max(actions[2], actions[3]) );
updatePolicy(i, j, max, actions, Policy);
return max;
}
public static double calcValue(double[][] V, int oldi, int oldj, int newi, int newj) {
double newv = 0;
// Barriers
if ( barrier(oldi, oldj, newi, newj) ) {
newv = (1)*(-5 + V[oldi][oldj]);
//System.out.println("barrier");
}
// Partial Barriers
else if (
( (newi==1 && newj==8) && (newi==oldi-1) || (newi==2 && newj==8) && (newi==oldi+1) ) ||
( (newi==6 && newj==9) && (newi==oldi-1) || (newi==7 && newj==9) && (newi==oldi+1) ) ||
( (newi==7 && newj==1) && (newj==oldj+1) || (newi==7 && newj==0) && (newj==oldj-1) )
) {
newv = (1)*(-3 + V[newi][newj]);
//System.out.println("partial barrier");
}
// Teleport Cell a
else if (newi==1 && newj==3) {
newv = (0.6)*(-1 + V[1][7]) + (0.4)*(-1 + V[9][0]);
//System.out.println("teleport cell a");
}
// Teleport Cell b
else if (newi==4 && newj==1) {
newv = (0.4)*(-1 + V[7][5]) + (0.6)*(-1 + V[2][1]);
//System.out.println("teleport cell b");
}
// Teleport Cell c
else if (newi==8 && newj==4) {
newv = (0.7)*(-1 + V[6][9]) + (0.3)*(-1 + V[5][2]);
//System.out.println("teleport cell c");
}
// Off Grid
else if ( (newi>9) || (newi<0) || (newj>9) || (newj<0) ) {
newv = (1)*(-5 + V[oldi][oldj]);
//System.out.println("off grid");
}
// Goal Cell
else if ( (newi==9) && (newj==9) ) {
newv = (1)*(100 + V[newi][newj]);
//System.out.println("goal cell");
}
// Open Cell (none of the above)
else {
newv = (1)*(-1 + V[newi][newj]);
//System.out.println("open cell");
}
return newv;
}
public static boolean barrier(int oldi, int oldj, int newi, int newj) {
if (
// down and up barriers
( (newi==0 && newj==1) && (newi==oldi-1) || (newi==1 && newj==1) && (newi==oldi+1) ) ||
( (newi==0 && newj==2) && (newi==oldi-1) || (newi==1 && newj==2) && (newi==oldi+1) ) ||
( (newi==1 && newj==6) && (newi==oldi-1) || (newi==2 && newj==6) && (newi==oldi+1) ) ||
( (newi==1 && newj==7) && (newi==oldi-1) || (newi==2 && newj==7) && (newi==oldi+1) ) ||
( (newi==6 && newj==8) && (newi==oldi-1) || (newi==7 && newj==8) && (newi==oldi+1) ) ||
( (newi==6 && newj==1) && (newi==oldi-1) || (newi==7 && newj==1) && (newi==oldi+1) ) ||
( (newi==6 && newj==2) && (newi==oldi-1) || (newi==7 && newj==2) && (newi==oldi+1) ) ||
( (newi==6 && newj==3) && (newi==oldi-1) || (newi==7 && newj==3) && (newi==oldi+1) ) ||
// right and left barriers
( (newi==1 && newj==3) && (newj==oldj+1) || (newi==1 && newj==2) && (newj==oldj-1) ) ||
( (newi==0 && newj==6) && (newj==oldj+1) || (newi==0 && newj==5) && (newj==oldj-1) ) ||
( (newi==1 && newj==6) && (newj==oldj+1) || (newi==1 && newj==5) && (newj==oldj-1) ) ||
( (newi==6 && newj==8) && (newj==oldj+1) || (newi==6 && newj==7) && (newj==oldj-1) ) ||
( (newi==8 && newj==1) && (newj==oldj+1) || (newi==8 && newj==0) && (newj==oldj-1) ) ||
( (newi==9 && newj==1) && (newj==oldj+1) || (newi==9 && newj==0) && (newj==oldj-1) )
)
return true;
else
return false;
}
public static void updatePolicy(int i, int j, double max, double[] actions, char[][] Policy) {
if (max == actions[0]) {
if ( Policy[i][j-1] != 'u' && !barrier(i, j, i, j-1) ){
Policy[i][j] = 'd';
//return;
}
}
if (max == actions[1]) {
if (Policy[i-1][j] != 'r' && !barrier(i, j, i-1, j) ){
Policy[i][j] = 'l';
//return;
}
}
if (max == actions[2]) {
if (Policy[i][j+1] != 'd' && !barrier(i, j, i, j+1) ){
Policy[i][j] = 'u';
//return;
}
}
if (max == actions[3]) {
if (Policy[i+1][j] != 'l' && !barrier(i, j, i+1, j) ){
Policy[i][j] = 'r';
//return;
}
}
}
public static void printResults(double[][] V, char[][] Policy) {
// Print value function
System.out.println("Value Function");
for (int i=V.length-1; i>=0; i--) {
String s = "";
for (int j=0; j<V[0].length; j++) {
s += V[i][j] + " ";
}
System.out.println(s);
}
// Print policy function
System.out.println("\nPolicy Function");
for (int j=Policy[0].length-1; j>=0; j--) {
String s = "";
for (int i=0; i<Policy.length; i++) {
s += Policy[i][j] + " ";
}
System.out.println(s);
}
}
}