CppADCodeGen  HEAD
A C++ Algorithmic Differentiation Package with Source Code Generation
hessian_with_loops_info.hpp
1 #ifndef CPPAD_CG_HESSIAN_WITH_LOOPS_INFO_INCLUDED
2 #define CPPAD_CG_HESSIAN_WITH_LOOPS_INFO_INCLUDED
3 /* --------------------------------------------------------------------------
4  * CppADCodeGen: C++ Algorithmic Differentiation with Source Code Generation:
5  * Copyright (C) 2013 Ciengis
6  *
7  * CppADCodeGen is distributed under multiple licenses:
8  *
9  * - Eclipse Public License Version 1.0 (EPL1), and
10  * - GNU General Public License Version 3 (GPL3).
11  *
12  * EPL1 terms and conditions can be found in the file "epl-v10.txt", while
13  * terms and conditions for the GPL3 can be found in the file "gpl3.txt".
14  * ----------------------------------------------------------------------------
15  * Author: Joao Leal
16  */
17 
18 namespace CppAD {
19 namespace cg {
20 
21 namespace loops {
22 
23 template<class Base>
24 class HessianWithLoopsEquationGroupInfo {
25 public:
26  std::vector<std::set<size_t> > evalHessSparsity;
27  // (tapeJ1, tapeJ2) -> [positions]
28  std::map<pairss, std::vector<HessianElement> > indexedIndexedPositions;
29  // (tapeJ1, tapeJ2(j2)) -> [positions]
30  std::map<pairss, std::vector<HessianElement> > indexedNonIndexedPositions;
31  // (tapeJ1, j2) -> [positions]
32  std::map<pairss, std::vector<HessianElement> > indexedTempPositions;
33  // (tapeJ1(j1), tapeJ2) -> [positions]
34  std::map<pairss, std::vector<HessianElement> > nonIndexedIndexedPositions;
35  // (j1, tapeJ2) -> [positions]
36  std::map<pairss, std::vector<HessianElement> > tempIndexedPositions;
37  // (tapeJ1, j2) -> [k]
38  std::map<pairss, std::set<size_t> > indexedTempEvals;
39  // [(j1, j2)]
40  std::set<pairss> nonIndexedNonIndexedEvals;
41  // (j2, j1) -> [k]
42  std::map<pairss, std::set<size_t> > nonIndexedTempEvals;
43  // (j1, j2) -> [k1]
44  std::map<pairss, std::set<size_t> > tempNonIndexedEvals;
45  // (j1, j2) -> k1 -> [k2]
46  std::map<pairss, std::map<size_t, std::set<size_t> > > tempTempEvals;
50  std::map<size_t, std::map<size_t, CG<Base> > > hess;
51 
53  }
54 
56  evalHessSparsity(loop.getTapeIndependentCount()) {
57 
58  }
59 };
60 
61 template<class Base>
63 public:
64  LoopModel<Base>* model;
65  //
66  std::vector<std::set<size_t> > evalJacSparsity;
67  //
68  std::vector<HessianWithLoopsEquationGroupInfo<Base> > equationGroups;
69  //(j1, j2) -> position
70  std::map<pairss, size_t> nonIndexedNonIndexedPosition;
71  // (j1 ,j2) -> [k1]
72  std::map<pairss, std::set<size_t> > nonLoopNonIndexedNonIndexed;
73 
76  IndexOperationNode<Base>* iterationIndexOp;
77  std::vector<CG<Base> > x; // loop independent variables
78  std::vector<CG<Base> > w;
82  std::vector<std::map<size_t, CG<Base> > > dyiDzk;
83  std::vector<std::set<size_t> > noLoopEvalHessTempsSparsity;
84  std::map<size_t, std::map<size_t, CG<Base> > > dzDxx;
85 
86  // if-else branches
87  std::vector<IfElseInfo<Base> > ifElses;
88 
89  inline HessianWithLoopsInfo() :
90  model(nullptr),
91  loopStart(nullptr),
92  loopEnd(nullptr),
93  iterationIndexOp(nullptr) {
94 
95  }
96 
98  model(&loop),
99  evalJacSparsity(loop.getTapeDependentCount()),
100  equationGroups(loop.getEquationsGroups().size(), HessianWithLoopsEquationGroupInfo<Base>(loop)),
101  loopStart(nullptr),
102  loopEnd(nullptr),
103  iterationIndexOp(nullptr) {
104 
105  }
106 
113  inline void evalLoopModelJacobianHessian(bool individualColoring) {
114  using std::vector;
115 
116  ADFun<CG<Base> >& fun = model->getTape();
117  const std::vector<IterEquationGroup<Base> >& eqGroups = model->getEquationsGroups();
118 
119  vector<vector<CG<Base> > > vw(1);
120  vw[0].resize(w.size());
121 
122  vector<CG<Base> > y;
123 
124  size_t nEqGroups = equationGroups.size();
125 
126  for (size_t g = 0; g < nEqGroups; g++) {
127  const IterEquationGroup<Base>& group = eqGroups[g];
128 
130 
131  for (size_t i = 0; i < w.size(); i++) {
132  vw[0][i] = Base(0);
133  }
134 
135  for (size_t itI : group.tapeI) {
136  vw[0][itI] = w[itI];
137  }
138 
139  generateLoopForJacHes(fun, x, vw, y,
140  model->getJacobianSparsity(),
141  evalJacSparsity,
142  dyiDzk,
143  model->getHessianSparsity(),
144  equationGroups[g].evalHessSparsity,
145  vhess,
146  individualColoring);
147 
148  //Hessian
149  equationGroups[g].hess = vhess[0];
150  }
151  }
152 
153 };
154 
155 } // END loops namespace
156 
157 } // END cg namespace
158 } // END CppAD namespace
159 
160 #endif
void evalLoopModelJacobianHessian(bool individualColoring)
std::map< size_t, std::map< size_t, CG< Base > > > hess
size_t getTapeDependentCount() const
Definition: loop_model.hpp:274
std::vector< std::map< size_t, CG< Base > > > dyiDzk
ADFun< CGB > & getTape() const
Definition: loop_model.hpp:263
const std::vector< IterEquationGroup< Base > > & getEquationsGroups() const
Definition: loop_model.hpp:298
std::set< size_t > tapeI
equations indexes in tape of the loop model
size_t getTapeIndependentCount() const
Definition: loop_model.hpp:284