private void variablize(Clause p) { cnt = p.maxIndex() + 1; Dictionary <string, string> replaceList = new Dictionary <string, string>(); //TODO: check the count of f1: more than one add f1 foreach (Literal pc in p.getPSide()) { foreach (string f1 in pc.items) { if (!f1.StartsWith("X")) { if (p.numberOfOccurence(f1) > 1) { if (!replaceList.ContainsKey(f1)) { replaceList.Add(f1, "X" + cnt); cnt++; } } } } } foreach (KeyValuePair <string, string> kv in replaceList.ToArray()) { if (p.containsMoreThanOne(kv.Key)) { p.replace(kv.Key, kv.Value); } } p.replaceList = replaceList; }
public void sortVariables(Clause p) { string s = p.ToString(); int index = 0; int index2 = 0; int i = 0; while (index != -1) { index = s.IndexOf("X", index2); if (index != -1) { index2 = s.IndexOf(",", index); string s2 = s.Substring(index, index2 - index); s = s.Replace(s2, "Y" + i); p.replace(s2, "Y" + i); i++; } } p.changeY2X(); s = s.Replace("Y", "X"); }
/* public void addPredicate(Clause p) * { * predicates.Add(p); * p.positiveCoverage = computeCoverage(p, true, false); * } */ public ArrayList induceOnePredicate(Clause pred, int step) { // Console.WriteLine(">>>>>InduceOnePredicate " + pred.ToString() + "step: " + step); ArrayList H = new ArrayList(); if (step > MaxSteps) { return(H); } HashSet <Literal> set = new HashSet <Literal>(); ArrayList seen = new ArrayList(); ArrayList notSeen = new ArrayList(); foreach (string s in pred.q_side.items) { if (!s.StartsWith("X")) { notSeen.Add(s); } } foreach (Literal p in pred.getPSide()) { foreach (string s in p.items) { if (!s.StartsWith("X")) { notSeen.Add(s); } } } while (notSeen.Count > 0) { string first = (string)notSeen[0]; notSeen.RemoveAt(0); seen.Add(first); try { foreach (Literal c1 in hash.getList(first)) { //contains: first + predicate name must be unique int comparison = pred.getLastLiteral().CompareTo(c1.ToString()); if (c1.fact != pred.q_side.fact)// && !contains(set, first, c1.fact)) { if (comparison <= 0) { set.Add(c1); } } } } catch (Exception e) { //we have some literal in positive examples but not in background knowledge } } foreach (Literal p1 in set) //all subsets { Clause newp = new Clause(); newp.q_side = pred.q_side.clone(); foreach (Literal c in pred.getPSide()) { newp.addPSide(c.clone()); } if (p1 != pred.q_side) { newp.addPSide(p1.clone()); } if (pred.replaceList != null) { foreach (KeyValuePair <string, string> kv in pred.replaceList.ToArray()) { newp.replace(kv.Key, kv.Value); } } variablize(newp); newp.step = step; H.Add(newp); } return(H); }
public bool checkCoverage(Clause p, Literal cls) { // Console.WriteLine(p.ToString()); // Console.WriteLine(cls.ToString()); Substitution substitution = new Substitution(); bool flag = true; if (p.q_side.fact.Equals(cls.fact)) { for (int i = 0; i < cls.items.Count; i++) { string s2 = (string)p.q_side.items[i]; string s1 = (string)cls.items[i]; if (s2.StartsWith("X")) { substitution.add(s2, s1); } else if (!s1.Equals(s2)) { flag = false; // if they are different in constant value, then they cannot be the same e.g. egg(a1,false) and egg(X0,true) } } if (flag) { foreach (KeyValuePair <string, string> kv in substitution.pairs) { p.replace(kv.Key, kv.Value); } } else { return(false); } } else { return(false); } while (true) { // Binding b = new Binding(); Literal min = null; int minChoice = 100000000; bool novariable = true; foreach (Literal c2 in p.getPSide()) { if (c2.hasVariable()) { novariable = false; ArrayList result = findAllPossibleReplacement(c2); if (result.Count < minChoice) { minChoice = result.Count; min = c2; } if (result.Count == 1) { break; } } } if (novariable) { if (checkExistanceOfAllClauses(p)) { return(true); } else { return(false); } } if (minChoice == 0) { return(false); } else if (minChoice == 1) { ArrayList ar = findAllPossibleReplacement(min); Substitution s = (Substitution)ar[0]; foreach (KeyValuePair <string, string> kv in s.pairs) { p.replace(kv.Key, kv.Value); } } else if (minChoice > 1) { ArrayList ar = findAllPossibleReplacement(min); Substitution s = (Substitution)ar[0]; foreach (KeyValuePair <string, string> kv in s.pairs) { p.replace(kv.Key, kv.Value); } }//TODO: more than one } }