- 3.0.2 optimal control module.
utilities.hpp
Go to the documentation of this file.
1 /**********************************************************************************************************************
2 This file is part of the Control Toolbox (https://github.com/ethz-adrl/control-toolbox), copyright by ETH Zurich.
3 Licensed under the BSD-2 license (see LICENSE file in main directory)
4 **********************************************************************************************************************/
5 
6 #pragma once
7 
8 #include <iostream>
9 
10 #include <boost/property_tree/ptree.hpp>
11 #include <boost/property_tree/info_parser.hpp>
12 
14 
15 #include "../CostFunction.hpp"
16 
17 namespace ct {
18 namespace optcon {
19 
20 template <typename SCALAR>
21 void loadScalarCF(const std::string& filename,
22  const std::string& scalarName,
23  SCALAR& scalar,
24  const std::string& termName = "")
25 {
26  boost::property_tree::ptree pt;
27  boost::property_tree::read_info(filename, pt);
28 
29  scalar = pt.get<SCALAR>(termName + ".weights." + scalarName);
30 }
31 
32 template <typename SCALAR>
33 void loadScalarOptionalCF(const std::string& filename,
34  const std::string& scalarName,
35  SCALAR& scalar,
36  const std::string& termName,
37  const SCALAR& defaultValue)
38 {
39  boost::property_tree::ptree pt;
40  boost::property_tree::read_info(filename, pt);
41 
42  scalar = pt.get<SCALAR>(termName + ".weights." + scalarName, defaultValue);
43 }
44 
45 template <typename SCALAR, int ROW, int COL>
46 void loadMatrixCF(const std::string& filename,
47  const std::string& matrixName,
48  Eigen::Matrix<SCALAR, ROW, COL>& matrix,
49  const std::string& termName = "")
50 {
51  size_t rows = matrix.rows();
52  size_t cols = matrix.cols();
53 
54  boost::property_tree::ptree pt;
55  boost::property_tree::read_info(filename, pt);
56 
57  double scaling = pt.get<double>(termName + ".weights." + matrixName + ".scaling", 1);
58 
59  for (size_t i = 0; i < rows; i++)
60  {
61  for (size_t j = 0; j < cols; j++)
62  {
63  if (termName == "")
64  {
65  matrix(i, j) =
66  scaling *
67  pt.get<double>(matrixName + "." + "(" + std::to_string(i) + "," + std::to_string(j) + ")", 0.0);
68  }
69  else
70  {
71  matrix(i, j) = scaling *
72  pt.get<double>(termName + ".weights." + matrixName + "." + "(" + std::to_string(i) +
73  "," + std::to_string(j) + ")",
74  0.0);
75  }
76  }
77  }
78 }
79 
80 template <typename TERM_PTR, typename costFuncType>
81 void addTerm(const std::string& filename,
82  std::string& currentTerm,
83  int currentTermType,
84  TERM_PTR term,
85  costFuncType* costFunc,
86  bool verbose = false)
87 {
88  switch (currentTermType)
89  {
90  case 0:
91  costFunc->addIntermediateTerm(term, verbose);
92  break;
93  case 1:
94  costFunc->addFinalTerm(term, verbose);
95  break;
96  default:
97  if (verbose)
98  {
99  std::cout << "error code 1 => term type other than term0 and term1 encountered" << std::endl;
100  }
101  BOOST_PROPERTY_TREE_THROW(boost::property_tree::info_parser::info_parser_error(
102  "read error code = ", "", 1)); //error code 1 => term type otherthan term0 and term1 encountered
103  break;
104  }
105  term->loadTimeActivation(filename, currentTerm, verbose);
106  term->loadConfigFile(filename, currentTerm, verbose);
107 
108  if (verbose)
109  std::cout << "Successfully loaded term " + currentTerm << std::endl;
110 }
111 
112 template <typename TERM_PTR, typename costFuncType>
113 void addADTerm(const std::string& filename,
114  std::string& currentTerm,
115  int currentTermType,
116  TERM_PTR term,
117  costFuncType* costFunc,
118  bool verbose = false)
119 {
120  switch (currentTermType)
121  {
122  case 0:
123  costFunc->addIntermediateADTerm(term, verbose);
124  break;
125  case 1:
126  costFunc->addFinalADTerm(term, verbose);
127  break;
128  default:
129  if (verbose)
130  {
131  std::cout << "error code 1 => term type other than term0 and term1 encountered" << std::endl;
132  }
133  BOOST_PROPERTY_TREE_THROW(boost::property_tree::info_parser::info_parser_error(
134  "read error code = ", "", 1)); //error code 1 => term type otherthan term0 and term1 encountered
135  break;
136  }
137  term->loadTimeActivation(filename, currentTerm, verbose);
138  term->loadConfigFile(filename, currentTerm, verbose);
139 
140  if (verbose)
141  std::cout << "Successfully loaded term " + currentTerm << std::endl;
142 }
143 
144 } // namespace optcon
145 } // namespace ct
CppAD::AD< CppAD::cg::CG< double > > SCALAR
void addADTerm(const std::string &filename, std::string &currentTerm, int currentTermType, TERM_PTR term, costFuncType *costFunc, bool verbose=false)
Definition: utilities.hpp:113
void addTerm(const std::string &filename, std::string &currentTerm, int currentTermType, TERM_PTR term, costFuncType *costFunc, bool verbose=false)
Definition: utilities.hpp:81
for i
Definition: mpc_unittest_plotting.m:14
void loadScalarCF(const std::string &filename, const std::string &scalarName, SCALAR &scalar, const std::string &termName="")
Definition: utilities.hpp:21
const bool verbose
Definition: ConstraintComparison.h:18
void loadMatrixCF(const std::string &filename, const std::string &matrixName, Eigen::Matrix< SCALAR, ROW, COL > &matrix, const std::string &termName="")
Definition: utilities.hpp:46
void loadScalarOptionalCF(const std::string &filename, const std::string &scalarName, SCALAR &scalar, const std::string &termName, const SCALAR &defaultValue)
Definition: utilities.hpp:33