Skip to content

Commit d914daf

Browse files
committed
HierachicalSearchTest added to PlanningTestSuite.
1 parent 4ceb340 commit d914daf

File tree

4 files changed

+29
-28
lines changed

4 files changed

+29
-28
lines changed

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,20 @@ private void sortEffects() {
6161

6262
@Override
6363
public String toString() {
64-
String result = "Action(" + this.getName() + ")\n\tPRECOND:";
64+
StringBuilder result = new StringBuilder();
65+
result.append("Action(").append(this.getName()).append(")\n\tPRECOND:");
6566
String and = "";
6667
for (Literal precond : getPrecondition()) {
67-
result = result + and + precond.toString();
68+
result.append(and).append(precond);
6869
and = "^";
6970
}
70-
result = result + "\n\tEFFECT:";
71+
result.append("\n\tEFFECT:");
7172
and = "";
7273
for (Literal effect : getEffects()) {
73-
result = result + and + effect.toString();
74+
result.append(and).append(effect);
7475
and = "^";
7576
}
76-
return result;
77+
return result.toString();
7778
}
7879

7980
@Override

aima-core/src/main/java/aima/core/logic/planning/hierarchicalsearch/HierarchicalSearchAlgorithm.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public class HierarchicalSearchAlgorithm {
4444
*/
4545
public List<ActionSchema> heirarchicalSearch(Problem problem) {
4646
// frontier ← a FIFO queue with [Act] as the only element
47-
Queue<List<ActionSchema>> frontier = new LinkedList<>();
47+
LinkedList<List<ActionSchema>> frontier = new LinkedList<>();
4848
frontier.add(new ArrayList<>(Collections.singletonList(PlanningProblemFactory.getHlaAct(problem))));
4949
// loop do
5050
while (true) {
@@ -79,16 +79,14 @@ public List<ActionSchema> heirarchicalSearch(Problem problem) {
7979
if (outcome.getFluents().containsAll(problem.getGoalState().getFluents()))
8080
return plan;
8181
} else {
82-
List<ActionSchema> tempInsertionList = new ArrayList<>();
8382
// else for each sequence in REFINEMENTS(hla, outcome, hierarchy) do
84-
for (List<ActionSchema> sequence :
85-
refinements(hla, outcome)) {
83+
for (List<ActionSchema> sequence : refinements(hla, outcome)) {
8684
// frontier ← INSERT(APPEND(prefix, sequence, suffix), frontier)
87-
tempInsertionList.clear();
88-
tempInsertionList.addAll(prefix);
89-
tempInsertionList.addAll(sequence);
90-
tempInsertionList.addAll(suffix);
91-
((LinkedList<List<ActionSchema>>) frontier).addLast(new ArrayList<>(tempInsertionList));
85+
List<ActionSchema> list = new ArrayList<>();
86+
list.addAll(prefix);
87+
list.addAll(sequence);
88+
list.addAll(suffix);
89+
frontier.addLast(list);
9290
}
9391
}
9492
}
@@ -105,8 +103,7 @@ public List<ActionSchema> heirarchicalSearch(Problem problem) {
105103
*/
106104
public List<List<ActionSchema>> refinements(ActionSchema hla, State outcome) {
107105
List<List<ActionSchema>> result = new ArrayList<>();
108-
for (List<ActionSchema> refinement :
109-
((HighLevelAction) hla).getRefinements()) {
106+
for (List<ActionSchema> refinement : ((HighLevelAction) hla).getRefinements()) {
110107
if (refinement.size() > 0) {
111108
if (outcome.isApplicable(refinement.get(0)))
112109
result.add(refinement);

aima-core/src/main/java/aima/core/logic/planning/hierarchicalsearch/HighLevelAction.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,19 @@
1414
* by definition).
1515
*
1616
* @author samagra
17+
* @author Ruediger Lunde
1718
*/
1819
public class HighLevelAction extends ActionSchema {
1920
List<List<ActionSchema>> refinements;
2021

21-
public HighLevelAction(String name, List<Term> variables, String precondition, String effects, List<List<ActionSchema>> refinements) {
22+
public HighLevelAction(String name, List<Term> variables, String precondition, String effects,
23+
List<List<ActionSchema>> refinements) {
2224
super(name, variables, precondition, effects);
2325
this.refinements = refinements;
2426
}
2527

2628
public void addRefinement(List<ActionSchema> newRefinement) {
27-
this.refinements.add(newRefinement);
29+
refinements.add(newRefinement);
2830
}
2931

3032
public List<List<ActionSchema>> getRefinements() {
@@ -33,16 +35,16 @@ public List<List<ActionSchema>> getRefinements() {
3335

3436
@Override
3537
public String toString() {
36-
String result = super.toString();
37-
result = result + "\n" + "REFINEMENTS : \n";
38-
for (List<ActionSchema> refinement :
39-
this.getRefinements()) {
40-
result += "\n";
41-
for (ActionSchema action :
42-
refinement) {
43-
result = result + "\n" + (action.getName());
38+
StringBuilder result = new StringBuilder(super.toString());
39+
result.append("\nREFINEMENTS:");
40+
for (List<ActionSchema> refinement : getRefinements()) {
41+
result.append("\n\t");
42+
String sep = "";
43+
for (ActionSchema action : refinement) {
44+
result.append(sep).append(action.getName());
45+
sep = ", ";
4446
}
4547
}
46-
return result;
48+
return result.toString();
4749
}
4850
}

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

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

3+
import aima.test.core.unit.logic.planning.hierarchicalsearch.HierarchicalSearchTest;
34
import org.junit.runner.RunWith;
45
import org.junit.runners.Suite;
56

67
@RunWith(Suite.class)
78
@Suite.SuiteClasses({ActionSchemaTest.class, GraphPlanAlgorithmTest.class, GraphTest.class, LevelTest.class,
8-
ProblemTest.class, StateTest.class, UtilsTest.class})
9+
ProblemTest.class, StateTest.class, UtilsTest.class, HierarchicalSearchTest.class})
910

1011
public class PlanningTestSuite {
1112

0 commit comments

Comments
 (0)