Пример #1
0
func main() {
	flag.Parse()
	reftest := flag.NFlag() > 0

	gdata0 := [][]float64{
		[]float64{12., 13., 12.},
		[]float64{6., -3., -12.},
		[]float64{-5., -5., 6.}}

	gdata1 := [][]float64{
		[]float64{3., 3., -1., 1.},
		[]float64{-6., -6., -9., 19.},
		[]float64{10., -2., -2., -3.}}

	c := matrix.FloatVector([]float64{-2.0, 1.0, 5.0})
	g0 := matrix.FloatMatrixStacked(gdata0, matrix.ColumnOrder)
	g1 := matrix.FloatMatrixStacked(gdata1, matrix.ColumnOrder)
	Ghq := cvx.FloatSetNew("Gq", "hq")
	Ghq.Append("Gq", g0, g1)

	h0 := matrix.FloatVector([]float64{-12.0, -3.0, -2.0})
	h1 := matrix.FloatVector([]float64{27.0, 0.0, 3.0, -42.0})
	Ghq.Append("hq", h0, h1)

	var Gl, hl, A, b *matrix.FloatMatrix = nil, nil, nil, nil
	var solopts cvx.SolverOptions
	solopts.MaxIter = 30
	solopts.ShowProgress = true
	sol, err := cvx.Socp(c, Gl, hl, A, b, Ghq, &solopts, nil, nil)
	fmt.Printf("status: %v\n", err)
	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("sq") {
			fmt.Printf("sq[%d]=\n%v\n", i, m.ToString("%.9f"))
		}
		for i, m := range sol.Result.At("zq") {
			fmt.Printf("zq[%d]=\n%v\n", i, m.ToString("%.9f"))
		}
		if reftest {
			sq0 := sol.Result.At("sq")[0]
			sq1 := sol.Result.At("sq")[1]
			zq0 := sol.Result.At("zq")[0]
			zq1 := sol.Result.At("zq")[1]
			check(x, sq0, sq1, zq0, zq1)
		}
	}
}
Пример #2
0
func main() {

	flag.Parse()
	reftest := flag.NFlag() > 0

	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.FloatMatrixStacked(gdata)
	h := matrix.FloatVector(hdata)

	dims := cvx.DSetNew("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
	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"))
		if reftest {
			check(x, s, z)
		}
	} else {
		fmt.Printf("status: %s\n", err)
	}
}
Пример #3
0
func main() {

	Sdata := [][]float64{
		[]float64{ 4e-2,  6e-3, -4e-3,   0.0 },
        []float64{ 6e-3,  1e-2,  0.0,    0.0 },
        []float64{-4e-3,  0.0,   2.5e-3, 0.0 },
        []float64{ 0.0,   0.0,   0.0,    0.0 }}

	pbar := matrix.FloatVector([]float64{.12, .10, .07, .03})
	S := matrix.FloatMatrixStacked(Sdata)
	n := pbar.Rows()
	G := matrix.FloatDiagonal(n, -1.0)
	h := matrix.FloatZeros(n, 1)
	A := matrix.FloatWithValue(1, n, 1.0)
	b := matrix.FloatNew(1,1, []float64{1.0})

	var solopts cvx.SolverOptions
	solopts.MaxIter = 30
	solopts.ShowProgress = true

	mu := 1.0
	Smu := S.Copy().Scale(mu)
	pbarNeg := pbar.Copy().Scale(-1.0)
	fmt.Printf("Smu=\n%v\n", Smu.String())
	fmt.Printf("-pbar=\n%v\n", pbarNeg.String())

	sol, err := cvx.Qp(Smu, pbarNeg, G, h, A, b, &solopts, nil)

	fmt.Printf("status: %v\n", err)
	if sol != nil && sol.Status == cvx.Optimal {
		x := sol.Result.At("x")[0]
		ret := blas.DotFloat(x, pbar)
		risk := math.Sqrt(blas.DotFloat(x, S.Times(x)))
		fmt.Printf("ret=%.3f, risk=%.3f\n", ret, risk)
		fmt.Printf("x=\n%v\n", x)
	}
}
Пример #4
0
func main() {
	flag.Parse()
	reftest := flag.NFlag() > 0

	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.FloatMatrixStacked(gdata0, matrix.ColumnOrder)
	g1 := matrix.FloatMatrixStacked(gdata1, matrix.ColumnOrder)
	Ghs := cvx.FloatSetNew("Gs", "hs")
	Ghs.Append("Gs", g0, g1)

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

	c := matrix.FloatVector([]float64{1.0, -1.0, 1.0})
	Ghs.Print()
	fmt.Printf("calling...\n")
	// nil variables
	var Gs, hs, A, b *matrix.FloatMatrix = nil, nil, nil, nil

	var solopts cvx.SolverOptions
	solopts.MaxIter = 30
	solopts.ShowProgress = true
	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("ss") {
			fmt.Printf("ss[%d]=\n%v\n", i, m.ToString("%.9f"))
		}
		for i, m := range sol.Result.At("zs") {
			fmt.Printf("zs[%d]=\n%v\n", i, m.ToString("%.9f"))
		}
		if reftest {
			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)
	}
}
Пример #5
0
func floorplan(Amin *matrix.FloatMatrix) *matrix.FloatMatrix {
	rho := 1.0
	gamma := 5.0

	c := matrix.FloatZeros(22, 1)
	c.SetAtColumnArray(0, []int{0, 1}, []float64{1.0, 1.0})

	G := matrix.FloatZeros(26, 22)
	h := matrix.FloatZeros(26, 1)

	// -x1 <= 0
	G.SetAt(0, 2, -1.0)

	// -x2 <= 0
	G.SetAt(1, 3, -1.0)

	// -x4 <= 0
	G.SetAt(2, 5, -1.0)

	// x1 - x3 + w1 <= -rho
	G.SetAtRowArray(3, []int{2, 4, 12}, []float64{1.0, -1.0, 1.0})
	h.SetAt(3, 0, -rho)

	// x2 - x3 + w2 <= -rho
	G.SetAtRowArray(4, []int{3, 4, 13}, []float64{1.0, -1.0, 1.0})
	h.SetAt(4, 0, -rho)

	// x3 - x5 + w3 <= -rho
	G.SetAtRowArray(5, []int{4, 6, 14}, []float64{1.0, -1.0, 1.0})
	h.SetAt(5, 0, -rho)

	// x4 - x5 + w4 <= -rho
	G.SetAtRowArray(6, []int{5, 6, 15}, []float64{1.0, -1.0, 1.0})
	h.SetAt(6, 0, -rho)

	// -W + x5 + w5 <= 0
	G.SetAtRowArray(7, []int{0, 6, 16}, []float64{-1.0, 1.0, 1.0})

	// -y2 <= 0
	G.SetAt(8, 8, -1.0)

	// -y3 <= 0
	G.SetAt(9, 9, -1.0)

	// -y5 <= 0
	G.SetAt(10, 11, -1.0)

	// -y1 + y2 + h2 <= -rho
	G.SetAtRowArray(11, []int{7, 8, 18}, []float64{-1.0, 1.0, 1.0})
	h.SetAt(11, 0, -rho)

	// y1 - y4 + h1 <= -rho
	G.SetAtRowArray(12, []int{7, 10, 17}, []float64{1.0, -1.0, 1.0})
	h.SetAt(12, 0, -rho)

	// y3 - y4 + h3 <= -rho
	G.SetAtRowArray(13, []int{9, 10, 19}, []float64{1.0, -1.0, 1.0})
	h.SetAt(13, 0, -rho)

	// -H + y4 + h4 <= 0
	G.SetAtRowArray(14, []int{1, 10, 20}, []float64{-1.0, 1.0, 1.0})

	// -H + y5 + h5 <= 0
	G.SetAtRowArray(15, []int{1, 11, 21}, []float64{-1.0, 1.0, 1.0})

	// -w1 + h1/gamma <= 0
	G.SetAtRowArray(16, []int{12, 17}, []float64{-1.0, 1.0 / gamma})

	// w1 - gamma * h1 <= 0
	G.SetAtRowArray(17, []int{12, 17}, []float64{1.0, -gamma})

	// -w2 + h2/gamma <= 0
	G.SetAtRowArray(18, []int{13, 18}, []float64{-1.0, 1.0 / gamma})

	//  w2 - gamma * h2 <= 0
	G.SetAtRowArray(19, []int{13, 18}, []float64{1.0, -gamma})

	// -w3 + h3/gamma <= 0
	G.SetAtRowArray(20, []int{14, 18}, []float64{-1.0, 1.0 / gamma})

	//  w3 - gamma * h3 <= 0
	G.SetAtRowArray(21, []int{14, 19}, []float64{1.0, -gamma})

	// -w4  + h4/gamma <= 0
	G.SetAtRowArray(22, []int{15, 19}, []float64{-1.0, 1.0 / gamma})

	//  w4 - gamma * h4 <= 0
	G.SetAtRowArray(23, []int{15, 20}, []float64{1.0, -gamma})

	// -w5 + h5/gamma <= 0
	G.SetAtRowArray(24, []int{16, 21}, []float64{-1.0, 1.0 / gamma})

	//  w5 - gamma * h5 <= 0.0
	G.SetAtRowArray(25, []int{16, 21}, []float64{1.0, -gamma})

	F := NewFloorPlan(Amin)

	var dims *cvx.DimensionSet = nil
	var solopts cvx.SolverOptions
	solopts.MaxIter = 50
	solopts.ShowProgress = true

	sol, err := cvx.Cpl(F, c, G, h, nil, nil, dims, &solopts)
	if err == nil && sol.Status == cvx.Optimal {
		return sol.Result.At("x")[0]
	} else {
		fmt.Printf("result: %v\n", err)
	}
	return nil
}