func (w *Walker) weight(_, v lattice.Node) (float64, error) { vmax, err := v.Maximal() if err != nil { return 0, err } if vmax { indeg, err := v.ParentCount() if err != nil { return 0, err } level := float64(v.Pattern().Level()) return (level) / float64(indeg), nil // maxLevel := float64(w.Dt.LargestLevel()) // return (level) / (float64(indeg) * maxLevel), nil } else { // level approximates indeg level := float64(v.Pattern().Level()) // indeg, err := v.ParentCount() // if err != nil { // return 0, err // } odeg, err := v.ChildCount() if err != nil { return 0, err } // return float64(odeg) / float64(indeg), nil return float64(odeg) / (level), nil } }
func (r *PrFormatter) SelectionProbability(n lattice.Node, m interface{}) (float64, error) { var nPr float64 err := r.w.Prs.DoFind(n.Pattern().Label(), func(_ []byte, npr float64) error { nPr = npr return nil }) if err != nil { return 0, err } return nPr, nil }
func newClusterNode(fmtr lattice.Formatter, n lattice.Node, attr string) (*clusterNode, error) { items, err := itemset(n, attr) if err != nil { return nil, err } labels, err := labelset(n) if err != nil { return nil, err } cn := &clusterNode{n.Pattern(), fmtr.PatternName(n), items, labels} return cn, nil }
func (w *Walker) transitionProbability(cur, next lattice.Node, pr float64) error { if next == nil { return nil } if has, err := w.Prs.Has(next.Pattern().Label()); err != nil { return err } else if has { return nil } var curPr float64 if bytes.Equal(w.Dt.Root().Pattern().Label(), cur.Pattern().Label()) { curPr = 1.0 } else { err := w.Prs.DoFind(cur.Pattern().Label(), func(_ []byte, cpr float64) error { // errors.Logf("PR", "cur %v curPr %v", cur, cpr) curPr = cpr return nil }) if err != nil { return err } } nextPr := curPr * pr // errors.Logf("PR", "adding next %v %v %v", next, next.Pattern().Label(), nextPr) err := w.Prs.Add(next.Pattern().Label(), nextPr) if err != nil { return err } // errors.Logf("PR", "curPr %v -> %v -> nextPr %v", curPr, pr, nextPr) return nil }
func MakeUniformWalk(restartPr float64, selfTransition bool) walker.Walk { return func(w *walker.Walker) (chan lattice.Node, chan bool, chan error) { samples := make(chan lattice.Node) terminate := make(chan bool) errs := make(chan error) go func() { cur := w.Dt.Root() loop: for { samples <- cur if <-terminate { break loop } if rand.Float64() < restartPr { errors.Logf("INFO", "a random restart occured with probability %v", restartPr) cur = w.Dt.Root() } else { curLabel := cur.Pattern().Label() nextLabel := curLabel var next lattice.Node = nil for bytes.Equal(curLabel, nextLabel) { var err error next, err = Next(w, cur) if err != nil { errs <- err break loop } if next == nil { errs <- errors.Errorf("next was nil!!") break loop } nextLabel = next.Pattern().Label() if selfTransition { break } } cur = next } } close(samples) close(errs) }() return samples, terminate, errs } }
func (w *Walker) estimateDepthDiameter(v lattice.Node, walks int) (depth, diameter float64, err error) { if kids, err := v.CanonKids(); err != nil { return 0, 0, err } else if len(kids) <= 0 { return 1, 1, nil } var maxDepth int = 0 var maxTail lattice.Pattern = nil tails := set.NewSortedSet(10) for i := 0; i < walks; i++ { errors.Logf("EST-WALK-DEBUG", "walk %v %v", i, v) var path []lattice.Node = nil var err error = nil path, err = w.walkFrom(v) if err != nil { return 0, 0, err } tail := path[len(path)-1].Pattern() tails.Add(tail) if len(path) > maxDepth { maxDepth = len(path) maxTail = tail } } level := maxDepth + v.Pattern().Level() if level < w.Dt.MinimumLevel() { return 0, 0, nil } patterns := make([]lattice.Pattern, 0, tails.Size()) for t, next := tails.Items()(); next != nil; t, next = next() { patterns = append(patterns, t.(lattice.Pattern)) } anc, err := CommonAncestor(patterns) if err != nil { return 0, 0, err } diameter = float64(maxTail.Level()-anc.Level()) + 1 depth = float64(maxDepth) + 1 return depth, diameter, nil }
func (w *Walker) weight(_, v lattice.Node) (float64, error) { label := v.Pattern().Label() if has, err := w.Ests.Has(label); err != nil { return 0, err } else if has { var est float64 err := w.Ests.DoFind(label, func(_ []byte, f float64) error { est = f return nil }) if err != nil { return 0, err } return est, nil } var est float64 if kids, err := v.CanonKids(); err != nil { return 0, err } else if len(kids) > 0 { depth, diameter, err := w.estimateDepthDiameter(v, w.EstimatingWalks) if err != nil { return 0, err } est = depth * diameter if est >= 1 { errors.Logf("DEBUG", "weight %v depth %v diameter %v est %v", v, depth, diameter, est) } } else if v.Pattern().Level() >= w.Dt.MinimumLevel() { est = 1.0 // errors.Logf("INFO", "node %v is max %v est %v", v, ismax, est) } else { est = 0.0 // errors.Logf("INFO", "node %v is max %v but too small v est %v", v, ismax, est) } err := w.Ests.Add(label, est) if err != nil { return 0, err } return est, nil }