Skip to content

Commit 900ebc9

Browse files
author
ctjoreilly
committed
- Added support classes for implementation of CurrentBestLearning algorithm.
1 parent 8b8eb41 commit 900ebc9

File tree

6 files changed

+313
-0
lines changed

6 files changed

+313
-0
lines changed

src/aima/learning/knowledge/CurrentBestLearning.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package aima.learning.knowledge;
22

3+
import java.util.List;
4+
35
/**
46
* Artificial Intelligence A Modern Approach (2nd Edition): Figure 19.2, page 681.
57
*
@@ -25,5 +27,36 @@
2527
*
2628
*/
2729
public class CurrentBestLearning {
30+
private FOLDataSetDomain folDSDomain = null;
31+
32+
//
33+
// PUBLIC METHODS
34+
//
35+
public CurrentBestLearning(FOLDataSetDomain folDSDomain) {
36+
this.folDSDomain = folDSDomain;
37+
}
38+
39+
// * function CURRENT-BEST-LEARNING(examples) returns a hypothesis
40+
public Hypothesis currentBestLearning(List<FOLExample> examples) {
41+
// TODO-remove this test behavior code
42+
// for (FOLExample fe : examples) {
43+
// System.out.println(fe.toString());
44+
// }
45+
46+
// TODO
47+
// * H <- any hypothesis consistent with the first example in examples.
48+
// * for each remaining example in examples do
49+
// * if e is false positive for H then
50+
// * H <- choose a specialization of H consistent with examples
51+
// * else if e is false negative for H then
52+
// * H <- choose a generalization of H consistent with examples
53+
// * if no consistent specialization/generalization can be found then
54+
// fail
55+
// * return H
56+
return null;
57+
}
2858

59+
//
60+
// PRIVATE METHODS
61+
//
2962
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package aima.learning.knowledge;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import aima.learning.framework.DataSetSpecification;
7+
import aima.logic.fol.domain.FOLDomain;
8+
9+
/**
10+
* @author Ciaran O'Reilly
11+
*
12+
*/
13+
public class FOLDataSetDomain extends FOLDomain {
14+
private DataSetSpecification dataSetSpecification;
15+
private String trueGoalValue = null;
16+
// Default example prefix, see pg679 of AIMA
17+
private String examplePrefix = "X";
18+
private List<String> descriptionPredicateNames = new ArrayList<String>();
19+
20+
//
21+
// PUBLIC METHODS
22+
//
23+
public FOLDataSetDomain(DataSetSpecification dataSetSpecification, String trueGoalValue) {
24+
this.dataSetSpecification = dataSetSpecification;
25+
this.trueGoalValue = trueGoalValue;
26+
constructFOLDomain();
27+
}
28+
29+
public String getGoalPredicateName() {
30+
return dataSetSpecification.getTarget();
31+
}
32+
33+
public String getTrueGoalValue() {
34+
return trueGoalValue;
35+
}
36+
37+
public List<String> getDescriptionPredicateNames() {
38+
return descriptionPredicateNames;
39+
}
40+
41+
public boolean isMultivalued(String descriptivePredicateName) {
42+
List<String> possibleValues = dataSetSpecification.getPossibleAttributeValues(descriptivePredicateName);
43+
// If more than two possible values
44+
// then is multivalued
45+
if (possibleValues.size() > 2) {
46+
return true;
47+
}
48+
// If one of the possible values for the attribute
49+
// matches the true goal value then consider
50+
// it not being multivalued.
51+
for (String pv : possibleValues) {
52+
if (trueGoalValue.equals(pv)) {
53+
return false;
54+
}
55+
}
56+
57+
return true;
58+
}
59+
60+
public String getExampleConstant(int egNo) {
61+
String egConstant = examplePrefix+egNo;
62+
addConstant(egConstant);
63+
return egConstant;
64+
}
65+
66+
//
67+
// PRIVATE METHODS
68+
//
69+
private void constructFOLDomain() {
70+
// Ensure the target predicate is included
71+
addPredicate(dataSetSpecification.getTarget());
72+
// Create the descriptive predicates
73+
for (String saName : dataSetSpecification.getNamesOfStringAttributes()) {
74+
if (dataSetSpecification.getTarget().equals(saName)) {
75+
// Don't add the target to the descriptive predicates
76+
continue;
77+
}
78+
// Add a predicate for the attribute
79+
addPredicate(saName);
80+
81+
descriptionPredicateNames.add(saName);
82+
83+
List<String> attributeValues = dataSetSpecification.getPossibleAttributeValues(saName);
84+
// If a multivalued attribute need to setup
85+
// Constants for the different possible values
86+
if (isMultivalued(saName)) {
87+
for (String av : attributeValues) {
88+
addConstant(av);
89+
}
90+
}
91+
}
92+
}
93+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package aima.learning.knowledge;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import aima.learning.framework.Example;
7+
import aima.logic.fol.Connectors;
8+
import aima.logic.fol.parsing.ast.ConnectedSentence;
9+
import aima.logic.fol.parsing.ast.Constant;
10+
import aima.logic.fol.parsing.ast.NotSentence;
11+
import aima.logic.fol.parsing.ast.Predicate;
12+
import aima.logic.fol.parsing.ast.Sentence;
13+
import aima.logic.fol.parsing.ast.Term;
14+
15+
/**
16+
* @author Ciaran O'Reilly
17+
*
18+
*/
19+
public class FOLExample {
20+
private FOLDataSetDomain folDSDomain = null;
21+
private Example example = null;
22+
private int egNo = 0;
23+
//
24+
private Constant ithExampleConstant = null;
25+
private Sentence classification = null;
26+
private Sentence description = null;
27+
28+
//
29+
// PUBLIC METHODS
30+
//
31+
public FOLExample(FOLDataSetDomain folDSDomain, Example example, int egNo) {
32+
this.folDSDomain = folDSDomain;
33+
this.example = example;
34+
this.egNo = egNo;
35+
constructFOLEg();
36+
}
37+
38+
public int getExampleNumber() {
39+
return egNo;
40+
}
41+
42+
public Sentence getClassification() {
43+
return classification;
44+
}
45+
46+
public Sentence getDescription() {
47+
return description;
48+
}
49+
50+
public String toString() {
51+
return classification.toString() + " " + Connectors.AND + " " + description.toString();
52+
}
53+
54+
//
55+
// PRIVATE METHODS
56+
//
57+
private void constructFOLEg() {
58+
ithExampleConstant = new Constant(folDSDomain.getExampleConstant(egNo));
59+
60+
List<Term> terms = new ArrayList<Term>();
61+
terms.add(ithExampleConstant);
62+
// Create the classification sentence
63+
classification = new Predicate(folDSDomain.getGoalPredicateName(), terms);
64+
if (!example.getAttributeValueAsString(
65+
folDSDomain.getGoalPredicateName()).equals(
66+
folDSDomain.getTrueGoalValue())) {
67+
// if not true then needs to be a Not sentence
68+
classification = new NotSentence(classification);
69+
}
70+
71+
// Create the description sentence
72+
List<Sentence> descParts = new ArrayList<Sentence>();
73+
for (String dname : folDSDomain.getDescriptionPredicateNames()) {
74+
terms = new ArrayList<Term>();
75+
terms.add(ithExampleConstant);
76+
// If multivalued becomes a two place predicate
77+
// e.g: Patrons(X1, Some)
78+
// otherwise: Hungry(X1) or ~ Hungry(X1)
79+
// see pg 679 of AIMA
80+
Sentence part = null;
81+
if (folDSDomain.isMultivalued(dname)) {
82+
terms.add(new Constant(example.getAttributeValueAsString(dname)));
83+
part = new Predicate(dname, terms);
84+
} else {
85+
part = new Predicate(dname, terms);
86+
// Need to determine if false
87+
if (!folDSDomain.getTrueGoalValue().equals(example.getAttributeValueAsString(dname))) {
88+
part = new NotSentence(part);
89+
}
90+
}
91+
descParts.add(part);
92+
}
93+
if (descParts.size() == 1) {
94+
description = descParts.get(0);
95+
} else if (descParts.size() > 1) {
96+
description = new ConnectedSentence(Connectors.AND, descParts.get(0), descParts.get(1));
97+
for (int i = 2; i < descParts.size(); i++) {
98+
description = new ConnectedSentence(Connectors.AND, description, descParts.get(i));
99+
}
100+
}
101+
}
102+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package aima.learning.knowledge;
2+
3+
/**
4+
* @author Ciaran O'Reilly
5+
*
6+
*/
7+
public class Hypothesis {
8+
9+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package aima.learning.learners;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import aima.learning.framework.DataSet;
7+
import aima.learning.framework.Example;
8+
import aima.learning.framework.Learner;
9+
import aima.learning.knowledge.CurrentBestLearning;
10+
import aima.learning.knowledge.FOLDataSetDomain;
11+
import aima.learning.knowledge.FOLExample;
12+
import aima.learning.knowledge.Hypothesis;
13+
14+
/**
15+
* @author Ciaran O'Reilly
16+
*
17+
*/
18+
public class CurrentBestLearner implements Learner {
19+
private String trueGoalValue = null;
20+
21+
//
22+
// PUBLIC METHODS
23+
//
24+
public CurrentBestLearner(String trueGoalValue) {
25+
this.trueGoalValue = trueGoalValue;
26+
}
27+
28+
//
29+
// START-Learner
30+
public void train(DataSet ds) {
31+
FOLDataSetDomain folDSDomain = new FOLDataSetDomain(ds.specification,
32+
trueGoalValue);
33+
List<FOLExample> folExamples = new ArrayList<FOLExample>();
34+
int egNo = 1;
35+
for (Example e : ds.examples) {
36+
folExamples.add(new FOLExample(folDSDomain, e, egNo));
37+
egNo++;
38+
}
39+
40+
CurrentBestLearning cbl = new CurrentBestLearning(folDSDomain);
41+
42+
Hypothesis h = cbl.currentBestLearning(folExamples);
43+
44+
// TODO
45+
}
46+
47+
public String predict(Example e) {
48+
// TODO
49+
return null;
50+
}
51+
52+
public int[] test(DataSet ds) {
53+
int[] results = new int[] { 0, 0 };
54+
55+
for (Example e : ds.examples) {
56+
if (e.targetValue().equals(predict(e))) {
57+
results[0] = results[0] + 1;
58+
} else {
59+
results[1] = results[1] + 1;
60+
}
61+
}
62+
return results;
63+
}
64+
// END-Learner
65+
//
66+
}

src/aima/test/learningtest/LearnerTests.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import aima.learning.framework.DataSetFactory;
1212
import aima.learning.inductive.DLTest;
1313
import aima.learning.inductive.DLTestFactory;
14+
import aima.learning.learners.CurrentBestLearner;
1415
import aima.learning.learners.DecisionListLearner;
1516
import aima.learning.learners.DecisionTreeLearner;
1617
import aima.learning.learners.MajorityLearner;
@@ -126,4 +127,13 @@ public void testDecisionListTestRunOnRestaurantDataSet() throws Exception {
126127
assertEquals(0, result[1]);
127128
}
128129

130+
public void testCurrentBestLearnerOnRestaurantDataSet() throws Exception {
131+
DataSet ds = DataSetFactory.getRestaurantDataSet();
132+
CurrentBestLearner learner = new CurrentBestLearner("Yes");
133+
learner.train(ds);
134+
// TODO
135+
//int[] result = learner.test(ds);
136+
//assertEquals(12, result[0]);
137+
//assertEquals(0, result[1]);
138+
}
129139
}

0 commit comments

Comments
 (0)