NConstraintInterface.hh 3.6 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


David Bommes's avatar
David Bommes committed
21
22
23
#define EIGEN_YES_I_KNOW_SPARSE_MODULE_IS_NOT_STABLE_YET
#include <Eigen/Sparse>

24

David Bommes's avatar
David Bommes committed
25
26
27
28
//== FORWARDDECLARATIONS ======================================================

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

29
namespace COMISO {
David Bommes's avatar
David Bommes committed
30
31
32

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

33

David Bommes's avatar
David Bommes committed
34
35
36
37

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

    Brief Description.
38

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

David Bommes's avatar
David Bommes committed
45
46
47
48
49
50
51
52
  // 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
53
  NConstraintInterface(const ConstraintType _type = NC_EQUAL, double _eps = 1e-6) : type_(_type) {}
54

David Bommes's avatar
David Bommes committed
55
  /// Destructor
David Bommes's avatar
David Bommes committed
56
  virtual ~NConstraintInterface() {}
David Bommes's avatar
David Bommes committed
57
58
59
60
61
62

  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;

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

65
  virtual bool            is_satisfied    ( const double* _x, double _eps = 1e-6)
66
67
68
69
70
71
72
73
74
75
  {
    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;
  }

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

82
  virtual double gradient_update_factor( const double* _x, double _eps = 1e-6)
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
  {
    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
100
101
102
103
104
105
106
107
108
109

private:
  // constraint type
  ConstraintType type_;
};


//=============================================================================
} // namespace COMISO
//=============================================================================
David Bommes's avatar
David Bommes committed
110
#endif // COMISO_EIGEN3_AVAILABLE
111
//=============================================================================
David Bommes's avatar
David Bommes committed
112
113
114
#endif // ACG_NCONSTRAINTINTERFACE_HH defined
//=============================================================================