NConstraintInterface.hh 3.01 KB
Newer Older
David Bommes's avatar
David Bommes committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//=============================================================================
//
//  CLASS NConstraintInterface
//
//=============================================================================


#ifndef COMISO_NCONSTRAINTINTERFACE_HH
#define COMISO_NCONSTRAINTINTERFACE_HH


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

#include "SuperSparseMatrixT.hh"
#define EIGEN_YES_I_KNOW_SPARSE_MODULE_IS_NOT_STABLE_YET
#include <Eigen/Sparse>

//== FORWARDDECLARATIONS ======================================================

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

namespace COMISO {

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

	      

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

    Brief Description.
  
    A more elaborate description follows.
*/
class NConstraintInterface
{
public:
  
  // 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
  NConstraintInterface(const ConstraintType _type = NC_EQUAL) : type_(_type) {}
 
  /// Destructor
  ~NConstraintInterface() {}

  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;

  virtual ConstraintType  constraint_type (                                      ) { return type_; }

  virtual bool            is_satisfied    ( const double* _x, double _eps = 1e-6 )
  {
    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;
  }

  virtual double gradient_update_factor( const double* _x, double _eps = 1e-6 )
  {
    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;
    }
  }


private:
  // constraint type
  ConstraintType type_;
};


//=============================================================================
} // namespace COMISO
//=============================================================================
#endif // ACG_NCONSTRAINTINTERFACE_HH defined
//=============================================================================