13 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
14 CostFunctionAD<STATE_DIM, CONTROL_DIM, SCALAR>::CostFunctionAD()
15 : CostFunctionQuadratic<STATE_DIM, CONTROL_DIM,
SCALAR>(),
16 stateControlTime_(
Eigen::Matrix<
SCALAR, STATE_DIM + CONTROL_DIM + 1, 1>::Zero())
18 intermediateFun_ = [&](
const Eigen::Matrix<CGScalar, STATE_DIM + CONTROL_DIM + 1, 1>& stateInputTime) {
19 return this->evaluateIntermediateCg(stateInputTime);
22 finalFun_ = [&](
const Eigen::Matrix<CGScalar, STATE_DIM + CONTROL_DIM + 1, 1>& stateInputTime) {
23 return this->evaluateTerminalCg(stateInputTime);
26 intermediateCostCodegen_ = std::shared_ptr<JacCG>(
new JacCG(intermediateFun_, STATE_DIM + CONTROL_DIM + 1, 1));
27 finalCostCodegen_ = std::shared_ptr<JacCG>(
new JacCG(finalFun_, STATE_DIM + CONTROL_DIM + 1, 1));
32 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
33 CostFunctionAD<STATE_DIM, CONTROL_DIM, SCALAR>::CostFunctionAD(
const CostFunctionAD& arg)
34 : CostFunctionQuadratic<STATE_DIM, CONTROL_DIM,
SCALAR>(arg),
35 stateControlTime_(arg.stateControlTime_),
36 intermediateFun_(arg.intermediateFun_),
37 finalFun_(arg.finalFun_)
39 intermediateTerms_.resize(arg.intermediateTerms_.size());
40 finalTerms_.resize(arg.finalTerms_.size());
42 for (
size_t i = 0;
i < intermediateTerms_.size(); ++
i)
43 intermediateTerms_[
i] =
44 std::shared_ptr<TermBase<STATE_DIM, CONTROL_DIM, SCALAR, CGScalar>>(arg.intermediateTerms_[
i]->clone());
46 for (
size_t i = 0;
i < finalTerms_.size(); ++
i)
48 std::shared_ptr<TermBase<STATE_DIM, CONTROL_DIM, SCALAR, CGScalar>>(arg.finalTerms_[
i]->clone());
50 intermediateCostCodegen_ = std::shared_ptr<JacCG>(arg.intermediateCostCodegen_->clone());
51 finalCostCodegen_ = std::shared_ptr<JacCG>(arg.finalCostCodegen_->clone());
54 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
55 CostFunctionAD<STATE_DIM, CONTROL_DIM, SCALAR>::CostFunctionAD(
const std::string& filename,
bool verbose)
58 loadFromConfigFile(filename, verbose);
61 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
62 CostFunctionAD<STATE_DIM, CONTROL_DIM, SCALAR>::~CostFunctionAD()
66 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
67 CostFunctionAD<STATE_DIM, CONTROL_DIM, SCALAR>* CostFunctionAD<STATE_DIM, CONTROL_DIM, SCALAR>::clone()
const 69 return new CostFunctionAD(*
this);
72 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
73 void CostFunctionAD<STATE_DIM, CONTROL_DIM, SCALAR>::initialize()
75 intermediateFun_ = [&](
const Eigen::Matrix<CGScalar, STATE_DIM + CONTROL_DIM + 1, 1>& stateInputTime) {
76 return this->evaluateIntermediateCg(stateInputTime);
79 finalFun_ = [&](
const Eigen::Matrix<CGScalar, STATE_DIM + CONTROL_DIM + 1, 1>& stateInputTime) {
80 return this->evaluateTerminalCg(stateInputTime);
83 intermediateCostCodegen_->update(intermediateFun_, STATE_DIM + CONTROL_DIM + 1, 1);
84 finalCostCodegen_->update(finalFun_, STATE_DIM + CONTROL_DIM + 1, 1);
92 finalCostCodegen_->compileJIT(settings,
"finalCosts");
93 intermediateCostCodegen_->compileJIT(settings,
"intermediateCosts");
96 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
97 void CostFunctionAD<STATE_DIM, CONTROL_DIM, SCALAR>::addIntermediateADTerm(
98 std::shared_ptr<TermBase<STATE_DIM, CONTROL_DIM, SCALAR, CGScalar>> term,
101 intermediateTerms_.push_back(term);
105 std::cout << term->getName() +
" added as intermediate AD term" << std::endl;
109 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
110 void CostFunctionAD<STATE_DIM, CONTROL_DIM, SCALAR>::addFinalADTerm(
111 std::shared_ptr<TermBase<STATE_DIM, CONTROL_DIM, SCALAR, CGScalar>> term,
114 finalTerms_.push_back(term);
118 std::cout << term->getName() +
" added as final AD term" << std::endl;
123 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
124 void CostFunctionAD<STATE_DIM, CONTROL_DIM, SCALAR>::setCurrentStateAndControl(
const state_vector_t&
x,
132 stateControlTime_ <<
x,
u,
t;
135 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
136 void CostFunctionAD<STATE_DIM, CONTROL_DIM, SCALAR>::loadFromConfigFile(
const std::string& filename,
bool verbose)
138 this->intermediateCostAnalytical_.clear();
139 this->finalCostAnalytical_.clear();
141 boost::property_tree::ptree pt;
142 boost::property_tree::read_info(filename, pt);
144 std::string currentTerm;
147 currentTerm =
"term" + std::to_string(i);
148 std::string termKind = pt.get<std::string>(currentTerm +
".kind");
149 boost::algorithm::to_lower(termKind);
150 int currentTermType = pt.get<
int>(currentTerm +
".type");
151 std::string termName;
154 termName = pt.get<std::string>(currentTerm +
".name");
155 }
catch (boost::property_tree::ptree_bad_path err)
157 termName =
"Unnamed";
160 std::cout <<
"Name field for " + currentTerm +
" does not exist" << std::endl;
164 std::shared_ptr<TermBase<STATE_DIM, CONTROL_DIM, SCALAR, CGScalar>> term;
170 throw std::runtime_error(
"Term type \"" + termKind +
"\" not supported");
175 addADTerm(filename, currentTerm, currentTermType, term,
this, verbose);
177 throw std::runtime_error(
"Term type \"" + termKind +
"\" loaded but unsupported.");
179 currentTerm =
"term" + std::to_string(++i);
180 }
while (pt.find(currentTerm) != pt.not_found());
183 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
184 typename CostFunctionAD<STATE_DIM, CONTROL_DIM, SCALAR>::MatrixCg
185 CostFunctionAD<STATE_DIM, CONTROL_DIM, SCALAR>::evaluateIntermediateCg(
186 const Eigen::Matrix<CGScalar, STATE_DIM + CONTROL_DIM + 1, 1>& stateInputTime)
188 CGScalar y = CGScalar(0.0);
190 for (
auto it : intermediateTerms_)
191 y += it->evaluateCppadCg(stateInputTime.segment(0, STATE_DIM), stateInputTime.segment(STATE_DIM, CONTROL_DIM),
192 stateInputTime(STATE_DIM + CONTROL_DIM));
194 Eigen::Matrix<CGScalar, 1, 1> out;
199 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
200 typename CostFunctionAD<STATE_DIM, CONTROL_DIM, SCALAR>::MatrixCg
201 CostFunctionAD<STATE_DIM, CONTROL_DIM, SCALAR>::evaluateTerminalCg(
202 const Eigen::Matrix<CGScalar, STATE_DIM + CONTROL_DIM + 1, 1>& stateInputTime)
204 CGScalar y = CGScalar(0.0);
206 for (
auto it : finalTerms_)
207 y += it->evaluateCppadCg(stateInputTime.segment(0, STATE_DIM), stateInputTime.segment(STATE_DIM, CONTROL_DIM),
208 stateInputTime(STATE_DIM + CONTROL_DIM));
210 Eigen::Matrix<CGScalar, 1, 1> out;
215 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
216 SCALAR CostFunctionAD<STATE_DIM, CONTROL_DIM, SCALAR>::evaluateIntermediate()
218 return this->evaluateIntermediateBase() + intermediateCostCodegen_->forwardZero(stateControlTime_)(0);
221 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
222 SCALAR CostFunctionAD<STATE_DIM, CONTROL_DIM, SCALAR>::evaluateTerminal()
224 return this->evaluateTerminalBase() + finalCostCodegen_->forwardZero(stateControlTime_)(0);
227 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
228 typename CostFunctionAD<STATE_DIM, CONTROL_DIM, SCALAR>::state_vector_t
229 CostFunctionAD<STATE_DIM, CONTROL_DIM, SCALAR>::stateDerivativeIntermediate()
231 Eigen::Matrix<SCALAR, 1, STATE_DIM + CONTROL_DIM + 1> jacTot =
232 intermediateCostCodegen_->jacobian(stateControlTime_);
233 return jacTot.template leftCols<STATE_DIM>().transpose() + this->stateDerivativeIntermediateBase();
236 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
237 typename CostFunctionAD<STATE_DIM, CONTROL_DIM, SCALAR>::state_vector_t
238 CostFunctionAD<STATE_DIM, CONTROL_DIM, SCALAR>::stateDerivativeTerminal()
240 Eigen::Matrix<SCALAR, 1, STATE_DIM + CONTROL_DIM + 1> jacTot = finalCostCodegen_->jacobian(stateControlTime_);
241 return jacTot.template leftCols<STATE_DIM>().transpose() + this->stateDerivativeTerminalBase();
244 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
245 typename CostFunctionAD<STATE_DIM, CONTROL_DIM, SCALAR>::control_vector_t
246 CostFunctionAD<STATE_DIM, CONTROL_DIM, SCALAR>::controlDerivativeIntermediate()
248 Eigen::Matrix<SCALAR, 1, STATE_DIM + CONTROL_DIM + 1> jacTot =
249 intermediateCostCodegen_->jacobian(stateControlTime_);
250 return jacTot.template block<1, CONTROL_DIM>(0, STATE_DIM).transpose() + this->controlDerivativeIntermediateBase();
253 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
254 typename CostFunctionAD<STATE_DIM, CONTROL_DIM, SCALAR>::control_vector_t
255 CostFunctionAD<STATE_DIM, CONTROL_DIM, SCALAR>::controlDerivativeTerminal()
257 Eigen::Matrix<SCALAR, 1, STATE_DIM + CONTROL_DIM + 1> jacTot = finalCostCodegen_->jacobian(stateControlTime_);
258 return jacTot.template block<1, CONTROL_DIM>(0, STATE_DIM).transpose() + this->controlDerivativeTerminalBase();
261 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
262 typename CostFunctionAD<STATE_DIM, CONTROL_DIM, SCALAR>::state_matrix_t
263 CostFunctionAD<STATE_DIM, CONTROL_DIM, SCALAR>::stateSecondDerivativeIntermediate()
265 Eigen::Matrix<SCALAR, 1, 1> w;
267 MatrixXs hesTot = intermediateCostCodegen_->hessian(stateControlTime_, w);
268 return hesTot.template block<STATE_DIM, STATE_DIM>(0, 0) + this->stateSecondDerivativeIntermediateBase();
271 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
272 typename CostFunctionAD<STATE_DIM, CONTROL_DIM, SCALAR>::state_matrix_t
273 CostFunctionAD<STATE_DIM, CONTROL_DIM, SCALAR>::stateSecondDerivativeTerminal()
275 Eigen::Matrix<SCALAR, 1, 1> w;
277 MatrixXs hesTot = finalCostCodegen_->hessian(stateControlTime_, w);
278 return hesTot.template block<STATE_DIM, STATE_DIM>(0, 0) + this->stateSecondDerivativeTerminalBase();
281 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
282 typename CostFunctionAD<STATE_DIM, CONTROL_DIM, SCALAR>::control_matrix_t
283 CostFunctionAD<STATE_DIM, CONTROL_DIM, SCALAR>::controlSecondDerivativeIntermediate()
285 Eigen::Matrix<SCALAR, 1, 1> w;
287 MatrixXs hesTot = intermediateCostCodegen_->hessian(stateControlTime_, w);
288 return hesTot.template block<CONTROL_DIM, CONTROL_DIM>(STATE_DIM, STATE_DIM) +
289 this->controlSecondDerivativeIntermediateBase();
292 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
293 typename CostFunctionAD<STATE_DIM, CONTROL_DIM, SCALAR>::control_matrix_t
294 CostFunctionAD<STATE_DIM, CONTROL_DIM, SCALAR>::controlSecondDerivativeTerminal()
296 Eigen::Matrix<SCALAR, 1, 1> w;
298 MatrixXs hesTot = finalCostCodegen_->hessian(stateControlTime_, w);
299 return hesTot.template block<CONTROL_DIM, CONTROL_DIM>(STATE_DIM, STATE_DIM) +
300 this->controlSecondDerivativeTerminalBase();
303 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
304 typename CostFunctionAD<STATE_DIM, CONTROL_DIM, SCALAR>::control_state_matrix_t
305 CostFunctionAD<STATE_DIM, CONTROL_DIM, SCALAR>::stateControlDerivativeIntermediate()
307 Eigen::Matrix<SCALAR, 1, 1> w;
309 MatrixXs hesTot = intermediateCostCodegen_->hessian(stateControlTime_, w);
310 return hesTot.template block<CONTROL_DIM, STATE_DIM>(STATE_DIM, 0) + this->stateControlDerivativeIntermediateBase();
313 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
314 typename CostFunctionAD<STATE_DIM, CONTROL_DIM, SCALAR>::control_state_matrix_t
315 CostFunctionAD<STATE_DIM, CONTROL_DIM, SCALAR>::stateControlDerivativeTerminal()
317 Eigen::Matrix<SCALAR, 1, 1> w;
319 MatrixXs hesTot = finalCostCodegen_->hessian(stateControlTime_, w);
320 return hesTot.template block<CONTROL_DIM, STATE_DIM>(STATE_DIM, 0) + this->stateControlDerivativeTerminalBase();
323 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
324 std::shared_ptr<ct::optcon::
325 TermBase<STATE_DIM, CONTROL_DIM, SCALAR, typename CostFunctionAD<STATE_DIM, CONTROL_DIM, SCALAR>::CGScalar>>
326 CostFunctionAD<STATE_DIM, CONTROL_DIM, SCALAR>::getIntermediateADTermById(
const size_t id)
328 return intermediateTerms_[id];
331 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
332 std::shared_ptr<ct::optcon::
333 TermBase<STATE_DIM, CONTROL_DIM, SCALAR, typename CostFunctionAD<STATE_DIM, CONTROL_DIM, SCALAR>::CGScalar>>
334 CostFunctionAD<STATE_DIM, CONTROL_DIM, SCALAR>::getFinalADTermById(
const size_t id)
336 return finalTerms_[id];
339 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
341 TermBase<STATE_DIM, CONTROL_DIM, SCALAR, typename CostFunctionAD<STATE_DIM, CONTROL_DIM, SCALAR>::CGScalar>>
342 CostFunctionAD<STATE_DIM, CONTROL_DIM, SCALAR>::getIntermediateADTermByName(
const std::string& name)
344 for (
auto term : intermediateTerms_)
345 if (term->getName() == name)
348 throw std::runtime_error(
"Term " + name +
" not found in the CostFunctionAD");
351 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
353 TermBase<STATE_DIM, CONTROL_DIM, SCALAR, typename CostFunctionAD<STATE_DIM, CONTROL_DIM, SCALAR>::CGScalar>>
354 CostFunctionAD<STATE_DIM, CONTROL_DIM, SCALAR>::getFinalADTermByName(
const std::string& name)
356 for (
auto term : finalTerms_)
357 if (term->getName() == name)
360 throw std::runtime_error(
"Term " + name +
" not found in the CostFunctionAD");
state_vector_t x_
Definition: CostFunction.hpp:103
virtual void setCurrentStateAndControl(const state_vector_t &x, const control_vector_t &u, const SCALAR &t=SCALAR(0.0))
Definition: CostFunction-impl.hpp:30
ct::core::ControlVector< control_dim > u
Definition: LoadFromFileTest.cpp:21
Eigen::Matrix< double, nControls, 1 > control_vector_t
SCALAR t_
Definition: CostFunction.hpp:105
clear all close all load ct GNMSLog0 mat reformat t
Definition: gnmsPlot.m:6
CppAD::AD< CppAD::cg::CG< double > > SCALAR
void addADTerm(const std::string &filename, std::string ¤tTerm, int currentTermType, TERM_PTR term, costFuncType *costFunc, bool verbose=false)
Definition: utilities.hpp:113
for i
Definition: mpc_unittest_plotting.m:14
ct::core::DerivativesCppadCG< state_dim, control_dim > JacCG
ct::core::StateVector< state_dim > x
Definition: LoadFromFileTest.cpp:20
control_vector_t u_
Definition: CostFunction.hpp:104
Eigen::Matrix< double, nStates, 1 > state_vector_t
const bool verbose
Definition: ConstraintComparison.h:18
#define CT_LOADABLE_TERMS(SCALAR_EVAL, SCALAR)
Definition: TermLoadMacros.hpp:18