Skip to content

Commit

Permalink
[DLG] Made the member enum of class Optimization_result scoped + adde…
Browse files Browse the repository at this point in the history
…d median function to utils
  • Loading branch information
diegolodares committed Nov 3, 2023
1 parent 2f0223a commit ede0967
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 26 deletions.
3 changes: 3 additions & 0 deletions lion/foundation/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,7 @@ template<typename ContainerOfGridVectorsType,
typename ScalarType = typename ContainerOfGridVectorsType::value_type::value_type>
std::vector<ScalarType> grid_vectors2points_rowmaj(const ContainerOfGridVectorsType &grid_vectors);

template<typename Container, typename ValueType = typename Container::value_type>
constexpr ValueType median(Container cont);

#endif
26 changes: 26 additions & 0 deletions lion/foundation/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -807,4 +807,30 @@ std::vector<ScalarType> grid_vectors2points_rowmaj(const ContainerOfGridVectorsT
return points_rowmaj;
}


template<typename Container, typename ValueType>
constexpr ValueType median(Container cont)
{
//
// Returns the median of the values stored in a container
// (NOTE: the container is copied in order to avoid
// altering its order).
//

const auto size = cont.size();
if (size == 0u) {
return std::numeric_limits<ValueType>::max();
}

const auto middle = std::next(cont.begin(), size >> 1u);
std::nth_element(cont.begin(), middle, cont.end());

if (size % 2u) {
return *middle;
}
else {
return (*middle + *std::max_element(cont.begin(), middle)) / ValueType{ 2 };
}
}

#endif
24 changes: 12 additions & 12 deletions lion/math/detail/ipopt_nlp_cppad.h
Original file line number Diff line number Diff line change
Expand Up @@ -1101,62 +1101,62 @@ class Ipopt_NLP_CppAD : public Ipopt::TNLP
switch(status)
{ // convert status from Ipopt enum to Optimization_result<DVector> enum
case Ipopt::SUCCESS:
solution_.status = Optimization_result<DVector>::success;
solution_.status = Optimization_result<DVector>::status_type::success;
break;

case Ipopt::MAXITER_EXCEEDED:
solution_.status =
Optimization_result<DVector>::maxiter_exceeded;
Optimization_result<DVector>::status_type::maxiter_exceeded;
break;

case Ipopt::STOP_AT_TINY_STEP:
solution_.status =
Optimization_result<DVector>::stop_at_tiny_step;
Optimization_result<DVector>::status_type::stop_at_tiny_step;
break;

case Ipopt::STOP_AT_ACCEPTABLE_POINT:
solution_.status =
Optimization_result<DVector>::stop_at_acceptable_point;
Optimization_result<DVector>::status_type::stop_at_acceptable_point;
break;

case Ipopt::LOCAL_INFEASIBILITY:
solution_.status =
Optimization_result<DVector>::local_infeasibility;
Optimization_result<DVector>::status_type::local_infeasibility;
break;

case Ipopt::USER_REQUESTED_STOP:
solution_.status =
Optimization_result<DVector>::user_requested_stop;
Optimization_result<DVector>::status_type::user_requested_stop;
break;

case Ipopt::DIVERGING_ITERATES:
solution_.status =
Optimization_result<DVector>::diverging_iterates;
Optimization_result<DVector>::status_type::diverging_iterates;
break;

case Ipopt::RESTORATION_FAILURE:
solution_.status =
Optimization_result<DVector>::restoration_failure;
Optimization_result<DVector>::status_type::restoration_failure;
break;

case Ipopt::ERROR_IN_STEP_COMPUTATION:
solution_.status =
Optimization_result<DVector>::error_in_step_computation;
Optimization_result<DVector>::status_type::error_in_step_computation;
break;

case Ipopt::INVALID_NUMBER_DETECTED:
solution_.status =
Optimization_result<DVector>::invalid_number_detected;
Optimization_result<DVector>::status_type::invalid_number_detected;
break;

case Ipopt::INTERNAL_ERROR:
solution_.status =
Optimization_result<DVector>::internal_error;
Optimization_result<DVector>::status_type::internal_error;
break;

default:
solution_.status =
Optimization_result<DVector>::unknown;
Optimization_result<DVector>::status_type::unknown;
}

solution_.x.resize(nx_);
Expand Down
4 changes: 2 additions & 2 deletions lion/math/ipopt_optimize_nlp_cppad.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ namespace lioncpp{
ok &= status == Ipopt::Solve_Succeeded;
if (!ok)
{
solution.status = Optimization_result<Dvector>::unknown;
solution.status = Optimization_result<Dvector>::status_type::unknown;
return;
}

Expand Down Expand Up @@ -126,7 +126,7 @@ namespace lioncpp{
ok &= status == Ipopt::Solve_Succeeded;
if (!ok)
{
solution.status = Optimization_result<Dvector>::unknown;
solution.status = Optimization_result<Dvector>::status_type::unknown;
return;
}

Expand Down
24 changes: 12 additions & 12 deletions lion/math/optimization_result.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace lioncpp {
struct Optimization_result
{
//! possible values for the result status
enum status_type {
enum class status_type {
not_defined,
success,
maxiter_exceeded,
Expand Down Expand Up @@ -46,17 +46,17 @@ namespace lioncpp {
const double_vector_type& constraint_lower_bounds,
const double_vector_type& constraint_upper_bounds)->Slack_and_bound_multipliers;

status_type status = not_defined; //! solution status
Ipopt::Number iter_count; //! The number of iterations
double_vector_type x; //! the approximation solution
double_vector_type zl; //! Lagrange multipliers corresponding to lower bounds on x
double_vector_type zu; //! Lagrange multipliers corresponding to upper bounds on x
double_vector_type g; //! value of g(x)
double_vector_type lambda; //! Lagrange multipliers correspondiing constraints on g(x)
double obj_value; //! value of f(x)
double_vector_type s; //! slack variables
double_vector_type vl; //! Lagrange multipliers corresponding to lower bounds on x
double_vector_type vu; //! Lagrange multipliers corresponding to upper bounds on x
status_type status = status_type::not_defined; //! solution status
Ipopt::Number iter_count; //! The number of iterations
double_vector_type x; //! the approximation solution
double_vector_type zl; //! Lagrange multipliers corresponding to lower bounds on x
double_vector_type zu; //! Lagrange multipliers corresponding to upper bounds on x
double_vector_type g; //! value of g(x)
double_vector_type lambda; //! Lagrange multipliers correspondiing constraints on g(x)
double obj_value; //! value of f(x)
double_vector_type s; //! slack variables
double_vector_type vl; //! Lagrange multipliers corresponding to lower bounds on x
double_vector_type vu; //! Lagrange multipliers corresponding to upper bounds on x
};

template<typename double_vector_type>
Expand Down

0 comments on commit ede0967

Please sign in to comment.