Skip to content

Commit 8b8eb41

Browse files
author
ctjoreilly
committed
- fixed subtle defect in Model Elimination inference algorithm.
- cleaned up some unnecessary API calls that were impacting performance.
1 parent de76095 commit 8b8eb41

12 files changed

+29
-26
lines changed

src/aima/logic/fol/StandardizeApart.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public Clause standardizeApart(Clause clause,
8888
literals.add(substVisitor.subst(renameSubstitution, l));
8989
}
9090
Clause renamed = new Clause(literals);
91-
renamed.setProofStep(new ProofStepRenaming(renamed.toString(),
91+
renamed.setProofStep(new ProofStepRenaming(renamed,
9292
clause.getProofStep()));
9393
return renamed;
9494
}
@@ -125,7 +125,7 @@ public Chain standardizeApart(Chain chain,
125125

126126
Chain renamed = new Chain(lits);
127127

128-
renamed.setProofStep(new ProofStepRenaming(renamed.toString(),
128+
renamed.setProofStep(new ProofStepRenaming(renamed,
129129
chain.getProofStep()));
130130

131131
return renamed;

src/aima/logic/fol/SubstVisitor.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ public SubstVisitor() {
3939
*
4040
*/
4141
public Sentence subst(Map<Variable, Term> theta, Sentence aSentence) {
42-
return (Sentence) ((Sentence) aSentence.accept(this, theta)).copy();
42+
return (Sentence) aSentence.accept(this, theta);
4343
}
4444

4545
public Term subst(Map<Variable, Term> theta, Term aTerm) {
46-
return (Term) ((Term) aTerm.accept(this, theta)).copy();
46+
return (Term) aTerm.accept(this, theta);
4747
}
4848

4949
public Function subst(Map<Variable, Term> theta, Function aFunction) {
50-
return (Function) ((Function) aFunction.accept(this, theta)).copy();
50+
return (Function) aFunction.accept(this, theta);
5151
}
5252

5353
public Literal subst(Map<Variable, Term> theta, Literal aLiteral) {
@@ -63,7 +63,7 @@ public Object visitVariable(Variable variable, Object arg) {
6363
if (substitution.containsKey(variable)) {
6464
return substitution.get(variable).copy();
6565
}
66-
return variable;
66+
return variable.copy();
6767
}
6868

6969
@SuppressWarnings("unchecked")
@@ -83,12 +83,12 @@ public Object visitQuantifiedSentence(QuantifiedSentence sentence,
8383
if (st instanceof Variable) {
8484
// Only if it is a variable to I replace it, otherwise
8585
// I drop it.
86-
variables.add((Variable) st);
86+
variables.add((Variable) st.copy());
8787
}
8888
} else {
8989
// No substitution for the quantified variable, so
9090
// keep it.
91-
variables.add(v);
91+
variables.add(v.copy());
9292
}
9393
}
9494

src/aima/logic/fol/inference/FOLModelElimination.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ private void recursiveDLS(int maxDepth,
178178
int noNextFarParents = indexedFarParents
179179
.getNumberFarParents(nextNearParent);
180180
// Add to indexed far parents
181-
indexedFarParents.addToIndex(nextNearParent);
181+
nextNearParent = indexedFarParents.addToIndex(nextNearParent);
182182

183183
// Check the next level
184184
recursiveDLS(maxDepth, currentDepth + 1, nextNearParent,
@@ -279,7 +279,7 @@ public AnswerHandler(FOLKnowledgeBase kb, Sentence aQuery,
279279
}
280280

281281
for (Chain s : sos) {
282-
s.setProofStep(new ProofStepGoal(s.toString()));
282+
s.setProofStep(new ProofStepGoal(s));
283283
}
284284
}
285285

@@ -521,7 +521,8 @@ public Chain attemptReduction(Chain nearParent,
521521
return nnpc;
522522
}
523523

524-
public void addToIndex(Chain c) {
524+
public Chain addToIndex(Chain c) {
525+
Chain added = null;
525526
Literal head = c.getHead();
526527
if (null != head) {
527528
Map<String, List<Chain>> toAddTo = null;
@@ -538,8 +539,10 @@ public void addToIndex(Chain c) {
538539
toAddTo.put(key, farParents);
539540
}
540541
// Ensure is standardized apart when added.
541-
farParents.add(standardizeApart.standardizeApart(c, _saIndexical));
542+
added = standardizeApart.standardizeApart(c, _saIndexical);
543+
farParents.add(added);
542544
}
545+
return added;
543546
}
544547

545548
public String toString() {

src/aima/logic/fol/inference/FOLOTTERLikeTheoremProver.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ public InferenceResult ask(FOLKnowledgeBase KB, Sentence alpha) {
189189
notAlpha, answerLiteral.getAtomicSentence());
190190
for (Clause c : KB.convertToClauses(notAlphaWithAnswer)) {
191191
c = KB.standardizeApart(c);
192-
c.setProofStep(new ProofStepGoal(c.toString()));
192+
c.setProofStep(new ProofStepGoal(c));
193193
c.setStandardizedApartCheckNotRequired();
194194
sos.addAll(c.getFactors());
195195
}
@@ -198,7 +198,7 @@ public InferenceResult ask(FOLKnowledgeBase KB, Sentence alpha) {
198198
} else {
199199
for (Clause c : KB.convertToClauses(notAlpha)) {
200200
c = KB.standardizeApart(c);
201-
c.setProofStep(new ProofStepGoal(c.toString()));
201+
c.setProofStep(new ProofStepGoal(c));
202202
c.setStandardizedApartCheckNotRequired();
203203
sos.addAll(c.getFactors());
204204
}

src/aima/logic/fol/inference/FOLTFMResolution.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public InferenceResult ask(FOLKnowledgeBase KB, Sentence alpha) {
9797
notAlpha, answerLiteral.getAtomicSentence());
9898
for (Clause c : KB.convertToClauses(notAlphaWithAnswer)) {
9999
c = KB.standardizeApart(c);
100-
c.setProofStep(new ProofStepGoal(c.toString()));
100+
c.setProofStep(new ProofStepGoal(c));
101101
c.setStandardizedApartCheckNotRequired();
102102
clauses.addAll(c.getFactors());
103103
}
@@ -106,7 +106,7 @@ public InferenceResult ask(FOLKnowledgeBase KB, Sentence alpha) {
106106
} else {
107107
for (Clause c : KB.convertToClauses(notAlpha)) {
108108
c = KB.standardizeApart(c);
109-
c.setProofStep(new ProofStepGoal(c.toString()));
109+
c.setProofStep(new ProofStepGoal(c));
110110
c.setStandardizedApartCheckNotRequired();
111111
clauses.addAll(c.getFactors());
112112
}

src/aima/logic/fol/inference/proof/ProofStepClauseClausifySentence.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class ProofStepClauseClausifySentence extends AbstractProofStep {
1818
public ProofStepClauseClausifySentence(Clause clausified,
1919
Sentence origSentence) {
2020
this.clausified = clausified;
21-
this.predecessors.add(new ProofStepPremise(origSentence.toString()));
21+
this.predecessors.add(new ProofStepPremise(origSentence));
2222
}
2323

2424
//

src/aima/logic/fol/inference/proof/ProofStepGoal.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ public class ProofStepGoal extends AbstractProofStep {
1212
//
1313
private static final List<ProofStep> _noPredecessors = new ArrayList<ProofStep>();
1414
//
15-
private String proof = "";
15+
private Object proof = "";
1616

17-
public ProofStepGoal(String proof) {
17+
public ProofStepGoal(Object proof) {
1818
this.proof = proof;
1919
}
2020

src/aima/logic/fol/inference/proof/ProofStepPremise.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ public class ProofStepPremise extends AbstractProofStep {
1212
//
1313
private static final List<ProofStep> _noPredecessors = new ArrayList<ProofStep>();
1414
//
15-
private String proof = "";
15+
private Object proof = "";
1616

17-
public ProofStepPremise(String proof) {
17+
public ProofStepPremise(Object proof) {
1818
this.proof = proof;
1919
}
2020

src/aima/logic/fol/inference/proof/ProofStepRenaming.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
*/
1111
public class ProofStepRenaming extends AbstractProofStep {
1212
private List<ProofStep> predecessors = new ArrayList<ProofStep>();
13-
private String proof = "";
13+
private Object proof = "";
1414

15-
public ProofStepRenaming(String proof, ProofStep predecessor) {
15+
public ProofStepRenaming(Object proof, ProofStep predecessor) {
1616
this.proof = proof;
1717
this.predecessors.add(predecessor);
1818
}

src/aima/logic/fol/kb/data/Chain.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public Chain(Set<Literal> literals) {
4141
public ProofStep getProofStep() {
4242
if (null == proofStep) {
4343
// Assume was a premise
44-
proofStep = new ProofStepPremise(this.toString());
44+
proofStep = new ProofStepPremise(this);
4545
}
4646
return proofStep;
4747
}

0 commit comments

Comments
 (0)