sparse_constraints {lintools} | R Documentation |
Generate sparse set of constraints.
Description
Generate a constraint set to be used by sparse_project
Usage
sparse_constraints(object, ...)
sparseConstraints(object, ...)
## S3 method for class 'data.frame'
sparse_constraints(object, b, neq = length(b), base = 1L, sorted = FALSE, ...)
## S3 method for class 'sparse_constraints'
print(x, range = 1L:10L, ...)
Arguments
object |
R object to be translated to sparse_constraints format. |
... |
options to be passed to other methods |
b |
Constant vector |
neq |
The first |
base |
are the indices in |
sorted |
is |
x |
an object of class |
range |
integer vector stating which constraints to print |
Value
Object of class sparse_constraints
(see details).
Note
As of version 0.1.1.0, sparseConstraints
is deprecated. Use sparse_constraints
instead.
Details
The sparse_constraints
objects holds coefficients of
\boldsymbol{A}
and \boldsymbol{b}
of the system
\boldsymbol{Ax}\leq \boldsymbol{b}
in sparse format, outside of
R
's memory. It can be reused to find solutions for vectors to adjust.
In R
, it is a reference object. In particular, it is meaningless to
Copy the object. You only will only generate a pointer to physically the same object.
Save the object. The physical object is destroyed when
R
closes, or whenR
's garbage collector cleans up a removedsparse_constraints
object.
The $project
method
Once a sparse_constraints
object sc
is created, you can reuse it to optimize
several vectors by calling sc$project()
with the following parameters:
x
:[numeric]
the vector to be optimizedw
:[numeric]
the weight vector (oflength(x)
). By default all weights equal 1.eps
:[numeric]
desired tolerance. By default10^{-2}
maxiter
:[integer]
maximum number of iterations. By default 1000.
The return value of $spa
is the same as that of sparse_project
.
See Also
Examples
# The following system of constraints, stored in
# row-column-coefficient format
#
# x1 + x8 == 950,
# x3 + x4 == 950 ,
# x6 + x7 == x8,
# x4 > 0
#
A <- data.frame(
row = c( 1, 1, 2, 2, 3, 3, 3, 4)
, col = c( 1, 2, 3, 4, 2, 5, 6, 4)
, coef = c(-1,-1,-1,-1, 1,-1,-1,-1)
)
b <- c(-950, -950, 0,0)
sc <- sparse_constraints(A, b, neq=3)
# Adjust the 0-vector minimally so all constraints are met:
sc$project(x=rep(0,8))
# Use the same object to adjust the 100*1-vector
sc$project(x=rep(100,8))
# use the same object to adjust the 0-vector, but with different weights
sc$project(x=rep(0,8),w=1:8)