-
Notifications
You must be signed in to change notification settings - Fork 0
/
RLPolicy.java
191 lines (139 loc) · 4.55 KB
/
RLPolicy.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
import java.lang.reflect.*;
import java.lang.*;
public class RLPolicy {
// Array qValuesTable;
int[] dimSize;
double[] qValues;
private Object qValuesTable;
int states, actions;
RLPolicy( int[] dimSize ) {
this.dimSize = dimSize;
// Create n-dimensional array with size given in dimSize array.
qValuesTable = Array.newInstance( double.class, dimSize );
// Get number of states.
states = dimSize[0];
for( int j = 1 ; j < dimSize.length - 1 ; j++)
states *= dimSize[j];
// Get number of actions.
actions = dimSize[dimSize.length - 1];
}
public void initValues( double initValue ) {
int i;
int actualdim = 0;
int state[] = new int[dimSize.length - 1];
System.out.println( "States: " + states );
for( int j = 0 ; j < states ; j++ ) {
qValues = (double[]) myQValues( state );
for( i = 0 ; i < actions ; i++ ) {
// System.out.print( i );
Array.setDouble( qValues, i, ( initValue )); //+ 0.0000000000000000001 * Math.random() ) );
}
state = getNextState( state );
}
}
private int[] getNextState( int[] state ) {
int i;
int actualdim = 0;
state[actualdim]++;
if( state[actualdim] >= dimSize[actualdim] ) {
while( ( actualdim < dimSize.length - 1 ) && ( state[actualdim] >= dimSize[actualdim] ) ) {
actualdim++;
if( actualdim == dimSize.length - 1 )
return state;
state[actualdim]++;
}
for( i = 0 ; i < actualdim ; i++ )
state[i] = 0;
actualdim = 0;
}
return state;
}
private double[] myQValues( int[] state ) {
int i;
Object curTable = qValuesTable;
for( i = 0 ; i < dimSize.length - 2 ; i++ ) {
//descend in each dimension
curTable = Array.get( curTable, state[i] );
}
//at last dimension of Array get QValues.
return (double[]) Array.get( curTable, state[i] );
}
public double[] getQValuesAt( int[] state ) {
int i;
Object curTable = qValuesTable;
double[] returnValues;
for( i = 0 ; i < dimSize.length - 2 ; i++ ) {
//descend in each dimension
curTable = Array.get( curTable, state[i] );
}
//at last dimension of Array get QValues.
qValues = (double[]) Array.get( curTable, state[i] );
returnValues = new double[ qValues.length ];
System.arraycopy( qValues, 0, returnValues, 0, qValues.length );
return returnValues;
}
public void setQValue( int[] state, int action, double newQValue ) {
qValues = myQValues( state );
Array.setDouble( qValues, action, newQValue );
}
public double getMaxQValue( int[] state ) {
double maxQ = -Double.MAX_VALUE;
qValues = myQValues( state );
for( int action = 0 ; action < qValues.length ; action++ ) {
if( qValues[action] > maxQ ) {
maxQ = qValues[action];
}
}
return maxQ;
}
public double getQValue( int[] state, int action ) {
double qValue = 0;
qValues = myQValues( state );
qValue = qValues[action];
return qValue;
}
public int getBestAction( int[] state ) {
double maxQ = -Double.MAX_VALUE;
int selectedAction = -1;
int[] doubleValues = new int[qValues.length];
int maxDV = 0;
qValues = myQValues( state );
for( int action = 0 ; action < qValues.length ; action++ ) {
//System.out.println( "STATE: [" + state[0] + "," + state[1] + "]" );
//System.out.println( "action:qValue, maxQ " + action + ":" + qValues[action] + "," + maxQ );
if( qValues[action] > maxQ ) {
selectedAction = action;
maxQ = qValues[action];
maxDV = 0;
doubleValues[maxDV] = selectedAction;
}
else if( qValues[action] == maxQ ) {
maxDV++;
doubleValues[maxDV] = action;
}
}
if( maxDV > 0 ) {
//System.out.println( "DOUBLE values, random selection, maxdv =" + maxDV );
int randomIndex = (int) ( Math.random() * ( maxDV + 1 ) );
selectedAction = doubleValues[ randomIndex ];
}
if( selectedAction == -1 ) {
//System.out.println("RANDOM Choice !" );
selectedAction = (int) ( Math.random() * qValues.length );
}
return selectedAction;
}
/*public double getBestAction( int[]state, int bestAction ) {
private double bestAction( int[] state, int bestAction ) {
double maxQ = 0;
bestAction = -1;
qValues = getQValuesAt( state );
for( int action = 0 ; action < qValues.length ; action++ ) {
if( qValues[action] > maxQ ) {
bestAction = action;
maxQ = qValues[action];
}
}
return maxQ;
*/
}