本文整理汇总了Golang中github.com/hrautila/cvx/sets.DimensionSet.Set方法的典型用法代码示例。如果您正苦于以下问题:Golang DimensionSet.Set方法的具体用法?Golang DimensionSet.Set怎么用?Golang DimensionSet.Set使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/hrautila/cvx/sets.DimensionSet
的用法示例。
在下文中一共展示了DimensionSet.Set方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: checkConeLpDimensions
func checkConeLpDimensions(dims *sets.DimensionSet) error {
if len(dims.At("l")) == 0 {
dims.Set("l", []int{0})
} else if dims.At("l")[0] < 0 {
return errors.New("dimension 'l' must be nonnegative integer")
}
for _, m := range dims.At("q") {
if m < 1 {
return errors.New("dimension 'q' must be list of positive integers")
}
}
for _, m := range dims.At("s") {
if m < 1 {
return errors.New("dimension 's' must be list of positive integers")
}
}
return nil
}
示例2: Cpl
// Solves a convex optimization problem with a linear objective
//
// minimize c'*x
// subject to f(x) <= 0
// 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 Cpl(F ConvexProg, c, 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 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")
//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
}
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 {
//.........这里部分代码省略.........
示例3: CplCustomMatrix
// 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 and custom constraints G and A.
//
func CplCustomMatrix(F ConvexProg, c *matrix.FloatMatrix, 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 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
}
// 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
}
mc := matrixVar{c}
mb := matrixVar{b}
var mG MatrixVarG
var mA MatrixVarA
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}
}
return cpl_problem(F, &mc, mG, h, mA, &mb, dims, kktsolver, solopts, x0, mnl)
}
示例4: 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)
}
示例5: 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)
}
示例6: 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
}
//.........这里部分代码省略.........