LinearConstraint.hh 3.18 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//=============================================================================
//
//  CLASS NConstraintGmmInterface
//
//=============================================================================


#ifndef COMISO_LINEARCONSTRAINT_HH
#define COMISO_LINEARCONSTRAINT_HH


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

#include <gmm/gmm.h>
#include "NConstraintGmmInterface.hh"

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

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

namespace COMISO {

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

	      

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

    Brief Description.
  
    A more elaborate description follows.
*/
class LinearConstraint : public NConstraintGmmInterface
{
public:
  
  // ToDo: appropriate Vector/MatrixType ???
  typedef gmm::wsvector<double>        SVectorNP;
  typedef gmm::row_matrix< SVectorNP > SMatrixNP;

  // use c-arrays as vectors for gmm
  typedef gmm::array1D_reference<double*> VectorPT;

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

  /// Default constructor
  LinearConstraint(const ConstraintType _type = NC_EQUAL) : NConstraintGmmInterface(_type)
  {}

  // linear equation of the form -> coeffs_^T * (x,1) =_type= 0
  LinearConstraint(const SVectorNP& _coeffs_plus_const, const ConstraintType _type = NC_EQUAL) : NConstraintGmmInterface(_type)
  {
    int n = gmm::vect_size(_coeffs_plus_const)-1;

    gmm::resize(coeffs_, n+1);
    gmm::copy(_coeffs_plus_const, coeffs_);

    if( n >= 0)
      b_ = coeffs_[n];
    else
      b_ = 0.0;

    gmm::resize(coeffs_, n);
  }

  // linear equation of the form -> coeffs_^T * (x,1) =_type= 0
  LinearConstraint(const SVectorNP& _coeffs, const double _b, const ConstraintType _type = NC_EQUAL) : NConstraintGmmInterface(_type)
  {
    int n = gmm::vect_size(_coeffs);
    gmm::resize(coeffs_, n);
    gmm::copy(_coeffs, coeffs_);
    b_ = _b;
  }

  /// Destructor
  ~LinearConstraint() {}

  virtual int n_unknowns()
  {
    return gmm::vect_size(coeffs_);
  }

  virtual double eval_constraint ( const double* _x )
  {
     return (gmm::vect_sp(coeffs_, VectorPT((double*)_x, gmm::vect_size(coeffs_))) + b_);
  }

  virtual void eval_gradient( const double* _x, SVectorNP& _g      )
  {
    gmm::resize(_g, gmm::vect_size(coeffs_));
    gmm::copy  (coeffs_, _g);
  }

  virtual void eval_hessian    ( const double* _x, SMatrixNP& _h      )
  {
    gmm::resize(_h, gmm::vect_size(coeffs_), gmm::vect_size(coeffs_));
    gmm::clear(_h);
  }

  // inherited from base
//  virtual ConstraintType  constraint_type (                                      ) { return type_; }

private:

  // linear equation of the form -> coeffs_^T * x + b_
  SVectorNP coeffs_;
  double    b_;
};


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