Skip to content

Commit 6183425

Browse files
committed
Bug in level-off detection fixed. The use of nogoods as proposed in the textbook seems not to guarantee termination.
1 parent d914daf commit 6183425

File tree

3 files changed

+29
-10
lines changed

3 files changed

+29
-10
lines changed

aima-core/src/main/java/aima/core/logic/planning/GraphPlanAlgorithm.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@
3434
*/
3535
public class GraphPlanAlgorithm {
3636

37+
/** Graph which was created in the last <code>graphPlan</code> call. */
38+
private Graph graph;
39+
40+
public Graph getGraph() {
41+
return graph;
42+
}
43+
3744
/**
3845
* function GRAPHPLAN(problem) returns solution or failure
3946
*
@@ -42,7 +49,7 @@ public class GraphPlanAlgorithm {
4249
*/
4350
public List<List<ActionSchema>> graphPlan(Problem problem) {
4451
// graph ← INITIAL-PLANNING-GRAPH(problem)
45-
Graph graph = new Graph(problem);
52+
graph = new Graph(problem);
4653
// goals ← CONJUNCTS(problem.GOAL)
4754
List<Literal> goals = problem.getGoalState().getFluents();
4855
// nogoods ← an empty hash table
@@ -59,7 +66,9 @@ public List<List<ActionSchema>> graphPlan(Problem problem) {
5966
// if solution ≠ failure then return solution
6067
if (solution != null && solution.size() != 0)
6168
return solution;
62-
}
69+
} else
70+
// seems to be missing in the book - but needed to guarantee termination! (RLu)
71+
nogoods.put(tl, goals);
6372
// if graph and nogoods have both leveled off then return failure
6473
if (levelledOff(graph) && leveledOff(nogoods))
6574
return null;

aima-core/src/main/java/aima/core/logic/planning/Level.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
* @author Ruediger Lunde
1010
*/
1111
public class Level<CURR, PREV> {
12-
List<CURR> levelObjects = new ArrayList<>();
12+
private List<CURR> levelObjects = new ArrayList<>();
1313
HashMap<CURR, List<PREV>> prevLinks = new HashMap<>();
1414
HashMap<CURR, List<PREV>> nextLinks = new HashMap<>();
15-
HashMap<CURR, List<CURR>> mutexLinks = new HashMap<>();; //can be planned alternatively
15+
private HashMap<CURR, List<CURR>> mutexLinks = new HashMap<>();; //can be planned alternatively
1616

17-
Level<PREV, CURR> prevLevel;
17+
private Level<PREV, CURR> prevLevel;
1818

1919
public Level(List<CURR> levelObjects, Problem problem) {
2020
// store level objects and prevLinks

aima-core/src/test/java/aima/test/core/unit/logic/planning/GraphPlanAlgorithmTest.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
package aima.test.core.unit.logic.planning;
22

3-
import aima.core.logic.planning.ActionSchema;
4-
import aima.core.logic.planning.GraphPlanAlgorithm;
5-
import aima.core.logic.planning.PlanningProblemFactory;
6-
import aima.core.logic.planning.Problem;
3+
import aima.core.logic.planning.*;
74
import org.junit.Assert;
85
import org.junit.Test;
96

7+
import javax.swing.*;
108
import java.util.Collections;
119
import java.util.List;
1210

@@ -15,7 +13,7 @@
1513
*/
1614
public class GraphPlanAlgorithmTest {
1715
@Test
18-
public void test() {
16+
public void spareTireTest() {
1917
GraphPlanAlgorithm algorithm = new GraphPlanAlgorithm();
2018
Problem spareTireProblem = PlanningProblemFactory.spareTireProblem();
2119
List<List<ActionSchema>> solution = algorithm.graphPlan(spareTireProblem);
@@ -33,4 +31,16 @@ public void test() {
3331
Assert.assertTrue(solution.get(0).contains(removeSpareTrunk));
3432
Assert.assertTrue(solution.get(1).contains(putOnSpareAxle));
3533
}
34+
35+
@Test
36+
public void levelOffTest() {
37+
GraphPlanAlgorithm algorithm = new GraphPlanAlgorithm();
38+
Problem stProblem = PlanningProblemFactory.spareTireProblem();
39+
State initialState = new State("Tire(Flat)^Tire(Spare)^At(Flat,Axle)"); //^At(Spare,Trunk)");
40+
Problem modifiedProblem = new Problem(initialState, stProblem.getGoalState(), stProblem.getActionSchemas());
41+
List<List<ActionSchema>> solution = algorithm.graphPlan(modifiedProblem);
42+
Assert.assertEquals(4, algorithm.getGraph().numLevels());
43+
Assert.assertNull(solution);
44+
45+
}
3646
}

0 commit comments

Comments
 (0)