/
CurrentBestLearning.java
97 lines (94 loc) · 3.77 KB
/
CurrentBestLearning.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package aima.core.learning.knowledge;
import java.util.List;
/**
* Artificial Intelligence A Modern Approach (3rd Edition): Figure 19.2, page
* 771.<br>
* <br>
*
* <pre>
* function CURRENT-BEST-LEARNING(examples, h) returns a hypothesis or fail
*
* if examples is empty then
* return h
* e <- FIRST(examples)
* if e is consistent with h then
* return CURRENT-BEST-LEARNING(REST(examples), h)
* else if e is a false positive for h then
* for each h' in specializations of h consistent with examples seen so far do
* h'' <- CURRENT-BEST-LEARNING(REST(examples), h')
* if h'' != fail then return h''
* else if e is a false negative for h then
* for each h' in generalization of h consistent with examples seen so far do
* h'' <- CURRENT-BEST-LEARNING(REST(examples), h')
* if h'' != fail then return h''
* return fail
* </pre>
* <p>
* Figure 19.2 The current-best-hypothesis learning algorithm. It searches for a
* consistent hypothesis that fits all the examples and backtracks when no
* consistent specialization/generalization can be found. To start the
* algorithm, any hypothesis can be passed in; it will be specialized or
* generalized as needed.
*
* @author Ciaran O'Reilly
* @author samagra
*/
public class CurrentBestLearning {
/**
* function CURRENT-BEST-LEARNING(examples, h) returns a hypothesis or fail
*
* @param examples
* @param h
* @param examplesSoFar
* @return
*/
public Hypothesis currentBestLearning(List<LogicalExample> examples, Hypothesis h, List<LogicalExample> examplesSoFar) {
// if examples is empty then
if (examples.isEmpty()) {
//return h
return h;
}
// e <- FIRST(examples)
LogicalExample e = examples.get(0);
examplesSoFar.add(e);
// if e is consistent with h then
if (h.isConsistent(e)) {
// return CURRENT-BEST-LEARNING(REST(examples), h)
return currentBestLearning(examples.subList(1, examples.size()), h, examplesSoFar);
}
// else if e is a false positive for h then
else if ((h.predict(e)) && (!e.getGoal())) {
// for each h' in specializations of h consistent with examples seen so far do
for (Hypothesis hypothesis :
h.specialisations(examplesSoFar)) {
if (hypothesis.isConsistent(examplesSoFar)) {
// h'' <- CURRENT-BEST-LEARNING(REST(examples), h')
Hypothesis newHypothesis = currentBestLearning(examples.subList(1, examples.size())
, hypothesis, examplesSoFar);
// if h'' != fail then return h''
if (newHypothesis != null) {
return newHypothesis;
}
}
}
}
// else if e is a false negative for h then
else if ((!h.predict(e)) && (e.getGoal())) {
// for each h' in generalization of h consistent with examples seen so far do
for (Hypothesis hypothesis :
h.generalisations(examplesSoFar)) {
if (hypothesis.isConsistent(examplesSoFar)) {
// h'' <- CURRENT-BEST-LEARNING(REST(examples), h')
Hypothesis newHypothesis = currentBestLearning(examples.subList(1, examples.size()),
hypothesis, examplesSoFar);
// if h'' != fail then return h''
if (newHypothesis != null) {
return newHypothesis;
}
}
}
}
// return fail
return null;
}
}