-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMLPlayerAlphaOne.java
More file actions
348 lines (299 loc) · 11 KB
/
Copy pathMLPlayerAlphaOne.java
File metadata and controls
348 lines (299 loc) · 11 KB
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
import java.io.*;
import java.net.*;
public class MLPlayerAlphaOne
{
// If debug should be printed or not.
private boolean PRINT_DEBUG = false;
// The constants that define the reinforcement learning algorithm.
double epsilon = 0.0f; // FOR TESTING!
double eta = 0.25f;
double gamma = 0.99f;
// An internal board representation.
private int internal_board[][] = null;
// The player.
Player thePlayer;
// The player id of this player.
int player_id = 0;
// Constructor.
public MLPlayerAlphaOne(Player p)
{
thePlayer = p;
}
// The play function, which connects to the socket and plays.
public void play(int my_id) throws IOException,ClassNotFoundException
{
// Open socket and in/out streams.
Socket sock = new Socket("localhost", Player.getSocketNumber(thePlayer));
ObjectOutputStream out = new ObjectOutputStream(sock.getOutputStream());
ObjectInputStream in = new ObjectInputStream(sock.getInputStream());
BufferedReader sysin = new BufferedReader(new InputStreamReader(System.in));
Weights weights = new Weights("alpha_weights.txt");
// Create the weights file if it doesn't exist and initialize to default values.
File weights_file = new File("alpha_weights.txt");
if (!weights_file.exists())
{
System.out.println("Weights file does not exist. Creating one with default weights");
weights_file.createNewFile();
double[] initialWeights = new double[FeatureExplorer.getNumFeatures()];
for(int x = 0; x < FeatureExplorer.getNumFeatures(); x++)
{
double k = 1.0f / Math.sqrt((double)FeatureExplorer.getNumFeatures());
initialWeights[x] = 2.0f * Math.random() * k - k;
}
weights.setWeights(initialWeights);
weights.saveWeights();
}
// Set the id.
player_id = my_id;
// Get the game rules.
Rules gameRules = (Rules)in.readObject();
if (PRINT_DEBUG) System.out.printf("Num Rows: %d, Num Cols: %d, Num Connect: %d\n", gameRules.numRows, gameRules.numCols, gameRules.numConnect);
// Create the internal board.
internal_board = new int[gameRules.numRows][gameRules.numCols];
for (int r = 0; r < gameRules.numRows; r++)
for (int c = 0; c < gameRules.numCols; c++)
internal_board[r][c] = 0;
// Start playing the game, first by waiting fo the initial message.
if (PRINT_DEBUG) System.out.println("Waiting...");
GameMessage mess = (GameMessage)in.readObject();
// The main game loop.
double approx_qsa = -1.0f;
double y_i = 0.0f; // For the current board (state i+1).
double[] x_i = new double[FeatureExplorer.getNumFeatures()]; // For the previous board after you took an action AND opponent took action (state i).
double[] x_ip1 = new double[FeatureExplorer.getNumFeatures()]; // For the current board after you took an action AND oppoent took action (state i+1).
boolean beginning = true;
int move = 0;
int selected_column = 0;
while(mess.win == Player.EMPTY)
{
if (PRINT_DEBUG) weights.printWeights();
// If the first message is not the begin message (-1), then record what the other player did.
if(mess.move != -1)
{
int r = 0;
for (r = 0; r < gameRules.numRows; r++)
{
if (internal_board[r][mess.move] != 0)
{
if (r == 0) break;
internal_board[r - 1][mess.move] = player_id % 2 + 1;
break;
}
}
if (r > 0 && r == gameRules.numRows) internal_board[r - 1][mess.move] = player_id % 2 + 1;
if (r == 0) System.out.println("Alpha has detected an improper move made by the other player.");
}
else
{
// Randomly move the first time if you are first.
mess.move = (int)((float)gameRules.numCols * Math.random());
// Create the features for the blank board.
FeatureExplorer ff = new FeatureExplorer();
ff.initialize(internal_board, gameRules.numRows, gameRules.numCols, -1, player_id);
x_ip1 = ff.getFeatures(); // Previous board given that we chose this action.
// Update internal representaiton and tell server about the move (e.g. take action).
internal_board[gameRules.numRows - 1][mess.move] = player_id;
out.writeObject(mess); // Tell the server.
mess = (GameMessage)in.readObject();
// Continue to the next state.
continue;
}
// (Determine an action) Create features based on the current board layout.
FeatureExplorer[] ff = new FeatureExplorer[gameRules.numCols];
boolean[] ff_use = new boolean[gameRules.numCols];
int numFeatures = FeatureExplorer.getNumFeatures();
double[][] features = new double[gameRules.numCols][numFeatures];
double[] wx = new double[gameRules.numCols];
double[] sig = new double[gameRules.numCols];
double[] w = weights.getWeights();
double max = 0;
int action = 0;
double temp;
for(int x = 0; x < gameRules.numCols; x++)
{
ff[x] = new FeatureExplorer();
ff_use[x] = ff[x].initialize(internal_board, gameRules.numRows, gameRules.numCols, x, player_id);
if(ff_use[x]) features[x] = ff[x].getFeatures();
if (PRINT_DEBUG) System.out.println("Action Column " + x + " Features:");
if (PRINT_DEBUG) printD(features[x]);
for(int y = 0; y < numFeatures; y++)
{
//System.out.println("ff_use: " + ff_use[x]);
if(ff_use[x])
{
//if(beginning)
//{
// if(mess.move == x)
// wx[x] += ((double)features[x][y])*w[y];
// else
// wx[x] = 0;
//}
//else
wx[x] += ((double)features[x][y]) * w[y];
}
else
wx[x] = 0;
}
if(ff_use[x])
sig[x] = sigmoid(wx[x]);
else
sig[x] = 0;
if (PRINT_DEBUG) System.out.printf("... and the weights: wx[%d]: %f approx_qsa[%d]: %f\n", x, wx[x], x, sig[x]);
if(x == 0)
{
max = sig[0];
action = 0;
}
else if(sig[x] > max)
{
max = sig[x];
action = x;
}
//System.out.println("\t" + sig[x]);
}
// The Q(s, a) value is the max of all of these. (Not used, just for a note).
approx_qsa = max;
if (PRINT_DEBUG) System.out.println("----------------------->>> Decided to take action " + action);
// (Exploration function) Epsilon-greedy.
if(Math.random() > epsilon)
selected_column = action;
else
{
selected_column = (int)(Math.random() * gameRules.numCols);
for (int i = 0; i < 1000 && internal_board[0][selected_column] != 0; i++) selected_column = (int)(Math.random() * gameRules.numCols);
System.out.println("I'm exploring a bit! I choose action: " + selected_column);
}
// (Compute previous board layout) Previous is equal to the old current.
for (int j = 0; j < FeatureExplorer.getNumFeatures(); j++) x_i[j] = x_ip1[j];
// (Compute Current board layout) Since this is after you have gone and your opponent has gone, calculate the ***current*** board layout.
FeatureExplorer ff_cur = new FeatureExplorer();
ff_cur.initialize(internal_board, gameRules.numRows, gameRules.numCols, -1, player_id);
x_ip1 = ff_cur.getFeatures();
// (Update weights) Do the update of the weight vector for the previous iteration.
if(beginning == false)
{
if (PRINT_DEBUG) System.out.println("Approximated Q(s, a) (Intermediate): " + approx_qsa + "\tplayer_id: " + player_id);
sarsa(0, weights, x_i, x_ip1);
}
// (Take action) Update the internal representation for where this player put his token. Then, send the action to the server.
int r = 0;
for (r = 0; r < gameRules.numRows; r++)
{
if (internal_board[r][selected_column] != 0)
{
internal_board[r - 1][selected_column] = player_id;
break;
}
}
if (r == gameRules.numRows) internal_board[r - 1][selected_column] = player_id;
mess.move = selected_column;
out.writeObject(mess);
mess = (GameMessage)in.readObject();
// The beginning has happened now.
beginning = false;
}
// Print out the final approximated Q(s, a) value and the player id.
System.out.println("Approximated Q(s, a): " + approx_qsa + "\tplayer_id: " + player_id);
// (Compute previous board layout) Previous is equal to the old current.
for (int j = 0; j < FeatureExplorer.getNumFeatures(); j++) x_i[j] = x_ip1[j];
// (Compute Current board layout) Since this is after you have gone and your opponent has gone, calculate the ***current*** board layout.
FeatureExplorer ff_cur = new FeatureExplorer();
ff_cur.initialize(internal_board, gameRules.numRows, gameRules.numCols, -1, player_id);
x_ip1 = ff_cur.getFeatures();
// (Final Weight Update) Do a final update based on win/loss.
if(mess.win == thePlayer)
{
sarsa(1, weights, x_i, x_ip1); //reward 1 for win
System.out.println("MLPlayerAlphaOne has won.");
}
else
{
sarsa(0, weights, x_i, x_ip1); //reward 0 for loss
System.out.println("MLPlayerAlphaOne has lost.");
}
// Save the weights.
weights.saveWeights();
// Close the socket.
sock.close();
}
private double sigmoid(double t)
{
return 1.0f/(1.0f+(double)Math.exp(-t));
}
private void sarsa(int reward, Weights weights, double x_i[], double[] x_ip1)
{
// Set some local variables.
double[] w = weights.getWeights();
double wx = 0.0f;
double sig = 0.0f;
// Compute y_i.
double wxp1 = 0.0f;
for (int j = 0; j < FeatureExplorer.getNumFeatures(); j++) wxp1 += w[j] * x_ip1[j];
double sigp1 = sigmoid(wxp1);
double y_i = reward + gamma * sigp1;
// Print out some debug if needed.
if (PRINT_DEBUG)
{
System.out.println("SARSA Weights:");
weights.printWeights();
}
// For each feature, compute the dot product of w and x.
for (int j = 0; j < FeatureExplorer.getNumFeatures(); j++) wx += w[j] * x_i[j];
if (PRINT_DEBUG)
System.out.println("\twx: " + wx);
// Compute the sigmoid of this.
sig = sigmoid(wx);
if (PRINT_DEBUG)
System.out.println("\tsigmoid(wx): " + sig);
// Weight vector update.
for (int j = 0; j < FeatureExplorer.getNumFeatures(); j++)
w[j] = w[j] + eta * ((y_i - sig) * (sig) * (1 - sig) * x_i[j]);
// Set the new weights.
weights.setWeights(w);
if (PRINT_DEBUG)
{
System.out.println("SARSA New Weights:");
weights.printWeights();
}
}
private void printD(double[] array)
{
for(int i = 0; i < array.length; i++)
System.out.println("\t" + array[i]);
}
// The main function.
public static void main(String[] args)
{
// If no argument is specified, throw an error.
if(args.length != 1)
{
System.out.println("Usage:\n java MPlayerAlphaOne [1|2]");
System.exit(-1);
}
// Get the player.
int my_id = Integer.parseInt(args[0]);
// Set the player object.
Player p = null;
if (my_id == 1) p = Player.ONE;
else if (my_id == 2) p = Player.TWO;
else
{
System.out.println("Usage:\n java MPlayerAlphaOne [1|2]");
System.exit(-1);
}
// Create the MLPlayer object, and begin play.
MLPlayerAlphaOne me = new MLPlayerAlphaOne(p);
try
{
me.play(my_id);
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
catch (ClassNotFoundException cnfe)
{
cnfe.printStackTrace();
}
}
}