本文整理汇总了Golang中github.com/henrylee2cn/algorithm/cvx/sets.DimensionSet.SumSquared方法的典型用法代码示例。如果您正苦于以下问题:Golang DimensionSet.SumSquared方法的具体用法?Golang DimensionSet.SumSquared怎么用?Golang DimensionSet.SumSquared使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/henrylee2cn/algorithm/cvx/sets.DimensionSet
的用法示例。
在下文中一共展示了DimensionSet.SumSquared方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: sgemv
/*
Matrix-vector multiplication.
A is a matrix or spmatrix of size (m, n) where
N = dims['l'] + sum(dims['q']) + sum( k**2 for k in dims['s'] )
representing a mapping from R^n to S.
If trans is 'N':
y := alpha*A*x + beta * y (trans = 'N').
x is a vector of length n. y is a vector of length N.
If trans is 'T':
y := alpha*A'*x + beta * y (trans = 'T').
x is a vector of length N. y is a vector of length n.
The 's' components in S are stored in unpacked 'L' storage.
*/
func sgemv(A, x, y *matrix.FloatMatrix, alpha, beta float64, dims *sets.DimensionSet, opts ...la_.Option) error {
m := dims.Sum("l", "q") + dims.SumSquared("s")
n := la_.GetIntOpt("n", -1, opts...)
if n == -1 {
n = A.Cols()
}
trans := la_.GetIntOpt("trans", int(la_.PNoTrans), opts...)
offsetX := la_.GetIntOpt("offsetx", 0, opts...)
offsetY := la_.GetIntOpt("offsety", 0, opts...)
offsetA := la_.GetIntOpt("offseta", 0, opts...)
if trans == int(la_.PTrans) && alpha != 0.0 {
trisc(x, dims, offsetX)
//fmt.Printf("trisc x=\n%v\n", x.ConvertToString())
}
//fmt.Printf("alpha=%.4f beta=%.4f m=%d n=%d\n", alpha, beta, m, n)
//fmt.Printf("A=\n%v\nx=\n%v\ny=\n%v\n", A, x.ConvertToString(), y.ConvertToString())
err := blas.GemvFloat(A, x, y, alpha, beta, &la_.IOpt{"trans", trans},
&la_.IOpt{"n", n}, &la_.IOpt{"m", m}, &la_.IOpt{"offseta", offsetA},
&la_.IOpt{"offsetx", offsetX}, &la_.IOpt{"offsety", offsetY})
//fmt.Printf("gemv y=\n%v\n", y.ConvertToString())
if trans == int(la_.PTrans) && alpha != 0.0 {
triusc(x, dims, offsetX)
}
return err
}
示例2: cp_problem
// Here problem is already translated to epigraph format except original convex problem.
// We wrap it and create special CP epigraph kktsolver.
func cp_problem(F ConvexProg, c MatrixVariable, G MatrixVarG, h *matrix.FloatMatrix, A MatrixVarA,
b MatrixVariable, dims *sets.DimensionSet, kktsolver KKTCpSolver,
solopts *SolverOptions, x0 *matrix.FloatMatrix, mnl int) (sol *Solution, err error) {
err = nil
F_e := &cpProg{F}
//mx0 := newEpigraph(x0, 0.0)
cdim := dims.Sum("l", "q") + dims.SumSquared("s")
ux := x0.Copy()
uz := matrix.FloatZeros(mnl+cdim, 1)
kktsolver_e := func(W *sets.FloatMatrixSet, xa MatrixVariable, znl *matrix.FloatMatrix) (KKTFuncVar, error) {
x, x_ok := xa.(*epigraph)
_ = x_ok
We := W.Copy()
// dnl is matrix
dnl := W.At("dnl")[0]
dnli := W.At("dnli")[0]
We.Set("dnl", matrix.FloatVector(dnl.FloatArray()[1:]))
We.Set("dnli", matrix.FloatVector(dnli.FloatArray()[1:]))
g, err := kktsolver(We, x.m(), znl)
_, Df, _ := F.F1(x.m())
gradf0 := Df.GetRow(0, nil).Transpose()
solve := func(xa, ya MatrixVariable, z *matrix.FloatMatrix) (err error) {
x, x_ok := xa.(*epigraph)
_ = x_ok // TODO: remove or use x_ok
y := ya.Matrix()
err = nil
a := z.GetIndex(0)
blas.Copy(x.m(), ux)
blas.AxpyFloat(gradf0, ux, x.t())
blas.Copy(z, uz, &la.IOpt{"offsetx", 1})
err = g(ux, y, uz)
z.SetIndex(0, -x.t()*dnl.GetIndex(0))
blas.Copy(uz, z, &la.IOpt{"offsety", 1})
blas.Copy(ux, x.m())
val := blas.DotFloat(gradf0, x.m()) + dnl.GetIndex(0)*dnl.GetIndex(0)*x.t() - a
x.set(val)
return
}
return solve, err
}
return cpl_solver(F_e, c, G, h, A, b, dims, kktsolver_e, solopts, nil, mnl)
}
示例3: ConeQp
// Solves a pair of primal and dual convex quadratic cone programs
//
// minimize (1/2)*x'*P*x + q'*x
// subject to G*x + s = h
// A*x = b
// s >= 0
//
// maximize -(1/2)*(q + G'*z + A'*y)' * pinv(P) * (q + G'*z + A'*y)
// - h'*z - b'*y
// subject to q + G'*z + A'*y in range(P)
// z >= 0.
//
// The inequalities are with respect to a cone C defined as the Cartesian
// product of N + M + 1 cones:
//
// C = C_0 x C_1 x .... x C_N x C_{N+1} x ... x C_{N+M}.
//
// The first cone C_0 is the nonnegative orthant of dimension ml.
// The next N cones are 2nd order cones of dimension r[0], ..., r[N-1].
// The second order cone of dimension m is defined as
//
// { (u0, u1) in R x R^{m-1} | u0 >= ||u1||_2 }.
//
// The next M cones are positive semidefinite cones of order t[0], ..., t[M-1] >= 0.
//
// The structure of C is specified by DimensionSet dims which holds following sets
//
// dims.At("l") l, the dimension of the nonnegative orthant (array of length 1)
// dims.At("q") r[0], ... r[N-1], list with the dimesions of the second-order cones
// dims.At("s") t[0], ... t[M-1], array with the dimensions of the positive
// semidefinite cones
//
// The default value for dims is l: []int{G.Rows()}, q: []int{}, s: []int{}.
//
// Argument initval contains optional starting points for primal and
// dual problems. If non-nil then initval is a FloatMatrixSet having following entries.
//
// initvals.At("x")[0] starting point for x
// initvals.At("s")[0] starting point for s
// initvals.At("y")[0] starting point for y
// initvals.At("z")[0] starting point for z
//
// On exit Solution contains the result and information about the accurancy of the
// solution. if SolutionStatus is Optimal then Solution.Result contains solutions
// for the problems.
//
// Result.At("x")[0] solution for x
// Result.At("y")[0] solution for y
// Result.At("s")[0] solution for s
// Result.At("z")[0] solution for z
//
func ConeQp(P, q, G, h, A, b *matrix.FloatMatrix, dims *sets.DimensionSet, solopts *SolverOptions,
initvals *sets.FloatMatrixSet) (sol *Solution, err error) {
if q == nil || q.Cols() != 1 {
err = errors.New("'q' must be non-nil matrix with one column")
return
}
if P == nil || P.Rows() != q.Rows() || P.Cols() != q.Rows() {
err = errors.New(fmt.Sprintf("'P' must be non-nil matrix of size (%d, %d)",
q.Rows(), q.Rows()))
return
}
if h == nil {
h = matrix.FloatZeros(0, 1)
}
if h.Cols() != 1 {
err = errors.New("'h' must be non-nil matrix with one column")
return
}
if dims == nil {
dims = sets.NewDimensionSet("l", "q", "s")
dims.Set("l", []int{h.Rows()})
}
err = checkConeQpDimensions(dims)
if err != nil {
return
}
cdim := dims.Sum("l", "q") + dims.SumSquared("s")
if h.Rows() != cdim {
err = errors.New(fmt.Sprintf("'h' must be float matrix of size (%d,1)", cdim))
return
}
if G == nil {
G = matrix.FloatZeros(0, q.Rows())
}
if !G.SizeMatch(cdim, q.Rows()) {
estr := fmt.Sprintf("'G' must be of size (%d,%d)", cdim, q.Rows())
err = errors.New(estr)
return
}
// Check A and set defaults if it is nil
if A == nil {
// zeros rows reduces Gemv to vector products
//.........这里部分代码省略.........
示例4: coneqp_solver
func coneqp_solver(P MatrixVarP, q MatrixVariable, G MatrixVarG, h *matrix.FloatMatrix,
A MatrixVarA, b MatrixVariable, dims *sets.DimensionSet, kktsolver KKTConeSolverVar,
solopts *SolverOptions, initvals *sets.FloatMatrixSet) (sol *Solution, err error) {
err = nil
EXPON := 3
STEP := 0.99
sol = &Solution{Unknown,
nil,
0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0, 0}
//var kktsolver func(*sets.FloatMatrixSet)(KKTFunc, error) = nil
var refinement int
var correction bool = true
feasTolerance := FEASTOL
absTolerance := ABSTOL
relTolerance := RELTOL
maxIter := MAXITERS
if solopts.FeasTol > 0.0 {
feasTolerance = solopts.FeasTol
}
if solopts.AbsTol > 0.0 {
absTolerance = solopts.AbsTol
}
if solopts.RelTol > 0.0 {
relTolerance = solopts.RelTol
}
if solopts.MaxIter > 0 {
maxIter = solopts.MaxIter
}
if q == nil {
err = errors.New("'q' must be non-nil MatrixVariable with one column")
return
}
if h == nil {
h = matrix.FloatZeros(0, 1)
}
if h.Cols() != 1 {
err = errors.New("'h' must be non-nil matrix with one column")
return
}
if dims == nil {
dims = sets.NewDimensionSet("l", "q", "s")
dims.Set("l", []int{h.Rows()})
}
err = checkConeQpDimensions(dims)
if err != nil {
return
}
cdim := dims.Sum("l", "q") + dims.SumSquared("s")
//cdim_pckd := dims.Sum("l", "q") + dims.SumPacked("s")
cdim_diag := dims.Sum("l", "q", "s")
if h.Rows() != cdim {
err = errors.New(fmt.Sprintf("'h' must be float matrix of size (%d,1)", cdim))
return
}
// Data for kth 'q' constraint are found in rows indq[k]:indq[k+1] of G.
indq := make([]int, 0)
indq = append(indq, dims.At("l")[0])
for _, k := range dims.At("q") {
indq = append(indq, indq[len(indq)-1]+k)
}
// Data for kth 's' constraint are found in rows inds[k]:inds[k+1] of G.
inds := make([]int, 0)
inds = append(inds, indq[len(indq)-1])
for _, k := range dims.At("s") {
inds = append(inds, inds[len(inds)-1]+k*k)
}
if P == nil {
err = errors.New("'P' must be non-nil MatrixVarP interface.")
return
}
fP := func(u, v MatrixVariable, alpha, beta float64) error {
return P.Pf(u, v, alpha, beta)
}
if G == nil {
err = errors.New("'G' must be non-nil MatrixG interface.")
return
}
fG := func(x, y MatrixVariable, alpha, beta float64, trans la.Option) error {
return G.Gf(x, y, alpha, beta, trans)
}
// Check A and set defaults if it is nil
fA := func(x, y MatrixVariable, alpha, beta float64, trans la.Option) error {
return A.Af(x, y, alpha, beta, trans)
}
// Check b and set defaults if it is nil
//.........这里部分代码省略.........
示例5: ConeQpCustomMatrix
// Solves a pair of primal and dual cone programs using custom KKT solver and custom
// matrices P, G and A.
//
// P must implement interface MatrixP, G must implement interface MatrixG
// and A must implement interface MatrixA.
//
func ConeQpCustomMatrix(P MatrixP, q *matrix.FloatMatrix, G MatrixG, h *matrix.FloatMatrix,
A MatrixA, b *matrix.FloatMatrix, dims *sets.DimensionSet, kktsolver KKTConeSolver,
solopts *SolverOptions, initvals *sets.FloatMatrixSet) (sol *Solution, err error) {
err = nil
if q == nil || q.Cols() != 1 {
err = errors.New("'q' must be non-nil matrix with one column")
return
}
if h == nil {
h = matrix.FloatZeros(0, 1)
}
if h.Cols() != 1 {
err = errors.New("'h' must be non-nil matrix with one column")
return
}
if dims == nil {
dims = sets.NewDimensionSet("l", "q", "s")
dims.Set("l", []int{h.Rows()})
}
err = checkConeQpDimensions(dims)
if err != nil {
return
}
cdim := dims.Sum("l", "q") + dims.SumSquared("s")
//cdim_diag := dims.Sum("l", "q", "s")
if h.Rows() != cdim {
err = errors.New(fmt.Sprintf("'h' must be float matrix of size (%d,1)", cdim))
return
}
if P == nil {
err = errors.New("'P' must be non-nil MatrixP interface.")
return
}
// Check b and set defaults if it is nil
if b == nil {
b = matrix.FloatZeros(0, 1)
}
if b.Cols() != 1 {
estr := fmt.Sprintf("'b' must be a matrix with 1 column")
err = errors.New(estr)
return
}
if b.Rows() > q.Rows() {
err = errors.New("Rank(A) < p or Rank[G; A] < n")
return
}
if kktsolver == nil {
err = errors.New("nil kktsolver not allowed.")
return
}
var mG MatrixVarG
var mP MatrixVarP
var mA MatrixVarA
if A == nil {
mA = &matrixVarA{matrix.FloatZeros(0, q.Rows())}
} else {
mA = &matrixIfA{A}
}
if G == nil {
mG = &matrixVarG{matrix.FloatZeros(0, q.Rows()), dims}
} else {
mG = &matrixIfG{G}
}
mP = &matrixIfP{P}
mq := &matrixVar{q}
mb := &matrixVar{b}
return coneqp_problem(mP, mq, mG, h, mA, mb, dims, kktsolver, solopts, initvals)
}
示例6: ConeQpCustomKKT
// Solves a pair of primal and dual convex quadratic cone programs using custom KKT solver.
//
func ConeQpCustomKKT(P, q, G, h, A, b *matrix.FloatMatrix, dims *sets.DimensionSet, kktsolver KKTConeSolver,
solopts *SolverOptions, initvals *sets.FloatMatrixSet) (sol *Solution, err error) {
if q == nil || q.Cols() != 1 {
err = errors.New("'q' must be non-nil matrix with one column")
return
}
if P == nil || P.Rows() != q.Rows() || P.Cols() != q.Rows() {
err = errors.New(fmt.Sprintf("'P' must be non-nil matrix of size (%d, %d)",
q.Rows(), q.Rows()))
return
}
if h == nil {
h = matrix.FloatZeros(0, 1)
}
if h.Cols() != 1 {
err = errors.New("'h' must be non-nil matrix with one column")
return
}
if dims == nil {
dims = sets.NewDimensionSet("l", "q", "s")
dims.Set("l", []int{h.Rows()})
}
err = checkConeQpDimensions(dims)
if err != nil {
return
}
cdim := dims.Sum("l", "q") + dims.SumSquared("s")
if h.Rows() != cdim {
err = errors.New(fmt.Sprintf("'h' must be float matrix of size (%d,1)", cdim))
return
}
if G == nil {
G = matrix.FloatZeros(0, q.Rows())
}
if !G.SizeMatch(cdim, q.Rows()) {
estr := fmt.Sprintf("'G' must be of size (%d,%d)", cdim, q.Rows())
err = errors.New(estr)
return
}
// Check A and set defaults if it is nil
if A == nil {
// zeros rows reduces Gemv to vector products
A = matrix.FloatZeros(0, q.Rows())
}
if A.Cols() != q.Rows() {
estr := fmt.Sprintf("'A' must have %d columns", q.Rows())
err = errors.New(estr)
return
}
// Check b and set defaults if it is nil
if b == nil {
b = matrix.FloatZeros(0, 1)
}
if b.Cols() != 1 {
estr := fmt.Sprintf("'b' must be a matrix with 1 column")
err = errors.New(estr)
return
}
if b.Rows() != A.Rows() {
estr := fmt.Sprintf("'b' must have length %d", A.Rows())
err = errors.New(estr)
return
}
if kktsolver == nil {
err = errors.New("nil kktsolver not allowed")
return
}
mA := &matrixVarA{A}
mG := &matrixVarG{G, dims}
mP := &matrixVarP{P}
mq := &matrixVar{q}
mb := &matrixVar{b}
return coneqp_problem(mP, mq, mG, h, mA, mb, dims, kktsolver, solopts, initvals)
}
示例7: kktChol
// Solution of KKT equations by reduction to a 2 x 2 system, a QR
// factorization to eliminate the equality constraints, and a dense
// Cholesky factorization of order n-p.
//
// Computes the QR factorization
//
// A' = [Q1, Q2] * [R; 0]
//
// and returns a function that (1) computes the Cholesky factorization
//
// Q_2^T * (H + GG^T * W^{-1} * W^{-T} * GG) * Q2 = L * L^T,
//
// given H, Df, W, where GG = [Df; G], and (2) returns a function for
// solving
//
// [ H A' GG' ] [ ux ] [ bx ]
// [ A 0 0 ] * [ uy ] = [ by ].
// [ GG 0 -W'*W ] [ uz ] [ bz ]
//
// H is n x n, A is p x n, Df is mnl x n, G is N x n where
// N = dims['l'] + sum(dims['q']) + sum( k**2 for k in dims['s'] ).
//
func kktChol(G *matrix.FloatMatrix, dims *sets.DimensionSet, A *matrix.FloatMatrix, mnl int) (kktFactor, error) {
p, n := A.Size()
cdim := mnl + dims.Sum("l", "q") + dims.SumSquared("s")
cdim_pckd := mnl + dims.Sum("l", "q") + dims.SumPacked("s")
QA := A.Transpose()
tauA := matrix.FloatZeros(p, 1)
lapack.Geqrf(QA, tauA)
Gs := matrix.FloatZeros(cdim, n)
K := matrix.FloatZeros(n, n)
bzp := matrix.FloatZeros(cdim_pckd, 1)
yy := matrix.FloatZeros(p, 1)
checkpnt.AddMatrixVar("tauA", tauA)
checkpnt.AddMatrixVar("Gs", Gs)
checkpnt.AddMatrixVar("K", K)
factor := func(W *sets.FloatMatrixSet, H, Df *matrix.FloatMatrix) (KKTFunc, error) {
// Compute
//
// K = [Q1, Q2]' * (H + GG' * W^{-1} * W^{-T} * GG) * [Q1, Q2]
//
// and take the Cholesky factorization of the 2,2 block
//
// Q_2' * (H + GG^T * W^{-1} * W^{-T} * GG) * Q2.
var err error = nil
minor := 0
if !checkpnt.MinorEmpty() {
minor = checkpnt.MinorTop()
}
// Gs = W^{-T} * GG in packed storage.
if mnl > 0 {
Gs.SetSubMatrix(0, 0, Df)
}
Gs.SetSubMatrix(mnl, 0, G)
checkpnt.Check("00factor_chol", minor)
scale(Gs, W, true, true)
pack2(Gs, dims, mnl)
//checkpnt.Check("10factor_chol", minor)
// K = [Q1, Q2]' * (H + Gs' * Gs) * [Q1, Q2].
blas.SyrkFloat(Gs, K, 1.0, 0.0, la.OptTrans, &la.IOpt{"k", cdim_pckd})
if H != nil {
K.SetSubMatrix(0, 0, matrix.Plus(H, K.GetSubMatrix(0, 0, H.Rows(), H.Cols())))
}
//checkpnt.Check("20factor_chol", minor)
symm(K, n, 0)
lapack.Ormqr(QA, tauA, K, la.OptLeft, la.OptTrans)
lapack.Ormqr(QA, tauA, K, la.OptRight)
//checkpnt.Check("30factor_chol", minor)
// Cholesky factorization of 2,2 block of K.
lapack.Potrf(K, &la.IOpt{"n", n - p}, &la.IOpt{"offseta", p * (n + 1)})
checkpnt.Check("40factor_chol", minor)
solve := func(x, y, z *matrix.FloatMatrix) (err error) {
// Solve
//
// [ 0 A' GG'*W^{-1} ] [ ux ] [ bx ]
// [ A 0 0 ] * [ uy ] = [ by ]
// [ W^{-T}*GG 0 -I ] [ W*uz ] [ W^{-T}*bz ]
//
// and return ux, uy, W*uz.
//
// On entry, x, y, z contain bx, by, bz. On exit, they contain
// the solution ux, uy, W*uz.
//
// If we change variables ux = Q1*v + Q2*w, the system becomes
//
// [ K11 K12 R ] [ v ] [Q1'*(bx+GG'*W^{-1}*W^{-T}*bz)]
// [ K21 K22 0 ] * [ w ] = [Q2'*(bx+GG'*W^{-1}*W^{-T}*bz)]
// [ R^T 0 0 ] [ uy ] [by ]
//
// W*uz = W^{-T} * ( GG*ux - bz ).
minor := 0
if !checkpnt.MinorEmpty() {
//.........这里部分代码省略.........
示例8: cpl_solver
// Internal CPL solver for CP and CLP problems. Everything is wrapped to proper interfaces
func cpl_solver(F ConvexVarProg, c MatrixVariable, G MatrixVarG, h *matrix.FloatMatrix,
A MatrixVarA, b MatrixVariable, dims *sets.DimensionSet, kktsolver KKTCpSolverVar,
solopts *SolverOptions, x0 MatrixVariable, mnl int) (sol *Solution, err error) {
const (
STEP = 0.99
BETA = 0.5
ALPHA = 0.01
EXPON = 3
MAX_RELAXED_ITERS = 8
)
var refinement int
sol = &Solution{Unknown,
nil,
0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0, 0}
feasTolerance := FEASTOL
absTolerance := ABSTOL
relTolerance := RELTOL
maxIter := MAXITERS
if solopts.FeasTol > 0.0 {
feasTolerance = solopts.FeasTol
}
if solopts.AbsTol > 0.0 {
absTolerance = solopts.AbsTol
}
if solopts.RelTol > 0.0 {
relTolerance = solopts.RelTol
}
if solopts.Refinement > 0 {
refinement = solopts.Refinement
} else {
refinement = 1
}
if solopts.MaxIter > 0 {
maxIter = solopts.MaxIter
}
if x0 == nil {
mnl, x0, err = F.F0()
if err != nil {
return
}
}
if c == nil {
err = errors.New("Must define objective.")
return
}
if h == nil {
h = matrix.FloatZeros(0, 1)
}
if dims == nil {
err = errors.New("Problem dimensions not defined.")
return
}
if err = checkConeLpDimensions(dims); err != nil {
return
}
cdim := dims.Sum("l", "q") + dims.SumSquared("s")
cdim_diag := dims.Sum("l", "q", "s")
if h.Rows() != cdim {
err = errors.New(fmt.Sprintf("'h' must be float matrix of size (%d,1)", cdim))
return
}
if G == nil {
err = errors.New("'G' must be non-nil MatrixG interface.")
return
}
fG := func(x, y MatrixVariable, alpha, beta float64, trans la.Option) error {
return G.Gf(x, y, alpha, beta, trans)
}
// Check A and set defaults if it is nil
if A == nil {
err = errors.New("'A' must be non-nil MatrixA interface.")
return
}
fA := func(x, y MatrixVariable, alpha, beta float64, trans la.Option) error {
return A.Af(x, y, alpha, beta, trans)
}
if b == nil {
err = errors.New("'b' must be non-nil MatrixVariable interface.")
return
}
if kktsolver == nil {
err = errors.New("nil kktsolver not allowed.")
return
}
//.........这里部分代码省略.........
示例9: ConeLpCustomKKT
// Solves a pair of primal and dual cone programs using custom KKT solver.
//
func ConeLpCustomKKT(c, G, h, A, b *matrix.FloatMatrix, dims *sets.DimensionSet,
kktsolver KKTConeSolver, solopts *SolverOptions, primalstart,
dualstart *sets.FloatMatrixSet) (sol *Solution, err error) {
if c == nil || c.Cols() > 1 {
err = errors.New("'c' must be matrix with 1 column")
return
}
if h == nil {
h = matrix.FloatZeros(0, 1)
}
if h.Cols() > 1 {
err = errors.New("'h' must be matrix with 1 column")
return
}
if dims == nil {
dims = sets.NewDimensionSet("l", "q", "s")
dims.Set("l", []int{h.Rows()})
}
cdim := dims.Sum("l", "q") + dims.SumSquared("s")
cdim_pckd := dims.Sum("l", "q") + dims.SumPacked("s")
//cdim_diag := dims.Sum("l", "q", "s")
if G == nil {
G = matrix.FloatZeros(0, c.Rows())
}
if !G.SizeMatch(cdim, c.Rows()) {
estr := fmt.Sprintf("'G' must be of size (%d,%d)", cdim, c.Rows())
err = errors.New(estr)
return
}
// Check A and set defaults if it is nil
if A == nil {
// zeros rows reduces Gemv to vector products
A = matrix.FloatZeros(0, c.Rows())
}
if A.Cols() != c.Rows() {
estr := fmt.Sprintf("'A' must have %d columns", c.Rows())
err = errors.New(estr)
return
}
// Check b and set defaults if it is nil
if b == nil {
b = matrix.FloatZeros(0, 1)
}
if b.Cols() != 1 {
estr := fmt.Sprintf("'b' must be a matrix with 1 column")
err = errors.New(estr)
return
}
if b.Rows() != A.Rows() {
estr := fmt.Sprintf("'b' must have length %d", A.Rows())
err = errors.New(estr)
return
}
if b.Rows() > c.Rows() || b.Rows()+cdim_pckd < c.Rows() {
err = errors.New("Rank(A) < p or Rank([G; A]) < n")
return
}
mA := &matrixVarA{A}
mG := &matrixVarG{G, dims}
mc := &matrixVar{c}
mb := &matrixVar{b}
return conelp_problem(mc, mG, h, mA, mb, dims, kktsolver, solopts, primalstart, dualstart)
}
示例10: ConeLp
// Solves a pair of primal and dual cone programs
//
// minimize c'*x
// subject to G*x + s = h
// A*x = b
// s >= 0
//
// maximize -h'*z - b'*y
// subject to G'*z + A'*y + c = 0
// z >= 0.
//
// The inequalities are with respect to a cone C defined as the Cartesian
// product of N + M + 1 cones:
//
// C = C_0 x C_1 x .... x C_N x C_{N+1} x ... x C_{N+M}.
//
// The first cone C_0 is the nonnegative orthant of dimension ml.
// The next N cones are second order cones of dimension r[0], ..., r[N-1].
// The second order cone of dimension m is defined as
//
// { (u0, u1) in R x R^{m-1} | u0 >= ||u1||_2 }.
//
// The next M cones are positive semidefinite cones of order t[0], ..., t[M-1] >= 0.
//
// The structure of C is specified by DimensionSet dims which holds following sets
//
// dims.At("l") l, the dimension of the nonnegative orthant (array of length 1)
// dims.At("q") r[0], ... r[N-1], list with the dimesions of the second-order cones
// dims.At("s") t[0], ... t[M-1], array with the dimensions of the positive
// semidefinite cones
//
// The default value for dims is l: []int{G.Rows()}, q: []int{}, s: []int{}.
//
// Arguments primalstart, dualstart are optional starting points for primal and
// dual problems. If non-nil then primalstart is a FloatMatrixSet having two entries.
//
// primalstart.At("x")[0] starting point for x
// primalstart.At("s")[0] starting point for s
// dualstart.At("y")[0] starting point for y
// dualstart.At("z")[0] starting point for z
//
// On exit Solution contains the result and information about the accurancy of the
// solution. if SolutionStatus is Optimal then Solution.Result contains solutions
// for the problems.
//
// Result.At("x")[0] solution for x
// Result.At("y")[0] solution for y
// Result.At("s")[0] solution for s
// Result.At("z")[0] solution for z
//
func ConeLp(c, G, h, A, b *matrix.FloatMatrix, dims *sets.DimensionSet, solopts *SolverOptions,
primalstart, dualstart *sets.FloatMatrixSet) (sol *Solution, err error) {
if c == nil || c.Cols() > 1 {
err = errors.New("'c' must be matrix with 1 column")
return
}
if c.Rows() < 1 {
err = errors.New("No variables, 'c' must have at least one row")
return
}
if h == nil || h.Cols() > 1 {
err = errors.New("'h' must be matrix with 1 column")
return
}
if dims == nil {
dims = sets.NewDimensionSet("l", "q", "s")
dims.Set("l", []int{h.Rows()})
}
cdim := dims.Sum("l", "q") + dims.SumSquared("s")
cdim_pckd := dims.Sum("l", "q") + dims.SumPacked("s")
if h.Rows() != cdim {
err = errors.New(fmt.Sprintf("'h' must be float matrix of size (%d,1)", cdim))
return
}
if G == nil {
G = matrix.FloatZeros(0, c.Rows())
}
if !G.SizeMatch(cdim, c.Rows()) {
estr := fmt.Sprintf("'G' must be of size (%d,%d)", cdim, c.Rows())
err = errors.New(estr)
return
}
// Check A and set defaults if it is nil
if A == nil {
// zeros rows reduces Gemv to vector products
A = matrix.FloatZeros(0, c.Rows())
}
if A.Cols() != c.Rows() {
estr := fmt.Sprintf("'A' must have %d columns", c.Rows())
err = errors.New(estr)
return
}
//.........这里部分代码省略.........
示例11: CpCustomMatrix
// Solves a convex optimization problem with a linear objective
//
// minimize f0(x)
// subject to fk(x) <= 0, k = 1, ..., mnl
// G*x <= h
// A*x = b.
//
// using custom solver for KKT equations and constraint equations G and A.
//
func CpCustomMatrix(F ConvexProg, G MatrixG, h *matrix.FloatMatrix, A MatrixA,
b *matrix.FloatMatrix, dims *sets.DimensionSet, kktsolver KKTCpSolver,
solopts *SolverOptions) (sol *Solution, err error) {
var mnl int
var x0 *matrix.FloatMatrix
mnl, x0, err = F.F0()
if err != nil {
return
}
if x0.Cols() != 1 {
err = errors.New("'x0' must be matrix with one column")
return
}
if h == nil {
h = matrix.FloatZeros(0, 1)
}
if h.Cols() > 1 {
err = errors.New("'h' must be matrix with 1 column")
return
}
if dims == nil {
dims = sets.NewDimensionSet("l", "q", "s")
dims.Set("l", []int{h.Rows()})
}
if err = checkConeLpDimensions(dims); err != nil {
return
}
cdim := dims.Sum("l", "q") + dims.SumSquared("s")
if h.Rows() != cdim {
err = errors.New(fmt.Sprintf("'h' must be float matrix of size (%d,1)", cdim))
return
}
var G_e MatrixVarG = nil
if G == nil {
G_e = &epMatrixG{matrix.FloatZeros(0, x0.Rows()), dims}
} else {
G_e = &epiMatrixG{G, dims}
}
var A_e MatrixVarA = nil
if A == nil {
A_e = &epMatrixA{matrix.FloatZeros(0, x0.Rows())}
} else {
A_e = &epiMatrixA{A}
}
// Check b and set defaults if it is nil
if b == nil {
b = matrix.FloatZeros(0, 1)
}
if b.Cols() != 1 {
estr := fmt.Sprintf("'b' must be a matrix with 1 column")
err = errors.New(estr)
return
}
if kktsolver == nil {
err = errors.New("'kktsolver' must be non-nil function.")
return
}
c_e := newEpigraph(x0, 1.0)
blas.ScalFloat(c_e.m(), 0.0)
b_e := matrixVar{b}
return cp_problem(F, c_e, G_e, h, A_e, &b_e, dims, kktsolver, solopts, x0, mnl)
}
示例12: CpCustomKKT
// Solves a convex optimization problem with a linear objective
//
// minimize f0(x)
// subject to fk(x) <= 0, k = 1, ..., mnl
// G*x <= h
// A*x = b.
//
// using custom solver for KKT equations.
//
func CpCustomKKT(F ConvexProg, G, h, A, b *matrix.FloatMatrix, dims *sets.DimensionSet,
kktsolver KKTCpSolver, solopts *SolverOptions) (sol *Solution, err error) {
var mnl int
var x0 *matrix.FloatMatrix
mnl, x0, err = F.F0()
if err != nil {
return
}
if x0.Cols() != 1 {
err = errors.New("'x0' must be matrix with one column")
return
}
if h == nil {
h = matrix.FloatZeros(0, 1)
}
if h.Cols() > 1 {
err = errors.New("'h' must be matrix with 1 column")
return
}
if dims == nil {
dims = sets.NewDimensionSet("l", "q", "s")
dims.Set("l", []int{h.Rows()})
}
if err = checkConeLpDimensions(dims); err != nil {
return
}
cdim := dims.Sum("l", "q") + dims.SumSquared("s")
if h.Rows() != cdim {
err = errors.New(fmt.Sprintf("'h' must be float matrix of size (%d,1)", cdim))
return
}
if G == nil {
G = matrix.FloatZeros(0, x0.Rows())
}
if !G.SizeMatch(cdim, x0.Rows()) {
estr := fmt.Sprintf("'G' must be of size (%d,%d)", cdim, x0.Rows())
err = errors.New(estr)
return
}
// Check A and set defaults if it is nil
if A == nil {
// zeros rows reduces Gemv to vector products
A = matrix.FloatZeros(0, x0.Rows())
}
if A.Cols() != x0.Rows() {
estr := fmt.Sprintf("'A' must have %d columns", x0.Rows())
err = errors.New(estr)
return
}
// Check b and set defaults if it is nil
if b == nil {
b = matrix.FloatZeros(0, 1)
}
if b.Cols() != 1 {
estr := fmt.Sprintf("'b' must be a matrix with 1 column")
err = errors.New(estr)
return
}
if b.Rows() != A.Rows() {
estr := fmt.Sprintf("'b' must have length %d", A.Rows())
err = errors.New(estr)
return
}
if kktsolver == nil {
err = errors.New("'kktsolver' must be non-nil function.")
return
}
c_e := newEpigraph(x0, 1.0)
blas.ScalFloat(x0, 0.0)
G_e := epMatrixG{G, dims}
A_e := epMatrixA{A}
b_e := matrixVar{b}
return cp_problem(F, c_e, &G_e, h, &A_e, &b_e, dims, kktsolver, solopts, x0, mnl)
}
示例13: Cp
// Solves a convex optimization problem with a linear objective
//
// minimize f0(x)
// subject to fk(x) <= 0, k = 1, ..., mnl
// G*x <= h
// A*x = b.
//
// f is vector valued, convex and twice differentiable. The linear
// inequalities are with respect to a cone C defined as the Cartesian
// product of N + M + 1 cones:
//
// C = C_0 x C_1 x .... x C_N x C_{N+1} x ... x C_{N+M}.
//
// The first cone C_0 is the nonnegative orthant of dimension ml. The
// next N cones are second order cones of dimension r[0], ..., r[N-1].
// The second order cone of dimension m is defined as
//
// { (u0, u1) in R x R^{m-1} | u0 >= ||u1||_2 }.
//
// The next M cones are positive semidefinite cones of order t[0], ..., t[M-1] >= 0.
//
// The structure of C is specified by DimensionSet dims which holds following sets
//
// dims.At("l") l, the dimension of the nonnegative orthant (array of length 1)
// dims.At("q") r[0], ... r[N-1], list with the dimesions of the second-order cones
// dims.At("s") t[0], ... t[M-1], array with the dimensions of the positive
// semidefinite cones
//
// The default value for dims is l: []int{h.Rows()}, q: []int{}, s: []int{}.
//
// On exit Solution contains the result and information about the accurancy of the
// solution. if SolutionStatus is Optimal then Solution.Result contains solutions
// for the problems.
//
// Result.At("x")[0] primal solution
// Result.At("snl")[0] non-linear constraint slacks
// Result.At("sl")[0] linear constraint slacks
// Result.At("y")[0] values for linear equality constraints y
// Result.At("znl")[0] values of dual variables for nonlinear inequalities
// Result.At("zl")[0] values of dual variables for linear inequalities
//
// If err is non-nil then sol is nil and err contains information about the argument or
// computation error.
//
func Cp(F ConvexProg, G, h, A, b *matrix.FloatMatrix, dims *sets.DimensionSet, solopts *SolverOptions) (sol *Solution, err error) {
var mnl int
var x0 *matrix.FloatMatrix
mnl, x0, err = F.F0()
if err != nil {
return
}
if x0.Cols() != 1 {
err = errors.New("'x0' must be matrix with one column")
return
}
if h == nil {
h = matrix.FloatZeros(0, 1)
}
if h.Cols() > 1 {
err = errors.New("'h' must be matrix with 1 column")
return
}
if dims == nil {
dims = sets.NewDimensionSet("l", "q", "s")
dims.Set("l", []int{h.Rows()})
}
if err = checkConeLpDimensions(dims); err != nil {
return
}
cdim := dims.Sum("l", "q") + dims.SumSquared("s")
if h.Rows() != cdim {
err = errors.New(fmt.Sprintf("'h' must be float matrix of size (%d,1)", cdim))
return
}
if G == nil {
G = matrix.FloatZeros(0, x0.Rows())
}
if !G.SizeMatch(cdim, x0.Rows()) {
estr := fmt.Sprintf("'G' must be of size (%d,%d)", cdim, x0.Rows())
err = errors.New(estr)
return
}
// Check A and set defaults if it is nil
if A == nil {
// zeros rows reduces Gemv to vector products
A = matrix.FloatZeros(0, x0.Rows())
}
if A.Cols() != x0.Rows() {
estr := fmt.Sprintf("'A' must have %d columns", x0.Rows())
err = errors.New(estr)
return
}
//.........这里部分代码省略.........
示例14: CplCustomKKT
// Solves a convex optimization problem with a linear objective
//
// minimize c'*x
// subject to f(x) <= 0
// G*x <= h
// A*x = b.
//
// using custom KTT equation solver.
//
func CplCustomKKT(F ConvexProg, c *matrix.FloatMatrix, G, h, A, b *matrix.FloatMatrix,
dims *sets.DimensionSet, kktsolver KKTCpSolver,
solopts *SolverOptions) (sol *Solution, err error) {
var mnl int
var x0 *matrix.FloatMatrix
mnl, x0, err = F.F0()
if err != nil {
return
}
if x0.Cols() != 1 {
err = errors.New("'x0' must be matrix with one column")
return
}
if c == nil {
err = errors.New("'c' must be non nil matrix")
return
}
if !c.SizeMatch(x0.Size()) {
err = errors.New(fmt.Sprintf("'c' must be matrix of size (%d,1)", x0.Rows()))
return
}
if h == nil {
h = matrix.FloatZeros(0, 1)
}
if h.Cols() > 1 {
err = errors.New("'h' must be matrix with 1 column")
return
}
if dims == nil {
dims = sets.NewDimensionSet("l", "q", "s")
dims.Set("l", []int{h.Rows()})
}
cdim := dims.Sum("l", "q") + dims.SumSquared("s")
if h.Rows() != cdim {
err = errors.New(fmt.Sprintf("'h' must be float matrix of size (%d,1)", cdim))
return
}
if G == nil {
G = matrix.FloatZeros(0, c.Rows())
}
if !G.SizeMatch(cdim, c.Rows()) {
estr := fmt.Sprintf("'G' must be of size (%d,%d)", cdim, c.Rows())
err = errors.New(estr)
return
}
// Check A and set defaults if it is nil
if A == nil {
// zeros rows reduces Gemv to vector products
A = matrix.FloatZeros(0, c.Rows())
}
if A.Cols() != c.Rows() {
estr := fmt.Sprintf("'A' must have %d columns", c.Rows())
err = errors.New(estr)
return
}
// Check b and set defaults if it is nil
if b == nil {
b = matrix.FloatZeros(0, 1)
}
if b.Cols() != 1 {
estr := fmt.Sprintf("'b' must be a matrix with 1 column")
err = errors.New(estr)
return
}
if b.Rows() != A.Rows() {
estr := fmt.Sprintf("'b' must have length %d", A.Rows())
err = errors.New(estr)
return
}
var mc = matrixVar{c}
var mb = matrixVar{b}
var mA = matrixVarA{A}
var mG = matrixVarG{G, dims}
return cpl_problem(F, &mc, &mG, h, &mA, &mb, dims, kktsolver, solopts, x0, mnl)
}
示例15: ConeLpCustomMatrix
// Solves a pair of primal and dual cone programs using custom KKT solver and constraint
// interfaces MatrixG and MatrixA
//
func ConeLpCustomMatrix(c *matrix.FloatMatrix, G MatrixG, h *matrix.FloatMatrix,
A MatrixA, b *matrix.FloatMatrix, dims *sets.DimensionSet, kktsolver KKTConeSolver,
solopts *SolverOptions, primalstart, dualstart *sets.FloatMatrixSet) (sol *Solution, err error) {
err = nil
if c == nil || c.Cols() > 1 {
err = errors.New("'c' must be matrix with 1 column")
return
}
if h == nil || h.Cols() > 1 {
err = errors.New("'h' must be matrix with 1 column")
return
}
if err = checkConeLpDimensions(dims); err != nil {
return
}
cdim := dims.Sum("l", "q") + dims.SumSquared("s")
cdim_pckd := dims.Sum("l", "q") + dims.SumPacked("s")
//cdim_diag := dims.Sum("l", "q", "s")
if h.Rows() != cdim {
err = errors.New(fmt.Sprintf("'h' must be float matrix of size (%d,1)", cdim))
return
}
// Data for kth 'q' constraint are found in rows indq[k]:indq[k+1] of G.
indq := make([]int, 0)
indq = append(indq, dims.At("l")[0])
for _, k := range dims.At("q") {
indq = append(indq, indq[len(indq)-1]+k)
}
// Data for kth 's' constraint are found in rows inds[k]:inds[k+1] of G.
inds := make([]int, 0)
inds = append(inds, indq[len(indq)-1])
for _, k := range dims.At("s") {
inds = append(inds, inds[len(inds)-1]+k*k)
}
// Check b and set defaults if it is nil
if b == nil {
b = matrix.FloatZeros(0, 1)
}
if b.Cols() != 1 {
estr := fmt.Sprintf("'b' must be a matrix with 1 column")
err = errors.New(estr)
return
}
if b.Rows() > c.Rows() || b.Rows()+cdim_pckd < c.Rows() {
err = errors.New("Rank(A) < p or Rank([G; A]) < n")
return
}
if kktsolver == nil {
err = errors.New("nil kktsolver not allowed.")
return
}
var mA MatrixVarA
var mG MatrixVarG
if G == nil {
mG = &matrixVarG{matrix.FloatZeros(0, c.Rows()), dims}
} else {
mG = &matrixIfG{G}
}
if A == nil {
mA = &matrixVarA{matrix.FloatZeros(0, c.Rows())}
} else {
mA = &matrixIfA{A}
}
var mc = &matrixVar{c}
var mb = &matrixVar{b}
return conelp_problem(mc, mG, h, mA, mb, dims, kktsolver, solopts, primalstart, dualstart)
}