linLsqLinConstraints

Solves a linear least-squares problem with linear constraints.

Synopsis

linLsqLinConstraints (a,  b,  c,  bl,  bu,  conType,  xlb,  xub)

Required Arguments

float a[[]] (Input)
Array of size nra × nca containing the coefficients of the nra least-squares equations.
float b[] (Input)
Array of length nra containing the right-hand sides of the least-squares equations.
float c[[]] (Input)
Array of size ncon × nca containing the coefficients of the ncon constraints.
float bl[] (Input)
Array of length ncon containing the lower limit of the general constraints. If there is no lower limit on the i‑th constraint, then bl[i] will not be referenced.
float bu[] (Input)
Array of length ncon containing the upper limit of the general constraints. If there is no upper limit on the i‑th constraint, then bu[i] will not be referenced. If there is no range constraint, bl and bu can share the same storage.
int conType[] (Input)
Array of length ncon indicating the type of constraints exclusive of simple bounds, where conType[i] = 0, 1, 2, 3 indicates =, <=, >= and range constraints, respectively.
float xlb[] (Input)
Array of length nca containing the lower bound on the variables. If there is no lower bound on the i‑th variable, then xlb[i] should be set to 1.0e30.
float xub[] (Input)
Array of length nca containing the upper bound on the variables. If there is no lower bound on the i‑th variable, then xub[i] should be set to −1.0e30.

Return Value

A vector of length nca containing the approximate solution. If no solution was computed, then None is returned.

Optional Arguments

residual, float residual (Output)
An array containing the residuals b − Ax of the least-squares equations at the approximate solution.
t_print,
Debug output flag. Choose this option if more detailed output is desired.
maxIter (Input)

Set the maximum number of add/drop iterations.

Default: maxIter = 5*max(nra, nca)

relFcnTol, float (Input)

Relative rank determination tolerance to be used.

Default: relFcnTol = sqrt(machine(4))

absFcnTol, float (Input)

Absolute rank determination tolerance to be used.

Default: absFcnTol = sqrt(machine(4))

Description

The function linLsqLinConstraints solves linear least-squares problems with linear constraints. These are systems of least-squares equations of the form

\[Ax ≅ b\]

subject to

\[b_l ≤Cx ≤b_u\]
\[x_l ≤x ≤ x_u\]

Here A is the coefficient matrix of the least-squares equations, b is the right-hand side, and C is the coefficient matrix of the constraints. The vectors \(b_l\), \(b_u\), \(x_l\) and \(x_u\) are the lower and upper bounds on the constraints and the variables, respectively. The system is solved by defining dependent variables y ≡ Cx and then solving the least-squares system with the lower and upper bounds on x and y. The equation \(Cx − y = 0\) is a set of equality constraints. These constraints are realized by heavy weighting, i.e., a penalty method, Hanson (1986, pp. 826-834).

Examples

Example 1

In this example, the following problem is solved in the least-squares sense:

\[3x_1 + 2x_2 + x_3 = 3.3\]
\[4x_1 +2x_2 + x_3 = 2.2\]
\[2x_1 + 2x_2 + x_3 = 1.3\]
\[x_1 + x_2 + x_3 = 1.0\]

Subject to

\[x_1 = x_2 + x_3 ≤ 1\]
\[0 ≤x_1 ≤ 0.5\]
\[0 ≤ x_2 ≤ 0.5\]
\[0 ≤ x_3 ≤ 0.5\]
from numpy import *
from pyimsl.math.linLsqLinConstraints import linLsqLinConstraints
from pyimsl.math.vectorNorm import vectorNorm
from pyimsl.math.writeMatrix import writeMatrix

a = array([[3.0, 2.0, 1.0],
           [4.0, 2.0, 1.0],
           [2.0, 2.0, 1.0],
           [1.0, 1.0, 1.0]])
b = array([3.3, 2.3, 1.3, 1.0])
c = array([[1.0, 1.0, 1.0]])
xlb = array([0.0, 0.0, 0.0])
xub = array([0.5, 0.5, 0.5])
con_type = array([1])
bc = array([1.0])
residual = []
x = linLsqLinConstraints(a, b, c,
                         bc, bc, con_type, xlb, xub)
writeMatrix("Solution", x)

Output

 
              Solution
          1            2            3
        0.5          0.3          0.2

Example 2

The same problem solved in the first example is solved again. This time residuals of the least-squares equations at the approximate solution are returned, and the norm of the residual vector is printed. Both the solution and residuals are returned in user-supplied space.

from __future__ import print_function
from numpy import *
from pyimsl.math.linLsqLinConstraints import linLsqLinConstraints
from pyimsl.math.vectorNorm import vectorNorm
from pyimsl.math.writeMatrix import writeMatrix

a = array([[3.0, 2.0, 1.0],
           [4.0, 2.0, 1.0],
           [2.0, 2.0, 1.0],
           [1.0, 1.0, 1.0]])
b = array([3.3, 2.3, 1.3, 1.0])
c = array([[1.0, 1.0, 1.0]])
xlb = array([0.0, 0.0, 0.0])
xub = array([0.5, 0.5, 0.5])
con_type = array([1])
bc = array([1.0])
residual = []
x = linLsqLinConstraints(a, b, c,
                         bc, bc, con_type, xlb, xub,
                         residual=residual)
writeMatrix("Solution", x)
writeMatrix("Residual", residual)
print("\nNorm of residual = %f" % (vectorNorm(residual)))

Output

Norm of residual = 1.224745
 
              Solution
          1            2            3
        0.5          0.3          0.2
 
                     Residual
          1            2            3            4
       -1.0          0.5          0.5          0.0

Fatal Errors

IMSL_BAD_COLUMN_ORDER The input order of columns must be between 1 and “nvar” while input order = # and “nvar” = # are given.
IMSL_BAD_POLARITY_FLAGS The bound polarity flags must be positive while component # flag “ibb[#]”.
IMSL_TOO_MANY_ITN Maximum numbers of iterations exceeded.