func MakeData(M, N, P int, randomData, diagonal bool) (A, B, C *matrix.FloatMatrix) { if diagonal && M != N { diagonal = false fmt.Printf("cannot make diagonal if B.rows != B.cols\n") } if randomData { A = matrix.FloatNormal(M, P) if diagonal { d := matrix.FloatNormal(P, 1) B := matrix.FloatDiagonal(P, 0.0) B.SetIndexesFromArray(d.FloatArray(), matrix.DiagonalIndexes(B)...) } else { B = matrix.FloatNormal(P, N) } } else { A = matrix.FloatWithValue(M, P, 1.0) if diagonal { B = matrix.FloatDiagonal(P, 1.0) } else { B = matrix.FloatWithValue(P, N, 1.0) } } C = matrix.FloatZeros(M, N) return }
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) } }
func (p *acenterProg) F2(x, z *matrix.FloatMatrix) (f, Df, H *matrix.FloatMatrix, err error) { f, Df, err = p.F1(x) u := matrix.Pow(x, 2.0).Scale(-1.0).Add(1.0) z0 := z.GetIndex(0) u2 := matrix.Pow(u, 2.0) hd := matrix.Div(matrix.Add(u2, 1.0), u2).Scale(2 * z0) H = matrix.FloatDiagonal(hd.NumElements(), hd.FloatArray()...) return }
func (p *floorPlan) F2(x, z *matrix.FloatMatrix) (f, Df, H *matrix.FloatMatrix, err error) { f, Df, err = p.F1(x) x17 := matrix.FloatVector(x.FloatArray()[17:]) tmp := matrix.Div(p.Amin, matrix.Pow(x17, 3.0)) tmp = matrix.Mul(z, tmp).Scale(2.0) diag := matrix.FloatDiagonal(5, tmp.FloatArray()...) H = matrix.FloatZeros(22, 22) H.SetSubMatrix(17, 17, diag) return }
func (p *floorPlan) F1(x *matrix.FloatMatrix) (f, Df *matrix.FloatMatrix, err error) { err = nil mn := x.Min(-1, -2, -3, -4, -5) if mn <= 0.0 { f, Df = nil, nil return } zeros := matrix.FloatZeros(5, 12) dk1 := matrix.FloatDiagonal(5, -1.0) dk2 := matrix.FloatZeros(5, 5) x17 := matrix.FloatVector(x.FloatArray()[17:]) // -( Amin ./ (x17 .* x17) ) diag := matrix.Div(p.Amin, matrix.Mul(x17, x17)).Scale(-1.0) dk2.SetIndexesFromArray(diag.FloatArray(), matrix.MakeDiagonalSet(5)...) Df, _ = matrix.FloatMatrixStacked(matrix.StackRight, zeros, dk1, dk2) x12 := matrix.FloatVector(x.FloatArray()[12:17]) // f = -x[12:17] + div(Amin, x[17:]) == div(Amin, x[17:]) - x[12:17] f = matrix.Minus(matrix.Div(p.Amin, x17), x12) return }
func TestInvLower(t *testing.T) { N := 36 nb := 12 A := matrix.FloatUniformSymmetric(N, matrix.Lower) I := matrix.FloatDiagonal(N, 1.0) I0 := matrix.FloatZeros(N, N) A0 := A.Copy() // A0 = A.-1 InverseTrm(A0, LOWER, 0) Mult(I0, A, A0, 1.0, 0.0, NOTRANS) I0.Minus(I) nrm := NormP(I0, NORM_ONE) t.Logf("unblk: ||I - A*A.-1||_1 : %e\n", nrm) A0 = A.Copy() // A0 = A.-1 InverseTrm(A0, LOWER, nb) Mult(I0, A, A0, 1.0, 0.0, NOTRANS) I0.Minus(I) nrm = NormP(I0, NORM_ONE) t.Logf("blk: ||I - A*A.-1||_1 : %e\n", nrm) }
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) } }
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) }
func TestConeQp(t *testing.T) { 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}} // reference values from cvxopt coneqp.py xref := []float64{0.72558318685981904, 0.61806264311119252, 0.30253527966423444} sref := []float64{ 0.72558318685981904, 0.61806264311119263, 0.30253527966423449, 1.00000000000041678, -0.72558318686012169, -0.61806264311145032, -0.30253527966436067} zref := []float64{ 0.00000003332583626, 0.00000005116586239, 0.00000009993673262, 0.56869648433154019, 0.41264857754144563, 0.35149286573190930, 0.17201618570052318} 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 := matrix.Times(At, A) q := matrix.Times(At, b).Scale(-1.0) dims := sets.DSetNew("l", "q", "s") dims.Set("l", []int{n}) dims.Set("q", []int{n + 1}) var solopts SolverOptions solopts.MaxIter = 10 solopts.ShowProgress = false sol, err := ConeQp(P, q, G, h, nil, nil, dims, &solopts, nil) if err == nil { fail := false x := sol.Result.At("x")[0] s := sol.Result.At("s")[0] z := sol.Result.At("z")[0] t.Logf("Optimal\n") 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")) xe, _ := nrmError(matrix.FloatVector(xref), x) if xe > TOL { t.Logf("x differs [%.3e] from exepted too much.", xe) fail = true } se, _ := nrmError(matrix.FloatVector(sref), s) if se > TOL { t.Logf("s differs [%.3e] from exepted too much.", se) 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() } } }