Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,8 @@ public void testCh18Ex32() throws Exception {
Assert.assertTrue(e >= 0 && e <= 1);
Assert.assertTrue(errors[best] <= e);
}
// the errors that java.util.Random gives with these seeds; the book fixes no outcome
Assert.assertArrayEquals(new double[] { 8 / 12.0, 10 / 12.0, 10 / 12.0, 9 / 12.0 }, errors, 1e-12);
}

// ---- partial exercises ----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,8 @@ public void testCh21Ex07() {
residuals[g] = leastSquaresResidual(rows, ys) / rows.size();
}
Assert.assertTrue(residuals[0] < residuals[1] / 4);
// the residuals that java.util.Random gives with this seed; the book fixes no outcome
Assert.assertArrayEquals(new double[] { 0.0067634213, 0.0471755584 }, residuals, 1e-10);
}

// 21-Reinforcement-Learning/ex_10
Expand Down Expand Up @@ -727,6 +729,14 @@ public void testCh21Ex09() {
// a pair of the player to move wins, and the centre is worth more to X than to O
Assert.assertTrue(weights[4] > 0 && weights[7] < 0);
Assert.assertTrue(weights[2] > weights[3]);
// the weights and the games below are those that java.util.Random gives with this seed; the book fixes
// no outcome
Assert.assertArrayEquals(new double[] { -0.1569866057, -0.0223159801, 0.0025902439, -0.0723811458,
0.2691241149, -0.0836255105, -0.0634565847, -0.3329931074, 0.4861153821 }, weights, 1e-10);
// won, drawn and lost as X and as O, for the trained and the untrained evaluation, looking 0 and 2 moves
// ahead
int[][][][] expected = { { { { 287, 8, 5 }, { 273, 14, 13 } }, { { 262, 4, 34 }, { 164, 5, 131 } } },
{ { { 299, 1, 0 }, { 208, 62, 30 } }, { { 288, 8, 4 }, { 202, 58, 40 } } } };
Random random = new Random(9);
java.util.function.Function<TicTacToeState, XYLocation> randomPlayer = s -> {
List<XYLocation> moves = game.getActions(s);
Expand All @@ -740,10 +750,15 @@ public void testCh21Ex09() {
double[] w = players[k];
int d = depth;
java.util.function.Function<TicTacToeState, XYLocation> player = s -> td.chooseMove(w, s, d);
int[][] wdl = new int[2][3];
for (int g = 0; g < 300; g++) {
losses[k] += playTicTacToe(game, player, randomPlayer) == 0 ? 1 : 0;
losses[k] += playTicTacToe(game, randomPlayer, player) == 1 ? 1 : 0;
double asX = playTicTacToe(game, player, randomPlayer);
double asO = 1 - playTicTacToe(game, randomPlayer, player);
wdl[0][asX == 1 ? 0 : asX == 0 ? 2 : 1]++;
wdl[1][asO == 1 ? 0 : asO == 0 ? 2 : 1]++;
}
losses[k] = wdl[0][2] + wdl[1][2];
Assert.assertArrayEquals(expected[depth / 2][k], wdl);
}
// on 220 seeds the ratio is at most 1/3 looking one move ahead and 3/4 with the search
Assert.assertTrue(losses[0] < (depth == 0 ? 0.5 : 1) * losses[1]);
Expand Down