Пример #1
0
Файл: max.go Проект: timtadh/sfp
func (r *CanonMax) Report(n lattice.Node) error {
	if kids, err := n.CanonKids(); err != nil {
		return err
	} else if len(kids) == 0 {
		return r.Reporter.Report(n)
	}
	return nil
}
Пример #2
0
func (m *Miner) step(wg *sync.WaitGroup, n lattice.Node, reports chan lattice.Node, stack *Stack) (err error) {
	if m.Dt.Acceptable(n) {
		wg.Add(1)
		reports <- n
	}
	kids, err := n.CanonKids()
	if err != nil {
		return err
	}
	for _, k := range kids {
		stack.Push(k)
	}
	return nil
}
Пример #3
0
func (w *Walker) Next(cur lattice.Node) (lattice.Node, error) {
	kids, err := cur.CanonKids()
	if err != nil {
		return nil, err
	}
	errors.Logf("DEBUG", "cur %v kids %v", cur, len(kids))
	pr, next, err := walker.Transition(cur, kids, w.weight, true)
	if err != nil {
		return nil, err
	}
	if next == nil && w.Max {
		if ismax, err := cur.Maximal(); err != nil {
			return nil, err
		} else if !ismax {
			return w.Dt.Root(), nil
		}
	}
	return next, w.transitionProbability(cur, next, pr)
}
Пример #4
0
func (m *Miner) mine() (err error) {
	add := func(stack []lattice.Node, n lattice.Node) ([]lattice.Node, error) {
		stack = append(stack, n)
		if len(stack) > m.MaxQueueSize {
			stack = m.dropOne(stack)
		}
		return stack, nil
	}
	root := m.Dt.Root()
	rootKids, err := root.CanonKids()
	if err != nil {
		return err
	}
	for _, rk := range rootKids {
		stack := make([]lattice.Node, 0, 10)
		stack, err = add(stack, rk)
		if err != nil {
			return err
		}
		for len(stack) > 0 {
			var n lattice.Node
			stack, n = m.takeOne(stack)
			if m.Dt.Acceptable(n) {
				err = m.Rptr.Report(n)
				if err != nil {
					return err
				}
			}
			kids, err := n.CanonKids()
			if err != nil {
				return err
			}
			for _, k := range kids {
				stack, err = add(stack, k)
				if err != nil {
					return err
				}
			}
		}
	}
	return nil
}
Пример #5
0
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
}
Пример #6
0
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
}