LinearConstraintHandlerPenalty.cc 3.24 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
//=============================================================================
//
//  CLASS LinearConstraintHandlerPenalty - IMPLEMENTATION TEMPLATES
//
//=============================================================================



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

#include "LinearConstraintHandlerPenalty.hh"

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

David Bommes's avatar
David Bommes committed
15
namespace COMISO {
16 17 18 19

//== IMPLEMENTATION ==========================================================


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
/// Constructor
LinearConstraintHandlerPenalty::LinearConstraintHandlerPenalty() : penalty_(10000) {}


//-----------------------------------------------------------------------------

/// Destructor
LinearConstraintHandlerPenalty::~LinearConstraintHandlerPenalty()
{}

//-----------------------------------------------------------------------------

// penalty weight
double& LinearConstraintHandlerPenalty::penalty()
{ return penalty_; }

//-----------------------------------------------------------------------------

// number of variables
int LinearConstraintHandlerPenalty::n()
{return n_;}

//-----------------------------------------------------------------------------

// number of linearly independent constraints (n-n_reduced)
int LinearConstraintHandlerPenalty::n_constraints()
{ return m_;}

//-----------------------------------------------------------------------------

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 118 119 120 121 122 123 124 125 126 127 128 129
void
LinearConstraintHandlerPenalty::
initialize( const std::vector<double>& _c)
{
 if( _c.size() )
   initialize( (double*) &(_c[0]));
}


//-----------------------------------------------------------------------------


void
LinearConstraintHandlerPenalty::
initialize( double* _c)
{
  for(unsigned int i=0; i<b_.size(); ++i)
    b_[i] = _c[i];

  penalty_grad_b_.resize(n_);
  gmm::mult(gmm::transposed(C_), b_, penalty_grad_b_);
  gmm::scale(penalty_grad_b_, -2.0*penalty_);
}

//-----------------------------------------------------------------------------

double
LinearConstraintHandlerPenalty::
add_penalty_f( double* _x, const double _f)
{

  temp_.resize(b_.size());
  gmm::mult(C_, VectorPT(_x, n_), temp_);
  gmm::add(gmm::scaled(b_, -1.0), temp_);

  double fp = ( penalty_* (gmm::vect_sp(temp_, temp_)) );

  return _f + fp;
}

//-----------------------------------------------------------------------------


void
LinearConstraintHandlerPenalty::
add_penalty_gradient( const std::vector<double>& _x, std::vector<double>& _g)
{
  _g.resize(n_);
  if( _x.size() && _g.size())
    add_penalty_gradient( (double*)&(_x[0]), &(_g[0]));
}


//-----------------------------------------------------------------------------


void
LinearConstraintHandlerPenalty::
add_penalty_gradient( double* _x, double* _g)
{
  gmm::add( penalty_grad_b_, VectorPT(_g, n_));
  gmm::mult_add(penalty_H_, VectorPT(_x, n_), VectorPT(_g, n_));
}


//-----------------------------------------------------------------------------


void
LinearConstraintHandlerPenalty::
add_penalty_hessian( RMatrix& _H)
{
  gmm::add(penalty_H_, _H);
}


//-----------------------------------------------------------------------------


//=============================================================================
David Bommes's avatar
David Bommes committed
130
} // namespace COMISO
131
//=============================================================================