Learn R Programming

lintools (version 0.1.7)

sparse_constraints: 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 data.frame sparse_constraints(object, b, neq = length(b), base = 1L, sorted = FALSE, ...)

# S3 method for sparse_constraints print(x, range = 1L:10L, ...)

Value

Object of class sparse_constraints (see details).

Arguments

object

R object to be translated to sparse_constraints format.

...

options to be passed to other methods

b

Constant vector

neq

The first new equations are interpreted as equality constraints, the rest as '<='

base

are the indices in object[,1:2] base 0 or base 1?

sorted

is object sorted by the first column?

x

an object of class sparse_constraints

range

integer vector stating which constraints to print

The <code>$project</code> 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 optimized

  • w: [numeric] the weight vector (of length(x)). By default all weights equal 1.

  • eps: [numeric] desired tolerance. By default \(10^{-2}\)

  • maxiter: [integer] maximum number of iterations. By default 1000.

The return value of $spa is the same as that of sparse_project.

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 when R's garbage collector cleans up a removed sparse_constraints object.

See Also

sparse_project, project

Examples

Run this code

# 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)


Run the code above in your browser using DataLab