Skip to content

Commit 32895df

Browse files
committed
Several bugs fixed in State (for planning).
1 parent 8ca060c commit 32895df

File tree

3 files changed

+31
-20
lines changed

3 files changed

+31
-20
lines changed

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

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import aima.core.logic.fol.kb.data.Literal;
44

55
import java.util.ArrayList;
6+
import java.util.Comparator;
67
import java.util.List;
78

89
/**
@@ -15,6 +16,7 @@
1516
* in a package delivery problem might be At(Truck1 , Melbourne) ∧ At(Truck2 , Sydney).
1617
*
1718
* @author samagra
19+
* @author Ruediger Lunde
1820
*/
1921
public class State {
2022
List<Literal> fluents;
@@ -38,16 +40,20 @@ public State(String fluents) {
3840
* that are positive literals in the action’s effects (what we call the add list or ADD(a)):
3941
* RESULT(s, a) = (s − DEL(a)) ∪ ADD(a).
4042
*
41-
* @param a The applicable action.
43+
* @param action The applicable action.
4244
* @return The new state.
4345
*/
44-
public State result(ActionSchema a) {
45-
if (this.isApplicable(a)) {
46-
for (Literal fluent : a.getEffectsNegativeLiterals()) {
47-
Literal tempFluent = new Literal(fluent.getAtomicSentence());
48-
fluents.remove(tempFluent);
46+
public State result(ActionSchema action) {
47+
if (isApplicable(action)) {
48+
List<Literal> result = new ArrayList<>(fluents);
49+
for (Literal literal : action.getEffect()) {
50+
if (literal.isNegativeLiteral())
51+
result.remove(literal.getComplementaryLiteral());
52+
else if (!result.contains(literal))
53+
result.add(literal);
4954
}
50-
fluents.addAll(a.getEffectsPositiveLiterals());
55+
result.sort(Comparator.comparing(Literal::toString));
56+
return new State(result);
5157
}
5258
return this;
5359
}
@@ -56,16 +62,12 @@ public State result(ActionSchema a) {
5662
* Returns the state obtained by the application of a list of applicable actions to
5763
* the current state. This method does not change the original state and in fact returns
5864
* a new state representing the changed state.
59-
*
60-
* @param actions
61-
* @return
6265
*/
6366
public State result(List<ActionSchema> actions) {
64-
State resultState = new State(new ArrayList<>(this.getFluents()));
65-
for (ActionSchema action : actions) {
66-
resultState = resultState.result(action);
67-
}
68-
return resultState;
67+
State state = this;
68+
for (ActionSchema action : actions)
69+
state = state.result(action);
70+
return state;
6971
}
7072

7173

@@ -74,11 +76,20 @@ public State result(List<ActionSchema> actions) {
7476
* <p>
7577
* We say that action a is applicable in state s if the preconditions are satisfied by s.
7678
*
77-
* @param a an action
79+
* @param action an action
7880
* @return a boolean stating if the action is applicable.
7981
*/
80-
public boolean isApplicable(ActionSchema a) {
81-
return this.getFluents().containsAll(a.getPrecondition());
82+
public boolean isApplicable(ActionSchema action) {
83+
boolean result = true;
84+
for (Literal literal : action.getPrecondition()) {
85+
if (literal.isPositiveLiteral()
86+
? !fluents.contains(literal)
87+
: fluents.contains(literal.getComplementaryLiteral())) {
88+
result = false;
89+
break;
90+
}
91+
}
92+
return result;
8293
}
8394

8495
public List<Literal> getFluents() {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public class HierarchicalSearchAlgorithm {
4242
* @param problem The planning problem.
4343
* @return A list of actions representing the plan.
4444
*/
45-
public List<ActionSchema> heirarchicalSearch(PlanningProblem problem) {
45+
public List<ActionSchema> hierarchicalSearch(PlanningProblem problem) {
4646
// frontier ← a FIFO queue with [Act] as the only element
4747
LinkedList<List<ActionSchema>> frontier = new LinkedList<>();
4848
frontier.add(new ArrayList<>(Collections.singletonList(PlanningProblemFactory.getHlaAct(problem))));

aima-core/src/test/java/aima/test/core/unit/logic/planning/hierarchicalsearch/HierarchicalSearchTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public void hierarchicalSearchTest() {
1717
"At(Home)",
1818
"~At(Home)^At(SFO)");
1919
for (ActionSchema action :
20-
algo.heirarchicalSearch(PlanningProblemFactory.goHomeToSFOProblem())) {
20+
algo.hierarchicalSearch(PlanningProblemFactory.goHomeToSFOProblem())) {
2121
Assert.assertEquals(action, taxiAction);
2222
}
2323
}

0 commit comments

Comments
 (0)