-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSolver.java
114 lines (82 loc) · 1.9 KB
/
Solver.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
public class Solver {
static ArrayList<HashSet> kb = new ArrayList();
static ArrayList<HashSet> clauses = new ArrayList();
//static ArrayList<String> newClause = null;
String[] colors = {"r","g","b"};
static HashSet<Integer> []connections = new HashSet[6];
public void makeKB()
{
HashSet a = new HashSet();
a.add("p!");
a.add("a");
a.add("c");
kb.add(a);
HashSet b = new HashSet();
b.add("a!");
kb.add(b);
HashSet c = new HashSet();
c.add("p");
c.add("c!");
kb.add(c);
}
public HashSet makeClauses()
{
while(true)
{
HashSet<HashSet> nClause = new HashSet();
HashSet<HashSet> answers = new HashSet();
for(HashSet i : kb)
{
for(HashSet j : kb)
{
if(i!=j)
{
answers = pl_resolve(i,j);
nClause.addAll(answers);
}
}
}
if(kb.containsAll(nClause))
{
return nClause;
}
else
kb.addAll(nClause);
}
}
public HashSet<HashSet> pl_resolve(HashSet<String> ci, HashSet<String> cj)
{
HashSet<HashSet> something = new HashSet();
String oliteral =null;
for(String literal: ci){
if(literal.endsWith("!"))
oliteral = literal.substring(0, literal.length()-1);
else
oliteral =literal+"!";
if(cj.contains(oliteral))
{
HashSet<String> newClause = new HashSet();
newClause.addAll(ci);
newClause.addAll(cj);
newClause.remove(literal);
newClause.remove(oliteral);
something.add(newClause);
}
}
return something;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Solver solve = new Solver();
// solve.makeConnections();
solve.makeKB();
HashSet<HashSet> answer = solve.makeClauses();
for(HashSet i : answer){
System.out.println(i);
}
// solve.printList();
}
}