forked from openturns/openturns
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementation of the Least Squares Solver for a system of non-linear equations The work is a continuation of the PR proposed by A.H accounting for some remarks
- Loading branch information
1 parent
75ca8cf
commit db722e5
Showing
20 changed files
with
533 additions
and
12 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 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,142 @@ | ||
// -*- C++ -*- | ||
/** | ||
* @brief Implementation class of an unbounded solver for systems of non-linear equations based on least square optimization | ||
* | ||
* Copyright 2005-2025 Airbus-EDF-IMACS-ONERA-Phimeca | ||
* | ||
* This library is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This library is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this library. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
#include "openturns/LeastSquaresEquationsSolver.hxx" | ||
#include "openturns/LeastSquaresProblem.hxx" | ||
#include "openturns/Log.hxx" | ||
#include "openturns/OptimizationAlgorithm.hxx" | ||
#include "openturns/Log.hxx" | ||
#include "openturns/PersistentObjectFactory.hxx" | ||
#include "openturns/SymbolicFunction.hxx" | ||
|
||
BEGIN_NAMESPACE_OPENTURNS | ||
|
||
/** | ||
* @class LeastSquaresEquationsSolver | ||
* | ||
* This class is an interface for the nonlinear LeastSquaresEquationsSolver | ||
*/ | ||
|
||
CLASSNAMEINIT(LeastSquaresEquationsSolver) | ||
|
||
static const Factory<LeastSquaresEquationsSolver> Factory_LeastSquaresEquationsSolver; | ||
|
||
/* Parameter constructor */ | ||
LeastSquaresEquationsSolver::LeastSquaresEquationsSolver(const Scalar absoluteError, | ||
const Scalar relativeError, | ||
const Scalar residualError, | ||
const UnsignedInteger maximumCallsNumber) | ||
: SolverImplementation(absoluteError, relativeError, residualError, maximumCallsNumber) | ||
{ | ||
// LeastSquares problem | ||
LeastSquaresProblem problem(SymbolicFunction("x", "x")); | ||
solver_ = OptimizationAlgorithm::Build(problem); | ||
solver_.setMaximumCallsNumber(maximumCallsNumber); | ||
solver_.setMaximumAbsoluteError(absoluteError); | ||
solver_.setMaximumRelativeError(relativeError); | ||
solver_.setMaximumResidualError(residualError); | ||
} | ||
|
||
/* Virtual constructor */ | ||
LeastSquaresEquationsSolver * LeastSquaresEquationsSolver::clone() const | ||
{ | ||
return new LeastSquaresEquationsSolver(*this); | ||
} | ||
|
||
/* String converter */ | ||
String LeastSquaresEquationsSolver::__repr__() const | ||
{ | ||
OSS oss; | ||
oss << "class=" << LeastSquaresEquationsSolver::GetClassName() | ||
<< " derived from " << SolverImplementation::__repr__(); | ||
return oss; | ||
} | ||
|
||
void LeastSquaresEquationsSolver::setOptimizationAlgorithm(const OptimizationAlgorithm & algorithm) | ||
{ | ||
solver_ = algorithm; | ||
} | ||
|
||
OptimizationAlgorithm LeastSquaresEquationsSolver::getOptimizationAlgorithm() const | ||
{ | ||
return solver_; | ||
} | ||
|
||
/* Solve attempt to find one root to the system of non-linear equations function(x) = 0 | ||
given a starting point x with a least square optimization method. | ||
*/ | ||
Point LeastSquaresEquationsSolver::solve(const Function & function, | ||
const Point & startingPoint) const | ||
{ | ||
const Interval bounds; | ||
return solve(function, startingPoint, bounds); | ||
} | ||
|
||
/* Solve attempt to find one root to the system of non-linear equations function(x) = 0 | ||
given a starting point x with a least square optimization method. | ||
*/ | ||
Point LeastSquaresEquationsSolver::solve(const Function & function, | ||
const Point & startingPoint, | ||
const Interval & bounds) const | ||
{ | ||
LeastSquaresProblem lsqProblem(function); | ||
const UnsignedInteger boundsDimension = bounds.getDimension(); | ||
if (boundsDimension == function.getInputDimension()) | ||
lsqProblem.setBounds(bounds); | ||
if ((boundsDimension > 0) && (boundsDimension != function.getInputDimension())) | ||
throw InvalidArgumentException(HERE) << "Bounds should be of dimension 0 or dimension = " << function.getInputDimension() | ||
<< ". Here bounds's dimension = " << boundsDimension; | ||
OptimizationAlgorithm solver(solver_); | ||
solver.setStartingPoint(startingPoint); | ||
try | ||
{ | ||
solver.setProblem(lsqProblem); | ||
} | ||
catch (const InvalidArgumentException &) | ||
{ | ||
LOGWARN("Default optimization algorithm could not solve the least squares problem. Trying to set up a new one..."); | ||
solver = OptimizationAlgorithm::Build(lsqProblem); | ||
} | ||
solver.setProblem(lsqProblem); | ||
solver.run(); | ||
callsNumber_ = solver.getResult().getCallsNumber(); | ||
const Point min_value_obtained = solver.getResult().getOptimalValue(); | ||
if ( getResidualError() < min_value_obtained[0]) | ||
throw InternalException(HERE) << "Error: solver did not find a solution that satisfies the threshold, here obtained residual=" << min_value_obtained[0]; | ||
const Point result = solver.getResult().getOptimalPoint(); | ||
return result; | ||
} | ||
|
||
/* Method save() stores the object through the StorageManager */ | ||
void LeastSquaresEquationsSolver::save(Advocate & adv) const | ||
{ | ||
SolverImplementation::save(adv); | ||
adv.saveAttribute( "solver_", solver_ ); | ||
} | ||
|
||
/* Method load() reloads the object from the StorageManager */ | ||
void LeastSquaresEquationsSolver::load(Advocate & adv) | ||
{ | ||
SolverImplementation::load(adv); | ||
adv.loadAttribute( "solver_", solver_ ); | ||
} | ||
|
||
END_NAMESPACE_OPENTURNS |
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
79 changes: 79 additions & 0 deletions
79
lib/src/Base/Solver/openturns/LeastSquaresEquationsSolver.hxx
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,79 @@ | ||
// -*- C++ -*- | ||
/** | ||
* @brief Implementation class of an unbounded solver for systems of non-linear equations based on least square optimization | ||
* | ||
* Copyright 2005-2025 Airbus-EDF-IMACS-ONERA-Phimeca | ||
* | ||
* This library is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This library is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this library. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
#ifndef OPENTURNS_LEASTSQUARESEQUATIONSSOLVER_HXX | ||
#define OPENTURNS_LEASTSQUARESEQUATIONSSOLVER_HXX | ||
|
||
#include "openturns/OTprivate.hxx" | ||
#include "openturns/SolverImplementation.hxx" | ||
#include "openturns/OptimizationAlgorithm.hxx" | ||
|
||
|
||
BEGIN_NAMESPACE_OPENTURNS | ||
|
||
/** | ||
* @class LeastSquaresEquationsSolver | ||
* | ||
* This class is an interface for the 1D nonlinear solverImplementations | ||
*/ | ||
class OT_API LeastSquaresEquationsSolver : | ||
public SolverImplementation | ||
{ | ||
CLASSNAME | ||
public: | ||
|
||
/** Parameter constructor */ | ||
explicit LeastSquaresEquationsSolver(const Scalar absoluteError = ResourceMap::GetAsScalar("Solver-DefaultAbsoluteError"), | ||
const Scalar relativeError = ResourceMap::GetAsScalar("Solver-DefaultRelativeError"), | ||
const Scalar residualError = ResourceMap::GetAsScalar("Solver-DefaultResidualError"), | ||
const UnsignedInteger maximumFunctionEvaluation = ResourceMap::GetAsUnsignedInteger("Solver-DefaultMaximumFunctionEvaluation")); | ||
|
||
|
||
/** Virtual constructor */ | ||
LeastSquaresEquationsSolver * clone() const override; | ||
|
||
/** String converter */ | ||
String __repr__() const override; | ||
|
||
/** Solve attempt to find one root to a system of equations function(x) = 0 given a starting point x_0 */ | ||
using SolverImplementation::solve; | ||
Point solve(const Function & function, | ||
const Point & startingPoint) const override; | ||
Point solve(const Function & function, | ||
const Point & startingPoint, | ||
const Interval & bounds) const override; | ||
|
||
/** optimization accessors */ | ||
OptimizationAlgorithm getOptimizationAlgorithm() const; | ||
void setOptimizationAlgorithm(const OptimizationAlgorithm & algorithm); | ||
|
||
/** save/load */ | ||
void save(Advocate & adv) const override; | ||
void load(Advocate & adv) override; | ||
|
||
private: | ||
/** optimization solver */ | ||
OptimizationAlgorithm solver_; | ||
|
||
}; /* Class LeastSquaresEquationsSolver */ | ||
|
||
END_NAMESPACE_OPENTURNS | ||
|
||
#endif /* OPENTURNS_LsqSolver_HXX */ |
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
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,59 @@ | ||
// -*- C++ -*- | ||
/** | ||
* @brief The test file of class LeastSquaresEquationsSolver for standard methods | ||
* | ||
* Copyright 2005-2025 Airbus-EDF-IMACS-ONERA-Phimeca | ||
* | ||
* This library is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This library is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this library. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
#include "openturns/OT.hxx" | ||
#include "openturns/OTtestcode.hxx" | ||
|
||
using namespace OT; | ||
using namespace OT::Test; | ||
|
||
int main(int, char *[]) | ||
{ | ||
TESTPREAMBLE; | ||
OStream fullprint(std::cout); | ||
|
||
try | ||
{ | ||
/** Analytical construction */ | ||
Description input = {"x","y"}; | ||
Description formulas = {"y * x - sin(2 * x)","1 + cos(y) + x"}; | ||
SymbolicFunction analytical(input, formulas); | ||
|
||
LeastSquaresEquationsSolver algo; | ||
algo.setResidualError(1e-5); | ||
algo.setMaximumCallsNumber(1000), | ||
fullprint << "algo=" << algo << std::endl; | ||
Point startingPoint = {2.0, 1.0}; | ||
const Point optimalValue(2); | ||
Point solution(algo.solve(analytical, startingPoint)); | ||
fullprint << "Solve " << formulas << "= [0,0] for " << input <<std::endl; | ||
fullprint << "[x,y] = " << solution << std::endl; | ||
fullprint << "algo=" << algo << std::endl; | ||
assert_almost_equal(analytical(solution), optimalValue, 1e-5, 1e-5); | ||
} | ||
catch (TestFailed & ex) | ||
{ | ||
std::cerr << ex << std::endl; | ||
return ExitCode::Error; | ||
} | ||
|
||
|
||
return ExitCode::Success; | ||
} |
Oops, something went wrong.