本文整理匯總了Golang中github.com/henrylee2cn/algorithm/cvx/sets.FloatMatrixSet.Set方法的典型用法代碼示例。如果您正苦於以下問題:Golang FloatMatrixSet.Set方法的具體用法?Golang FloatMatrixSet.Set怎麽用?Golang FloatMatrixSet.Set使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/henrylee2cn/algorithm/cvx/sets.FloatMatrixSet
的用法示例。
在下文中一共展示了FloatMatrixSet.Set方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Sdp
// Solves a pair of primal and dual SDPs
//
// minimize c'*x
// subject to Gl*x + sl = hl
// mat(Gs[k]*x) + ss[k] = hs[k], k = 0, ..., N-1
// A*x = b
// sl >= 0, ss[k] >= 0, k = 0, ..., N-1
//
// maximize -hl'*z - sum_k trace(hs[k]*zs[k]) - b'*y
// subject to Gl'*zl + sum_k Gs[k]'*vec(zs[k]) + A'*y + c = 0
// zl >= 0, zs[k] >= 0, k = 0, ..., N-1.
//
// The inequalities sl >= 0 and zl >= 0 are elementwise vector
// inequalities. The inequalities ss[k] >= 0, zs[k] >= 0 are matrix
// inequalities, i.e., the symmetric matrices ss[k] and zs[k] must be
// positive semidefinite. mat(Gs[k]*x) is the symmetric matrix X with
// X[:] = Gs[k]*x. For a symmetric matrix, zs[k], vec(zs[k]) is the
// vector zs[k][:].
//
func Sdp(c, Gl, hl, A, b *matrix.FloatMatrix, Ghs *sets.FloatMatrixSet, solopts *SolverOptions,
primalstart, dualstart *sets.FloatMatrixSet) (sol *Solution, err error) {
if c == nil {
err = errors.New("'c' must a column matrix")
return
}
n := c.Rows()
if n < 1 {
err = errors.New("Number of variables must be at least 1")
return
}
if Gl == nil {
Gl = matrix.FloatZeros(0, n)
}
if Gl.Cols() != n {
err = errors.New(fmt.Sprintf("'G' must be matrix with %d columns", n))
return
}
ml := Gl.Rows()
if hl == nil {
hl = matrix.FloatZeros(0, 1)
}
if !hl.SizeMatch(ml, 1) {
err = errors.New(fmt.Sprintf("'hl' must be matrix of size (%d,1)", ml))
return
}
Gsset := Ghs.At("Gs")
ms := make([]int, 0)
for i, Gs := range Gsset {
if Gs.Cols() != n {
err = errors.New(fmt.Sprintf("'Gs' must be list of matrices with %d columns", n))
return
}
sz := int(math.Sqrt(float64(Gs.Rows())))
if Gs.Rows() != sz*sz {
err = errors.New(fmt.Sprintf("the squareroot of the number of rows of 'Gq[%d]' is not an integer", i))
return
}
ms = append(ms, sz)
}
hsset := Ghs.At("hs")
if len(Gsset) != len(hsset) {
err = errors.New(fmt.Sprintf("'hs' must be a list of %d matrices", len(Gsset)))
return
}
for i, hs := range hsset {
if !hs.SizeMatch(ms[i], ms[i]) {
s := fmt.Sprintf("hq[%d] has size (%d,%d). Expected size is (%d,%d)",
i, hs.Rows(), hs.Cols(), ms[i], ms[i])
err = errors.New(s)
return
}
}
if A == nil {
A = matrix.FloatZeros(0, n)
}
if A.Cols() != n {
err = errors.New(fmt.Sprintf("'A' must be matrix with %d columns", n))
return
}
p := A.Rows()
if b == nil {
b = matrix.FloatZeros(0, 1)
}
if !b.SizeMatch(p, 1) {
err = errors.New(fmt.Sprintf("'b' must be matrix of size (%d,1)", p))
return
}
dims := sets.NewDimensionSet("l", "q", "s")
dims.Set("l", []int{ml})
dims.Set("s", ms)
N := dims.Sum("l") + dims.SumSquared("s")
// Map hs matrices to h vector
h := matrix.FloatZeros(N, 1)
h.SetIndexesFromArray(hl.FloatArray()[:ml], matrix.MakeIndexSet(0, ml, 1)...)
ind := ml
for k, hs := range hsset {
h.SetIndexesFromArray(hs.FloatArray(), matrix.MakeIndexSet(ind, ind+ms[k]*ms[k], 1)...)
ind += ms[k] * ms[k]
//.........這裏部分代碼省略.........
示例2: 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
//.........這裏部分代碼省略.........
示例3: Socp
// Solves a pair of primal and dual SOCPs
//
// minimize c'*x
// subject to Gl*x + sl = hl
// Gq[k]*x + sq[k] = hq[k], k = 0, ..., N-1
// A*x = b
// sl >= 0,
// sq[k] >= 0, k = 0, ..., N-1
//
// maximize -hl'*z - sum_k hq[k]'*zq[k] - b'*y
// subject to Gl'*zl + sum_k Gq[k]'*zq[k] + A'*y + c = 0
// zl >= 0, zq[k] >= 0, k = 0, ..., N-1.
//
// The inequalities sl >= 0 and zl >= 0 are elementwise vector
// inequalities. The inequalities sq[k] >= 0, zq[k] >= 0 are second
// order cone inequalities, i.e., equivalent to
//
// sq[k][0] >= || sq[k][1:] ||_2, zq[k][0] >= || zq[k][1:] ||_2.
//
func Socp(c, Gl, hl, A, b *matrix.FloatMatrix, Ghq *sets.FloatMatrixSet, solopts *SolverOptions,
primalstart, dualstart *sets.FloatMatrixSet) (sol *Solution, err error) {
if c == nil {
err = errors.New("'c' must a column matrix")
return
}
n := c.Rows()
if n < 1 {
err = errors.New("Number of variables must be at least 1")
return
}
if Gl == nil {
Gl = matrix.FloatZeros(0, n)
}
if Gl.Cols() != n {
err = errors.New(fmt.Sprintf("'G' must be matrix with %d columns", n))
return
}
ml := Gl.Rows()
if hl == nil {
hl = matrix.FloatZeros(0, 1)
}
if !hl.SizeMatch(ml, 1) {
err = errors.New(fmt.Sprintf("'hl' must be matrix of size (%d,1)", ml))
return
}
Gqset := Ghq.At("Gq")
mq := make([]int, 0)
for i, Gq := range Gqset {
if Gq.Cols() != n {
err = errors.New(fmt.Sprintf("'Gq' must be list of matrices with %d columns", n))
return
}
if Gq.Rows() == 0 {
err = errors.New(fmt.Sprintf("the number of rows of 'Gq[%d]' is zero", i))
return
}
mq = append(mq, Gq.Rows())
}
hqset := Ghq.At("hq")
if len(Gqset) != len(hqset) {
err = errors.New(fmt.Sprintf("'hq' must be a list of %d matrices", len(Gqset)))
return
}
for i, hq := range hqset {
if !hq.SizeMatch(Gqset[i].Rows(), 1) {
s := fmt.Sprintf("hq[%d] has size (%d,%d). Expected size is (%d,1)",
i, hq.Rows(), hq.Cols(), Gqset[i].Rows())
err = errors.New(s)
return
}
}
if A == nil {
A = matrix.FloatZeros(0, n)
}
if A.Cols() != n {
err = errors.New(fmt.Sprintf("'A' must be matrix with %d columns", n))
return
}
p := A.Rows()
if b == nil {
b = matrix.FloatZeros(0, 1)
}
if !b.SizeMatch(p, 1) {
err = errors.New(fmt.Sprintf("'b' must be matrix of size (%d,1)", p))
return
}
dims := sets.NewDimensionSet("l", "q", "s")
dims.Set("l", []int{ml})
dims.Set("q", mq)
//N := dims.Sum("l", "q")
hargs := make([]*matrix.FloatMatrix, 0, len(hqset)+1)
hargs = append(hargs, hl)
hargs = append(hargs, hqset...)
h, indh := matrix.FloatMatrixStacked(matrix.StackDown, hargs...)
Gargs := make([]*matrix.FloatMatrix, 0, len(Gqset)+1)
Gargs = append(Gargs, Gl)
Gargs = append(Gargs, Gqset...)
G, indg := matrix.FloatMatrixStacked(matrix.StackDown, Gargs...)
//.........這裏部分代碼省略.........
示例4: conelp_solver
func conelp_solver(c MatrixVariable, G MatrixVarG, h *matrix.FloatMatrix,
A MatrixVarA, b MatrixVariable, dims *sets.DimensionSet, kktsolver KKTConeSolverVar,
solopts *SolverOptions, primalstart, dualstart *sets.FloatMatrixSet) (sol *Solution, err error) {
err = nil
const EXPON = 3
const 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 refinement int
if solopts.Refinement > 0 {
refinement = solopts.Refinement
} else {
refinement = 0
if len(dims.At("q")) > 0 || len(dims.At("s")) > 0 {
refinement = 1
}
}
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 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)
}
Gf := func(x, y MatrixVariable, alpha, beta float64, trans la.Option) error {
return G.Gf(x, y, alpha, beta, trans)
}
Af := func(x, y MatrixVariable, alpha, beta float64, trans la.Option) error {
return A.Af(x, y, alpha, beta, trans)
}
// kktsolver(W) returns a routine for solving 3x3 block KKT system
//
// [ 0 A' G'*W^{-1} ] [ ux ] [ bx ]
// [ A 0 0 ] [ uy ] = [ by ].
// [ G 0 -W' ] [ uz ] [ bz ]
if kktsolver == nil {
err = errors.New("nil kktsolver not allowed.")
return
}
// res() evaluates residual in 5x5 block KKT system
//
// [ vx ] [ 0 ] [ 0 A' G' c ] [ ux ]
// [ vy ] [ 0 ] [-A 0 0 b ] [ uy ]
// [ vz ] += [ W'*us ] - [-G 0 0 h ] [ W^{-1}*uz ]
// [ vtau ] [ dg*ukappa ] [-c' -b' -h' 0 ] [ utau/dg ]
//
// vs += lmbda o (dz + ds)
// vkappa += lmbdg * (dtau + dkappa).
ws3 := matrix.FloatZeros(cdim, 1)
wz3 := matrix.FloatZeros(cdim, 1)
checkpnt.AddMatrixVar("ws3", ws3)
checkpnt.AddMatrixVar("wz3", wz3)
//
//.........這裏部分代碼省略.........