Debian Codegraph @

Bonmin/src/Algorithms/QuadCuts/BonQuadRow.hpp

raw
1 /// (C) Copyright International Business Machines Corporation 2007
2 // All Rights Reserved.
3 // This code is published under the Eclipse Public License.
4 //
5 // Authors :
6 // Pierre Bonami, International Business Machines Corporation
7 //
8 // Date : 10/06/2007
9
10 #ifndef BonQuadRow_H
11 #define BonQuadRow_H
12
13 #include "CoinPackedVector.hpp"
14 #include "BonTMatrix.hpp"
15 #include "BonQuadCut.hpp"
16
17 namespace Bonmin{
18
19 /** Store column and row of the entry.*/
20 typedef std::pair<int, int> matEntry;
21 /** Store the number of times entry is used and its index in the matrix.*/
22 typedef std::pair<int, int> matIdx;
23 #if HAS_HASH_MAP
24 typedef std::has_map<matEntry, matIdx, std::hash< matEntry> > AdjustableMat;
25 #else
26 typedef std::map<matEntry, matIdx> AdjustableMat;
27 #endif
28
29 /** Stores a quadratic row of the form l < c + ax + x^T Q x < u.
30 Does computation usefull for nlp-solver.
31 It can only be initialized from a QuadCut.*/
32 class QuadRow {
33 public:
34 /** Default constructor.*/
35 QuadRow();
36
37 /** Copy constructor.*/
38 QuadRow(const QuadRow & other);
39
40 /** Assignment operator.*/
41 QuadRow& operator=(const QuadRow& rhs);
42
43 /** Constructor from a quadratic cut.*/
44 QuadRow(const QuadCut &cut);
45
46 /** Assignment form a quadrattic &cut.*/
47 QuadRow& operator=(const QuadCut & rhs);
48
49 /** Constructor from a linear cut.*/
50 QuadRow(const OsiRowCut &cut);
51
52 /** Assignment form a linear &cut.*/
53 QuadRow& operator=(const OsiRowCut & rhs);
54
55 /** Evaluate quadratic form.*/
56 double eval_f(const double *x, bool new_x);
57
58 /** Get number of non-zeroes in the gradiant.*/
59 int nnz_grad();
60 /** Get structure of gradiant */
61 void gradiant_struct(const int nnz, int * indices, bool offset);
62 /** Evaluate gradiant of quadratic form.*/
63 void eval_grad(const int nnz, const double * x, bool new_x, double * values);
64
65 /** number of non-zeroes in hessian. */
66 int nnz_hessian(){
67 return Q_.nnz_;}
68
69 /** Says if the constraint is linear.*/
70 bool isLinear(){
71 return Q_.nnz_ == 0;}
72
73 /** Return hessian value (i.e. Q_).*/
74 void eval_hessian(double lambda, double * values);
75
76 /** Add row to a bigger hessian.*/
77 void add_to_hessian(AdjustableMat &H, bool offset);
78
79 /** Remove row from a bigger hessian.*/
80 void remove_from_hessian(AdjustableMat &H);
81 /** Print quadratic constraint.*/
82 void print();
83
84 private:
85 /** Initialize once quadratic form is know.*/
86 void initialize();
87
88 /** Does internal work to evaluate gradiant of this in x.*/
89 void internal_eval_grad(const double *x);
90
91 /** lower bound.*/
92 double lb_;
93 /** upper bound.*/
94 double ub_;
95 /** Constant term.*/
96 double c_;
97 /** linear term in sparse storage.*/
98 CoinPackedVector a_;
99 /** Quadratic term.*/
100 TMat Q_;
101
102
103 #if HAS_HASH_MAP
104 typedef std::has_map<int, std::pair<double, double >, std::hash<int> > gStore;
105 #else
106 typedef std::map<int, std::pair<double, double> > gStore;
107 #endif
108
109 gStore g_;
110 /** To have fast access to gradiant entries for a_.*/
111 std::vector<gStore::iterator> a_grad_idx_;
112 /** To have fast access to gradient entries for rows Q_*/
113 std::vector<gStore::iterator> Q_row_grad_idx_;
114 /** To have fast access to gradient entries for cols Q_*/
115 std::vector<gStore::iterator> Q_col_grad_idx_;
116 /** To have fast access to entries in full hessian of Q_*/
117 std::vector<AdjustableMat::iterator> Q_hessian_idx_;
118 /** Flag indicating if gradiant has been evaluated.*/
119 bool grad_evaled_;
120 };
121 }//End Bonmin namespace
122 #endif