Skip to content

Commit

Permalink
Replace all float INF values casted to double by proper double infini…
Browse files Browse the repository at this point in the history
…ty values
  • Loading branch information
rjoomen committed Dec 5, 2023
1 parent 2ee35aa commit 9f7812a
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 38 deletions.
21 changes: 11 additions & 10 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,16 @@ BreakBeforeBraces: Custom

# Control of individual brace wrapping cases
BraceWrapping: {
AfterClass: 'true'
AfterControlStatement: 'true'
AfterEnum : 'true'
AfterFunction : 'true'
AfterNamespace : 'true'
AfterStruct : 'true'
AfterUnion : 'true'
BeforeCatch : 'true'
BeforeElse : 'true'
IndentBraces : 'false'
AfterCaseLabel: 'true',
AfterClass: 'true',
AfterControlStatement: 'true',
AfterEnum : 'true',
AfterFunction : 'true',
AfterNamespace : 'true',
AfterStruct : 'true',
AfterUnion : 'true',
BeforeCatch : 'true',
BeforeElse : 'true',
IndentBraces : 'false',
}
...
4 changes: 2 additions & 2 deletions trajopt_optimizers/trajopt_sqp/include/trajopt_sqp/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ struct SQPParameters
/** @brief NLP converges if approx_merit_improves is smaller than this */
double min_approx_improve = 1e-4;
/** @brief NLP converges if approx_merit_improve / best_exact_merit < min_approx_improve_frac */
double min_approx_improve_frac = static_cast<double>(-INFINITY);
double min_approx_improve_frac = -std::numeric_limits<double>::infinity();
/** @brief Max number of QP calls allowed */
int max_iterations = 50;

Expand All @@ -77,7 +77,7 @@ struct SQPParameters
/** @brief Constraints are scaled by this amount when inflated */
double merit_coeff_increase_ratio = 10;
/** @brief Max time in seconds that the optimizer will run */
double max_time = static_cast<double>(INFINITY);
double max_time = std::numeric_limits<double>::infinity();
/** @brief Initial coefficient that is used to scale the constraints. The total constaint cost is constaint_value
* coeff * merit_coeff */
double initial_merit_error_coeff = 10;
Expand Down
10 changes: 5 additions & 5 deletions trajopt_optimizers/trajopt_sqp/src/ifopt_qp_problem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ void IfoptQPProblem::setup()
}

// Initialize the constraint bounds
bounds_lower_ = Eigen::VectorXd::Constant(num_qp_cnts_, -double(INFINITY));
bounds_upper_ = Eigen::VectorXd::Constant(num_qp_cnts_, double(INFINITY));
bounds_lower_ = Eigen::VectorXd::Constant(num_qp_cnts_, -std::numeric_limits<double>::infinity());
bounds_upper_ = Eigen::VectorXd::Constant(num_qp_cnts_, std::numeric_limits<double>::infinity());
}

