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; }
public void Prune(DoubleMap cache) { List<KeyValuePair<ulong, double>> pairs = new List<KeyValuePair<ulong, double>>(); foreach (KeyValuePair<ulong, double> kvp in cache) { pairs.Add(kvp); } pairs.Sort(Comparison); // keep track of the ones that have been pruned BitArray prunedSet = new BitArray(pairs.Count); for (int i = 0; i < pairs.Count; i++) { if (prunedSet.Get(i)) { continue; } Varset pi = new Varset(pairs[i].Key); // make sure this variable set is not in an incomplete last layer if (pi.Cardinality() > highestCompletedLayer) { prunedSet.Set(i, true); continue; } for (int j = i + 1; j < pairs.Count; j++) { if (prunedSet.Get(j)) { continue; } // check if parents[i] is a subset of parents[j] Varset pj = new Varset(pairs[j].Key); if (pi.And(pj).Equals(pi)) // 部分集合かどうかの判定 { // then we can prune pj prunedSet.Set(j, true); cache.Remove(pj.ToULong()); } } } }
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; }