/
MinimalConsistentDet.java
97 lines (92 loc) · 3.23 KB
/
MinimalConsistentDet.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 aima.core.util.math.permute.CombinationGenerator;
import java.util.*;
/**
* Artificial Intelligence A Modern Approach (3rd Edition): Figure 19.2, page
* 771.<br>
* <br>
*
* <pre>
*
* function Minimal-Consistent-Det(E, A) returns a set of attributes
* inputs: E, a set of examples
* A, a set of attributes, of size n
*
* for i = 0 to n do
* for each subset Ai of A of size i do
* if Consistent-Det?(Ai, E) then return Ai
* </pre>
* <pre>
* function Consistent-Det?(A, E) returns a truth value
* inputs: A, a set of attributes
* E, a set of examples
* local variables: H, a hash table
*
* for each example e in E do
* if some example in H has the same values as e for the attributes A
* but a different classification then return false
* store the class of e in_H_, indexed by the values for attributes A of the example e
* return true
* </pre>
* <p>
* Figure ?? An algorithm for finding a minimal consistent determination.
*
* @author samagra
*/
public class MinimalConsistentDet {
/**
* function Minimal-Consistent-Det(E, A) returns a set of attributes
*
* @param e
* inputs: E, a set of examples
* @param a
* A, a set of attributes, of size n
* @return
*/
public Set<String> minimalConsistentDet(List<LogicalExample> e, Set<String> a) {
// for i = 0 to n do
for (int i = 0; i < a.size(); i++) {
// for each subset Ai of A of size i do
for (List<String> subset :
CombinationGenerator.generateCombinations(new ArrayList<>(a), i)) {
// if Consistent-Det?(Ai, E) then return Ai
if (consistentDet(subset, e)) {
return new HashSet<>(subset);
}
}
}
return new HashSet<>();
}
/**
* function Consistent-Det?(A, E) returns a truth value
*
* @param attributes
* inputs: A, a set of attributes
* @param examples
* E, a set of examples
* @return
*/
public boolean consistentDet(List<String> attributes, List<LogicalExample> examples) {
// local variables: H, a hash table
HashMap<List<String>, String> hashTable = new HashMap<>();
// for each example e in E do
for (LogicalExample e :
examples) {
List<String> attributeValues = new ArrayList<>();
for (String attribute : attributes) {
attributeValues.add(e.getAttributes().get(attribute));
}
// if some example in H has the same values as e for the attributes A
if (hashTable.containsKey(attributeValues)) {
// but a different classification then return false
if (!hashTable.get(attributeValues).equals(e.getAttributes().get("Goal")))
return false;
}
// store the class of e in_H_, indexed by the values for
// attributes A of the example e
hashTable.put(attributeValues, e.getAttributes().get("Goal"));
}
// return true
return true;
}
}