From fe9135df7fd4cbdca63d08eda4274b2bb6e4ecb4 Mon Sep 17 00:00:00 2001 From: Donato Meoli Date: Wed, 2 Dec 2026 15:32:55 +0100 Subject: [PATCH 1/2] Measure the errors of the tabular and linear estimates in exercise 21.7 --- .../core/unit/exercises/Exercises21Test.java | 134 ++++++++++++++++-- 1 file changed, 121 insertions(+), 13 deletions(-) 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 e5197672e..e6211e0e4 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 @@ -460,6 +460,90 @@ private static double leastSquaresResidual(List rows, List ys) return rss; } + /** The greedy policy of value iteration in the 10x10 world. */ + private static int[] greedy(Tab w, double gamma) { + double[] u = values(w, gamma, 1e-4); + int[] policy = new int[100]; + for (int s = 0; s < 100; s++) + for (int a = 1; a < 4; a++) { + double va = 0, vb = 0; + for (int t = 0; t < 100; t++) { + double rt = -0.04 + (w.terminal[t] ? 1 : 0) + gamma * u[t]; + va += w.p[s][a][t] * rt; + vb += w.p[s][policy[s]][t] * rt; + } + if (va > vb) + policy[s] = a; + } + return policy; + } + + /** + * Tabular and linear direct utility estimation, gamma = 1, on the same trials from random starts: the linear + * one moves theta by Widrow-Hoff on each reward-to-go. Returns the mean absolute errors of the tabular and of + * the linear estimates w.r.t. the utilities of the policy, and their mean gap, over the visited non-terminal + * states. + */ + private static double[] dueErrors(Tab w, int[] policy, double[][] features, int trials, Random random) { + int n = w.r.length; + double[] truth = new double[n]; + for (int k = 0; k < 2000; k++) { + double[] next = new double[n]; + for (int s = 0; s < n; s++) { + next[s] = w.r[s]; + if (!w.terminal[s]) + for (int t = 0; t < n; t++) + next[s] += w.p[s][policy[s]][t] * truth[t]; + } + truth = next; + } + List starts = new ArrayList<>(); + for (int s = 0; s < n; s++) + if (!w.terminal[s]) + starts.add(s); + double[] sum = new double[n], theta = new double[features[0].length]; + int[] visits = new int[n]; + for (int trial = 0; trial < trials; trial++) { + List path = new ArrayList<>(); + for (int s = starts.get(random.nextInt(starts.size()));; s = w.sample(s, policy[s], random.nextDouble())) { + path.add(s); + if (w.terminal[s]) + break; + } + double[] togo = new double[path.size()]; + for (int i = path.size() - 1; i >= 0; i--) + togo[i] = w.r[path.get(i)] + (i + 1 < path.size() ? togo[i + 1] : 0); + for (int i = 0; i < path.size(); i++) { + int s = path.get(i); + sum[s] += togo[i]; + visits[s]++; + double error = togo[i] - dot(theta, features[s]); + for (int j = 0; j < theta.length; j++) + theta[j] += 0.05 * error * features[s][j]; + } + } + double[] errors = new double[3]; + int seen = 0; + for (int s : starts) + if (visits[s] > 0) { + double tabular = sum[s] / visits[s], linear = dot(theta, features[s]); + errors[0] += Math.abs(tabular - truth[s]); + errors[1] += Math.abs(linear - truth[s]); + errors[2] += Math.abs(linear - tabular); + seen++; + } + for (int i = 0; i < 3; i++) + errors[i] /= seen; + return errors; + } + + private static double dot(double[] a, double[] b) { + double d = 0; + for (int i = 0; i < a.length; i++) + d += a[i] * b[i]; + return d; + } + // 21-Reinforcement-Learning/ex_7 @Test public void testCh21Ex07() { @@ -467,19 +551,7 @@ public void testCh21Ex07() { int[][] goals = { { 9, 9 }, { 4, 4 } }; for (int g = 0; g < 2; g++) { Tab w = grid10(goals[g][0], goals[g][1], false); - double[] u = values(w, 0.9, 1e-4); - int[] policy = new int[100]; - for (int s = 0; s < 100; s++) - for (int a = 1; a < 4; a++) { - double va = 0, vb = 0; - for (int t = 0; t < 100; t++) { - double rt = -0.04 + (w.terminal[t] ? 1 : 0) + 0.9 * u[t]; - va += w.p[s][a][t] * rt; - vb += w.p[s][policy[s]][t] * rt; - } - if (va > vb) - policy[s] = a; - } + int[] policy = greedy(w, 0.9); // direct utility estimation: the average discounted reward-to-go of each visit, R(s) in the states double[] sum = new double[100]; int[] visits = new int[100]; @@ -515,6 +587,42 @@ public void testCh21Ex07() { 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); + // as in the community answer: the mean absolute errors (tabular, linear) w.r.t. the true utilities after 300 + // trials, with the features (1, x, y) scaled to [0, 1] + Random random = new Random(7); + double[][] errors = new double[3][]; + for (int g = 0; g < 2; g++) { + Tab w = grid10(goals[g][0], goals[g][1], false); + double[][] features = new double[100][]; + for (int s = 0; s < 100; s++) + features[s] = new double[] { 1, s / 10 / 9.0, s % 10 / 9.0 }; + errors[g] = dueErrors(w, greedy(w, 0.999), features, 300, random); + } + Tab w = fourByThree(); + CellWorld cw = CellWorldFactory.createCellWorldForFig17_1(); + Map, CellWorldAction> pi = optimalPolicy(cw); + int[] policy = new int[w.r.length]; + double[][] features = new double[w.r.length][]; + for (int s = 0; s < w.r.length; s++) { + Cell c = w.cells.get(s); + if (!w.terminal[s]) + policy[s] = MOVES.indexOf(pi.get(cw.getCellAt(c.getX(), c.getY()))); + features[s] = new double[] { 1, (c.getX() - 1) / 3.0, (c.getY() - 1) / 2.0 }; + } + errors[2] = dueErrors(w, policy, features, 300, random); + // U is about linear in x and y with the goal in the corner, not in the middle: there the linear estimates are + // close to the tabular ones and to the true utilities, elsewhere worse than the tabular ones; unlike the + // community answer, the linear ones are not better than the tabular ones in the corner, since this DUE keeps + // the running average of the rewards-to-go, while that of aima-python halves the old estimate at each trial + Assert.assertTrue(errors[0][1] < errors[1][1] / 3); + Assert.assertTrue(errors[1][1] > errors[1][0]); + Assert.assertTrue(errors[2][1] > errors[2][0]); + Assert.assertTrue(errors[0][2] < errors[1][2] / 3); + // the errors that java.util.Random gives with this seed; they cannot equal those of aima-python, (0.044, + // 0.020), (0.030, 0.145) and (0.076, 0.117) + Assert.assertArrayEquals(new double[] { 0.0190533192, 0.0208230868 }, Arrays.copyOf(errors[0], 2), 1e-10); + Assert.assertArrayEquals(new double[] { 0.0208514293, 0.1384152882 }, Arrays.copyOf(errors[1], 2), 1e-10); + Assert.assertArrayEquals(new double[] { 0.0233248130, 0.0956447493 }, Arrays.copyOf(errors[2], 2), 1e-10); } // 21-Reinforcement-Learning/ex_10 From 524c7d04d74cf49bf3790058827d7e214fd58939 Mon Sep 17 00:00:00 2001 From: Donato Meoli Date: Wed, 2 Dec 2026 15:46:59 +0100 Subject: [PATCH 2/2] Compare the errors as aima-python does in exercise 21.7 --- .../java/aima/test/core/unit/exercises/Exercises21Test.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 e6211e0e4..05fd3178e 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 @@ -614,10 +614,10 @@ public void testCh21Ex07() { // close to the tabular ones and to the true utilities, elsewhere worse than the tabular ones; unlike the // community answer, the linear ones are not better than the tabular ones in the corner, since this DUE keeps // the running average of the rewards-to-go, while that of aima-python halves the old estimate at each trial - Assert.assertTrue(errors[0][1] < errors[1][1] / 3); + Assert.assertTrue(errors[0][1] < errors[1][1] / 2); Assert.assertTrue(errors[1][1] > errors[1][0]); Assert.assertTrue(errors[2][1] > errors[2][0]); - Assert.assertTrue(errors[0][2] < errors[1][2] / 3); + Assert.assertTrue(errors[0][2] < errors[1][2] / 2); // the errors that java.util.Random gives with this seed; they cannot equal those of aima-python, (0.044, // 0.020), (0.030, 0.145) and (0.076, 0.117) Assert.assertArrayEquals(new double[] { 0.0190533192, 0.0208230868 }, Arrays.copyOf(errors[0], 2), 1e-10);