Exemple #1
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.FloatMatrixFromTable(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 := matrix.Scale(S, mu)
	pbarNeg := matrix.Scale(pbar, -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)
	}
}
Exemple #2
0
func main() {
	m := 6
	Vdata := [][]float64{
		[]float64{1.0, -1.0, -2.0, -2.0, 0.0, 1.5, 1.0},
		[]float64{1.0, 2.0, 1.0, -1.0, -2.0, -1.0, 1.0}}

	V := matrix.FloatMatrixFromTable(Vdata, matrix.RowOrder)

	// V[1, :m] - V[1,1:]
	a0 := matrix.Minus(V.GetSubMatrix(1, 0, 1, m), V.GetSubMatrix(1, 1, 1))
	// V[0, :m] - V[0,1:]
	a1 := matrix.Minus(V.GetSubMatrix(0, 0, 1, m), V.GetSubMatrix(0, 1, 1))
	A0, _ := matrix.FloatMatrixStacked(matrix.StackDown, a0.Scale(-1.0), a1)
	A0 = A0.Transpose()
	b0 := matrix.Mul(A0, V.GetSubMatrix(0, 0, 2, m).Transpose())
	b0 = matrix.Times(b0, matrix.FloatWithValue(2, 1, 1.0))

	A := make([]*matrix.FloatMatrix, 0)
	b := make([]*matrix.FloatMatrix, 0)
	A = append(A, A0)
	b = append(b, b0)

	// List of symbols
	C := make([]*matrix.FloatMatrix, 0)
	C = append(C, matrix.FloatZeros(2, 1))
	var row *matrix.FloatMatrix = nil
	for k := 0; k < m; k++ {
		row = A0.GetRow(k, row)
		nrm := blas.Nrm2Float(row)
		row.Scale(2.0 * b0.GetIndex(k) / (nrm * nrm))
		C = append(C, row.Transpose())
	}

	// Voronoi set around C[1]
	A1 := matrix.FloatZeros(3, 2)
	A1.SetSubMatrix(0, 0, A0.GetSubMatrix(0, 0, 1).Scale(-1.0))
	A1.SetSubMatrix(1, 0, matrix.Minus(C[m], C[1]).Transpose())
	A1.SetSubMatrix(2, 0, matrix.Minus(C[2], C[1]).Transpose())
	b1 := matrix.FloatZeros(3, 1)
	b1.SetIndex(0, -b0.GetIndex(0))
	v := matrix.Times(A1.GetRow(1, nil), matrix.Plus(C[m], C[1])).Float() * 0.5
	b1.SetIndex(1, v)
	v = matrix.Times(A1.GetRow(2, nil), matrix.Plus(C[2], C[1])).Float() * 0.5
	b1.SetIndex(2, v)
	A = append(A, A1)
	b = append(b, b1)

	// Voronoi set around C[2] ... C[5]
	for k := 2; k < 6; k++ {
		A1 = matrix.FloatZeros(3, 2)
		A1.SetSubMatrix(0, 0, A0.GetSubMatrix(k-1, 0, 1).Scale(-1.0))
		A1.SetSubMatrix(1, 0, matrix.Minus(C[k-1], C[k]).Transpose())
		A1.SetSubMatrix(2, 0, matrix.Minus(C[k+1], C[k]).Transpose())
		b1 = matrix.FloatZeros(3, 1)
		b1.SetIndex(0, -b0.GetIndex(k-1))
		v := matrix.Times(A1.GetRow(1, nil), matrix.Plus(C[k-1], C[k])).Float() * 0.5
		b1.SetIndex(1, v)
		v = matrix.Times(A1.GetRow(2, nil), matrix.Plus(C[k+1], C[k])).Float() * 0.5
		b1.SetIndex(2, v)
		A = append(A, A1)
		b = append(b, b1)
	}

	// Voronoi set around C[6]
	A1 = matrix.FloatZeros(3, 2)
	A1.SetSubMatrix(0, 0, A0.GetSubMatrix(5, 0, 1).Scale(-1.0))
	A1.SetSubMatrix(1, 0, matrix.Minus(C[1], C[6]).Transpose())
	A1.SetSubMatrix(2, 0, matrix.Minus(C[5], C[6]).Transpose())
	b1 = matrix.FloatZeros(3, 1)
	b1.SetIndex(0, -b0.GetIndex(5))
	v = matrix.Times(A1.GetRow(1, nil), matrix.Plus(C[1], C[6])).Float() * 0.5
	b1.SetIndex(1, v)
	v = matrix.Times(A1.GetRow(2, nil), matrix.Plus(C[5], C[6])).Float() * 0.5
	b1.SetIndex(2, v)

	A = append(A, A1)
	b = append(b, b1)

	P := matrix.FloatIdentity(2)
	q := matrix.FloatZeros(2, 1)
	solopts := &cvx.SolverOptions{ShowProgress: false, MaxIter: 30}
	ovals := make([]float64, 0)
	for k := 1; k < 7; k++ {
		sol, err := cvx.Qp(P, q, A[k], b[k], nil, nil, solopts, nil)
		_ = err
		x := sol.Result.At("x")[0]
		ovals = append(ovals, math.Pow(blas.Nrm2Float(x), 2.0))
	}

	optvals := matrix.FloatVector(ovals)
	//fmt.Printf("optvals=\n%v\n", optvals)

	rangeFunc := func(n int) []float64 {
		r := make([]float64, 0)
		for i := 0; i < n; i++ {
			r = append(r, float64(i))
		}
		return r
	}

	nopts := 200
	sigmas := matrix.FloatVector(rangeFunc(nopts))
	sigmas.Scale((0.5 - 0.2) / float64(nopts)).Add(0.2)

	bndsVal := func(sigma float64) float64 {
		// 1.0 - sum(exp( -optvals/(2*sigma**2)))
		return 1.0 - matrix.Exp(matrix.Scale(optvals, -1.0/(2*sigma*sigma))).Sum()
	}
	bnds := matrix.FloatZeros(sigmas.NumElements(), 1)
	for j, v := range sigmas.FloatArray() {
		bnds.SetIndex(j, bndsVal(v))
	}
	plotData("plot.png", sigmas.FloatArray(), bnds.FloatArray())
}
Exemple #3
0
func main() {
	flag.Parse()

	m := len(udata)
	nvars := 2 * m
	u := matrix.FloatVector(udata[:m])
	y := matrix.FloatVector(ydata[:m])

	// minimize    (1/2) * || yhat - y ||_2^2
	// subject to  yhat[j] >= yhat[i] + g[i]' * (u[j] - u[i]), j, i = 0,...,m-1
	//
	// Variables  yhat (m), g (m).

	P := matrix.FloatZeros(nvars, nvars)
	// set m first diagonal indexes to 1.0
	//P.SetIndexes(1.0, matrix.DiagonalIndexes(P)[:m]...)
	P.Diag().SubMatrix(0, 0, 1, m).SetIndexes(1.0)
	q := matrix.FloatZeros(nvars, 1)
	q.SubMatrix(0, 0, y.NumElements(), 1).Plus(matrix.Scale(y, -1.0))

	// m blocks (i = 0,...,m-1) of linear inequalities
	//
	//     yhat[i] + g[i]' * (u[j] - u[i]) <= yhat[j], j = 0,...,m-1.

	G := matrix.FloatZeros(m*m, nvars)
	I := matrix.FloatDiagonal(m, 1.0)

	for i := 0; i < m; i++ {
		// coefficients of yhat[i] (column i)
		//G.Set(1.0, matrix.ColumnIndexes(G, i)[i*m:(i+1)*m]...)
		column(G, i).SetIndexes(1.0)

		// coefficients of gi[i] (column i, rows i*m ... (i+1)*m)
		//rows := matrix.Indexes(i*m, (i+1)*m)
		//G.SetAtColumnArray(m+i, rows, matrix.Add(u, -u.GetIndex(i)).FloatArray())

		// coefficients of gi[i] (column i, rows i*m ... (i+1)*m)
		// from column m+i staring at row i*m select m rows and one column
		G.SubMatrix(i*m, m+i, m, 1).Plus(matrix.Add(u, -u.GetIndex(i)))

		// coeffients of yhat[i]) from rows i*m ... (i+1)*m, cols 0 ... m
		//G.SetSubMatrix(i*m, 0, matrix.Minus(G.GetSubMatrix(i*m, 0, m, m), I))
		G.SubMatrix(i*m, 0, m, m).Minus(I)
	}

	h := matrix.FloatZeros(m*m, 1)
	var A, b *matrix.FloatMatrix = nil, nil
	var solopts cvx.SolverOptions
	solopts.ShowProgress = true
	solopts.KKTSolverName = solver

	sol, err := cvx.Qp(P, q, G, h, A, b, &solopts, nil)
	if err != nil {
		fmt.Printf("error: %v\n", err)
		return
	}
	if sol != nil && sol.Status != cvx.Optimal {
		fmt.Printf("status not optimal\n")
		return
	}
	x := sol.Result.At("x")[0]
	//yhat := matrix.FloatVector(x.FloatArray()[:m])
	//g := matrix.FloatVector(x.FloatArray()[m:])
	yhat := x.SubMatrix(0, 0, m, 1).Copy()
	g := x.SubMatrix(m, 0).Copy()

	rangeFunc := func(n int) []float64 {
		r := make([]float64, 0)
		for i := 0; i < n; i++ {
			r = append(r, float64(i)*2.2/float64(n))
		}
		return r
	}
	ts := rangeFunc(1000)
	fitFunc := func(points []float64) []float64 {
		res := make([]float64, len(points))
		for k, t := range points {
			res[k] = matrix.Plus(yhat, matrix.Mul(g, matrix.Scale(u, -1.0).Add(t))).Max()
		}
		return res
	}
	fs := fitFunc(ts)
	plotData("cvxfit.png", u.FloatArray(), y.FloatArray(), ts, fs)
}