public double Calculate(int variable, Varset parents, Dictionary<ulong, int> paCounts) { parents.Set(variable, true); double score = 0; ContingencyTableNode ct = adTree.MakeContab(parents); Calculate(ct, 1, 0, paCounts, variable, parents, -1, ref score); foreach(KeyValuePair<ulong, int> pair in paCounts) { score -= ilogi[pair.Value]; } parents.Set(variable, false); return score; }
public override double CalculateScore(int variable, Varset parents, DoubleMap cache) { Varset parentsCp = new Varset(parents); // TODO check if this violates the constraints //if(constraints != NULL && !constraints.SatisfiesConstraints(variable, parents)) //{ //} // check for prunning double tVal = T(variable, parentsCp); for (int x = 0; x < network.Size(); x++) { if (parentsCp.Get(x)) { parentsCp.Set(x, false); // TODO check the constraints //if (invalidParents.Count > 0 && invalidParents.Contains(parentsCp.ToULong())) //{ // parentsCp.Set(x); // continue; //} double tmp = cache.ContainsKey(parentsCp.ToULong()) ? cache[parentsCp.ToULong()] : 0; if (tmp + tVal > 0) { return 0; } parentsCp.Set(x, true); } } double score = llc.Calculate(variable, parentsCp); // structure penalty score -= tVal * baseComplexityPenalty; //Console.WriteLine("score: " + score); return score; }
private ADNode MakeADTree(int i, BitArray recordNums, int depth, Varset variables) { // since this is index i, there are (variableCount - i) remaining variables. // therefore, it will have that many children int count = 0; for(int idx = 0; idx < recordNums.Count; idx++) { if (recordNums[idx]) { count += 1; } } ADNode adn = new ADNode(network.Size() - i, count); // check if we should just use a leaf list if (adn.Count < rMin) { BitArray leafList = new BitArray(recordNums); adn.LeafList = leafList; return adn; } // for each of the remaining variables for (int j = i; j < network.Size(); j++) { // create a vary node variables.Set(j, true); Varset newVariables = new Varset(variables); VaryNode child = MakeVaryNode(j, recordNums, depth, newVariables); adn.SetChild(j - i, child); } return adn; }
private ContingencyTableNode MakeContabLeafList(Varset variables, BitArray records) { Varset variablesCp = new Varset(variables); if (variablesCp.Equals(zero)) { int count = 0; for (int i = 0; i < records.Count; i++) { if (records[i]) { count += 1; } } return new ContingencyTableNode(count, 0, 1); } int firstIndex = variables.FindFirst(); int cardinality = network.GetCardinality(firstIndex); ContingencyTableNode ct = new ContingencyTableNode(0, cardinality, 0); variablesCp.Set(firstIndex, false); Varset remainingVariables = new Varset(variablesCp); for (int k = 0; k < cardinality; k++) { BitArray r = new BitArray(recordCount); r = r.Or(records); r = r.And(consistentRecords[firstIndex][k]); int count = 0; for (int i = 0; i < r.Count; i++) { if (r[i]) { count += 1; } } if (count > 0) { ContingencyTableNode child = MakeContabLeafList(remainingVariables, r); ct.SetChild(k, child); ct.LeafCount += child.LeafCount; } } return ct; }
public Varset Divide(Varset varset) { int length = this.item.Count; Varset n = new Varset(this); Varset d = new Varset(varset); //Console.Write("n: "); //n.Print(); //Console.Write("d: "); //d.Print(); Varset m = new Varset(n.item.Length); m.Set(0, true); Varset q = new Varset(n.item.Count); Varset zero = new Varset(n.item.Count); if (n.Equals(zero)) { return zero; } else if (d.Equals(zero)) { throw new ArgumentException("Zero Division."); } while (d.LessThan(n) || d.Equals(n)) { d = d.LeftShift(1); m = m.LeftShift(1); } Varset one = new Varset(n.item.Length); one.Set(0, true); while (one.LessThan(m)) { d = d.RightShift(1); m = m.RightShift(1); if (d.LessThan(n) || d.Equals(n)) { n = n.Subtract(d); q = q.Or(m); } } q = q.SubVarset(length); n = n.SubVarset(length); //Console.Write("q: "); //q.Print(); //Console.Write("n: "); //n.Print(); return q; }
public static Varset ClearCopy(Varset varset, int index) { Varset cp = new Varset(varset); cp.Set(index, false); return cp; }
public Varset Subtract(Varset varset) { Varset cp = new Varset(varset); if (cp.item.Length < item.Length) { AlignLength(cp); } Varset one = new Varset(1); one.Set(0, true); cp = cp.Not(); cp = cp.StaticAdd(one); cp = cp.StaticAdd(this); return cp; }
public Varset NextPermutation() { Varset vs = new Varset(this); Varset one = new Varset(0); one.Set(0, true); Varset tmp = vs.Or(vs.Subtract(one)).StaticAdd(one); Varset nextVariables = tmp.Or(tmp.And(tmp.Not().StaticAdd(one)).Divide(vs.And(vs.Not().StaticAdd(one))).RightShift(1).Subtract(one)); return nextVariables; }
private void CalculateScoresInternal(int variable, DoubleMap cache) { // calculate initial score Varset empty = new Varset(variableCount + 1); // 注意: c++だと0 double score = scoringFunction.CalculateScore(variable, empty, cache); if (score < 1) { cache[empty.ToULong()] = score; } int prunedCount = 0; for (int layer = 1; layer <= maxParents && !outOfTime; layer++) { // DEBUG Console.WriteLine("layer: " + layer + ", prunedCount: " + prunedCount); Varset variables = new Varset(variableCount + 1); // 注意: c++だと0 for (int i = 0; i < layer; i++) { variables.Set(i, true); } Varset max = new Varset(variableCount); max.Set(variableCount, true); while (variables.LessThan(max) && !outOfTime) { if (!variables.Get(variable)) { score = scoringFunction.CalculateScore(variable, variables, cache); if (score < 0) { cache[variables.ToULong()] = score; } else { prunedCount++; } } variables = variables.NextPermutation(); } if (!outOfTime) { highestCompletedLayer = layer; } } }
public override double CalculateScore(int variable, Varset parents, DoubleMap cache) { Scratch s = scratchSpace[variable]; // TODO check if this violates the constraints //if (constraints != NULL && !constraints->satisfiesConstraints(variable, parents)) //{ // s->invalidParents.insert(parents); // return 1; //} for (int x = 0; x < network.Size(); x++) { if (parents.Get(x)) { parents.Set(x, false); // TODO check the constraints //if (invalidParents.Count > 0 && invalidParents.Contains(parents.ToULong())) //{ // // we cannot say anything if we skipped this because of constraints // parents.Set(x, true); // continue; //} parents.Set(x, true); } } Lg(parents, ref s); Varset variables = new Varset(parents); variables.Set(variable, true); s.Score = 0; ContingencyTableNode ct = adTree.MakeContab(variables); Dictionary<ulong, int> paCounts = new Dictionary<ulong, int>(); Calculate(ct, 1, 0, paCounts, variables, -1, ref s); // check constraints (Theorem 9 from de Campos and Ji '11) // only bother if the alpha bound is okay // check if can prune if (s.Aij <= 0.8349) { double bound = -1.0 * ct.LeafCount * s.Lri; // check each subset for (int x = 0; x < network.Size(); x++) { if (parents.Get(x)) { parents.Set(x, false); // check the constraints //if (s->invalidParents.find(parents) != s->invalidParents.end()) //{ // // we cannot say anything if we skipped this because of constraints // VARSET_SET(parents, x); // continue; //} double tmp = cache.ContainsKey(parents.ToULong()) ? cache[parents.ToULong()] : 0; // if the score is larger (better) than the bound, then we can prune if (tmp > bound) { return 0; } parents.Set(x, true); } } } foreach (KeyValuePair<ulong, int> kvp in paCounts) { s.Score += s.Lgij; s.Score -= ScoreCalculator.GammaLn(s.Aij + kvp.Value); } //parents.Print(); //Console.WriteLine(variable); return s.Score; }