Beispiel #1
0
func TestScatterRatio(t *testing.T) {
	points := []*matrix.DenseMatrix{
		matrix.MakeDenseMatrix([]float64{-1, 0}, 2, 1),
		matrix.MakeDenseMatrix([]float64{1, 0}, 2, 1),
		matrix.MakeDenseMatrix([]float64{0, 1}, 2, 1),
		matrix.MakeDenseMatrix([]float64{0, -1}, 2, 1),
	}

	iw1 := NewIWPosterior(1, matrix.Eye(2))
	iw2 := NewIWPosterior(1, matrix.Eye(2))

	scatter1 := NewScatter(2)
	scatter2 := NewScatter(2)

	for _, point := range points {
		scatter1.Insert(point)
		offset, _ := point.PlusDense(matrix.MakeDenseMatrix([]float64{5, 0}, 2, 1))
		scatter2.Insert(offset)
	}

	iw1.InsertScatter(scatter1)
	iw2.InsertScatter(scatter2)

	fmt.Printf("%f\n", iw1.InsertScatterLogRatio(scatter1))
	fmt.Printf("%f\n", iw1.InsertScatterLogRatio(scatter2))

	fmt.Printf("%f\n", iw2.InsertScatterLogRatio(scatter1))
	fmt.Printf("%f\n", iw2.InsertScatterLogRatio(scatter2))
}
Beispiel #2
0
func TestIW(t *testing.T) {
	psi := matrix.MakeDenseMatrix([]float64{1, 0, 0, 1}, 2, 2)
	iwp := NewIWPosterior(1, psi)

	iwp.Insert(matrix.MakeDenseMatrix([]float64{1, 1}, 2, 1))
	iwp.Insert(matrix.MakeDenseMatrix([]float64{2, 1}, 2, 1))
	lr := iwp.InsertLogRatio(matrix.MakeDenseMatrix([]float64{1, 2}, 2, 1))
	println(lr)
}
Beispiel #3
0
func (this *ROARAgent) AgentStep(reward float64, obs rlglue.Observation) rlglue.Action {
	last := matrix.MakeDenseMatrix(this.LastObs.Doubles(), this.numFeatures, 1)
	current := matrix.MakeDenseMatrix(obs.Doubles(), this.numFeatures, 1)
	rm := matrix.MakeDenseMatrix([]float64{reward}, 1, 1)
	outcome, _ := current.MinusDense(last)
	sor, _ := last.Augment(outcome)
	sor, _ = sor.Augment(rm)
	actionIndex := this.task.Act.Ints.Index(this.LastAct.Ints())
	this.rpost[actionIndex].Insert(sor)
	this.LastObs = obs
	return this.GetAction()
}
Beispiel #4
0
func (this *Posterior) ConditionalSample(x1 *matrix.DenseMatrix) (x2 *matrix.DenseMatrix) {

	this.block <- true
	defer func() { <-this.block }()
	//first we choose one

	plls := CRPPrior(this.Cfg.Calpha, this.C)
	//fmt.Printf("plls = %v\n", plls)
	for i, pll := range plls {
		if pll == NegInf {
			continue
		}
		plls[i] += this.SmallClusterLoglihoodRatio(x1, i)
	}

	chosenCluster := LogChoice(plls)

	newCluster := this.C.Count(chosenCluster) == 0
	chosenGroup := this.G.Get(chosenCluster)
	if chosenGroup == -1 || newCluster {
		glls := CRPPrior(this.Cfg.Galpha, this.G)
		chosenGroup = LogChoice(glls)
	}

	this.PrepareGroup(chosenGroup)
	theV := this.V[chosenGroup]
	Sigma := theV.NextCovar()
	x2f := stat.NextMVNormal(theV.S.Mean.Array(), Sigma.Arrays())
	x2 = matrix.MakeDenseMatrix(x2f, Sigma.Rows(), 1)

	return
}
Beispiel #5
0
func TestScatterNeg(t *testing.T) {
	points := []*matrix.DenseMatrix{
		matrix.MakeDenseMatrix([]float64{-5}, 1, 1),
		matrix.MakeDenseMatrix([]float64{-3}, 1, 1),
		matrix.MakeDenseMatrix([]float64{-4}, 1, 1),
		matrix.MakeDenseMatrix([]float64{-6}, 1, 1),
	}
	s1 := NewScatter(1)
	s2 := NewScatter(1)
	for _, point := range points {
		s1.Insert(point)
	}
	fmt.Printf("%v\n", s1)
	for _, point := range points {
		offset, _ := point.PlusDense(matrix.MakeDenseMatrix([]float64{10}, 1, 1))
		s2.Insert(offset)
		//s1.Insert(offset)
	}
	s1.InsertScatter(s2)
	s1.RemoveScatter(s2)
	fmt.Printf("%v\n", s1)
}
Beispiel #6
0
func drawField() {
	scr := window.Screen()
	min, max := scr.Bounds().Min, scr.Bounds().Max
	for dx := min.X; dx < max.X; dx += 3 {
		for dy := min.Y; dy < max.Y; dy += 3 {
			rx := float64(dx) / spanX
			ry := float64(dy) / spanY
			x := matrix.MakeDenseMatrix([]float64{rx, ry}, 2, 1)
			clusterIndex := rpost.BestCluster(x)
			var clusterColor image.RGBAColor
			if clusterIndex == -1 {
				continue
			} else {
				clusterColor = colors[clusterIndex%len(colors)]
			}
			window.Screen().Set(dx, dy, clusterColor)
		}
	}
}
Beispiel #7
0
func rclick(x float64) {
	x1 := matrix.MakeDenseMatrix([]float64{x}, 1, 1)
	x2 := rpost.ConditionalSample(x1)
	click(x1.Get(0, 0), x2.Get(0, 0))
}
Beispiel #8
0
func click(x, y float64) {
	x_t := matrix.MakeDenseMatrix([]float64{x, y}, 2, 1)
	rpost.Insert(x_t)
}