Skip to content

Commit

Permalink
Add GoogleTest value printers for ExpressionType and SolverExitCondition
Browse files Browse the repository at this point in the history
  • Loading branch information
calcmogul committed Dec 16, 2023
1 parent 13b4f01 commit cea871c
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 30 deletions.
27 changes: 5 additions & 22 deletions include/sleipnir/autodiff/Expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,9 @@
#include "sleipnir/IntrusiveSharedPtr.hpp"
#include "sleipnir/Pool.hpp"
#include "sleipnir/SymbolExports.hpp"
#include "sleipnir/autodiff/ExpressionType.hpp"

namespace sleipnir {

/**
* Expression type.
*
* Used for autodiff caching.
*/
enum class ExpressionType {
/// There is no expression.
kNone,
/// The expression is a constant.
kConstant,
/// The expression is composed of linear and lower-order operators.
kLinear,
/// The expression is composed of quadratic and lower-order operators.
kQuadratic,
/// The expression is composed of nonlinear and lower-order operators.
kNonlinear
};

namespace detail {
namespace sleipnir::detail {

struct SLEIPNIR_DLLEXPORT Expression;

Expand Down Expand Up @@ -442,7 +423,9 @@ SLEIPNIR_DLLEXPORT ExpressionPtr tan( // NOLINT
*/
SLEIPNIR_DLLEXPORT ExpressionPtr tanh(const ExpressionPtr& x);

} // namespace detail
} // namespace sleipnir::detail

namespace sleipnir {

// FIXME: Doxygen is confused:
//
Expand Down
35 changes: 35 additions & 0 deletions include/sleipnir/autodiff/ExpressionType.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) Sleipnir contributors

#pragma once

#include <iosfwd>

namespace sleipnir {

/**
* Expression type.
*
* Used for autodiff caching.
*/
enum class ExpressionType {
/// There is no expression.
kNone,
/// The expression is a constant.
kConstant,
/// The expression is composed of linear and lower-order operators.
kLinear,
/// The expression is composed of quadratic and lower-order operators.
kQuadratic,
/// The expression is composed of nonlinear and lower-order operators.
kNonlinear
};

/**
* GoogleTest value formatter for ExpressionType.
*
* @param type ExpressionType to print.
* @param os Output stream to which to print.
*/
void PrintTo(const ExpressionType& type, std::ostream* os);

} // namespace sleipnir
10 changes: 10 additions & 0 deletions include/sleipnir/optimization/SolverExitCondition.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#pragma once

#include <iosfwd>

namespace sleipnir {

/**
Expand Down Expand Up @@ -35,4 +37,12 @@ enum class SolverExitCondition {
kMaxWallClockTimeExceeded = -7
};

/**
* GoogleTest value formatter for SolverExitCondition.
*
* @param cond SolverExitCondition to print.
* @param os Output stream to which to print.
*/
void PrintTo(const SolverExitCondition& cond, std::ostream* os);

} // namespace sleipnir
16 changes: 8 additions & 8 deletions src/autodiff/Expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,6 @@
#include <cmath>
#include <numbers>

namespace sleipnir {

// Instantiate Expression pool in Expression.cpp instead to avoid ODR violation
template EXPORT_TEMPLATE_DEFINE(SLEIPNIR_DLLEXPORT)
PoolAllocator<detail::Expression> GlobalPoolAllocator<detail::Expression>();

} // namespace sleipnir

namespace sleipnir::detail {

namespace {
Expand Down Expand Up @@ -846,3 +838,11 @@ ExpressionPtr tanh(const ExpressionPtr& x) {
}

} // namespace sleipnir::detail

namespace sleipnir {

// Instantiate Expression pool in Expression.cpp instead to avoid ODR violation
template EXPORT_TEMPLATE_DEFINE(SLEIPNIR_DLLEXPORT)
PoolAllocator<detail::Expression> GlobalPoolAllocator<detail::Expression>();

} // namespace sleipnir
31 changes: 31 additions & 0 deletions src/autodiff/ExpressionType.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) Sleipnir contributors

#include "sleipnir/autodiff/ExpressionType.hpp"

#include <ostream>

namespace sleipnir {

void PrintTo(const ExpressionType& type, std::ostream* os) {
using enum sleipnir::ExpressionType;

switch (type) {
case kNone:
*os << "ExpressionType::kNone";
break;
case kConstant:
*os << "ExpressionType::kConstant";
break;
case kLinear:
*os << "ExpressionType::kLinear";
break;
case kQuadratic:
*os << "ExpressionType::kQuadratic";
break;
case kNonlinear:
*os << "ExpressionType::kNonlinear";
break;
}
}

} // namespace sleipnir
46 changes: 46 additions & 0 deletions src/optimization/SolverExitCondition.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) Sleipnir contributors

#include "sleipnir/optimization/SolverExitCondition.hpp"

#include <ostream>

namespace sleipnir {

void PrintTo(const SolverExitCondition& cond, std::ostream* os) {
using enum sleipnir::SolverExitCondition;

switch (cond) {
case kSuccess:
*os << "SolverExitCondition::kSuccess";
break;
case kSolvedToAcceptableTolerance:
*os << "SolverExitCondition::kSolvedToAcceptableTolerance";
break;
case kCallbackRequestedStop:
*os << "SolverExitCondition::kCallbackRequestedStop";
break;
case kTooFewDOFs:
*os << "SolverExitCondition::kTooFewDOFs";
break;
case kLocallyInfeasible:
*os << "SolverExitCondition::kLocallyInfeasible";
break;
case kBadSearchDirection:
*os << "SolverExitCondition::kBadSearchDirection";
break;
case kMaxSearchDirectionTooSmall:
*os << "SolverExitCondition::kMaxSearchDirectionTooSmall";
break;
case kDivergingIterates:
*os << "SolverExitCondition::kDivergingIterates";
break;
case kMaxIterationsExceeded:
*os << "SolverExitCondition::kMaxIterationsExceeded";
break;
case kMaxWallClockTimeExceeded:
*os << "SolverExitCondition::kMaxWallClockTimeExceeded";
break;
}
}

} // namespace sleipnir

0 comments on commit cea871c

Please sign in to comment.