Beispiel #1
0
func TestVectors(t *testing.T) {
	a := matrix.FloatWithValue(1, 5, 1.0)
	b := matrix.FloatWithValue(1, 5, 2.0)
	c := matrix.FloatWithValue(5, 1, 2.0)
	ar := a.FloatArray()
	br := b.FloatArray()
	cr := c.FloatArray()
	v := DDot(ar, br, 1.0, a.LeadingIndex(), b.LeadingIndex(), a.NumElements())
	t.Logf("a*b = %.1f\n", v)
	v = DDot(cr, br, 1.0, 1, b.LeadingIndex(), c.NumElements())
	t.Logf("c*b = %.1f\n", v)
	v = DNorm2(br, 1, b.NumElements())
	t.Logf("norm2(b) = %.1f\n", v)

	b0 := matrix.FloatNew(1, 5, []float64{-1.0, 0.0, 2.0, -3.0, 5.0})
	br = b0.FloatArray()
	ix := DIAMax(br, 1, b0.NumElements())
	t.Logf("iamax = %d: %.1f %v\n", ix, b0.GetIndex(ix), b0)

	b0 = matrix.FloatNew(1, 5, []float64{-8.0, 0.0, 2.0, -3.0, 5.0})
	br = b0.FloatArray()
	ix = DIAMax(br, 1, b0.NumElements())
	t.Logf("iamax = %d: %.1f %v\n", ix, b0.GetIndex(ix), b0)

	b0 = matrix.FloatNew(1, 5, []float64{-8.0, 0.0, 2.0, -9.0, 5.0})
	br = b0.FloatArray()
	ix = DIAMax(br, 1, b0.NumElements())
	t.Logf("iamax = %d: %.1f %v\n", ix, b0.GetIndex(ix), b0)
}
Beispiel #2
0
// min(x) st. x+y = 1, x >= y
func TestSimple(t *testing.T) {

	A := matrix.FloatNew(2, 3, []float64{1.0, -1.0, 0.0, 1.0, 0.0, 1.0})
	b := matrix.FloatNew(2, 1, []float64{1.0, 0.0})
	c := matrix.FloatNew(3, 1, []float64{0.0, 1.0, 0.0})
	G := matrix.FloatNew(1, 3, []float64{0.0, -1.0, 1.0})
	h := matrix.FloatNew(1, 1, []float64{0.0})
	dims := sets.NewDimensionSet("l", "q", "s")
	dims.Set("l", []int{1})

	t.Logf("A=\n%v\n", A)
	t.Logf("b=\n%v\n", b)
	t.Logf("G=\n%v\n", G)
	t.Logf("h=\n%v\n", h)
	t.Logf("c=\n%v\n", c)

	// this should work...
	t.Logf("Ldl solver ...\n")
	sol, err := solve("ldl", c, G, h, A, b)
	if sol != nil && sol.Status == Optimal {
		x := sol.Result.At("x")[0]
		s := sol.Result.At("s")[0]
		z := sol.Result.At("z")[0]
		t.Logf("x=\n%v\n", x.ToString("%.9f"))
		t.Logf("s=\n%v\n", s.ToString("%.9f"))
		t.Logf("z=\n%v\n", z.ToString("%.9f"))
	} else {
		t.Logf("status: %v\n", err)
		t.Fail()
	}

	// this should work too
	t.Logf("chol2 solver ...\n")
	sol, err = solve("chol2", c, G, h, A, b)
	if err != nil {
		t.Logf("chol2 status: %v\n", err)
		t.Fail()
	}
}
Beispiel #3
0
func TestDgemv(t *testing.T) {
	fmt.Printf("* L2 * test gemv: Y = alpha * A * X + beta * Y\n")
	A := matrix.FloatNew(3, 2, []float64{1, 1, 1, 2, 2, 2})
	X := matrix.FloatVector([]float64{1, 1})
	Y := matrix.FloatVector([]float64{0, 0, 0})
	alpha := matrix.FScalar(1.0)
	beta := matrix.FScalar(0.0)
	fmt.Printf("before: alpha=1.0, beta=0.0\nA=\n%v\nX=\n%v\nY=\n%v\n", A, X, Y)
	err := Gemv(A, X, Y, alpha, beta)
	fmt.Printf("after:\nA=\n%v\nX=\n%v\nY=\n%v\n", A, X, Y)
	fmt.Printf("* L2 * test gemv: X = alpha * A.T * Y + beta * X\n")
	err = Gemv(A, Y, X, alpha, beta, linalg.OptTrans)
	if err != nil {
		fmt.Printf("error: %s\n", err)
	}
	fmt.Printf("after:\nA=\n%v\nX=\n%v\nY=\n%v\n", A, X, Y)
}
Beispiel #4
0
// The small GP of section 9.3 (Geometric programming).
func TestGp(t *testing.T) {

	xref := []float64{1.06032641296944741, 1.75347359157296845, 2.44603683900611868}

	aflr := 1000.0
	awall := 100.0
	alpha := 0.5
	beta := 2.0
	gamma := 0.5
	delta := 2.0

	fdata := [][]float64{
		[]float64{-1.0, 1.0, 1.0, 0.0, -1.0, 1.0, 0.0, 0.0},
		[]float64{-1.0, 1.0, 0.0, 1.0, 1.0, -1.0, 1.0, -1.0},
		[]float64{-1.0, 0.0, 1.0, 1.0, 0.0, 0.0, -1.0, 1.0}}

	gdata := []float64{1.0, 2.0 / awall, 2.0 / awall, 1.0 / aflr, alpha, 1.0 / beta, gamma, 1.0 / delta}

	g := matrix.FloatNew(8, 1, gdata).Log()
	F := matrix.FloatMatrixFromTable(fdata)
	K := []int{1, 2, 1, 1, 1, 1, 1}

	var solopts SolverOptions
	solopts.MaxIter = 40
	solopts.ShowProgress = false
	solopts.KKTSolverName = "ldl"
	sol, err := Gp(K, F, g, nil, nil, nil, nil, &solopts)
	if sol != nil && sol.Status == Optimal {
		x := sol.Result.At("x")[0]
		r := matrix.Exp(x)
		h := r.GetIndex(0)
		w := r.GetIndex(1)
		d := r.GetIndex(2)
		t.Logf("x=\n%v\n", x.ToString("%.9f"))
		t.Logf("h = %f,  w = %f, d = %f.\n", h, w, d)
		xe, _ := nrmError(matrix.FloatVector(xref), x)
		if xe > TOL {
			t.Logf("x differs [%.3e] from exepted too much.", xe)
			t.Fail()
		}
	} else {
		t.Logf("status: %v\n", err)
		t.Fail()
	}
}
Beispiel #5
0
// Solves a pair of primal and dual SDPs
//
//        minimize    c'*x
//        subject to  Gl*x + sl = hl
//                    mat(Gs[k]*x) + ss[k] = hs[k], k = 0, ..., N-1
//                    A*x = b
//                    sl >= 0,  ss[k] >= 0, k = 0, ..., N-1
//
//        maximize    -hl'*z - sum_k trace(hs[k]*zs[k]) - b'*y
//        subject to  Gl'*zl + sum_k Gs[k]'*vec(zs[k]) + A'*y + c = 0
//                    zl >= 0,  zs[k] >= 0, k = 0, ..., N-1.
//
// The inequalities sl >= 0 and zl >= 0 are elementwise vector
// inequalities.  The inequalities ss[k] >= 0, zs[k] >= 0 are matrix
// inequalities, i.e., the symmetric matrices ss[k] and zs[k] must be
// positive semidefinite.  mat(Gs[k]*x) is the symmetric matrix X with
// X[:] = Gs[k]*x.  For a symmetric matrix, zs[k], vec(zs[k]) is the
// vector zs[k][:].
//
func Sdp(c, Gl, hl, A, b *matrix.FloatMatrix, Ghs *sets.FloatMatrixSet, solopts *SolverOptions,
	primalstart, dualstart *sets.FloatMatrixSet) (sol *Solution, err error) {
	if c == nil {
		err = errors.New("'c' must a column matrix")
		return
	}
	n := c.Rows()
	if n < 1 {
		err = errors.New("Number of variables must be at least 1")
		return
	}
	if Gl == nil {
		Gl = matrix.FloatZeros(0, n)
	}
	if Gl.Cols() != n {
		err = errors.New(fmt.Sprintf("'G' must be matrix with %d columns", n))
		return
	}
	ml := Gl.Rows()
	if hl == nil {
		hl = matrix.FloatZeros(0, 1)
	}
	if !hl.SizeMatch(ml, 1) {
		err = errors.New(fmt.Sprintf("'hl' must be matrix of size (%d,1)", ml))
		return
	}
	Gsset := Ghs.At("Gs")
	ms := make([]int, 0)
	for i, Gs := range Gsset {
		if Gs.Cols() != n {
			err = errors.New(fmt.Sprintf("'Gs' must be list of matrices with %d columns", n))
			return
		}
		sz := int(math.Sqrt(float64(Gs.Rows())))
		if Gs.Rows() != sz*sz {
			err = errors.New(fmt.Sprintf("the squareroot of the number of rows of 'Gq[%d]' is not an integer", i))
			return
		}
		ms = append(ms, sz)
	}

	hsset := Ghs.At("hs")
	if len(Gsset) != len(hsset) {
		err = errors.New(fmt.Sprintf("'hs' must be a list of %d matrices", len(Gsset)))
		return
	}
	for i, hs := range hsset {
		if !hs.SizeMatch(ms[i], ms[i]) {
			s := fmt.Sprintf("hq[%d] has size (%d,%d). Expected size is (%d,%d)",
				i, hs.Rows(), hs.Cols(), ms[i], ms[i])
			err = errors.New(s)
			return
		}
	}
	if A == nil {
		A = matrix.FloatZeros(0, n)
	}
	if A.Cols() != n {
		err = errors.New(fmt.Sprintf("'A' must be matrix with %d columns", n))
		return
	}
	p := A.Rows()
	if b == nil {
		b = matrix.FloatZeros(0, 1)
	}
	if !b.SizeMatch(p, 1) {
		err = errors.New(fmt.Sprintf("'b' must be matrix of size (%d,1)", p))
		return
	}
	dims := sets.NewDimensionSet("l", "q", "s")
	dims.Set("l", []int{ml})
	dims.Set("s", ms)
	N := dims.Sum("l") + dims.SumSquared("s")

	// Map hs matrices to h vector
	h := matrix.FloatZeros(N, 1)
	h.SetIndexesFromArray(hl.FloatArray()[:ml], matrix.MakeIndexSet(0, ml, 1)...)
	ind := ml
	for k, hs := range hsset {
		h.SetIndexesFromArray(hs.FloatArray(), matrix.MakeIndexSet(ind, ind+ms[k]*ms[k], 1)...)
		ind += ms[k] * ms[k]
	}

	Gargs := make([]*matrix.FloatMatrix, 0)
	Gargs = append(Gargs, Gl)
	Gargs = append(Gargs, Gsset...)
	G, sizeg := matrix.FloatMatrixStacked(matrix.StackDown, Gargs...)

	var pstart, dstart *sets.FloatMatrixSet = nil, nil
	if primalstart != nil {
		pstart = sets.NewFloatSet("x", "s")
		pstart.Set("x", primalstart.At("x")[0])
		slset := primalstart.At("sl")
		margs := make([]*matrix.FloatMatrix, 0, len(slset)+1)
		margs = append(margs, primalstart.At("s")[0])
		margs = append(margs, slset...)
		sl, _ := matrix.FloatMatrixStacked(matrix.StackDown, margs...)
		pstart.Set("s", sl)
	}

	if dualstart != nil {
		dstart = sets.NewFloatSet("y", "z")
		dstart.Set("y", dualstart.At("y")[0])
		zlset := primalstart.At("zl")
		margs := make([]*matrix.FloatMatrix, 0, len(zlset)+1)
		margs = append(margs, dualstart.At("z")[0])
		margs = append(margs, zlset...)
		zl, _ := matrix.FloatMatrixStacked(matrix.StackDown, margs...)
		dstart.Set("z", zl)
	}

	//fmt.Printf("h=\n%v\n", h.ToString("%.3f"))
	//fmt.Printf("G=\n%v\n", G.ToString("%.3f"))

	sol, err = ConeLp(c, G, h, A, b, dims, solopts, pstart, dstart)
	// unpack sol.Result
	if err == nil {
		s := sol.Result.At("s")[0]
		sl := matrix.FloatVector(s.FloatArray()[:ml])
		sol.Result.Append("sl", sl)
		ind := ml
		for _, m := range ms {
			sk := matrix.FloatNew(m, m, s.FloatArray()[ind:ind+m*m])
			sol.Result.Append("ss", sk)
			ind += m * m
		}

		z := sol.Result.At("z")[0]
		zl := matrix.FloatVector(s.FloatArray()[:ml])
		sol.Result.Append("zl", zl)
		ind = ml
		for i, k := range sizeg[1:] {
			zk := matrix.FloatNew(ms[i], ms[i], z.FloatArray()[ind:ind+k])
			sol.Result.Append("zs", zk)
			ind += k
		}
	}
	sol.Result.Remove("s")
	sol.Result.Remove("z")

	return

}
Beispiel #6
0
func TestMultMVTransASmall(t *testing.T) {
	data6 := [][]float64{
		[]float64{-1.59e+00, 6.56e-02, 2.14e-01, 6.79e-01, 2.93e-01, 5.24e-01},
		[]float64{4.28e-01, 1.57e-01, 3.81e-01, 2.19e-01, 2.97e-01, 2.83e-02},
		[]float64{3.02e-01, 9.70e-02, 3.18e-01, 2.03e-01, 7.53e-01, 1.58e-01},
		[]float64{1.99e-01, 3.01e-01, 4.69e-01, 3.61e-01, 2.07e-01, 6.07e-01},
		[]float64{1.93e-01, 5.15e-01, 2.83e-01, 5.71e-01, 8.65e-01, 9.75e-01},
		[]float64{3.13e-01, 8.14e-01, 2.93e-01, 8.62e-01, 6.97e-01, 7.95e-02}}
	data5 := [][]float64{
		[]float64{1.57e-01, 3.81e-01, 2.19e-01, 2.97e-01, 2.83e-02},
		[]float64{9.70e-02, 3.18e-01, 2.03e-01, 7.53e-01, 1.58e-01},
		[]float64{3.01e-01, 4.69e-01, 3.61e-01, 2.07e-01, 6.07e-01},
		[]float64{5.15e-01, 2.83e-01, 5.71e-01, 8.65e-01, 9.75e-01},
		[]float64{8.14e-01, 2.93e-01, 8.62e-01, 6.97e-01, 7.95e-02}}
	data2 := []float64{4.28e-01, 3.02e-01, 1.99e-01, 1.93e-01, 3.13e-01}

	bM := 5
	bN := 4
	nb := 2
	//A := matrix.FloatNormal(bN, bM)
	//X := matrix.FloatWithValue(bN, 1, 1.0)

	A := matrix.FloatMatrixFromTable(data5, matrix.RowOrder)
	X := matrix.FloatNew(5, 1, data2)
	bM = A.Rows()
	bN = A.Cols()
	Ym := matrix.FloatZeros(3, bM)
	Y1 := matrix.FloatZeros(bM, 1)
	Y0 := matrix.FloatZeros(bM, 1)

	Ar := A.FloatArray()
	Xr := X.FloatArray()
	Y1r := Y1.FloatArray()

	blas.GemvFloat(A, X, Y0, 1.0, 1.0, linalg.OptTrans)

	DMultMV(Y1r, Ar, Xr, 1.0, 1.0, TRANSA, 1, A.LeadingIndex(), 1, 0, bN, 0, bM, nb, nb)
	ok := Y0.AllClose(Y1)
	t.Logf("Y0 == Y1: %v\n", ok)
	if ok || !ok {
		t.Logf("blas: Y=A.T*X\n%v\n", Y0)
		t.Logf("Y1: Y1 = A*X\n%v\n", Y1)
	}

	// zero Y0, Y1
	Y0.Scale(0.0)
	Y1.Scale(0.0)

	// test with matrix view; A is view
	var A0 matrix.FloatMatrix
	A6 := matrix.FloatMatrixFromTable(data6, matrix.RowOrder)
	A0.SubMatrixOf(A6, 1, 1)

	blas.GemvFloat(&A0, X, Y0, 1.0, 1.0, linalg.OptTrans)

	Ar = A0.FloatArray()
	DMultMV(Y1r, Ar, Xr, 1.0, 1.0, TRANSA, 1, A0.LeadingIndex(), 1, 0, bN, 0, bM, nb, nb)
	ok = Y0.AllClose(Y1)
	t.Logf("lda>rows: Y0 == Y1: %v\n", ok)
	if ok || !ok {
		t.Logf("blas: Y=A.T*X\n%v\n", Y0)
		t.Logf("Y1: Y1 = A*X\n%v\n", Y1)
	}

	// Y is view too.
	Y1.SubMatrixOf(Ym, 0, 0, 1, bM)
	Y1r = Y1.FloatArray()
	DMultMV(Y1r, Ar, Xr, 1.0, 1.0, TRANSA, Y1.LeadingIndex(), A0.LeadingIndex(), 1, 0, bN, 0, bM, nb, nb)
	ok = Y0.AllClose(Y1.Transpose())
	t.Logf("Y0 == Y1 row: %v\n", ok)
	t.Logf("row Y1: %v\n", Y1)
}
Beispiel #7
0
func TestMcSdp(t *testing.T) {
	dataref := []float64{
		0.13391860811867590, -0.08810099183143839, 1.67440840625377385,
		0.73364110729257948, 0.99752463160201232, -1.27750208100276641,
		-2.39671528273320744, -0.67928016472921104, -0.03909131843358723,
		0.89355554552081806, -0.01764779458978329, -1.29655690877732033,
		-0.66798050600863512, 0.18170877830799323, 0.83105079034069296,
		-0.54824847139682842, -0.63803150269339892, 0.00708853169542829,
		-0.66985369437758802, -0.82776233138694755, 0.61250003036052025,
		-0.36758841015776106, -0.28949340911906524, -0.91687467081464402,
		-0.58380545879822354, 0.06461529015869494, 0.04674385977341894,
		-0.75072697866800864, -0.14423268736469502, 1.47556079692186803,
		-0.20647897491996936, -0.26069727887528138, -0.13821797189084586,
		-2.54339721705159461, 0.57741853670968823, -0.03610986310676272,
		-0.22617256815925429, 0.71500221712914425, 1.57726688654131597,
		-0.39227386148741172, 1.18277085579223740, 1.79311368741147459,
		-0.73134596730591273, -0.62981768127016369, 2.07069457080308439,
		-0.90300600743388137, 1.73554553761132668, 1.38308886012672638,
		-0.37536663987692281, -0.30431019951136606, 1.25357124882638882,
		-0.27384517924722629, -0.04632938142394107, 0.79898247010790735,
		0.53995588924910287, 0.08585033894586751, 1.35529122074053832,
		0.01385086083133875, 0.75136369502320788, -1.57490917820568810,
		-0.23737825305680132, 0.33649059262790887, -0.07959967279201274,
		-0.54087224229887154, -1.38043394277880926, 1.71655186027439033,
		0.41811180400013570, 1.52495658744607199, 0.68647633127251673,
		-0.58855597085742983, 1.31674790938058739, -0.83298414482940919,
		0.77396029737830319, -0.80351540646387032, -0.08543027502251639,
		1.49619789524058855, 0.39776679351920241, 3.33842986686760090,
		1.80093502635427316, 1.75627861060973078, 0.66747365962131211,
		-1.05016810484723888, 0.32937053624566975, 0.45508103845913089,
		-2.34700732048193306, -0.61389923742031549, -2.16180058868422442,
		-1.00134312023216387, -0.46226762826081341, -0.81756106137679618,
		0.18439203820434547, -0.39491697602688680, -1.66955730897081911,
		-0.94514784362504667, 0.00356999348571242, 1.15573946640829073,
		1.87743135406613315, 0.11219893028802071, 0.18121323774935597,
		1.27830445029226136}

	xref := []float64{
		4.35021720027799841, 3.05093154317696458, 3.81967245723734372,
		4.46546919954538968, 1.84870016923725555, 1.98581804497831516,
		4.10446362983979540, 0.71734387468946914, 4.36803763631339592, 4.18804903479807233}

	zref := []float64{
		1.00000000000000022, 0.85303260971097450, -0.85020234203223444,
		-0.64630863726711674, -0.66211228030949409, 0.12515757728046226,
		0.96725545921055622, -0.37698782755909727, 0.39233601196202428,
		0.43202712322170089, 0.85303260971097450, 0.99999999999999956,
		-0.99998464240308171, -0.94953899113609297, -0.17372147858022127,
		0.62451665675716073, 0.69265128272781040, -0.80493649049792482,
		0.81469110300198433, 0.83917543899780545, -0.85020234203223444,
		-0.99998464240308171, 1.00000000000000089, 0.95121850038903877,
		0.16840142730281188, -0.62872513058587987, -0.68874630140860726,
		0.80812857148627615, -0.81781005005790097, -0.84210005596350634,
		-0.64630863726711674, -0.94953899113609297, 0.95121850038903877,
		1.00000000000000000, -0.14392344681149352, -0.83796540396053343,
		-0.43147384025661650, 0.95042502132018603, -0.95546406276523821,
		-0.96741055070299309, -0.66211228030949409, -0.17372147858022127,
		0.16840142730281188, -0.14392344681149352, 1.00000000000000067,
		0.66064328265023031, -0.83063373455540346, -0.44450355615883147,
		0.42954780821049410, 0.38980770530806869, 0.12515757728046226,
		0.62451665675716073, -0.62872513058587987, -0.83796540396053343,
		0.66064328265023031, 0.99999999999999978, -0.13074907207216546,
		-0.96611746250486030, 0.96169189357427165, 0.94884018282016180,
		0.96725545921055622, 0.69265128272781040, -0.68874630140860726,
		-0.43147384025661650, -0.83063373455540346, -0.13074907207216546,
		1.00000000000000044, -0.12956585354159514, 0.14603486320665809,
		0.18898493355529175, -0.37698782755909727, -0.80493649049792482,
		0.80812857148627615, 0.95042502132018603, -0.44450355615883147,
		-0.96611746250486030, -0.12956585354159514, 1.00000000000000067,
		-0.99986146360473316, -0.99818853872682889, 0.39233601196202428,
		0.81469110300198433, -0.81781005005790097, -0.95546406276523821,
		0.42954780821049410, 0.96169189357427165, 0.14603486320665809,
		-0.99986146360473316, 1.00000000000000022, 0.99905052020024310,
		0.43202712322170089, 0.83917543899780545, -0.84210005596350634,
		-0.96741055070299309, 0.38980770530806869, 0.94884018282016180,
		0.18898493355529175, -0.99818853872682889, 0.99905052020024310, 0.99999999999999933}

	data := matrix.FloatNew(10, 10, dataref)

	sol, err := mcsdp(data)
	if sol != nil && sol.Status == Optimal {
		fail := false
		x := sol.Result.At("x")[0]
		z := sol.Result.At("z")[0]
		//matrix.Reshape(z, data.Rows(), data.Rows())
		t.Logf("x=\n%v\n", x.ToString("%.9f"))
		xe, _ := nrmError(matrix.FloatVector(xref), x)
		if xe > TOL {
			t.Logf("x differs [%.3e] from exepted too much.", xe)
			fail = true
		}
		ze, _ := nrmError(matrix.FloatVector(zref), z)
		if ze > TOL {
			t.Logf("z differs [%.3e] from exepted too much.", ze)
			fail = true
		}
		if fail {
			t.Fail()
		}
	} else {
		t.Logf("status: %v\n", err)
		t.Fail()
	}
}