-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Drop cppoptlib from Problem and Solver * Cleanup changes * Private CMake target_link_libraries * Use TVector * Remove cppoptlib line-search methods * Add custom statuses * Move extra solver logic to Criteria * Add our cases to operator<< * Update Criteria::print * Removed nan setting * Remove unused line search names * Fix not descent direction failsafe * Add codecov.yml
- Loading branch information
Showing
18 changed files
with
421 additions
and
378 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
coverage: | ||
status: | ||
project: | ||
default: | ||
target: auto | ||
threshold: 1% | ||
patch: | ||
default: | ||
target: 75% | ||
threshold: 5% | ||
only_pulls: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
// Source: https://github.com/PatWie/CppNumericalSolvers/blob/7eddf28fa5a8872a956d3c8666055cac2f5a535d/include/cppoptlib/meta.h | ||
// License: MIT | ||
#include "Criteria.hpp" | ||
|
||
namespace polysolve::nonlinear | ||
{ | ||
bool is_converged_status(const Status s) | ||
{ | ||
return s == Status::XDeltaTolerance || s == Status::FDeltaTolerance || s == Status::GradNormTolerance; | ||
} | ||
|
||
Criteria::Criteria() | ||
{ | ||
reset(); | ||
} | ||
|
||
void Criteria::reset() | ||
{ | ||
iterations = 0; | ||
xDelta = 0; | ||
fDelta = 0; | ||
gradNorm = 0; | ||
firstGradNorm = 0; | ||
fDeltaCount = 0; | ||
xDeltaDotGrad = 0; | ||
} | ||
|
||
void Criteria::print(std::ostream &os) const | ||
{ | ||
os << "Iterations: " << iterations << std::endl; | ||
os << "xDelta: " << xDelta << std::endl; | ||
os << "fDelta: " << fDelta << std::endl; | ||
os << "GradNorm: " << gradNorm << std::endl; | ||
os << "xDeltaDotGrad: " << xDeltaDotGrad << std::endl; | ||
os << "FirstGradNorm: " << firstGradNorm << std::endl; | ||
os << "fDeltaCount: " << fDeltaCount << std::endl; | ||
} | ||
|
||
Status checkConvergence(const Criteria &stop, const Criteria ¤t) | ||
{ | ||
if (stop.iterations > 0 && current.iterations > stop.iterations) | ||
{ | ||
return Status::IterationLimit; | ||
} | ||
if (stop.xDelta > 0 && current.xDelta < stop.xDelta) | ||
{ | ||
return Status::XDeltaTolerance; | ||
} | ||
if (stop.fDelta > 0 && current.fDelta < stop.fDelta && current.fDeltaCount >= stop.fDeltaCount) | ||
{ | ||
return Status::FDeltaTolerance; | ||
} | ||
const double stopGradNorm = current.iterations == 0 ? stop.firstGradNorm : stop.gradNorm; | ||
if (stopGradNorm > 0 && current.gradNorm < stopGradNorm) | ||
{ | ||
return Status::GradNormTolerance; | ||
} | ||
// Δx⋅∇f ≥ 0 means that the search direction is not a descent direction | ||
if (stop.xDeltaDotGrad < 0 && current.xDeltaDotGrad > stop.xDeltaDotGrad) | ||
{ | ||
return Status::NotDescentDirection; | ||
} | ||
return Status::Continue; | ||
} | ||
|
||
std::ostream &operator<<(std::ostream &os, const Status &s) | ||
{ | ||
switch (s) | ||
{ | ||
case Status::NotStarted: | ||
os << "Solver not started."; | ||
break; | ||
case Status::Continue: | ||
os << "Convergence criteria not reached."; | ||
break; | ||
case Status::IterationLimit: | ||
os << "Iteration limit reached."; | ||
break; | ||
case Status::XDeltaTolerance: | ||
os << "Change in parameter vector too small."; | ||
break; | ||
case Status::FDeltaTolerance: | ||
os << "Change in cost function value too small."; | ||
break; | ||
case Status::GradNormTolerance: | ||
os << "Gradient vector norm too small."; | ||
break; | ||
case Status::ObjectiveCustomStop: | ||
os << "Objective function specified to stop."; | ||
break; | ||
case Status::NanEncountered: | ||
os << "Objective or gradient function returned NaN."; | ||
break; | ||
case Status::NotDescentDirection: | ||
os << "Search direction not a descent direction."; | ||
break; | ||
case Status::LineSearchFailed: | ||
os << "Line search failed."; | ||
break; | ||
default: | ||
os << "Unknown status."; | ||
break; | ||
} | ||
return os; | ||
} | ||
|
||
std::ostream &operator<<(std::ostream &os, const Criteria &c) | ||
{ | ||
c.print(os); | ||
return os; | ||
} | ||
} // namespace polysolve::nonlinear |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#pragma once | ||
|
||
#include <cstddef> | ||
#include <iostream> | ||
|
||
namespace polysolve::nonlinear | ||
{ | ||
// Source: https://github.com/PatWie/CppNumericalSolvers/blob/7eddf28fa5a8872a956d3c8666055cac2f5a535d/include/cppoptlib/meta.h | ||
// License: MIT | ||
|
||
enum class Status | ||
{ | ||
NotStarted = -1, ///< The solver has not been started | ||
Continue = 0, ///< The solver should continue | ||
// Success cases | ||
IterationLimit, ///< The maximum number of iterations has been reached | ||
XDeltaTolerance, ///< The change in the parameter vector is below the tolerance | ||
FDeltaTolerance, ///< The change in the cost function is below the tolerance | ||
GradNormTolerance, ///< The norm of the gradient vector is below the tolerance | ||
ObjectiveCustomStop, ///< The objective function specified to stop | ||
// Failure cases | ||
NanEncountered, ///< The objective function returned NaN | ||
NotDescentDirection, ///< The search direction is not a descent direction | ||
LineSearchFailed, ///< The line search failed | ||
}; | ||
|
||
bool is_converged_status(const Status s); | ||
|
||
class Criteria | ||
{ | ||
public: | ||
size_t iterations; ///< Maximum number of iterations | ||
double xDelta; ///< Minimum change in parameter vector | ||
double fDelta; ///< Minimum change in cost function | ||
double gradNorm; ///< Minimum norm of gradient vector | ||
double firstGradNorm; ///< Initial norm of gradient vector | ||
double xDeltaDotGrad; ///< Dot product of parameter vector and gradient vector | ||
unsigned fDeltaCount; ///< Number of steps where fDelta is satisfied | ||
|
||
Criteria(); | ||
|
||
void reset(); | ||
|
||
void print(std::ostream &os) const; | ||
}; | ||
|
||
Status checkConvergence(const Criteria &stop, const Criteria ¤t); | ||
|
||
std::ostream &operator<<(std::ostream &os, const Status &s); | ||
|
||
std::ostream &operator<<(std::ostream &os, const Criteria &c); | ||
} // namespace polysolve::nonlinear |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.