pinocchio  3.2.0
A fast and flexible implementation of Rigid Body Dynamics algorithms and their analytical derivatives
factor.py
1 from pinocchio.utils import zero, eye
2 import numpy as np
3 import numpy.linalg as npl
4 
5 """
6 This file implements a sparse linear problem (quadric cost, linear constraints -- LCQP)
7 where the decision variables are denoted by x=(x1 ... xn), n being the number of factors.
8 The problem can be written:
9  min Sum_i=1^p || A_i x - b_i ||^2
10 x1...xn
11 
12 so that forall j=1:q C_j x = d_i
13 
14 Matrices A_i and C_j are block sparse, i.e. they are acting only on some (few) of the variables
15 x1 .. xn.
16 
17 The file implements the main class FactorGraph, which stores the LCQP problem and solve it.
18 It also provides a secondary class Factor, used to set up FactorGraph
19 """
20 
21 
22 class Factor(object):
23  """
24  A factor is a part of a linear constraint corresponding either a cost ||A x - b|| or
25  a constraint Cx = d.
26  In both cases, we have Ax = sum A_i x_i, where some A_i are null. One object of class
27  Factor stores one of the A_i, along with the correspond <i> index. It is simply a pair
28  (index, matrix).
29 
30  This class is used as a arguments of some of the setup functions of FactorGraph.
31  """
32 
33  def __init__(self, index, matrix):
34  self.indexindex = index
35  self.matrixmatrix = matrix
36 
37 
38 class FactorGraph(object):
39  """
40  The class FactorGraph stores a block-sparse linear-constrained quadratic program (LCQP)
41  of variable x=(x1...xn). The size of the problem is set up at construction of the object.
42  Methods add_factor() and add_factor_constraint() are used to set up the problem.
43  Method solve() is used to compute the solution to the problem.
44  """
45 
46  def __init__(self, variableSize, nbVariables):
47  """
48  Initialize a QP sparse problem as min || A x - b || so that C x = d
49  where x = (x1, .., xn), and dim(xi) = variableSize and n = nbVariables
50  After construction, A, b, C and d are allocated and set to 0.
51  """
52  self.nxnx = variableSize
53  self.NN = nbVariables
54  self.AA = zero([0, self.NN * self.nxnx])
55  self.bb = zero(0)
56  self.CC = zero([0, self.NN * self.nxnx])
57  self.dd = zero(0)
58 
59  def matrix_form_factor(self, factors):
60  """
61  Internal function: not designed to be called by the user.
62  Create a factor matrix [ A1 0 A2 0 A3 ... ] where the Ai's are placed at
63  the indexes of the factors.
64  """
65  assert len(factors) > 0
66  nr = factors[0].matrix.shape[0] # nb rows of the factor
67  nc = self.nxnx * self.NN # nb cols
68 
69  # Define and fill the new rows to be added
70  A = zero([nr, nc]) # new factor to be added to self.A
71  for factor in factors:
72  assert factor.matrix.shape == (nr, self.nxnx)
73  A[:, self.nxnx * factor.index : self.nxnx * (factor.index + 1)] = factor.matrix
74  return A
75 
76  def add_factor(self, factors, reference):
77  """
78  Add a factor || sum_{i} factor[i].matrix * x_{factor[i].index} - reference ||
79  to the cost.
80  """
81  # Add the new rows to the cost matrix.
82  self.AA = np.vstack([self.AA, self.matrix_form_factormatrix_form_factor(factors)])
83  self.bb = np.vstack([self.bb, reference])
84 
85  def add_factor_constraint(self, factors, reference):
86  """
87  Add a factor sum_{i} factor[i].matrix * x_{factor[i].index} = reference
88  to the constraints.
89  """
90  # Add the new rows to the cost matrix.
91  self.CC = np.vstack([self.CC, self.matrix_form_factormatrix_form_factor(factors)])
92  self.dd = np.vstack([self.dd, reference])
93 
94  def solve(self, eps=1e-8):
95  """
96  Implement a LCQP solver, with numerical threshold eps.
97  """
98  Cp = npl.pinv(self.CC, eps)
99  xopt = Cp * self.dd
100  P = eye(self.nxnx * self.NN) - Cp * self.CC
101  xopt += npl.pinv(self.AA * P, eps) * (self.bb - self.AA * xopt)
102  return xopt
def solve(self, eps=1e-8)
Definition: factor.py:94
def add_factor_constraint(self, factors, reference)
Definition: factor.py:85
def matrix_form_factor(self, factors)
Definition: factor.py:59
def __init__(self, variableSize, nbVariables)
Definition: factor.py:46
def add_factor(self, factors, reference)
Definition: factor.py:76