CppADCodeGen  HEAD
A C++ Algorithmic Differentiation Package with Source Code Generation
augment_path_depth_lookahead.hpp
1 #ifndef CPPAD_CG_AUGMENTPATHDEPTHLOOKAHEAD_INCLUDED
2 #define CPPAD_CG_AUGMENTPATHDEPTHLOOKAHEAD_INCLUDED
3 /* --------------------------------------------------------------------------
4  * CppADCodeGen: C++ Algorithmic Differentiation with Source Code Generation:
5  * Copyright (C) 2016 Ciengis
6  * Copyright (C) 2018 Joao Leal
7  *
8  * CppADCodeGen is distributed under multiple licenses:
9  *
10  * - Eclipse Public License Version 1.0 (EPL1), and
11  * - GNU General Public License Version 3 (GPL3).
12  *
13  * EPL1 terms and conditions can be found in the file "epl-v10.txt", while
14  * terms and conditions for the GPL3 can be found in the file "gpl3.txt".
15  * ----------------------------------------------------------------------------
16  * Author: Joao Leal
17  */
18 
19 #include <cppad/cg/dae_index_reduction/augment_path.hpp>
20 
21 namespace CppAD {
22 namespace cg {
23 
30 template<class Base>
31 class AugmentPathDepthLookahead : public AugmentPath<Base> {
32 protected:
34  using ADCG = CppAD::AD<CGBase>;
35 public:
36 
37  bool augmentPath(Enode<Base>& i) override final {
38  i.color(this->logger_->log(), this->logger_->getVerbosity()); // avoids infinite recursion
39 
40  const std::vector<Vnode<Base>*>& vars = i.variables();
41 
42  // first look for derivative variables
43  for (Vnode<Base>* jj : vars) {
44  if (jj->antiDerivative() != nullptr && // not an algebraic variable
45  jj->assignmentEquation() == nullptr) { // not assigned yet
46 
47  jj->setAssignmentEquation(i, this->logger_->log(), this->logger_->getVerbosity());
48  return true;
49  }
50  }
51 
52  // look for algebraic variables
53  for (Vnode<Base>* jj : vars) {
54  if (jj->antiDerivative() == nullptr &&
55  jj->assignmentEquation() == nullptr) { // not assigned yet
56 
57  jj->setAssignmentEquation(i, this->logger_->log(), this->logger_->getVerbosity());
58  return true;
59  }
60  }
61 
62 
63  for (Vnode<Base>* jj : vars) {
64  if (!jj->isColored()) {
65  jj->color(this->logger_->log(), this->logger_->getVerbosity());
66 
67  Enode<Base>& k = *jj->assignmentEquation(); // all variables are assigned to another equation
68  if(!k.isColored()) {
69  bool pathFound = augmentPath(k);
70  if (pathFound) {
71  jj->setAssignmentEquation(i, this->logger_->log(), this->logger_->getVerbosity());
72  return true;
73  }
74  }
75  }
76  }
77 
78  return false;
79  }
80 
81 };
82 
83 } // END cg namespace
84 } // END CppAD namespace
85 
86 #endif
bool augmentPath(Enode< Base > &i) override final