NConstraintInterface.hh 3.76 KB
Newer Older
David Bommes's avatar
David Bommes committed
1 2 3 4 5 6 7 8 9 10
//=============================================================================
//
//  CLASS NConstraintInterface
//
//=============================================================================


#ifndef COMISO_NCONSTRAINTINTERFACE_HH
#define COMISO_NCONSTRAINTINTERFACE_HH

11 12
//== COMPILE-TIME PACKAGE REQUIREMENTS ========================================
#include <CoMISo/Config/config.hh>
David Bommes's avatar
David Bommes committed
13
#if COMISO_EIGEN3_AVAILABLE
David Bommes's avatar
David Bommes committed
14 15 16

//== INCLUDES =================================================================

Jan Möbius's avatar
Jan Möbius committed
17
#include <CoMISo/Config/CoMISoDefines.hh>
David Bommes's avatar
David Bommes committed
18
#include "SuperSparseMatrixT.hh"
19 20


Max Lyon's avatar
Max Lyon committed
21 22
#include <Base/Code/Quality.hh>
LOW_CODE_QUALITY_SECTION_BEGIN
David Bommes's avatar
David Bommes committed
23 24
#define EIGEN_YES_I_KNOW_SPARSE_MODULE_IS_NOT_STABLE_YET
#include <Eigen/Sparse>
Max Lyon's avatar
Max Lyon committed
25
LOW_CODE_QUALITY_SECTION_END
David Bommes's avatar
David Bommes committed
26

27

David Bommes's avatar
David Bommes committed
28 29 30 31
//== FORWARDDECLARATIONS ======================================================

//== NAMESPACES ===============================================================

32
namespace COMISO {
David Bommes's avatar
David Bommes committed
33 34 35

//== CLASS DEFINITION =========================================================

36

David Bommes's avatar
David Bommes committed
37 38 39 40

/** \class NProblemGmmInterface NProblemGmmInterface.hh <ACG/.../NPRoblemGmmInterface.hh>

    Brief Description.
41

David Bommes's avatar
David Bommes committed
42 43
    A more elaborate description follows.
*/
Jan Möbius's avatar
Jan Möbius committed
44
class COMISODLLEXPORT NConstraintInterface
David Bommes's avatar
David Bommes committed
45 46
{
public:
47

David Bommes's avatar
David Bommes committed
48 49 50 51 52 53 54 55
  // define Sparse Datatypes
  typedef Eigen::SparseVector<double> SVectorNC;
  typedef SuperSparseMatrixT<double>  SMatrixNC;

  // different types of constraints
  enum ConstraintType {NC_EQUAL, NC_LESS_EQUAL, NC_GREATER_EQUAL};

  /// Default constructor
56
  NConstraintInterface(const ConstraintType _type = NC_EQUAL, double _eps = 1e-6) : type_(_type) {}
57

David Bommes's avatar
David Bommes committed
58
  /// Destructor
David Bommes's avatar
David Bommes committed
59
  virtual ~NConstraintInterface() {}
David Bommes's avatar
David Bommes committed
60 61 62 63 64 65

  virtual int             n_unknowns      (                                      ) = 0;
  virtual double          eval_constraint ( const double* _x                     ) = 0;
  virtual void            eval_gradient   ( const double* _x, SVectorNC& _g      ) = 0;
  virtual void            eval_hessian    ( const double* _x, SMatrixNC& _h      ) = 0;

66
  virtual ConstraintType& constraint_type (                                      ) { return type_; }
David Bommes's avatar
David Bommes committed
67

68
  virtual bool            is_satisfied    ( const double* _x, double _eps = 1e-6)
69 70 71 72 73 74 75 76 77 78
  {
    switch( type_)
    {
      case NC_EQUAL        : return (fabs(eval_constraint(_x)) <=  _eps); break;
      case NC_LESS_EQUAL   : return (     eval_constraint(_x)  <=  _eps); break;
      case NC_GREATER_EQUAL: return (     eval_constraint(_x)  >= -_eps); break;
    }
    return false;
  }

79 80 81 82
  // provide special properties
  virtual bool   is_linear()         const { return false;}
  virtual bool   constant_gradient() const { return false;}
  virtual bool   constant_hessian () const { return false;}
83
  virtual bool   sparse_hessian()    const { return true; }
David Bommes's avatar
David Bommes committed
84

85 86
  bool linear_equality() const { return is_linear() && type_ == NC_EQUAL; }

87
  virtual double gradient_update_factor( const double* _x, double _eps = 1e-6)
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
  {
    double val = eval_constraint(_x);
    bool   upper_bound_ok = ( val <=  _eps);
    bool   lower_bound_ok = ( val >= -_eps);

    if(upper_bound_ok)
    {
      if(lower_bound_ok || type_ == NC_LESS_EQUAL) return 0.0;
      else                                         return 1.0;
    }
    else
    {
      if(lower_bound_ok && type_ == NC_GREATER_EQUAL) return  0.0;
      else                                            return -1.0;
    }
  }

David Bommes's avatar
David Bommes committed
105 106 107 108 109 110 111 112 113 114

private:
  // constraint type
  ConstraintType type_;
};


//=============================================================================
} // namespace COMISO
//=============================================================================
David Bommes's avatar
David Bommes committed
115
#endif // COMISO_EIGEN3_AVAILABLE
116
//=============================================================================
David Bommes's avatar
David Bommes committed
117 118 119
#endif // ACG_NCONSTRAINTINTERFACE_HH defined
//=============================================================================