本文整理匯總了Golang中github.com/hrautila/cvx/sets.FloatMatrixSet.At方法的典型用法代碼示例。如果您正苦於以下問題:Golang FloatMatrixSet.At方法的具體用法?Golang FloatMatrixSet.At怎麽用?Golang FloatMatrixSet.At使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/hrautila/cvx/sets.FloatMatrixSet
的用法示例。
在下文中一共展示了FloatMatrixSet.At方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: AddScaleVar
// Add or update scaling matrix set to checkpoint variables.
func AddScaleVar(w *sets.FloatMatrixSet) {
if !active {
return
}
// add all matrices of scale set to variable table
for _, key := range w.Keys() {
mset := w.At(key)
for k, m := range mset {
name := fmt.Sprintf("%s.%d", key, k)
variables[name] = &dataPoint{vvar: &mVariable{m}}
}
}
}
示例2: cpl_solver
//.........這裏部分代碼省略.........
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
}
x := x0.Copy()
y := b.Copy()
y.Scal(0.0)
z := matrix.FloatZeros(mnl+cdim, 1)
s := matrix.FloatZeros(mnl+cdim, 1)
ind := mnl + dims.At("l")[0]
z.SetIndexes(1.0, matrix.MakeIndexSet(0, ind, 1)...)
s.SetIndexes(1.0, matrix.MakeIndexSet(0, ind, 1)...)
for _, m := range dims.At("q") {
z.SetIndexes(1.0, ind)
s.SetIndexes(1.0, ind)
ind += m
}
for _, m := range dims.At("s") {
iset := matrix.MakeIndexSet(ind, ind+m*m, m+1)
z.SetIndexes(1.0, iset...)
s.SetIndexes(1.0, iset...)
ind += m * m
}
rx := x0.Copy()
ry := b.Copy()
dx := x.Copy()
dy := y.Copy()
rznl := matrix.FloatZeros(mnl, 1)
rzl := matrix.FloatZeros(cdim, 1)
dz := matrix.FloatZeros(mnl+cdim, 1)
ds := matrix.FloatZeros(mnl+cdim, 1)
lmbda := matrix.FloatZeros(mnl+cdim_diag, 1)
lmbdasq := matrix.FloatZeros(mnl+cdim_diag, 1)
sigs := matrix.FloatZeros(dims.Sum("s"), 1)
sigz := matrix.FloatZeros(dims.Sum("s"), 1)
dz2 := matrix.FloatZeros(mnl+cdim, 1)
ds2 := matrix.FloatZeros(mnl+cdim, 1)
newx := x.Copy()
newy := y.Copy()
示例3: 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]
//.........這裏部分代碼省略.........
示例4: 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...)
//.........這裏部分代碼省略.........
示例5: scale
/*
Applies Nesterov-Todd scaling or its inverse.
Computes
x := W*x (trans is false 'N', inverse = false 'N')
x := W^T*x (trans is true 'T', inverse = false 'N')
x := W^{-1}*x (trans is false 'N', inverse = true 'T')
x := W^{-T}*x (trans is true 'T', inverse = true 'T').
x is a dense float matrix.
W is a MatrixSet with entries:
- W['dnl']: positive vector
- W['dnli']: componentwise inverse of W['dnl']
- W['d']: positive vector
- W['di']: componentwise inverse of W['d']
- W['v']: lists of 2nd order cone vectors with unit hyperbolic norms
- W['beta']: list of positive numbers
- W['r']: list of square matrices
- W['rti']: list of square matrices. rti[k] is the inverse transpose
of r[k].
The 'dnl' and 'dnli' entries are optional, and only present when the
function is called from the nonlinear solver.
*/
func scale(x *matrix.FloatMatrix, W *sets.FloatMatrixSet, trans, inverse bool) (err error) {
/*DEBUGGED*/
var wl []*matrix.FloatMatrix
var w *matrix.FloatMatrix
ind := 0
err = nil
// var minor int = 0
//if ! checkpnt.MinorEmpty() {
// minor = checkpnt.MinorTop()
//}
//fmt.Printf("\n%d.%04d scaling x=\n%v\n", checkpnt.Major(), minor, x.ToString("%.17f"))
// Scaling for nonlinear component xk is xk := dnl .* xk; inverse
// scaling is xk ./ dnl = dnli .* xk, where dnl = W['dnl'],
// dnli = W['dnli'].
if wl = W.At("dnl"); wl != nil {
if inverse {
w = W.At("dnli")[0]
} else {
w = W.At("dnl")[0]
}
for k := 0; k < x.Cols(); k++ {
err = blas.TbmvFloat(w, x, &la_.IOpt{"n", w.Rows()}, &la_.IOpt{"k", 0},
&la_.IOpt{"lda", 1}, &la_.IOpt{"offsetx", k * x.Rows()})
if err != nil {
//fmt.Printf("1. TbmvFloat: %v\n", err)
return
}
}
ind += w.Rows()
}
//if ! checkpnt.MinorEmpty() {
// checkpnt.Check("000scale", minor)
//}
// Scaling for linear 'l' component xk is xk := d .* xk; inverse
// scaling is xk ./ d = di .* xk, where d = W['d'], di = W['di'].
if inverse {
w = W.At("di")[0]
} else {
w = W.At("d")[0]
}
for k := 0; k < x.Cols(); k++ {
err = blas.TbmvFloat(w, x, &la_.IOpt{"n", w.Rows()}, &la_.IOpt{"k", 0},
&la_.IOpt{"lda", 1}, &la_.IOpt{"offsetx", k*x.Rows() + ind})
if err != nil {
//fmt.Printf("2. TbmvFloat: %v\n", err)
return
}
}
ind += w.Rows()
//if ! checkpnt.MinorEmpty() {
// checkpnt.Check("010scale", minor)
//}
// Scaling for 'q' component is
//
// xk := beta * (2*v*v' - J) * xk
// = beta * (2*v*(xk'*v)' - J*xk)
//
// where beta = W['beta'][k], v = W['v'][k], J = [1, 0; 0, -I].
//
//Inverse scaling is
//
// xk := 1/beta * (2*J*v*v'*J - J) * xk
// = 1/beta * (-J) * (2*v*((-J*xk)'*v)' + xk).
//.........這裏部分代碼省略.........
示例6: updateScaling
func updateScaling(W *sets.FloatMatrixSet, lmbda, s, z *matrix.FloatMatrix) (err error) {
err = nil
var stmp, ztmp *matrix.FloatMatrix
/*
Nonlinear and 'l' blocks
d := d .* sqrt( s ./ z )
lmbda := lmbda .* sqrt(s) .* sqrt(z)
*/
mnl := 0
dnlset := W.At("dnl")
dnliset := W.At("dnli")
dset := W.At("d")
diset := W.At("di")
beta := W.At("beta")[0]
if dnlset != nil && dnlset[0].NumElements() > 0 {
mnl = dnlset[0].NumElements()
}
ml := dset[0].NumElements()
m := mnl + ml
//fmt.Printf("ml=%d, mnl=%d, m=%d'n", ml, mnl, m)
stmp = matrix.FloatVector(s.FloatArray()[:m])
stmp.Apply(math.Sqrt)
s.SetIndexesFromArray(stmp.FloatArray(), matrix.MakeIndexSet(0, m, 1)...)
ztmp = matrix.FloatVector(z.FloatArray()[:m])
ztmp.Apply(math.Sqrt)
z.SetIndexesFromArray(ztmp.FloatArray(), matrix.MakeIndexSet(0, m, 1)...)
// d := d .* s .* z
if len(dnlset) > 0 {
blas.TbmvFloat(s, dnlset[0], &la_.IOpt{"n", mnl}, &la_.IOpt{"k", 0}, &la_.IOpt{"lda", 1})
blas.TbsvFloat(z, dnlset[0], &la_.IOpt{"n", mnl}, &la_.IOpt{"k", 0}, &la_.IOpt{"lda", 1})
//dnliset[0].Apply(dnlset[0], func(a float64)float64 { return 1.0/a})
//--dnliset[0] = matrix.Inv(dnlset[0])
matrix.Set(dnliset[0], dnlset[0])
dnliset[0].Inv()
}
blas.TbmvFloat(s, dset[0], &la_.IOpt{"n", ml},
&la_.IOpt{"k", 0}, &la_.IOpt{"lda", 1}, &la_.IOpt{"offseta", mnl})
blas.TbsvFloat(z, dset[0], &la_.IOpt{"n", ml},
&la_.IOpt{"k", 0}, &la_.IOpt{"lda", 1}, &la_.IOpt{"offseta", mnl})
//diset[0].Apply(dset[0], func(a float64)float64 { return 1.0/a})
//--diset[0] = matrix.Inv(dset[0])
matrix.Set(diset[0], dset[0])
diset[0].Inv()
// lmbda := s .* z
blas.CopyFloat(s, lmbda, &la_.IOpt{"n", m})
blas.TbmvFloat(z, lmbda, &la_.IOpt{"n", m}, &la_.IOpt{"k", 0}, &la_.IOpt{"lda", 1})
// 'q' blocks.
// Let st and zt be the new variables in the old scaling:
//
// st = s_k, zt = z_k
//
// and a = sqrt(st' * J * st), b = sqrt(zt' * J * zt).
//
// 1. Compute the hyperbolic Householder transformation 2*q*q' - J
// that maps st/a to zt/b.
//
// c = sqrt( (1 + st'*zt/(a*b)) / 2 )
// q = (st/a + J*zt/b) / (2*c).
//
// The new scaling point is
//
// wk := betak * sqrt(a/b) * (2*v[k]*v[k]' - J) * q
//
// with betak = W['beta'][k].
//
// 3. The scaled variable:
//
// lambda_k0 = sqrt(a*b) * c
// lambda_k1 = sqrt(a*b) * ( (2vk*vk' - J) * (-d*q + u/2) )_1
//
// where
//
// u = st/a - J*zt/b
// d = ( vk0 * (vk'*u) + u0/2 ) / (2*vk0 *(vk'*q) - q0 + 1).
//
// 4. Update scaling
//
// v[k] := wk^1/2
// = 1 / sqrt(2*(wk0 + 1)) * (wk + e).
// beta[k] *= sqrt(a/b)
ind := m
for k, v := range W.At("v") {
m = v.NumElements()
// ln = sqrt( lambda_k' * J * lambda_k ) !! NOT USED!!
jnrm2(lmbda, m, ind) // ?? NOT USED ??
// a = sqrt( sk' * J * sk ) = sqrt( st' * J * st )
// s := s / a = st / a
aa := jnrm2(s, m, ind)
blas.ScalFloat(s, 1.0/aa, &la_.IOpt{"n", m}, &la_.IOpt{"offset", ind})
// b = sqrt( zk' * J * zk ) = sqrt( zt' * J * zt )
//.........這裏部分代碼省略.........
示例7: 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)
//
//.........這裏部分代碼省略.........