From 4dad243fb79bb2e73f4b479178ddf61e2f8892c3 Mon Sep 17 00:00:00 2001 From: Donato Meoli Date: Wed, 2 Dec 2026 14:33:20 +0100 Subject: [PATCH] Pin the outcomes of the random experiments of exercises 18.32, 21.7 and 21.9 The values are those java.util.Random gives with the fixed seeds; the qualitative asserts stay. --- .../core/unit/exercises/Exercises18Test.java | 2 ++ .../core/unit/exercises/Exercises21Test.java | 19 +++++++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/aima-core/src/test/java/aima/test/core/unit/exercises/Exercises18Test.java b/aima-core/src/test/java/aima/test/core/unit/exercises/Exercises18Test.java index 3f8d00134..f2a8b29da 100644 --- a/aima-core/src/test/java/aima/test/core/unit/exercises/Exercises18Test.java +++ b/aima-core/src/test/java/aima/test/core/unit/exercises/Exercises18Test.java @@ -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 ---- diff --git a/aima-core/src/test/java/aima/test/core/unit/exercises/Exercises21Test.java b/aima-core/src/test/java/aima/test/core/unit/exercises/Exercises21Test.java index 444ce47d7..e5197672e 100644 --- a/aima-core/src/test/java/aima/test/core/unit/exercises/Exercises21Test.java +++ b/aima-core/src/test/java/aima/test/core/unit/exercises/Exercises21Test.java @@ -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 @@ -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 randomPlayer = s -> { List moves = game.getActions(s); @@ -740,10 +750,15 @@ public void testCh21Ex09() { double[] w = players[k]; int d = depth; java.util.function.Function 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]);