From 60c2c13a733549fd7660e046aca4f93fd9c2f66e Mon Sep 17 00:00:00 2001 From: Donato Meoli Date: Wed, 2 Dec 2026 12:34:44 +0100 Subject: [PATCH 1/5] Test BEST-FIRST-SEARCH as GraphSearch4e from the goal, without a solution and on a tie of f --- .../unit/search/informed/AStarSearchTest.java | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/aima-core/src/test/java/aima/test/core/unit/search/informed/AStarSearchTest.java b/aima-core/src/test/java/aima/test/core/unit/search/informed/AStarSearchTest.java index e606e90ba..8b818684b 100644 --- a/aima-core/src/test/java/aima/test/core/unit/search/informed/AStarSearchTest.java +++ b/aima-core/src/test/java/aima/test/core/unit/search/informed/AStarSearchTest.java @@ -18,6 +18,7 @@ import org.junit.Assert; import org.junit.Test; +import java.util.Arrays; import java.util.List; import java.util.function.Predicate; import java.util.function.ToDoubleFunction; @@ -238,4 +239,34 @@ public void testEdgeCaseTieBetweenEqualCostPaths() { Assert.assertTrue(actions.isPresent()); Assert.assertEquals(2.0, edgeCaseCost(p, actions.get()), 0.0); } + + // BEST-FIRST-SEARCH as GraphSearch4e (figure 3.7): the start is a goal, the + // goal cannot be reached, and two paths to the goal tie on f = g + + @Test + public void testBestFirstSearchOfFigure3_7FromTheGoalAndWithoutASolution() { + Assert.assertEquals(Optional.of(Collections.emptyList()), new AStarSearch( + new GraphSearch4e<>(), n -> 0.0).findActions(edgeCaseProblem("A", "A"))); + Assert.assertEquals(Optional.empty(), new AStarSearch(new GraphSearch4e<>(), n -> 0.0) + .findActions(edgeCaseProblem("A", "Z"))); + } + + @Test + public void testBestFirstSearchOfFigure3_7OnATie() { + // S - A - G and S - B - G both cost 2: either is returned, at cost 2 + ExtendableMap map = new ExtendableMap(); + map.addUnidirectionalLink("S", "A", 1.0); + map.addUnidirectionalLink("S", "B", 1.0); + map.addUnidirectionalLink("A", "G", 1.0); + map.addUnidirectionalLink("B", "G", 1.0); + Problem p = new GeneralProblem<>("S", MapFunctions.createActionsFunction(map), + MapFunctions.createResultFunction(), Predicate.isEqual("G"), + MapFunctions.createDistanceStepCostFunction(map)); + Optional> actions = new AStarSearch(new GraphSearch4e<>(), n -> 0.0) + .findActions(p); + Assert.assertTrue(actions.isPresent()); + Assert.assertEquals(2, actions.get().size()); + Assert.assertTrue(Arrays.asList("A", "B").contains(actions.get().get(0).getToLocation())); + Assert.assertEquals(2.0, edgeCaseCost(p, actions.get()), 0.0); + } } From 4b3ec1d9574d4e47cc5e0be298e2148e7ae9d2b8 Mon Sep 17 00:00:00 2001 From: Donato Meoli Date: Wed, 2 Dec 2026 12:34:44 +0100 Subject: [PATCH 2/5] Test the doubles tennis problem with its goal at the start and with an unreachable goal --- .../core/unit/logic/planning/StateSpacePlanningTest.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/aima-core/src/test/java/aima/test/core/unit/logic/planning/StateSpacePlanningTest.java b/aima-core/src/test/java/aima/test/core/unit/logic/planning/StateSpacePlanningTest.java index d5080cf8d..c01106834 100644 --- a/aima-core/src/test/java/aima/test/core/unit/logic/planning/StateSpacePlanningTest.java +++ b/aima-core/src/test/java/aima/test/core/unit/logic/planning/StateSpacePlanningTest.java @@ -257,14 +257,16 @@ public void unsolvableProblemHasNoForwardPlan() { { "air cargo", "At(C1,SFO)^Plane(P1)", "Plane(C1)" }, { "spare tire", "At(Flat,Axle)", "Tire(Axle)" }, { "three-block tower", "On(C,A)^Clear(B)", "Block(Table)" }, - { "blocks world", "On(A,Table)^Clear(C)", "Block(Table)" } }; + { "blocks world", "On(A,Table)^Clear(C)", "Block(Table)" }, + { "doubles tennis", "At(A,LeftBaseline)^Partner(A,B)", "Actor(Ball)" } }; private static PlanningProblem standardProblem(int i, String goal) { PlanningProblem p = i == 0 ? PlanningProblemFactory.airCargoTransportProblem() : i == 1 ? PlanningProblemFactory.spareTireProblem() : i == 2 ? PlanningProblemFactory.threeBlockTowerProblem() - : PlanningProblemFactory.blocksWorldProblem("On(A,Table)^On(B,A)^On(C,Table)^Clear(B)^Clear(C)", - "On(A,B)", "A", "B", "C"); + : i == 3 ? PlanningProblemFactory.blocksWorldProblem( + "On(A,Table)^On(B,A)^On(C,Table)^Clear(B)^Clear(C)", "On(A,B)", "A", "B", "C") + : PlanningProblemFactory.doublesTennisProblem(); return new PlanningProblem(p.getInitialState(), Utils.parse(goal), p.getActionSchemas()); } From 039bc9bf799d1360c9c0ab0757a89cac91284524 Mon Sep 17 00:00:00 2001 From: Donato Meoli Date: Wed, 2 Dec 2026 12:34:45 +0100 Subject: [PATCH 3/5] Test particle filtering with a perfect sensor, whose weights of 0 rule out the contradicting particles --- .../bayes/approx/ParticleFilterTest.java | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/aima-core/src/test/java/aima/test/core/unit/probability/bayes/approx/ParticleFilterTest.java b/aima-core/src/test/java/aima/test/core/unit/probability/bayes/approx/ParticleFilterTest.java index 297f88e1c..391a52324 100644 --- a/aima-core/src/test/java/aima/test/core/unit/probability/bayes/approx/ParticleFilterTest.java +++ b/aima-core/src/test/java/aima/test/core/unit/probability/bayes/approx/ParticleFilterTest.java @@ -3,7 +3,16 @@ import org.junit.Assert; import org.junit.Test; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +import aima.core.probability.RandomVariable; +import aima.core.probability.bayes.FiniteNode; import aima.core.probability.bayes.approx.ParticleFiltering; +import aima.core.probability.bayes.impl.BayesNet; +import aima.core.probability.bayes.impl.DynamicBayesNet; +import aima.core.probability.bayes.impl.FullCPTNode; import aima.core.probability.example.DynamicBayesNetExampleFactory; import aima.core.probability.example.ExampleRV; import aima.core.probability.proposition.AssignmentProposition; @@ -97,4 +106,29 @@ public void testASingleParticle() { Assert.assertEquals(ExampleRV.RAIN_t_RV, S[0][0].getTermVariable()); Assert.assertEquals(true, S[0][0].getValue()); } + + @Test + public void testAPerfectSensorRulesOutTheParticlesThatContradictIt() { + // the umbrella world with P(umbrella | rain) = 1 and P(umbrella | not + // rain) = 0: the particles without rain get weight 0 and are never + // resampled + FiniteNode prior = new FullCPTNode(ExampleRV.RAIN_tm1_RV, new double[] { 0.5, 0.5 }); + FiniteNode rainTm1 = new FullCPTNode(ExampleRV.RAIN_tm1_RV, new double[] { 0.5, 0.5 }); + FiniteNode rainT = new FullCPTNode(ExampleRV.RAIN_t_RV, new double[] { 0.7, 0.3, 0.3, 0.7 }, rainTm1); + new FullCPTNode(ExampleRV.UMBREALLA_t_RV, new double[] { 1, 0, 0, 1 }, rainT); + Map x0ToX1 = new HashMap<>(); + x0ToX1.put(ExampleRV.RAIN_tm1_RV, ExampleRV.RAIN_t_RV); + DynamicBayesNet dbn = new DynamicBayesNet(new BayesNet(prior), x0ToX1, + Collections.singleton(ExampleRV.UMBREALLA_t_RV), rainTm1); + // the prior gives rain to the first two particles; the transitions + // give rain at t to the first and the third; resampling draws from + // the weights (1/2, 0, 1/2, 0) + ParticleFiltering pf = new ParticleFiltering(4, dbn, new MockRandomizer( + new double[] { 0.4, 0.4, 0.6, 0.6, 0.7, 0.71, 0.29, 0.31, 0.1, 0.45, 0.55, 0.99 })); + AssignmentProposition[][] S = pf.particleFiltering(new AssignmentProposition[] { + new AssignmentProposition(ExampleRV.UMBREALLA_t_RV, true) }); + Assert.assertEquals(4, S.length); + for (AssignmentProposition[] particle : S) + Assert.assertEquals(true, particle[0].getValue()); + } } From 2086dd775659f245d51568d5b9507377c8f1f3e2 Mon Sep 17 00:00:00 2001 From: Donato Meoli Date: Wed, 2 Dec 2026 12:34:45 +0100 Subject: [PATCH 4/5] Give the optical flow of sumSquaredDifference a largest shift, 30 by default, and test it with no shift allowed --- .../aima/core/perception/ImageProcessing.java | 16 ++++++++++++++-- .../core/unit/perception/PerceptionTest.java | 18 ++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/aima-core/src/main/java/aima/core/perception/ImageProcessing.java b/aima-core/src/main/java/aima/core/perception/ImageProcessing.java index eb249a03c..269292c6d 100644 --- a/aima-core/src/main/java/aima/core/perception/ImageProcessing.java +++ b/aima-core/src/main/java/aima/core/perception/ImageProcessing.java @@ -282,6 +282,18 @@ public static double[] hog(double[][] image, int cellSize, int bins, int blockSi * differences. */ public static FlowEstimate sumSquaredDifference(double[][] pic1, double[][] pic2) { + return sumSquaredDifference(pic1, pic2, 30); + } + + /** + * As {@link #sumSquaredDifference(double[][], double[][])}, with the shifts + * between -maxShift and maxShift in each direction; with maxShift = 0 the + * only shift is no motion. + */ + public static FlowEstimate sumSquaredDifference(double[][] pic1, double[][] pic2, int maxShift) { + if (maxShift < 0) { + throw new IllegalArgumentException("The largest shift cannot be negative."); + } int rows = pic1.length; int cols = pic1[0].length; if (rows != pic2.length || cols != pic2[0].length) { @@ -290,8 +302,8 @@ public static FlowEstimate sumSquaredDifference(double[][] pic1, double[][] pic2 double ssd = Double.POSITIVE_INFINITY; int bestDx = 0; int bestDy = 0; - for (int dx = -30; dx <= 30; dx++) { - for (int dy = -30; dy <= 30; dy++) { + for (int dx = -maxShift; dx <= maxShift; dx++) { + for (int dy = -maxShift; dy <= maxShift; dy++) { double diff = 0; for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { diff --git a/aima-core/src/test/java/aima/test/core/unit/perception/PerceptionTest.java b/aima-core/src/test/java/aima/test/core/unit/perception/PerceptionTest.java index a66afb6b8..d7bbc7d1e 100644 --- a/aima-core/src/test/java/aima/test/core/unit/perception/PerceptionTest.java +++ b/aima-core/src/test/java/aima/test/core/unit/perception/PerceptionTest.java @@ -322,6 +322,24 @@ public void testFlowOfASinglePixel() { Assert.assertEquals(4, flow.getSsd(), DELTA); } + @Test + public void testFlowWithNoShiftAllowed() { + // the second frame is the first moved down by one row; with no shift + // allowed the flow is (0, 0), with the sum of squared differences of + // the frames as they are, while a shift of 1 finds the motion + double[][] pic1 = { { 0, 0, 0 }, { 0, 9, 0 }, { 0, 0, 0 }, { 0, 0, 0 } }; + double[][] pic2 = { { 0, 0, 0 }, { 0, 0, 0 }, { 0, 9, 0 }, { 0, 0, 0 } }; + ImageProcessing.FlowEstimate still = ImageProcessing.sumSquaredDifference(pic1, pic2, 0); + Assert.assertEquals(0, still.getDx()); + Assert.assertEquals(0, still.getDy()); + Assert.assertEquals(162, still.getSsd(), DELTA); + ImageProcessing.FlowEstimate moved = ImageProcessing.sumSquaredDifference(pic1, pic2, 1); + Assert.assertEquals(0, moved.getSsd(), DELTA); + Assert.assertEquals(moved.getDx(), ImageProcessing.sumSquaredDifference(pic1, pic2).getDx()); + Assert.assertEquals(1, Math.abs(moved.getDx())); + Assert.assertEquals(0, moved.getDy()); + } + @Test public void testSegmentationEdgeCases() { // a single pixel, and a single level, give a black picture From 7d07ce7d7dc60db6f4e793762de9ee91915cecc8 Mon Sep 17 00:00:00 2001 From: Donato Meoli Date: Wed, 2 Dec 2026 12:44:52 +0100 Subject: [PATCH 5/5] Fail WalkSAT on an empty clause and leave the constants True and False out of its models, and test the propositional solvers on sentences with the constants --- .../propositional/inference/WalkSAT.java | 16 ++++++- .../inference/SatSolverTest.java | 46 +++++++++++++++++++ 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/aima-core/src/main/java/aima/core/logic/propositional/inference/WalkSAT.java b/aima-core/src/main/java/aima/core/logic/propositional/inference/WalkSAT.java index 6ce765c15..09e9a6abd 100644 --- a/aima-core/src/main/java/aima/core/logic/propositional/inference/WalkSAT.java +++ b/aima-core/src/main/java/aima/core/logic/propositional/inference/WalkSAT.java @@ -58,7 +58,14 @@ public class WalkSAT { */ public Model walkSAT(Set clauses, double p, int maxFlips) { assertLegalProbability(p); - + // the empty clause (e.g., from False) is false in every model: no + // flip can satisfy it + for (Clause c : clauses) { + if (c.isFalse()) { + return null; + } + } + // model <- a random assignment of true/false to the symbols in clauses Model model = randomAssignmentToSymbolsInClauses(clauses); // for i = 1 to max_flips do (Note: maxFlips < 0 means infinity) @@ -122,7 +129,12 @@ protected Model randomAssignmentToSymbolsInClauses(Set clauses) { // Collect the symbols in clauses Set symbols = new LinkedHashSet(); for (Clause c : clauses) { - symbols.addAll(c.getSymbols()); + for (PropositionSymbol symbol : c.getSymbols()) { + // True and False are constants, not symbols to assign + if (!symbol.isAlwaysTrue() && !symbol.isAlwaysFalse()) { + symbols.add(symbol); + } + } } // Make initial set of assignments diff --git a/aima-core/src/test/java/aima/test/core/unit/logic/propositional/inference/SatSolverTest.java b/aima-core/src/test/java/aima/test/core/unit/logic/propositional/inference/SatSolverTest.java index fe1132c56..0ce174275 100644 --- a/aima-core/src/test/java/aima/test/core/unit/logic/propositional/inference/SatSolverTest.java +++ b/aima-core/src/test/java/aima/test/core/unit/logic/propositional/inference/SatSolverTest.java @@ -6,6 +6,7 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.Random; import java.util.Set; import org.junit.Assert; @@ -18,16 +19,21 @@ import aima.core.logic.propositional.inference.DimacsParser; import aima.core.logic.propositional.inference.MapColoringSAT; import aima.core.logic.propositional.inference.OptimizedDPLL; +import aima.core.logic.propositional.inference.OptimizedPLResolution; +import aima.core.logic.propositional.inference.PLResolution; import aima.core.logic.propositional.inference.RestartStrategies; import aima.core.logic.propositional.inference.RestartStrategy; import aima.core.logic.propositional.inference.SATPlan; import aima.core.logic.propositional.inference.SATSolver; import aima.core.logic.propositional.inference.TwoWLClauseDatabase; +import aima.core.logic.propositional.inference.WalkSAT; +import aima.core.logic.propositional.kb.KnowledgeBase; import aima.core.logic.propositional.kb.data.Clause; import aima.core.logic.propositional.kb.data.Literal; import aima.core.logic.propositional.kb.data.Model; import aima.core.logic.propositional.parsing.PLParser; import aima.core.logic.propositional.parsing.ast.PropositionSymbol; +import aima.core.logic.propositional.parsing.ast.Sentence; import aima.core.logic.propositional.transformations.ConvertToConjunctionOfClauses; import aima.core.util.datastructure.Pair; import java.util.function.IntUnaryOperator; @@ -403,4 +409,44 @@ public void testMapColoringOfASingleRegionAndOfNoRegions() { // and without colors a region cannot be colored Assert.assertNull(new DPLL().solve(MapColoringSAT.create(new ArrayList<>(), "T: "))); } + + @Test + public void testTheConstantsTrueAndFalseInsideASentence() { + // False and A & False are unsatisfiable, the others satisfiable; the + // constants are no symbols, so a model never assigns them + PLParser parser = new PLParser(); + String[] unsatisfiable = { "False", "A & False", "~True" }; + String[] satisfiable = { "True", "A | False", "A | True", "~False", "~A | ~False", "A <=> False" }; + for (String text : unsatisfiable) { + Sentence s = parser.parse(text); + Set clauses = ConvertToConjunctionOfClauses.apply(s).getClauses(); + Assert.assertTrue(text, clauses.stream().anyMatch(Clause::isFalse)); + Assert.assertFalse(text, new DPLL().isSatisfiable(s)); + Assert.assertFalse(text, new OptimizedDPLL().isSatisfiable(s)); + Assert.assertFalse(text, new CDCL().isSatisfiable(s)); + Assert.assertNull(text, new WalkSAT(new Random(0)).walkSAT(clauses, 0.5, 100)); + KnowledgeBase kb = new KnowledgeBase(); + kb.tell(s); + Assert.assertTrue(text, new PLResolution().isEntailed(kb, PropositionSymbol.FALSE)); + Assert.assertTrue(text, new OptimizedPLResolution().isEntailed(kb, PropositionSymbol.FALSE)); + } + for (String text : satisfiable) { + Sentence s = parser.parse(text); + Set clauses = ConvertToConjunctionOfClauses.apply(s).getClauses(); + Assert.assertFalse(text, clauses.stream().anyMatch(Clause::isFalse)); + Assert.assertTrue(text, new DPLL().isSatisfiable(s)); + Assert.assertTrue(text, new OptimizedDPLL().isSatisfiable(s)); + Assert.assertTrue(text, new CDCL().isSatisfiable(s)); + for (Model model : Arrays.asList(new DPLL().solve(clauses), new CDCL().solve(clauses), + new WalkSAT(new Random(0)).walkSAT(clauses, 0.5, 100))) { + Assert.assertTrue(text, model.satisfies(clauses)); + Assert.assertNull(text, model.getValue(PropositionSymbol.TRUE)); + Assert.assertNull(text, model.getValue(PropositionSymbol.FALSE)); + } + KnowledgeBase kb = new KnowledgeBase(); + kb.tell(s); + Assert.assertFalse(text, new PLResolution().isEntailed(kb, PropositionSymbol.FALSE)); + Assert.assertFalse(text, new OptimizedPLResolution().isEntailed(kb, PropositionSymbol.FALSE)); + } + } }