示例#1
0
func main() {
	blas.PanicOnError(true)
	matrix.PanicOnError(true)

	var data *matrix.FloatMatrix = nil
	flag.Parse()
	if len(dataVal) > 0 {
		data, _ = matrix.FloatParse(dataVal)
		if data == nil {
			fmt.Printf("could not parse:\n%s\n", dataVal)
			return
		}
	} else {
		data = matrix.FloatNormal(20, 20)
	}

	if len(spPath) > 0 {
		checkpnt.Reset(spPath)
		checkpnt.Activate()
		checkpnt.Verbose(spVerbose)
		checkpnt.Format("%.7f")
	}

	sol, err := mcsdp(data)
	if sol != nil && sol.Status == cvx.Optimal {
		x := sol.Result.At("x")[0]
		z := sol.Result.At("z")[0]
		matrix.Reshape(z, data.Rows(), data.Rows())
		fmt.Printf("x=\n%v\n", x.ToString("%.9f"))
		//fmt.Printf("z=\n%v\n", z.ToString("%.9f"))
		check(x, z)
	} else {
		fmt.Printf("status: %v\n", err)
	}
}
示例#2
0
func main() {
	flag.Parse()
	if len(spPath) > 0 {
		checkpnt.Reset(spPath)
		checkpnt.Activate()
		checkpnt.Verbose(spVerbose)
		checkpnt.Format("%.17f")
	}

	adata := [][]float64{
		[]float64{0.3, -0.4, -0.2, -0.4, 1.3},
		[]float64{0.6, 1.2, -1.7, 0.3, -0.3},
		[]float64{-0.3, 0.0, 0.6, -1.2, -2.0}}

	A := matrix.FloatMatrixFromTable(adata, matrix.ColumnOrder)
	b := matrix.FloatVector([]float64{1.5, 0.0, -1.2, -0.7, 0.0})

	_, n := A.Size()
	N := n + 1 + n

	h := matrix.FloatZeros(N, 1)
	h.SetIndex(n, 1.0)

	I0 := matrix.FloatDiagonal(n, -1.0)
	I1 := matrix.FloatIdentity(n)
	G, _ := matrix.FloatMatrixStacked(matrix.StackDown, I0, matrix.FloatZeros(1, n), I1)

	At := A.Transpose()
	P := At.Times(A)
	q := At.Times(b).Scale(-1.0)

	dims := sets.NewDimensionSet("l", "q", "s")
	dims.Set("l", []int{n})
	dims.Set("q", []int{n + 1})

	var solopts cvx.SolverOptions
	solopts.MaxIter = 20
	solopts.ShowProgress = true
	if maxIter > 0 {
		solopts.MaxIter = maxIter
	}
	if len(solver) > 0 {
		solopts.KKTSolverName = solver
	}
	sol, err := cvx.ConeQp(P, q, G, h, nil, nil, dims, &solopts, nil)
	if err == nil {
		x := sol.Result.At("x")[0]
		s := sol.Result.At("s")[0]
		z := sol.Result.At("z")[0]
		fmt.Printf("Optimal\n")
		fmt.Printf("x=\n%v\n", x.ToString("%.9f"))
		fmt.Printf("s=\n%v\n", s.ToString("%.9f"))
		fmt.Printf("z=\n%v\n", z.ToString("%.9f"))
		check(x, s, z)
	}

}
示例#3
0
文件: testgp.go 项目: hrautila/go.opt
func main() {
	flag.Parse()

	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 cvx.SolverOptions
	solopts.MaxIter = 40
	if maxIter > 0 {
		solopts.MaxIter = maxIter
	}
	if len(spPath) > 0 {
		checkpnt.Reset(spPath)
		checkpnt.Activate()
		checkpnt.Verbose(spVerbose)
		checkpnt.Format("%.7f")
	}
	solopts.ShowProgress = true
	if maxIter > 0 {
		solopts.MaxIter = maxIter
	}
	if len(solver) > 0 {
		solopts.KKTSolverName = solver
	}
	sol, err := cvx.Gp(K, F, g, nil, nil, nil, nil, &solopts)
	if sol != nil && sol.Status == cvx.Optimal {
		x := sol.Result.At("x")[0]
		r := matrix.Exp(x)
		h := r.GetIndex(0)
		w := r.GetIndex(1)
		d := r.GetIndex(2)
		fmt.Printf("x=\n%v\n", x.ToString("%.9f"))
		fmt.Printf("\n h = %f,  w = %f, d = %f.\n", h, w, d)
		check(x)
	} else {
		fmt.Printf("status: %v\n", err)
	}
}
示例#4
0
func main() {

	flag.Parse()
	if len(spPath) > 0 {
		checkpnt.Reset(spPath)
		checkpnt.Activate()
		checkpnt.Verbose(spVerbose)
		checkpnt.Format("%.17f")
	}

	gdata := [][]float64{
		[]float64{16., 7., 24., -8., 8., -1., 0., -1., 0., 0., 7.,
			-5., 1., -5., 1., -7., 1., -7., -4.},
		[]float64{-14., 2., 7., -13., -18., 3., 0., 0., -1., 0., 3.,
			13., -6., 13., 12., -10., -6., -10., -28.},
		[]float64{5., 0., -15., 12., -6., 17., 0., 0., 0., -1., 9.,
			6., -6., 6., -7., -7., -6., -7., -11.}}

	hdata := []float64{-3., 5., 12., -2., -14., -13., 10., 0., 0., 0., 68.,
		-30., -19., -30., 99., 23., -19., 23., 10.}

	c := matrix.FloatVector([]float64{-6., -4., -5.})
	G := matrix.FloatMatrixFromTable(gdata)
	h := matrix.FloatVector(hdata)

	dims := sets.NewDimensionSet("l", "q", "s")
	dims.Set("l", []int{2})
	dims.Set("q", []int{4, 4})
	dims.Set("s", []int{3})

	var solopts cvx.SolverOptions
	solopts.MaxIter = 30
	solopts.ShowProgress = true
	if maxIter > 0 {
		solopts.MaxIter = maxIter
	}
	if len(solver) > 0 {
		solopts.KKTSolverName = solver
	}
	sol, err := cvx.ConeLp(c, G, h, nil, nil, dims, &solopts, nil, nil)
	if err == nil {
		x := sol.Result.At("x")[0]
		s := sol.Result.At("s")[0]
		z := sol.Result.At("z")[0]
		fmt.Printf("Optimal\n")
		fmt.Printf("x=\n%v\n", x.ToString("%.9f"))
		fmt.Printf("s=\n%v\n", s.ToString("%.9f"))
		fmt.Printf("z=\n%v\n", z.ToString("%.9f"))
		check(x, s, z)
	} else {
		fmt.Printf("status: %s\n", err)
	}
}
示例#5
0
文件: testcp.go 项目: hrautila/go.opt
func main() {
	flag.Parse()
	if len(spPath) > 0 {
		checkpnt.Reset(spPath)
		checkpnt.Activate()
		checkpnt.Verbose(spVerbose)
		checkpnt.Format("%.7f")
	}

	x := acenter()
	if x != nil {
		fmt.Printf("x = \n%v\n", x.ToString("%.5f"))
		check(x)
	}
}
示例#6
0
func main() {

	var A, b *matrix.FloatMatrix = nil, nil
	m, n := 20, 20

	blas.PanicOnError(true)
	matrix.PanicOnError(true)

	flag.Parse()
	if len(spPath) > 0 {
		checkpnt.Reset(spPath)
		checkpnt.Activate()
		checkpnt.Verbose(spVerbose)
		checkpnt.Format("%.17f")
	}
	if len(AVal) > 0 {
		A, _ = matrix.FloatParse(AVal)
		if A == nil {
			fmt.Printf("could not parse:\n%s\n", AVal)
			return
		}
	} else {
		A = matrix.FloatNormal(m, n)
	}
	if len(bVal) > 0 {
		b, _ = matrix.FloatParse(bVal)
		if b == nil {
			fmt.Printf("could not parse:\n%s\n", bVal)
			return
		}
	} else {
		b = matrix.FloatNormal(m, 1)
	}

	sol, err := qcl1(A, b)
	if sol != nil {
		r := sol.Result.At("x")[0]
		x := matrix.FloatVector(r.FloatArray()[:A.Cols()])
		r = sol.Result.At("z")[0]
		z := matrix.FloatVector(r.FloatArray()[r.NumElements()-A.Rows():])
		fmt.Printf("x=\n%v\n", x.ToString("%.9f"))
		fmt.Printf("z=\n%v\n", z.ToString("%.9f"))
		check(x, z)
	} else {
		fmt.Printf("status: %v\n", err)
	}
}
示例#7
0
func main() {
	flag.Parse()
	if len(spPath) > 0 {
		checkpnt.Reset(spPath)
		checkpnt.Activate()
		checkpnt.Verbose(spVerbose)
		checkpnt.Format("%.17f")
	}

	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})

	fmt.Printf("A=\n%v\n", A)
	fmt.Printf("b=\n%v\n", b)
	fmt.Printf("G=\n%v\n", G)
	fmt.Printf("h=\n%v\n", h)
	fmt.Printf("c=\n%v\n", c)

	var solopts cvx.SolverOptions
	solopts.MaxIter = 30
	solopts.ShowProgress = true
	if maxIter > -1 {
		solopts.MaxIter = maxIter
	}
	if len(solver) > 0 {
		solopts.KKTSolverName = solver
	}
	sol, err := cvx.Lp(c, G, h, A, b, &solopts, nil, nil)
	if sol != nil && sol.Status == cvx.Optimal {
		x := sol.Result.At("x")[0]
		s := sol.Result.At("s")[0]
		z := sol.Result.At("z")[0]
		fmt.Printf("x=\n%v\n", x.ToString("%.9f"))
		fmt.Printf("s=\n%v\n", s.ToString("%.9f"))
		fmt.Printf("z=\n%v\n", z.ToString("%.9f"))
		check(x, s, z)
	} else {
		fmt.Printf("status: %v\n", err)
	}
}
示例#8
0
文件: testlp.go 项目: hrautila/go.opt
func main() {
	flag.Parse()
	if len(spPath) > 0 {
		checkpnt.Reset(spPath)
		checkpnt.Activate()
		checkpnt.Verbose(spVerbose)
		checkpnt.Format("%.17f")
	}

	gdata := [][]float64{
		[]float64{2.0, 1.0, -1.0, 0.0},
		[]float64{1.0, 2.0, 0.0, -1.0}}

	c := matrix.FloatVector([]float64{-4.0, -5.0})
	G := matrix.FloatMatrixFromTable(gdata, matrix.ColumnOrder)
	h := matrix.FloatVector([]float64{3.0, 3.0, 0.0, 0.0})

	var solopts cvx.SolverOptions
	solopts.MaxIter = 30
	solopts.ShowProgress = true
	if maxIter > -1 {
		solopts.MaxIter = maxIter
	}
	if len(solver) > 0 {
		solopts.KKTSolverName = solver
	}
	sol, err := cvx.Lp(c, G, h, nil, nil, &solopts, nil, nil)
	if sol != nil && sol.Status == cvx.Optimal {
		x := sol.Result.At("x")[0]
		s := sol.Result.At("s")[0]
		z := sol.Result.At("z")[0]
		fmt.Printf("x=\n%v\n", x.ToString("%.9f"))
		fmt.Printf("s=\n%v\n", s.ToString("%.9f"))
		fmt.Printf("z=\n%v\n", z.ToString("%.9f"))
		check(x, s, z)
	} else {
		fmt.Printf("status: %v\n", err)
	}
}
示例#9
0
func main() {
	flag.Parse()
	if len(spPath) > 0 {
		checkpnt.Reset(spPath)
		checkpnt.Activate()
		checkpnt.Verbose(spVerbose)
		checkpnt.Format("%.17f")
	}

	gdata0 := [][]float64{
		[]float64{-7., -11., -11., 3.},
		[]float64{7., -18., -18., 8.},
		[]float64{-2., -8., -8., 1.}}

	gdata1 := [][]float64{
		[]float64{-21., -11., 0., -11., 10., 8., 0., 8., 5.},
		[]float64{0., 10., 16., 10., -10., -10., 16., -10., 3.},
		[]float64{-5., 2., -17., 2., -6., 8., -17., -7., 6.}}

	hdata0 := [][]float64{
		[]float64{33., -9.},
		[]float64{-9., 26.}}

	hdata1 := [][]float64{
		[]float64{14., 9., 40.},
		[]float64{9., 91., 10.},
		[]float64{40., 10., 15.}}

	g0 := matrix.FloatMatrixFromTable(gdata0, matrix.ColumnOrder)
	g1 := matrix.FloatMatrixFromTable(gdata1, matrix.ColumnOrder)
	Ghs := sets.FloatSetNew("Gs", "hs")
	Ghs.Append("Gs", g0, g1)

	h0 := matrix.FloatMatrixFromTable(hdata0, matrix.ColumnOrder)
	h1 := matrix.FloatMatrixFromTable(hdata1, matrix.ColumnOrder)
	Ghs.Append("hs", h0, h1)

	c := matrix.FloatVector([]float64{1.0, -1.0, 1.0})

	var Gs, hs, A, b *matrix.FloatMatrix = nil, nil, nil, nil
	var solopts cvx.SolverOptions
	solopts.MaxIter = 30
	solopts.ShowProgress = true
	if maxIter > -1 {
		solopts.MaxIter = maxIter
	}
	if len(solver) > 0 {
		solopts.KKTSolverName = solver
	}

	sol, err := cvx.Sdp(c, Gs, hs, A, b, Ghs, &solopts, nil, nil)
	if sol != nil && sol.Status == cvx.Optimal {
		x := sol.Result.At("x")[0]
		fmt.Printf("x=\n%v\n", x.ToString("%.9f"))
		for i, m := range sol.Result.At("zs") {
			fmt.Printf("zs[%d]=\n%v\n", i, m.ToString("%.9f"))
		}
		ss0 := sol.Result.At("ss")[0]
		ss1 := sol.Result.At("ss")[1]
		zs0 := sol.Result.At("zs")[0]
		zs1 := sol.Result.At("zs")[1]
		check(x, ss0, ss1, zs0, zs1)
	} else {
		fmt.Printf("status: %v\n", err)
	}
	checkpnt.Report()
}