Skip to content

Commit 84bcb92

Browse files
committed
Implementations for forward and backward state-space search added.
1 parent 8dd02a8 commit 84bcb92

File tree

23 files changed

+410
-215
lines changed

23 files changed

+410
-215
lines changed

aima-core/src/main/java/aima/core/logic/fol/StandardizeApart.java

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ public StandardizeApartResult standardizeApart(Sentence sentence,
4040
StandardizeApartIndexical standardizeApartIndexical) {
4141
Set<Variable> toRename = variableCollector
4242
.collectAllVariables(sentence);
43-
Map<Variable, Term> renameSubstitution = new HashMap<Variable, Term>();
44-
Map<Variable, Term> reverseSubstitution = new HashMap<Variable, Term>();
43+
Map<Variable, Term> renameSubstitution = new HashMap<>();
44+
Map<Variable, Term> reverseSubstitution = new HashMap<>();
4545

4646
for (Variable var : toRename) {
4747
Variable v = null;
@@ -67,7 +67,7 @@ public Clause standardizeApart(Clause clause,
6767
StandardizeApartIndexical standardizeApartIndexical) {
6868

6969
Set<Variable> toRename = variableCollector.collectAllVariables(clause);
70-
Map<Variable, Term> renameSubstitution = new HashMap<Variable, Term>();
70+
Map<Variable, Term> renameSubstitution = new HashMap<>();
7171

7272
for (Variable var : toRename) {
7373
Variable v = null;
@@ -82,7 +82,7 @@ public Clause standardizeApart(Clause clause,
8282
}
8383

8484
if (renameSubstitution.size() > 0) {
85-
List<Literal> literals = new ArrayList<Literal>();
85+
List<Literal> literals = new ArrayList<>();
8686

8787
for (Literal l : clause.getLiterals()) {
8888
literals.add(substVisitor.subst(renameSubstitution, l));
@@ -100,10 +100,10 @@ public Chain standardizeApart(Chain chain,
100100
StandardizeApartIndexical standardizeApartIndexical) {
101101

102102
Set<Variable> toRename = variableCollector.collectAllVariables(chain);
103-
Map<Variable, Term> renameSubstitution = new HashMap<Variable, Term>();
103+
Map<Variable, Term> renameSubstitution = new HashMap<>();
104104

105105
for (Variable var : toRename) {
106-
Variable v = null;
106+
Variable v;
107107
do {
108108
v = new Variable(standardizeApartIndexical.getPrefix()
109109
+ standardizeApartIndexical.getNextIndex());
@@ -115,11 +115,10 @@ public Chain standardizeApart(Chain chain,
115115
}
116116

117117
if (renameSubstitution.size() > 0) {
118-
List<Literal> lits = new ArrayList<Literal>();
118+
List<Literal> lits = new ArrayList<>();
119119

120120
for (Literal l : chain.getLiterals()) {
121-
AtomicSentence atom = (AtomicSentence) substVisitor.subst(
122-
renameSubstitution, l.getAtomicSentence());
121+
AtomicSentence atom = (AtomicSentence) substVisitor.subst(renameSubstitution, l.getAtomicSentence());
123122
lits.add(l.newInstance(atom));
124123
}
125124

@@ -134,10 +133,9 @@ public Chain standardizeApart(Chain chain,
134133
return chain;
135134
}
136135

137-
public Map<Variable, Term> standardizeApart(List<Literal> l1Literals,
138-
List<Literal> l2Literals,
136+
public Map<Variable, Term> standardizeApart(List<Literal> l1Literals, List<Literal> l2Literals,
139137
StandardizeApartIndexical standardizeApartIndexical) {
140-
Set<Variable> toRename = new HashSet<Variable>();
138+
Set<Variable> toRename = new HashSet<>();
141139

142140
for (Literal pl : l1Literals) {
143141
toRename.addAll(variableCollector.collectAllVariables(pl
@@ -148,7 +146,7 @@ public Map<Variable, Term> standardizeApart(List<Literal> l1Literals,
148146
.getAtomicSentence()));
149147
}
150148

151-
Map<Variable, Term> renameSubstitution = new HashMap<Variable, Term>();
149+
Map<Variable, Term> renameSubstitution = new HashMap<>();
152150

153151
for (Variable var : toRename) {
154152
Variable v = null;
@@ -162,8 +160,8 @@ public Map<Variable, Term> standardizeApart(List<Literal> l1Literals,
162160
renameSubstitution.put(var, v);
163161
}
164162

165-
List<Literal> posLits = new ArrayList<Literal>();
166-
List<Literal> negLits = new ArrayList<Literal>();
163+
List<Literal> posLits = new ArrayList<>();
164+
List<Literal> negLits = new ArrayList<>();
167165

168166
for (Literal pl : l1Literals) {
169167
posLits.add(substVisitor.subst(renameSubstitution, pl));

aima-core/src/main/java/aima/core/logic/fol/kb/data/Literal.java

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*
1616
*/
1717
public class Literal {
18-
private AtomicSentence atom = null;
18+
private AtomicSentence atom;
1919
private boolean negativeLiteral = false;
2020
private String strRep = null;
2121
private int hashCode = 0;
@@ -33,6 +33,10 @@ public Literal newInstance(AtomicSentence atom) {
3333
return new Literal(atom, negativeLiteral);
3434
}
3535

36+
public Literal getComplementaryLiteral() {
37+
return new Literal(getAtomicSentence(), !isNegativeLiteral());
38+
}
39+
3640
public boolean isPositiveLiteral() {
3741
return !negativeLiteral;
3842
}
@@ -49,35 +53,24 @@ public AtomicSentence getAtomicSentence() {
4953
public String toString() {
5054
if (null == strRep) {
5155
StringBuilder sb = new StringBuilder();
52-
if (isNegativeLiteral()) {
56+
if (isNegativeLiteral())
5357
sb.append("~");
54-
}
5558
sb.append(getAtomicSentence().toString());
5659
strRep = sb.toString();
5760
}
58-
5961
return strRep;
6062
}
6163

6264
@Override
6365
public boolean equals(Object o) {
64-
65-
if (this == o) {
66+
if (this == o)
6667
return true;
67-
}
68-
if (o.getClass() != getClass()) {
69-
// This prevents ReducedLiterals
70-
// being treated as equivalent to
71-
// normal Literals.
72-
return false;
73-
}
74-
if (!(o instanceof Literal)) {
68+
if (o.getClass() != getClass())
7569
return false;
76-
}
70+
7771
Literal l = (Literal) o;
7872
return l.isPositiveLiteral() == isPositiveLiteral()
79-
&& l.getAtomicSentence().getSymbolicName()
80-
.equals(atom.getSymbolicName())
73+
&& l.getAtomicSentence().getSymbolicName().equals(atom.getSymbolicName())
8174
&& l.getAtomicSentence().getArgs().equals(atom.getArgs());
8275
}
8376

@@ -94,17 +87,4 @@ public int hashCode() {
9487
}
9588
return hashCode;
9689
}
97-
98-
public Literal substitute(List<Constant> constants){
99-
List<Term> terms = new ArrayList<>();
100-
for (int i = 0; i < this.getAtomicSentence().getArgs().size(); i++) {
101-
if (this.getAtomicSentence().getArgs().get(i) instanceof Variable){
102-
if (constants.size()>i){
103-
terms.add(constants.get(i));
104-
}
105-
}
106-
}
107-
List<Term> toAdd = new ArrayList<>(constants);
108-
return new Literal(new Predicate(this.getAtomicSentence().getSymbolicName(),toAdd),this.isNegativeLiteral());
109-
}
11090
}

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

Lines changed: 63 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* precondition and an effect.
2020
*
2121
* @author samagra
22+
* @author Ruediger Lunde
2223
*/
2324
public class ActionSchema {
2425

@@ -27,51 +28,75 @@ public class ActionSchema {
2728
*/
2829
public final static String NO_OP = "No-op";
2930

30-
List<Term> variables;// list of variables
31-
List<Literal> precondition; //PRECONDITION: treated as a conjunction of fluents
32-
List<Literal> effects;//EFFECT: treated as a conjunction of fluents
31+
List<Term> variables; // list of variables
32+
List<Literal> precondition; // PRECONDITION: treated as a conjunction of fluents
33+
List<Literal> effect; // EFFECT: treated as a conjunction of fluents
3334
List<Literal> effectsPositiveLiterals;
3435
List<Literal> effectsNegativeLiterals;
35-
private final String name;//action name
36+
private final String name; // action name
3637

37-
public ActionSchema(String name, List<Term> variables, List<Literal> precondition, List<Literal> effects) {
38+
public ActionSchema(String name, List<Term> variables, List<Literal> precondition, List<Literal> effect) {
3839
if (variables == null)
3940
variables = new ArrayList<>();
4041
this.name = name;
4142
this.variables = variables;
4243
this.precondition = precondition;
43-
this.effects = effects;
44+
this.effect = effect;
4445
effectsNegativeLiterals = new ArrayList<>();
4546
effectsPositiveLiterals = new ArrayList<>();
4647
this.sortEffects();
4748
}
4849

49-
public ActionSchema(String name, List<Term> variables, String precondition, String effects) {
50-
this(name, variables, Utils.parse(precondition), Utils.parse(effects));
50+
public ActionSchema(String name, List<Term> variables, String precondition, String effect) {
51+
this(name, variables, Utils.parse(precondition), Utils.parse(effect));
5152
}
5253

5354
private void sortEffects() {
54-
for (Literal effect : effects) {
55-
if (effect.isNegativeLiteral())
56-
effectsNegativeLiterals.add(effect);
55+
for (Literal eff : effect) {
56+
if (eff.isNegativeLiteral())
57+
effectsNegativeLiterals.add(eff);
5758
else
58-
effectsPositiveLiterals.add(effect);
59+
effectsPositiveLiterals.add(eff);
5960
}
6061
}
6162

63+
public String getName() {
64+
return name;
65+
}
66+
67+
public List<Term> getVariables() {
68+
return variables;
69+
}
70+
71+
public List<Literal> getPrecondition() {
72+
return precondition;
73+
}
74+
75+
public List<Literal> getEffect() {
76+
return effect;
77+
}
78+
79+
public List<Literal> getEffectsPositiveLiterals() {
80+
return effectsPositiveLiterals;
81+
}
82+
83+
public List<Literal> getEffectsNegativeLiterals() {
84+
return effectsNegativeLiterals;
85+
}
86+
6287
@Override
6388
public String toString() {
6489
StringBuilder result = new StringBuilder();
6590
result.append("Action(").append(this.getName()).append(")\n\tPRECOND:");
6691
String and = "";
67-
for (Literal precond : getPrecondition()) {
68-
result.append(and).append(precond);
92+
for (Literal pre : precondition) {
93+
result.append(and).append(pre);
6994
and = "^";
7095
}
7196
result.append("\n\tEFFECT:");
7297
and = "";
73-
for (Literal effect : getEffects()) {
74-
result.append(and).append(effect);
98+
for (Literal eff : effect) {
99+
result.append(and).append(eff);
75100
and = "^";
76101
}
77102
return result.toString();
@@ -81,22 +106,22 @@ public String toString() {
81106
public boolean equals(Object obj) {
82107
if (this == obj)
83108
return true;
84-
if (!(obj instanceof ActionSchema))
109+
if (obj == null || getClass() != obj.getClass())
85110
return false;
86111
return this.getName().equals(((ActionSchema) obj).getName()) &&
87112
this.getPrecondition().containsAll(((ActionSchema) obj).getPrecondition())
88113
&& ((ActionSchema) obj).getPrecondition().containsAll(this.getPrecondition())
89-
&& this.getEffects().containsAll(((ActionSchema) obj).getEffects())
90-
&& ((ActionSchema) obj).getEffects().containsAll(this.getEffects());
114+
&& this.getEffect().containsAll(((ActionSchema) obj).getEffect())
115+
&& ((ActionSchema) obj).getEffect().containsAll(this.getEffect());
91116
}
92117

93118
@Override
94119
public int hashCode() {
95120
int hashCode = 17;
96-
for (Literal preCo : this.getPrecondition())
97-
hashCode = 37 * hashCode + preCo.hashCode();
98-
for (Literal effect : this.getEffects())
99-
hashCode = 37 * hashCode + effect.hashCode();
121+
for (Literal literal : precondition)
122+
hashCode = 37 * hashCode + literal.hashCode();
123+
for (Literal literal : effect)
124+
hashCode = 37 * hashCode + literal.hashCode();
100125
for (Term var : this.getVariables())
101126
hashCode = 37 * hashCode + var.hashCode();
102127
return hashCode;
@@ -109,37 +134,31 @@ public int hashCode() {
109134
* @return A ground action.
110135
*/
111136
public ActionSchema getActionBySubstitution(List<Constant> constants) {
112-
List<Literal> precondList = this.getPrecondition();
113-
List<Term> vars = this.getVariables();
114-
List<Literal> effectList = this.getEffects();
115-
List<Literal> newPrecond = new ArrayList<>();
116-
List<Literal> newEffects = new ArrayList<>();
117-
for (Literal precondition : precondList) {
137+
List<Literal> newPrecondition = new ArrayList<>();
138+
List<Literal> newEffect = new ArrayList<>();
139+
for (Literal pre : precondition) {
118140
List<Term> newTerms = new ArrayList<>();
119-
for (Term variable : precondition.getAtomicSentence().getArgs()) {
141+
for (Term variable : pre.getAtomicSentence().getArgs()) {
120142
if (variable instanceof Variable) {
121-
newTerms.add(constants.get(vars.lastIndexOf(variable)));
143+
newTerms.add(constants.get(variables.lastIndexOf(variable)));
122144
} else
123145
newTerms.add(variable);
124146
}
125-
newPrecond.add(new Literal(new
126-
Predicate(precondition.getAtomicSentence().getSymbolicName(),
127-
newTerms), precondition.isNegativeLiteral()));
147+
newPrecondition.add(new Literal(new Predicate(pre.getAtomicSentence().getSymbolicName(), newTerms),
148+
pre.isNegativeLiteral()));
128149
}
129-
for (Literal effect : effectList) {
150+
for (Literal eff : effect) {
130151
List<Term> newTerms = new ArrayList<>();
131-
for (Term variable :
132-
effect.getAtomicSentence().getArgs()) {
152+
for (Term variable : eff.getAtomicSentence().getArgs()) {
133153
if (variable instanceof Variable)
134-
newTerms.add(constants.get(vars.lastIndexOf(variable)));
154+
newTerms.add(constants.get(variables.lastIndexOf(variable)));
135155
else
136156
newTerms.add(variable);
137157
}
138-
newEffects.add(new Literal(new
139-
Predicate(effect.getAtomicSentence().getSymbolicName(),
140-
newTerms), effect.isNegativeLiteral()));
158+
newEffect.add(new Literal(new
159+
Predicate(eff.getAtomicSentence().getSymbolicName(), newTerms), eff.isNegativeLiteral()));
141160
}
142-
return new ActionSchema(this.getName(), null, newPrecond, newEffects);
161+
return new ActionSchema(this.getName(), null, newPrecondition, newEffect);
143162
}
144163

145164
/**
@@ -149,11 +168,11 @@ public ActionSchema getActionBySubstitution(List<Constant> constants) {
149168
*/
150169
public List<Constant> getConstants() {
151170
List<Constant> constants = new ArrayList<>();
152-
for (Constant constant : extractConstant(getPrecondition())) {
171+
for (Constant constant : extractConstant(precondition)) {
153172
if (!constants.contains(constant))
154173
constants.add(constant);
155174
}
156-
for (Constant constant : extractConstant(getEffects())) {
175+
for (Constant constant : extractConstant(effect)) {
157176
if (!constants.contains(constant))
158177
constants.add(constant);
159178
}
@@ -170,28 +189,4 @@ public List<Constant> extractConstant(List<Literal> list) {
170189
}
171190
return result;
172191
}
173-
174-
public String getName() {
175-
return name;
176-
}
177-
178-
public List<Term> getVariables() {
179-
return variables;
180-
}
181-
182-
public List<Literal> getPrecondition() {
183-
return precondition;
184-
}
185-
186-
public List<Literal> getEffects() {
187-
return effects;
188-
}
189-
190-
public List<Literal> getEffectsPositiveLiterals() {
191-
return effectsPositiveLiterals;
192-
}
193-
194-
public List<Literal> getEffectsNegativeLiterals() {
195-
return effectsNegativeLiterals;
196-
}
197192
}

0 commit comments

Comments
 (0)