11 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
14 eps_ = sqrt(Eigen::NumTraits<SCALAR>::epsilon());
17 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
21 doubleSidedDerivative_(arg.doubleSidedDerivative_)
39 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
45 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
50 throw std::runtime_error(
"not implemented");
53 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
58 throw std::runtime_error(
"not implemented");
62 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
66 throw std::runtime_error(
"not implemented");
69 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
73 throw std::runtime_error(
"controlDerivativeTerminal() not implemented in CostFunctionQuadratic");
76 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
80 throw std::runtime_error(
"controlSecondDerivativeTerminal() not implemented");
83 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
87 throw std::runtime_error(
"stateControlDerivativeTerminal() not implemented");
90 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
94 costIntermediate->updateReferenceState(x_ref);
97 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
101 costFinal->updateReferenceState(x_final);
104 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
108 costIntermediate->updateReferenceControl(u_ref);
111 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
118 std::cout <<
"norm error between derivative/numdiff state : " << std::endl
119 << (derivative - derivativeNd).norm() << std::endl;
121 return (derivative.isApprox(derivativeNd, 1e-6));
124 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
131 std::cout <<
"norm error between derivative/numdiff control : " << std::endl
132 << (derivative - derivativeNd).norm() << std::endl;
134 return (derivative.isApprox(derivativeNd, 1e-6));
137 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
138 std::shared_ptr<TermBase<STATE_DIM, CONTROL_DIM, SCALAR>>
144 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
145 std::shared_ptr<TermBase<STATE_DIM, CONTROL_DIM, SCALAR>>
151 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
152 std::shared_ptr<TermBase<STATE_DIM, CONTROL_DIM, SCALAR>>
156 if (term->getName() == name)
159 throw std::runtime_error(
"Term " + name +
" not found in the CostFunctionQuadratic");
162 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
163 std::shared_ptr<TermBase<STATE_DIM, CONTROL_DIM, SCALAR>>
167 if (term->getName() == name)
170 throw std::runtime_error(
"Term " + name +
" not found in the CostFunctionQuadratic");
173 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
184 for (
size_t i = 0;
i < STATE_DIM; ++
i)
187 SCALAR h =
eps_ * std::max(std::abs(x_local(
i)), 1.0);
188 volatile SCALAR x_ph = x_local(
i) + h;
189 SCALAR dxp = x_ph - x_local(
i);
192 x_perturbed(
i) = x_ph;
202 volatile SCALAR x_mh = x_local(
i) - h;
203 SCALAR dxm = x_local(
i) - x_mh;
205 x_perturbed = x_local;
206 x_perturbed(
i) = x_mh;
209 dFdx(
i, 0) = (dxdt - dxdt_low) / (dxp + dxm);
213 dFdx(
i, 0) = (dxdt - dxdt_ref) / dxp;
220 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
231 for (
size_t i = 0;
i < CONTROL_DIM; ++
i)
234 SCALAR h =
eps_ * std::max(std::abs(u_local(
i)), 1.0);
235 volatile SCALAR u_ph = u_local(
i) + h;
236 SCALAR dup = u_ph - u_local(
i);
239 u_perturbed(
i) = u_ph;
249 volatile SCALAR u_mh = u_local(
i) - h;
250 SCALAR dum = u_local(
i) - u_mh;
252 u_perturbed = u_local;
253 u_perturbed(
i) = u_mh;
257 dFdu(
i, 0) = (dxdt - dxdt_low) / (dup + dum);
261 dFdu(
i, 0) = (dxdt - dxdt_ref) / dup;
268 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
278 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
286 std::string name = term->getName();
287 std::cout <<
"Trying to add term as intermediate" << std::endl;
293 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
301 std::string name = term->getName();
302 std::cout <<
"Trying to add term as final" << std::endl;
309 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
316 if (!it->isActiveAtTime(this->t_))
320 y += it->computeActivation(this->
t_) * it->eval(this->
x_, this->
u_, this->
t_);
327 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
333 y += it->evaluate(this->
x_, this->
u_, this->
t_);
339 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
344 derivative.setZero();
348 if (!it->isActiveAtTime(this->t_))
352 derivative += it->computeActivation(this->
t_) * it->stateDerivative(this->
x_, this->
u_, this->
t_);
359 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
364 derivative.setZero();
367 derivative += it->stateDerivative(this->
x_, this->
u_, this->
t_);
372 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
377 derivative.setZero();
381 if (!it->isActiveAtTime(this->t_))
385 derivative += it->computeActivation(this->
t_) * it->stateSecondDerivative(this->
x_, this->
u_, this->
t_);
391 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
396 derivative.setZero();
399 derivative += it->stateSecondDerivative(this->
x_, this->
u_, this->
t_);
405 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
410 derivative.setZero();
414 if (!it->isActiveAtTime(this->t_))
418 derivative += it->computeActivation(this->
t_) * it->controlDerivative(this->
x_, this->
u_, this->
t_);
424 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
429 derivative.setZero();
432 derivative += it->controlDerivative(this->
x_, this->
u_, this->
t_);
438 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
443 derivative.setZero();
447 if (!it->isActiveAtTime(this->t_))
451 derivative += it->computeActivation(this->
t_) * it->controlSecondDerivative(this->
x_, this->
u_, this->
t_);
457 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
462 derivative.setZero();
465 derivative += it->controlSecondDerivative(this->
x_, this->
u_, this->
t_);
471 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
476 derivative.setZero();
480 if (!it->isActiveAtTime(this->t_))
484 derivative += it->computeActivation(this->
t_) * it->stateControlDerivative(this->
x_, this->
u_, this->
t_);
490 template <
size_t STATE_DIM,
size_t CONTROL_DIM,
typename SCALAR>
495 derivative.setZero();
498 derivative += it->stateControlDerivative(this->
x_, this->
u_, this->
t_);
std::vector< std::shared_ptr< TermBase< STATE_DIM, CONTROL_DIM, SCALAR > > > intermediateCostAnalytical_
Definition: CostFunctionQuadratic.hpp:238
control_state_matrix_t stateControlDerivativeIntermediateBase()
evaluate intermediate analytical control mixed state control derivatives
Definition: CostFunctionQuadratic-impl.hpp:473
control_matrix_t controlSecondDerivativeIntermediateBase()
evaluate intermediate analytical control second derivatives
Definition: CostFunctionQuadratic-impl.hpp:440
virtual state_vector_t stateDerivativeIntermediate()=0
Computes intermediate-cost first-order derivative with respect to state.
SCALAR eps_
stepsize for numerical differentiation
Definition: CostFunctionQuadratic.hpp:232
state_vector_t x_
Definition: CostFunction.hpp:103
virtual SCALAR evaluateIntermediate()=0
evaluate intermediate costs
A base function for cost functions. All cost functions should derive from this.
Definition: CostFunction.hpp:25
virtual void setCurrentStateAndControl(const state_vector_t &x, const control_vector_t &u, const SCALAR &t=SCALAR(0.0))
Definition: CostFunction-impl.hpp:30
SCALAR evaluateIntermediateBase()
evaluate intermediate analytical cost terms
Definition: CostFunctionQuadratic-impl.hpp:310
virtual void updateReferenceControl(const control_vector_t &u_ref)
update the reference control for intermediate cost terms
Definition: CostFunctionQuadratic-impl.hpp:105
Eigen::Matrix< SCALAR, CONTROL_DIM, STATE_DIM > control_state_matrix_t
Definition: CostFunctionQuadratic.hpp:36
virtual void updateFinalState(const state_vector_t &x_final)
update the reference state for final cost terms
Definition: CostFunctionQuadratic-impl.hpp:98
virtual void updateReferenceState(const state_vector_t &x_ref)
update the reference state for intermediate cost terms
Definition: CostFunctionQuadratic-impl.hpp:91
virtual void initialize()
initialize the cost function (e.g. to be used in CostFunctionAD)
Definition: CostFunctionQuadratic-impl.hpp:269
SCALAR t_
Definition: CostFunction.hpp:105
virtual control_state_matrix_t stateControlDerivativeTerminal()
Computes final-cost derivative with respect to state and control.
Definition: CostFunctionQuadratic-impl.hpp:85
An interface for a term, supporting both analytical and auto-diff terms.
Definition: TermBase.hpp:30
state_vector_t stateDerivativeTerminalBase()
evaluate terminal analytical state derivatives
Definition: CostFunctionQuadratic-impl.hpp:361
Describes a cost function with a quadratic approximation, i.e. one that can compute first and second ...
Definition: CostFunctionQuadratic.hpp:29
virtual void loadFromConfigFile(const std::string &filename, bool verbose=false)
Loads cost function from config file.
Definition: CostFunctionQuadratic-impl.hpp:63
state_matrix_t stateSecondDerivativeIntermediateBase()
evaluate intermediate analytical state second derivatives
Definition: CostFunctionQuadratic-impl.hpp:374
control_vector_t controlDerivativeIntermediateBase()
evaluate intermediate analytical control derivatives
Definition: CostFunctionQuadratic-impl.hpp:407
CppAD::AD< CppAD::cg::CG< double > > SCALAR
control_vector_t controlDerivativeTerminalBase()
evaluate terminal analytical control derivatives
Definition: CostFunctionQuadratic-impl.hpp:426
state_matrix_t stateSecondDerivativeTerminalBase()
evaluate terminal analytical state second derivatives
Definition: CostFunctionQuadratic-impl.hpp:393
state_vector_t stateDerivativeIntermediateBase()
evaluate intermediate analytical state derivatives
Definition: CostFunctionQuadratic-impl.hpp:341
SCALAR evaluateTerminalBase()
evaluate terminal analytical cost terms
Definition: CostFunctionQuadratic-impl.hpp:328
virtual size_t addIntermediateTerm(std::shared_ptr< TermBase< STATE_DIM, CONTROL_DIM, SCALAR >> term, bool verbose=false)
Adds an intermediate term.
Definition: CostFunctionQuadratic-impl.hpp:279
for i
Definition: mpc_unittest_plotting.m:14
CostFunctionQuadratic()
Definition: CostFunctionQuadratic-impl.hpp:12
std::shared_ptr< TermBase< STATE_DIM, CONTROL_DIM, SCALAR > > getIntermediateTermByName(const std::string &name)
Definition: CostFunctionQuadratic-impl.hpp:153
virtual size_t addFinalTerm(std::shared_ptr< TermBase< STATE_DIM, CONTROL_DIM, SCALAR >> term, bool verbose=false)
Adds a final term.
Definition: CostFunctionQuadratic-impl.hpp:294
std::shared_ptr< TermBase< STATE_DIM, CONTROL_DIM, SCALAR > > getIntermediateTermById(const size_t id)
Definition: CostFunctionQuadratic-impl.hpp:139
bool stateDerivativeIntermediateTest(bool verbose=false)
compare the state derivative against numerical differentiation
Definition: CostFunctionQuadratic-impl.hpp:112
Eigen::Matrix< SCALAR, CONTROL_DIM, CONTROL_DIM > control_matrix_t
Definition: CostFunctionQuadratic.hpp:35
control_vector_t u_
Definition: CostFunction.hpp:104
bool doubleSidedDerivative_
use double sided derivatives in numerical differentiation
Definition: CostFunctionQuadratic.hpp:235
bool controlDerivativeIntermediateTest(bool verbose=false)
compare the control derivative against numerical differentiation
Definition: CostFunctionQuadratic-impl.hpp:125
virtual control_vector_t controlDerivativeIntermediate()=0
Computes intermediate-cost first-order derivative with respect to control.
virtual control_vector_t controlDerivativeTerminal()
Computes terminal-cost first-order derivative with respect to control.
Definition: CostFunctionQuadratic-impl.hpp:71
Eigen::Matrix< double, nStates, nStates > state_matrix_t
const bool verbose
Definition: ConstraintComparison.h:18
control_vector_t controlDerivativeIntermediateNumDiff()
compute the control derivative by numerical differentiation (can be used for testing) ...
Definition: CostFunctionQuadratic-impl.hpp:222
control_matrix_t controlSecondDerivativeTerminalBase()
evaluate terminal analytical control second derivatives
Definition: CostFunctionQuadratic-impl.hpp:459
control_state_matrix_t stateControlDerivativeTerminalBase()
evaluate terminal analytical control mixed state control derivatives
Definition: CostFunctionQuadratic-impl.hpp:492
std::vector< std::shared_ptr< TermBase< STATE_DIM, CONTROL_DIM, SCALAR > > > finalCostAnalytical_
Definition: CostFunctionQuadratic.hpp:241
virtual void getCurrentStateAndControl(Eigen::Matrix< SCALAR, STATE_DIM, 1 > &x, Eigen::Matrix< SCALAR, CONTROL_DIM, 1 > &u, SCALAR &t) const
sets current state, control and time
Definition: CostFunction-impl.hpp:40
virtual control_matrix_t controlSecondDerivativeTerminal()
Computes final-cost second-order derivative with respect to input.
Definition: CostFunctionQuadratic-impl.hpp:78
state_vector_t stateDerivativeIntermediateNumDiff()
compute the state derivative by numerical differentiation (can be used for testing) ...
Definition: CostFunctionQuadratic-impl.hpp:175
virtual ~CostFunctionQuadratic()
Definition: CostFunctionQuadratic-impl.hpp:40
std::shared_ptr< TermBase< STATE_DIM, CONTROL_DIM, SCALAR > > getFinalTermById(const size_t id)
Definition: CostFunctionQuadratic-impl.hpp:146
std::shared_ptr< TermBase< STATE_DIM, CONTROL_DIM, SCALAR > > getFinalTermByName(const std::string &name)
Definition: CostFunctionQuadratic-impl.hpp:164