Commit 6bef7e44 authored by David Bommes's avatar David Bommes
Browse files

added CombinedProblem class

parent 1e68822a
Pipeline #3500 passed with stage
in 6 minutes and 41 seconds
//== COMPILE-TIME PACKAGE REQUIREMENTS ========================================
#include <CoMISo/Config/config.hh>
#if COMISO_EIGEN3_AVAILABLE
//== INCLUDES =================================================================
#include "CombinedProblem.hh"
namespace COMISO {
CombinedProblem::CombinedProblem (NProblemInterface* _p1, NProblemInterface* _p2, const double _c1, const double _c2)
: p1_(_p1) , p2_(_p2), c1_(_c1), c2_(_c2)
{
if(p1_->n_unknowns() != p2_->n_unknowns())
std::cerr << "Warning: CombinedProblem received two problems with different #unknowns!!!" << std::endl;
g_temp_.resize(p1_->n_unknowns());
}
CombinedProblem::~CombinedProblem()
{
}
int CombinedProblem::n_unknowns()
{
return p1_->n_unknowns();
}
void CombinedProblem::initial_x(double* _x)
{
// use initial_x of first problem
p1_->initial_x(_x);
}
double CombinedProblem::eval_f( const double* _x)
{
return c1_*p1_->eval_f(_x) + c2_*p2_->eval_f(_x);
}
void CombinedProblem::eval_gradient( const double* _x, double* _g)
{
p1_->eval_gradient(_x, _g);
p2_->eval_gradient(_x, g_temp_.data());
for( int i=0; i<this->n_unknowns(); ++i)
_g[i] = c1_*_g[i]+c2_*g_temp_[i];
}
void CombinedProblem::eval_hessian ( const double* _x, SMatrixNP& _H)
{
SMatrixNP H1, H2;
p1_->eval_hessian(_x, H1);
p2_->eval_hessian(_x, H2);
_H.resize(n_unknowns(), n_unknowns());
_H = c1_*H1 + c2_*H2;
}
void CombinedProblem::store_result ( const double* _x )
{
p1_->store_result(_x);
p2_->store_result(_x);
}
bool CombinedProblem::constant_gradient() const
{
return (p1_->constant_gradient() && p2_->constant_gradient());
}
bool CombinedProblem::constant_hessian() const
{
return (p1_->constant_hessian() && p2_->constant_hessian());
}
//=============================================================================
} // namespace COMISO
//=============================================================================
#endif // COMISO_EIGEN3_AVAILABLE
//=============================================================================
//=============================================================================
//
// CLASS CombinedProblem
//
//=============================================================================
#ifndef COMISO_COMBINEDPROBLEM_HH
#define COMISO_COMBINEDPROBLEM_HH
//== COMPILE-TIME PACKAGE REQUIREMENTS ========================================
#include <CoMISo/Config/config.hh>
#if COMISO_EIGEN3_AVAILABLE
//== INCLUDES =================================================================
#include <stdio.h>
#include <iostream>
#include <vector>
#include <CoMISo/Config/CoMISoDefines.hh>
#include <CoMISo/NSolver/NProblemInterface.hh>
//== FORWARDDECLARATIONS ======================================================
//== NAMESPACES ===============================================================
namespace COMISO {
//== CLASS DEFINITION =========================================================
/** \class CombinedProblem CombinedProblem.hh <CoMISo/NSolver/CombinedProblem.hh>
Create Problem of the type f(x) = _c1*_p1(x) + _c2*_p2(x), which is the linear combination
of two other problems (specified in the constructor).
Note: the initial_x is taken from _p1! And the result is stored for both _p1 and _p2
A more elaborate description follows.
*/
class COMISODLLEXPORT CombinedProblem : public NProblemInterface
{
public:
/// Default constructor
CombinedProblem (NProblemInterface* _p1, NProblemInterface* _p2, const double _c1 = 1.0, const double _c2 = 1.0);
/// Destructor
virtual ~CombinedProblem();
// problem definition
virtual int n_unknowns();
virtual void initial_x(double* _x);
virtual double eval_f( const double* _x);
virtual void eval_gradient( const double* _x, double* _g);
virtual void eval_hessian ( const double* _x, SMatrixNP& _H);
virtual void store_result ( const double* _x );
// advanced properties
virtual bool constant_gradient() const;
virtual bool constant_hessian() const;
private:
// pointer to two original problems
NProblemInterface* p1_;
NProblemInterface* p2_;
// weighting coefficients
double c1_;
double c2_;
// temporary variables
std::vector<double> g_temp_;
};
//=============================================================================
} // namespace COMISO
//=============================================================================
#endif // COMISO_EIGEN3_AVAILABLE
//=============================================================================
#endif // COMISO_NPROBLEMGMMINTERFACE_HH defined
//=============================================================================
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment