Esempio n. 1
0
func (this *DepLearner) Consider(np ParentSet) {
	if np == this.parents {
		return
	}
	if stat.NextBernoulli(1/(1+float64(this.consistency))) == 0 {
		return
	}
	alternateRanges := np.CutRanges(this.bg.ranges)
	alternateHistory := this.MakeMappedHistory(np, alternateRanges)
	alternateLoglihood := this.MappedLoglihoodRatio(alternateHistory)
	choiceLL := []float64{this.mappedLoglihood, alternateLoglihood}
	sizeDiff := float64(this.parents.Size(uint32(len(this.bg.ranges))) - np.Size(uint32(len(this.bg.ranges))))
	if sizeDiff > 0 {
		choiceLL[0] += sizeDiff * this.bg.cfg.Kappa
		choiceLL[1] += sizeDiff * (1 - this.bg.cfg.Kappa)
	} else {
		choiceLL[0] -= sizeDiff * (1 - this.bg.cfg.Kappa)
		choiceLL[1] -= sizeDiff * this.bg.cfg.Kappa
	}
	if stat.NextLogChoice(choiceLL) == 1 {
		this.parents = np
		this.cutRanges = alternateRanges
		this.mappedHistory = alternateHistory
		this.mappedLoglihood = alternateLoglihood
		this.consistency = 0
	} else {
		this.consistency++
	}
}
Esempio n. 2
0
func (this *DepLearner) Next(s discrete.State, a discrete.Action) (o int32) {
	sv := this.bg.stateValues[s]
	mv := this.parents.CutValues(sv)
	ms := this.cutRanges.Index(mv)
	mk := a.Hashcode() + this.bg.numActions*ms
	h := this.mappedHistory[mk]
	lls := make([]float64, len(h))
	usePrior := h.Sum() < this.bg.cfg.M
	for i, c := range h {
		if usePrior {
			lls[i] = math.Log(this.bg.cfg.Alpha + float64(c))
		} else {
			lls[i] = math.Log(float64(c))
		}
	}
	oi := uint64(stat.NextLogChoice(lls))
	o = this.bg.myRange.Value(oi)
	return
}