Skip to content

Commit 59e9246

Browse files
committed
make cppad optional
1 parent d9aeebc commit 59e9246

35 files changed

+104
-19
lines changed

ct_core/cmake/FindCppAD.cmake

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11

2+
UNSET(CPPAD_INCLUDE_DIRS CACHE)
3+
UNSET(CPPAD_LIBRARIES CACHE)
4+
5+
26
IF (CPPAD_INCLUDES AND CPPAD_LIBRARIES)
37
SET(CPPAD_FIND_QUIETLY TRUE)
48
ENDIF ()
59

610

711
IF(DEFINED CPPAD_HOME)
8-
12+
913
FIND_PATH(CPPAD_INCLUDE_DIR NAMES cppad/cppad.hpp
1014
PATHS "${CPPAD_HOME}"
1115
NO_DEFAULT_PATH)

ct_core/include/ct/core/common/activations/ActivationBase.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ class ActivationBase
1919
{
2020
public:
2121
//! constructor
22-
ActivationBase() {}
22+
ActivationBase() = default;
2323
//! destructor
24-
virtual ~ActivationBase() {}
24+
virtual ~ActivationBase() = default;
2525
//! load activations from file
2626
virtual void loadConfigFile(const std::string& filename, const std::string& termName, bool verbose = false)
2727
{

ct_core/include/ct/core/core-prespec.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,16 @@ Licensed under the BSD-2 license (see LICENSE file in main directory)
1111
#include <cstdlib>
1212
#include <functional>
1313

14+
#ifdef CPPADCG
1415
#include <cppad/cg.hpp>
16+
#endif
1517

18+
#ifdef CPPAD
1619
#include <cppad/cppad.hpp>
1720
#include <cppad/example/cppad_eigen.hpp>
1821
#include <cppad/example/eigen_mat_inv.hpp>
1922
#include "internal/autodiff/CppadParallel.h"
23+
#endif
2024

2125
// Include file for convenience
2226
#include <Eigen/Core>

ct_core/include/ct/core/core.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,16 @@ Licensed under the BSD-2 license (see LICENSE file in main directory)
1212
#include <algorithm>
1313
#include <cstdlib>
1414

15+
#ifdef CPPADCG
1516
#include <cppad/cg.hpp>
17+
#endif
1618

19+
#ifdef CPPAD
1720
#include <cppad/cppad.hpp>
1821
#include <cppad/example/cppad_eigen.hpp>
1922
#include <cppad/example/eigen_mat_inv.hpp>
2023
#include "internal/autodiff/CppadParallel.h"
24+
#endif
2125

2226
// Include file for convenience
2327
#include <Eigen/Core>

ct_core/include/ct/core/integration/Integrator.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,22 +285,23 @@ class Integrator
285285
{
286286
}
287287

288+
#ifdef CPPADCG
288289
template <typename S = SCALAR>
289290
typename std::enable_if<std::is_same<S, ADCGScalar>::value, void>::type initializeODEIntSteppers(
290291
const IntegrationType& intType)
291292
{
292293
}
293-
294+
#endif
294295

295296
/**
296-
* @brief Initializes the ODEint fixed size steppers. Does not work for
297+
* @brief Initializes the ODEint fixed size steppers for double type. Does not work for
297298
* ad types
298299
*
299300
* @param[in] intType The int type
300301
*
301302
*/
302303
template <typename S = SCALAR>
303-
typename std::enable_if<!std::is_same<S, ADCGScalar>::value, void>::type initializeODEIntSteppers(
304+
typename std::enable_if<std::is_same<S, double>::value, void>::type initializeODEIntSteppers(
304305
const IntegrationType& intType)
305306
{
306307
switch (intType)
@@ -349,6 +350,7 @@ class Integrator
349350
}
350351
}
351352

353+
352354
//! resets the observer
353355
void reset();
354356

ct_core/include/ct/core/internal/traits/TraitSelectorSpecs.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,33 @@ Licensed under the BSD-2 license (see LICENSE file in main directory)
55

66
#pragma once
77

8+
#ifdef CPPADCG
89
#include "CppADCodegenTrait.h"
10+
#endif
11+
#ifdef CPPAD
912
#include "CppADDoubleTrait.h"
13+
#endif
1014
#include <ct/core/internal/traits/TraitSelector.h>
1115

1216
namespace ct {
1317
namespace core {
1418
namespace tpl {
1519

20+
#ifdef CPPAD
1621
template <>
1722
struct TraitSelector<CppAD::AD<double>>
1823
{
1924
typedef internal::CppADDoubleTrait Trait;
2025
};
26+
#endif
2127

28+
#ifdef CPPADCG
2229
template <>
2330
struct TraitSelector<CppAD::AD<CppAD::cg::CG<double>>>
2431
{
2532
typedef internal::CppADCodegenTrait Trait;
2633
};
27-
34+
#endif
2835

2936
} // namespace tpl
3037
} // namespace core

ct_core/include/ct/core/types/AutoDiff.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,24 @@ Licensed under the BSD-2 license (see LICENSE file in main directory)
55

66
#pragma once
77

8+
#ifdef CPPADCG
89
#include <cppad/cg.hpp>
10+
#endif
11+
12+
#ifdef CPPAD
913
#include <cppad/cppad.hpp>
14+
#endif
1015

1116
namespace ct {
1217
namespace core {
1318

19+
#ifdef CPPAD
1420
typedef CppAD::AD<double> ADScalar;
15-
typedef CppAD::cg::CG<double> ADCGValueType;
21+
#endif
1622

23+
#ifdef CPPADCG
24+
typedef CppAD::cg::CG<double> ADCGValueType;
1725
typedef CppAD::AD<ADCGValueType> ADCGScalar; //!< scalar type
26+
#endif
1827
}
1928
}

ct_optcon/include/ct/optcon/constraint/ConstraintContainerAnalytical.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Licensed under the BSD-2 license (see LICENSE file in main directory)
55

66
#pragma once
77

8-
#include <cppad/example/cppad_eigen.hpp>
8+
#include <ct/core/core.h>
99

1010
#include "LinearConstraintContainer.h"
1111
#include "term/ConstraintBase.h"

ct_optcon/include/ct/optcon/constraint/term/BoxConstraintBase.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,12 @@ class BoxConstraintBase : public ConstraintBase<STATE_DIM, CONTROL_DIM, SCALAR>
6767

6868
virtual VectorXs evaluate(const state_vector_t& x, const control_vector_t& u, const SCALAR t) override = 0;
6969

70+
#ifdef CPPADCG
7071
virtual Eigen::Matrix<ct::core::ADCGScalar, Eigen::Dynamic, 1> evaluateCppadCg(
7172
const core::StateVector<STATE_DIM, ct::core::ADCGScalar>& x,
7273
const core::ControlVector<CONTROL_DIM, ct::core::ADCGScalar>& u,
7374
ct::core::ADCGScalar t) override = 0;
75+
#endif
7476

7577
virtual MatrixXs jacobianState(const state_vector_t& x, const control_vector_t& u, const SCALAR t) override = 0;
7678

ct_optcon/include/ct/optcon/constraint/term/ConstraintBase-impl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ ConstraintBase<STATE_DIM, CONTROL_DIM, SCALAR>::~ConstraintBase()
2424
{
2525
}
2626

27+
#ifdef CPPADCG
2728
template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
2829
Eigen::Matrix<ct::core::ADCGScalar, Eigen::Dynamic, 1> ConstraintBase<STATE_DIM, CONTROL_DIM, SCALAR>::evaluateCppadCg(
2930
const core::StateVector<STATE_DIM, ct::core::ADCGScalar>& x,
@@ -32,6 +33,7 @@ Eigen::Matrix<ct::core::ADCGScalar, Eigen::Dynamic, 1> ConstraintBase<STATE_DIM,
3233
{
3334
throw std::runtime_error("Term " + name_ + " has no Implementation of evaluateCppaCg.");
3435
}
36+
#endif
3537

3638
template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
3739
typename ConstraintBase<STATE_DIM, CONTROL_DIM, SCALAR>::MatrixXs

ct_optcon/include/ct/optcon/constraint/term/ConstraintBase.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,12 @@ class ConstraintBase
7979
*
8080
* @return The constraint violation
8181
*/
82+
#ifdef CPPADCG
8283
virtual Eigen::Matrix<ct::core::ADCGScalar, Eigen::Dynamic, 1> evaluateCppadCg(
8384
const core::StateVector<STATE_DIM, ct::core::ADCGScalar>& x,
8485
const core::ControlVector<CONTROL_DIM, ct::core::ADCGScalar>& u,
8586
ct::core::ADCGScalar t);
86-
87+
#endif
8788

8889
/**
8990
* @brief Returns the number of constraints

ct_optcon/include/ct/optcon/constraint/term/ControlInputConstraint-impl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Eigen::Matrix<SCALAR, Eigen::Dynamic, 1> ControlInputConstraint<STATE_DIM, CONTR
5151
return this->sparsity_J_ * u;
5252
}
5353

54+
#ifdef CPPADCG
5455
template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
5556
Eigen::Matrix<ct::core::ADCGScalar, Eigen::Dynamic, 1>
5657
ControlInputConstraint<STATE_DIM, CONTROL_DIM, SCALAR>::evaluateCppadCg(
@@ -60,6 +61,7 @@ ControlInputConstraint<STATE_DIM, CONTROL_DIM, SCALAR>::evaluateCppadCg(
6061
{
6162
return this->sparsity_J_.template cast<ct::core::ADCGScalar>() * u;
6263
}
64+
#endif
6365

6466
template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
6567
typename ControlInputConstraint<STATE_DIM, CONTROL_DIM, SCALAR>::MatrixXs

ct_optcon/include/ct/optcon/constraint/term/ControlInputConstraint.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,12 @@ class ControlInputConstraint : public BoxConstraintBase<CONTROL_DIM, STATE_DIM,
5959

6060
virtual VectorXs evaluate(const state_vector_t& x, const control_vector_t& u, const SCALAR t) override;
6161

62+
#ifdef CPPADCG
6263
virtual Eigen::Matrix<ct::core::ADCGScalar, Eigen::Dynamic, 1> evaluateCppadCg(
6364
const core::StateVector<STATE_DIM, ct::core::ADCGScalar>& x,
6465
const core::ControlVector<CONTROL_DIM, ct::core::ADCGScalar>& u,
6566
ct::core::ADCGScalar t) override;
67+
#endif
6668

6769
virtual MatrixXs jacobianState(const state_vector_t& x, const control_vector_t& u, const SCALAR t) override;
6870

ct_optcon/include/ct/optcon/constraint/term/StateConstraint-impl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ StateConstraint<STATE_DIM, CONTROL_DIM, SCALAR>::evaluate(const state_vector_t&
4848
return this->sparsity_J_ * x;
4949
}
5050

51+
#ifdef CPPADCG
5152
template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
5253
Eigen::Matrix<ct::core::ADCGScalar, Eigen::Dynamic, 1> StateConstraint<STATE_DIM, CONTROL_DIM, SCALAR>::evaluateCppadCg(
5354
const core::StateVector<STATE_DIM, ct::core::ADCGScalar>& x,
@@ -56,6 +57,7 @@ Eigen::Matrix<ct::core::ADCGScalar, Eigen::Dynamic, 1> StateConstraint<STATE_DIM
5657
{
5758
return this->sparsity_J_.template cast<ct::core::ADCGScalar>() * x;
5859
}
60+
#endif
5961

6062
template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
6163
typename StateConstraint<STATE_DIM, CONTROL_DIM, SCALAR>::MatrixXs

ct_optcon/include/ct/optcon/constraint/term/StateConstraint.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,12 @@ class StateConstraint : public BoxConstraintBase<STATE_DIM, STATE_DIM, CONTROL_D
6060

6161
virtual VectorXs evaluate(const state_vector_t& x, const control_vector_t& u, const SCALAR t) override;
6262

63+
#ifdef CPPADCG
6364
virtual Eigen::Matrix<ct::core::ADCGScalar, Eigen::Dynamic, 1> evaluateCppadCg(
6465
const core::StateVector<STATE_DIM, ct::core::ADCGScalar>& x,
6566
const core::ControlVector<CONTROL_DIM, ct::core::ADCGScalar>& u,
6667
ct::core::ADCGScalar t) override;
68+
#endif
6769

6870
virtual MatrixXs jacobianState(const state_vector_t& x, const control_vector_t& u, const SCALAR t) override;
6971

ct_optcon/include/ct/optcon/constraint/term/TerminalConstraint-impl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ TerminalConstraint<STATE_DIM, CONTROL_DIM, SCALAR>::evaluate(const state_vector_
5151
return x - xF_;
5252
}
5353

54+
#ifdef CPPADCG
5455
template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
5556
Eigen::Matrix<ct::core::ADCGScalar, Eigen::Dynamic, 1>
5657
TerminalConstraint<STATE_DIM, CONTROL_DIM, SCALAR>::evaluateCppadCg(
@@ -60,6 +61,7 @@ TerminalConstraint<STATE_DIM, CONTROL_DIM, SCALAR>::evaluateCppadCg(
6061
{
6162
return x - xF_.template cast<ct::core::ADCGScalar>();
6263
}
64+
#endif
6365

6466
template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
6567
typename TerminalConstraint<STATE_DIM, CONTROL_DIM, SCALAR>::MatrixXs

ct_optcon/include/ct/optcon/constraint/term/TerminalConstraint.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,12 @@ class TerminalConstraint : public ConstraintBase<STATE_DIM, CONTROL_DIM, SCALAR>
4949

5050
virtual VectorXs evaluate(const state_vector_t& x, const control_vector_t& u, const SCALAR t) override;
5151

52+
#ifdef CPPADCG
5253
virtual Eigen::Matrix<ct::core::ADCGScalar, Eigen::Dynamic, 1> evaluateCppadCg(
5354
const core::StateVector<STATE_DIM, ct::core::ADCGScalar>& x,
5455
const core::ControlVector<CONTROL_DIM, ct::core::ADCGScalar>& u,
5556
ct::core::ADCGScalar t) override;
57+
#endif
5658

5759
virtual MatrixXs jacobianState(const state_vector_t& x, const control_vector_t& u, const SCALAR t) override;
5860

ct_optcon/include/ct/optcon/costfunction/CostFunctionQuadratic-impl.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ CostFunctionQuadratic<STATE_DIM, CONTROL_DIM, SCALAR>::~CostFunctionQuadratic()
4141
{
4242
}
4343

44+
#ifdef CPPADCG
4445
template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
4546
void CostFunctionQuadratic<STATE_DIM, CONTROL_DIM, SCALAR>::addIntermediateADTerm(
4647
std::shared_ptr<TermBase<STATE_DIM, CONTROL_DIM, SCALAR, ct::core::ADCGScalar>> term,
@@ -49,14 +50,14 @@ void CostFunctionQuadratic<STATE_DIM, CONTROL_DIM, SCALAR>::addIntermediateADTer
4950
throw std::runtime_error("not implemented");
5051
}
5152

52-
5353
template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
5454
void CostFunctionQuadratic<STATE_DIM, CONTROL_DIM, SCALAR>::addFinalADTerm(
5555
std::shared_ptr<TermBase<STATE_DIM, CONTROL_DIM, SCALAR, ct::core::ADCGScalar>> term,
5656
bool verbose)
5757
{
5858
throw std::runtime_error("not implemented");
5959
}
60+
#endif
6061

6162
template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR>
6263
void CostFunctionQuadratic<STATE_DIM, CONTROL_DIM, SCALAR>::loadFromConfigFile(const std::string& filename,

ct_optcon/include/ct/optcon/costfunction/CostFunctionQuadratic.hpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,6 @@ class CostFunctionQuadratic : public CostFunction<STATE_DIM, CONTROL_DIM, SCALAR
7171
virtual size_t addIntermediateTerm(std::shared_ptr<TermBase<STATE_DIM, CONTROL_DIM, SCALAR>> term,
7272
bool verbose = false);
7373

74-
virtual void addIntermediateADTerm(
75-
std::shared_ptr<TermBase<STATE_DIM, CONTROL_DIM, SCALAR, ct::core::ADCGScalar>> term,
76-
bool verbose = false);
77-
7874
/**
7975
* \brief Adds a final term
8076
* @param term final term
@@ -83,8 +79,15 @@ class CostFunctionQuadratic : public CostFunction<STATE_DIM, CONTROL_DIM, SCALAR
8379
*/
8480
virtual size_t addFinalTerm(std::shared_ptr<TermBase<STATE_DIM, CONTROL_DIM, SCALAR>> term, bool verbose = false);
8581

82+
83+
#ifdef CPPADCG
84+
virtual void addIntermediateADTerm(
85+
std::shared_ptr<TermBase<STATE_DIM, CONTROL_DIM, SCALAR, ct::core::ADCGScalar>> term,
86+
bool verbose = false);
87+
8688
virtual void addFinalADTerm(std::shared_ptr<TermBase<STATE_DIM, CONTROL_DIM, SCALAR, ct::core::ADCGScalar>> term,
8789
bool verbose = false);
90+
#endif
8891

8992
/**
9093
* \brief Loads cost function from config file

ct_optcon/include/ct/optcon/costfunction/term/TermBase-impl.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ SCALAR_EVAL TermBase<STATE_DIM, CONTROL_DIM, SCALAR_EVAL, SCALAR>::eval(
3737
return computeActivation(t) * evaluate(x, u, t);
3838
}
3939

40-
40+
#ifdef CPPADCG
4141
template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
4242
ct::core::ADCGScalar TermBase<STATE_DIM, CONTROL_DIM, SCALAR_EVAL, SCALAR>::evaluateCppadCg(
4343
const core::StateVector<STATE_DIM, ct::core::ADCGScalar>& x,
@@ -46,6 +46,7 @@ ct::core::ADCGScalar TermBase<STATE_DIM, CONTROL_DIM, SCALAR_EVAL, SCALAR>::eval
4646
{
4747
throw std::runtime_error("The cost function term term " + name_ + " does not implement evaluate CppadCg.");
4848
}
49+
#endif
4950

5051
template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
5152
bool TermBase<STATE_DIM, CONTROL_DIM, SCALAR_EVAL, SCALAR>::isActiveAtTime(SCALAR_EVAL t)

ct_optcon/include/ct/optcon/costfunction/term/TermBase.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ class TermBase
7979
const Eigen::Matrix<SCALAR, CONTROL_DIM, 1>& u,
8080
const SCALAR& t) = 0;
8181

82+
#ifdef CPPADCG
8283
/**
8384
* @brief The evaluate method used for jit compilation in CostfunctionAD
8485
*
@@ -91,6 +92,7 @@ class TermBase
9192
virtual ct::core::ADCGScalar evaluateCppadCg(const core::StateVector<STATE_DIM, ct::core::ADCGScalar>& x,
9293
const core::ControlVector<CONTROL_DIM, ct::core::ADCGScalar>& u,
9394
ct::core::ADCGScalar t);
95+
#endif
9496

9597
/**
9698
* @brief Gets called by the analytical costfunction. Adds time

ct_optcon/include/ct/optcon/costfunction/term/TermLinear-impl.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ SCALAR TermLinear<STATE_DIM, CONTROL_DIM, SCALAR_EVAL, SCALAR>::evaluate(const E
4848
return evalLocal<SCALAR>(x, u, t);
4949
}
5050

51+
#ifdef CPPADCG
5152
template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
5253
ct::core::ADCGScalar TermLinear<STATE_DIM, CONTROL_DIM, SCALAR_EVAL, SCALAR>::evaluateCppadCg(
5354
const core::StateVector<STATE_DIM, ct::core::ADCGScalar>& x,
@@ -56,6 +57,7 @@ ct::core::ADCGScalar TermLinear<STATE_DIM, CONTROL_DIM, SCALAR_EVAL, SCALAR>::ev
5657
{
5758
return evalLocal<ct::core::ADCGScalar>(x, u, t);
5859
}
60+
#endif
5961

6062
template <size_t STATE_DIM, size_t CONTROL_DIM, typename SCALAR_EVAL, typename SCALAR>
6163
core::StateVector<STATE_DIM, SCALAR_EVAL> TermLinear<STATE_DIM, CONTROL_DIM, SCALAR_EVAL, SCALAR>::stateDerivative(

0 commit comments

Comments
 (0)