void IfoptQPProblem::setVariables(const double* x) { nlp_->SetVariables(x); }
Expand Down Expand Up @@ -405,16 +405,16 @@ void IfoptQPProblem::updateSlackVariableBounds()
if (constraint_types_[static_cast<std::size_t>(i)] == ConstraintType::EQ)
{
bounds_lower_[current_cnt_index] = 0;
bounds_upper_[current_cnt_index] = double(INFINITY);
bounds_upper_[current_cnt_index] = std::numeric_limits<double>::infinity();
bounds_lower_[current_cnt_index + 1] = 0;
bounds_upper_[current_cnt_index + 1] = double(INFINITY);
bounds_upper_[current_cnt_index + 1] = std::numeric_limits<double>::infinity();

current_cnt_index += 2;
}
else
{
bounds_lower_[current_cnt_index] = 0;
bounds_upper_[current_cnt_index] = double(INFINITY);
bounds_upper_[current_cnt_index] = std::numeric_limits<double>::infinity();

current_cnt_index++;
}
Expand Down
8 changes: 4 additions & 4 deletions trajopt_optimizers/trajopt_sqp/src/osqp_eigen_solver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,21 +275,21 @@ bool OSQPEigenSolver::updateGradient(const Eigen::Ref<const Eigen::VectorXd>& gr

bool OSQPEigenSolver::updateLowerBound(const Eigen::Ref<const Eigen::VectorXd>& lowerBound)
{
bounds_lower_ = lowerBound.cwiseMax(Eigen::VectorXd::Ones(num_cnts_) * -INFINITY);
bounds_lower_ = lowerBound.cwiseMax(Eigen::VectorXd::Ones(num_cnts_) * -std::numeric_limits<double>::infinity());
return solver_.updateLowerBound(bounds_lower_);
}

bool OSQPEigenSolver::updateUpperBound(const Eigen::Ref<const Eigen::VectorXd>& upperBound)
{
bounds_upper_ = upperBound.cwiseMin(Eigen::VectorXd::Ones(num_cnts_) * INFINITY);
bounds_upper_ = upperBound.cwiseMin(Eigen::VectorXd::Ones(num_cnts_) * std::numeric_limits<double>::infinity());
return solver_.updateUpperBound(bounds_upper_);
}

bool OSQPEigenSolver::updateBounds(const Eigen::Ref<const Eigen::VectorXd>& lowerBound,
const Eigen::Ref<const Eigen::VectorXd>& upperBound)
{
bounds_lower_ = lowerBound.cwiseMax(Eigen::VectorXd::Ones(num_cnts_) * -INFINITY);
bounds_upper_ = upperBound.cwiseMin(Eigen::VectorXd::Ones(num_cnts_) * INFINITY);
bounds_lower_ = lowerBound.cwiseMax(Eigen::VectorXd::Ones(num_cnts_) * -std::numeric_limits<double>::infinity());
bounds_upper_ = upperBound.cwiseMin(Eigen::VectorXd::Ones(num_cnts_) * std::numeric_limits<double>::infinity());

if (solver_.isInitialized())
return solver_.updateBounds(bounds_lower_, bounds_upper_);
Expand Down
16 changes: 8 additions & 8 deletions trajopt_optimizers/trajopt_sqp/src/trajopt_qp_problem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,8 @@ void TrajOptQPProblem::setup()
}

// Initialize the constraint bounds
bounds_lower_ = Eigen::VectorXd::Constant(num_qp_cnts_, -double(INFINITY));
bounds_upper_ = Eigen::VectorXd::Constant(num_qp_cnts_, double(INFINITY));
bounds_lower_ = Eigen::VectorXd::Constant(num_qp_cnts_, -std::numeric_limits<double>::infinity());
bounds_upper_ = Eigen::VectorXd::Constant(num_qp_cnts_, std::numeric_limits<double>::infinity());

// Set initialized
initialized_ = true;
Expand Down Expand Up @@ -593,30 +593,30 @@ void TrajOptQPProblem::updateSlackVariableBounds()
for (Eigen::Index i = 0; i < hinge_costs_.GetRows(); i++)
{
bounds_lower_[current_cnt_index] = 0;
bounds_upper_[current_cnt_index++] = double(INFINITY);
bounds_upper_[current_cnt_index++] = std::numeric_limits<double>::infinity();
}

for (Eigen::Index i = 0; i < abs_costs_.GetRows(); i++)
{
bounds_lower_[current_cnt_index] = 0;
bounds_upper_[current_cnt_index++] = double(INFINITY);
bounds_upper_[current_cnt_index++] = std::numeric_limits<double>::infinity();
bounds_lower_[current_cnt_index] = 0;
bounds_upper_[current_cnt_index++] = double(INFINITY);
bounds_upper_[current_cnt_index++] = std::numeric_limits<double>::infinity();
}

for (Eigen::Index i = 0; i < getNumNLPConstraints(); i++)
{
if (constraint_types_[static_cast<std::size_t>(i)] == ConstraintType::EQ)
{
bounds_lower_[current_cnt_index] = 0;
bounds_upper_[current_cnt_index++] = double(INFINITY);
bounds_upper_[current_cnt_index++] = std::numeric_limits<double>::infinity();
bounds_lower_[current_cnt_index] = 0;
bounds_upper_[current_cnt_index++] = double(INFINITY);
bounds_upper_[current_cnt_index++] = std::numeric_limits<double>::infinity();
}
else
{
bounds_lower_[current_cnt_index] = 0;
bounds_upper_[current_cnt_index++] = double(INFINITY);
bounds_upper_[current_cnt_index++] = std::numeric_limits<double>::infinity();
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions trajopt_sco/include/trajopt_sco/optimizers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ struct BasicTrustRegionSQPParameters
/** @brief If model improves less than this, exit and report convergence */
double min_approx_improve = 1e-4;
/** @brief If model improves less than this, exit and report convergence */
double min_approx_improve_frac = static_cast<double>(-INFINITY);
double min_approx_improve_frac = -std::numeric_limits<double>::infinity();
/** @brief The max number of iterations */
double max_iter = 50;
/** @brief If improvement is less than improve_ratio_threshold, shrink trust region by this ratio */
Expand All @@ -106,7 +106,7 @@ struct BasicTrustRegionSQPParameters
/** @brief Ratio that we increase coeff each time */
double merit_coeff_increase_ratio = 10;
/** @brief Max time in seconds that the optimizer will run */
double max_time = static_cast<double>(INFINITY);
double max_time = std::numeric_limits<double>::infinity();
/** @brief Initial coefficient that is used to scale the constraints. The total constaint cost is constaint_value *
* coeff * merit_coeff */
double initial_merit_error_coeff = 10;
Expand Down
13 changes: 7 additions & 6 deletions trajopt_sco/src/modeling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ void ConvexObjective::addAffExpr(const AffExpr& affexpr) { exprInc(quad_, affexp
void ConvexObjective::addQuadExpr(const QuadExpr& quadexpr) { exprInc(quad_, quadexpr); }
void ConvexObjective::addHinge(const AffExpr& affexpr, double coeff)
{
Var hinge = model_->addVar("hinge", 0, static_cast<double>(INFINITY));
Var hinge = model_->addVar("hinge", 0, std::numeric_limits<double>::infinity());
vars_.push_back(hinge);
ineqs_.push_back(affexpr);
exprDec(ineqs_.back(), hinge);
Expand All @@ -29,8 +29,8 @@ void ConvexObjective::addHinge(const AffExpr& affexpr, double coeff)
void ConvexObjective::addAbs(const AffExpr& affexpr, double coeff)
{
// Add variables that will enforce ABS
Var neg = model_->addVar("neg", 0, static_cast<double>(INFINITY));
Var pos = model_->addVar("pos", 0, static_cast<double>(INFINITY));
Var neg = model_->addVar("neg", 0, std::numeric_limits<double>::infinity());
Var pos = model_->addVar("pos", 0, std::numeric_limits<double>::infinity());
vars_.push_back(neg);
vars_.push_back(pos);
// Coeff will be applied whenever neg/pos are not 0
Expand Down Expand Up @@ -75,7 +75,7 @@ void ConvexObjective::addL2Norm(const AffExprVector& ev)

void ConvexObjective::addMax(const AffExprVector& ev)
{
Var m = model_->addVar("max", static_cast<double>(-INFINITY), static_cast<double>(INFINITY));
Var m = model_->addVar("max", -std::numeric_limits<double>::infinity(), std::numeric_limits<double>::infinity());
ineqs_.reserve(ineqs_.size() + ev.size());
for (const auto& i : ev)
{
Expand Down Expand Up @@ -174,8 +174,9 @@ OptProb::OptProb(ModelType convex_solver, const ModelConfig::ConstPtr& convex_so
}
VarVector OptProb::createVariables(const std::vector<std::string>& names)
{
return createVariables(
names, DblVec(names.size(), static_cast<double>(-INFINITY)), DblVec(names.size(), static_cast<double>(INFINITY)));
return createVariables(names,
DblVec(names.size(), -std::numeric_limits<double>::infinity()),
DblVec(names.size(), std::numeric_limits<double>::infinity()));
}

VarVector OptProb::createVariables(const std::vector<std::string>& names, const DblVec& lb, const DblVec& ub)
Expand Down
2 changes: 1 addition & 1 deletion trajopt_sco/src/optimizers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ struct MultiCritFilter {
*/
vector<DblVec> errvecs;
double improvement(const DblVec& errvec) {
double leastImprovement=INFINITY;
double leastImprovement=std::numeric_limits<double>::infinity();
for (const DblVec& olderrvec : errvecs) {
double improvement=0;
for (int i=0; i < errvec.size(); ++i) improvement += pospart(olderrvec[i] - errvec[i]);
Expand Down

0 comments on commit 9f7812a

Please sign in to comment.