summaryrefslogtreecommitdiff
path: root/build/Bonmin/include/coin/BonQuadCut.hpp
blob: 8cbf0c8ff34e27e55bf419d3302f4fc37267781f (plain)
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// (C) Copyright International Business Machines Corporation 2007
// All Rights Reserved.
// This code is published under the Eclipse Public License.
//
// Authors :
// Pierre Bonami, International Business Machines Corporation
//
// Date : 10/06/2007

#ifndef BonQuadCut_H
#define BonQuadCut_H

#include "CoinPackedMatrix.hpp"
#include "OsiRowCut.hpp"
#include "OsiCuts.hpp"
#include "BonTypes.hpp"
#include <list>


namespace Bonmin {

  enum MatrixStorageType {
   Upper /** Stores only the upper triangle of a symetric Q.*/,
   Lower /** Stores the lower triangle of a symetric Q.*/,
   Full /** Stores the whole matrix of a non-symetric Q.*/};

class QuadCut : public OsiRowCut {
 public:

  /// Default constructor
  QuadCut();

  /// Copy constructor
  QuadCut(const QuadCut & other);

  /// Assignment operator
  QuadCut& operator=(const QuadCut & rhs);

  /// Virtual copy
  virtual OsiRowCut * clone() const;

  /// Destructor
  ~QuadCut(); 

  /// Print
  void print() const; 

  ///Return the matrix stored
  CoinPackedMatrix& Q(){
   return Q_;
  }

  ///Return the matrix stored
  const CoinPackedMatrix& Q() const{
   return Q_;
  }

  /// Acces storage type
  /// Acces storage type
  MatrixStorageType& type(){
    return type_;}

  const MatrixStorageType& type() const{
    return type_;}

  /// Acces the constant
  double & c(){return c_;}

  /// Acces the constant
  const double & c() const {return c_;}

  /// Compute cut violation
  double violated(const double * solution) const;

 private:
   /// Stores the constant part of the cut
   double c_;
   ///Stores quadratic part of cut
   CoinPackedMatrix Q_;
   ///Storage type
   MatrixStorageType type_;

   /** \name Arithmetic operators not implemented.*/
  //@{
    /// add <code>value</code> to every vector entry
    void operator+=(double value);

    /// subtract <code>value</code> from every vector entry
    void operator-=(double value);

    /// multiply every vector entry by <code>value</code>
    void operator*=(double value);

    /// divide every vector entry by <code>value</code>
    void operator/=(double value);
  //@}

};

/** Generalizes OsiCuts to handle quadratic cuts.*/
class Cuts : public OsiCuts {
 public:
  typedef vector<QuadCut *> QuadCutPtrStorage;
  /** Default constructor.*/
  Cuts();

  /** Copy constructor.*/
  Cuts(const Cuts& other);

  /** Assignment operator.*/
  Cuts& operator=(const Cuts & rhs);

 /** Destructor */
 ~Cuts();

 /** insert a quadratic cut into the collection. */
 inline void insert(const QuadCut& c);

 /** insert a quadratic cut into the collection (take control of the pointer and
     put a NULL on return).
     \warning c has to have been created with new (no malloc).
   */
  inline void insert(QuadCut* &c);

 /** insert a set of Cuts.*/
  inline void insert(const Cuts &cs);

 /** Number of quadratic cuts in the collection.*/
  inline int sizeQuadCuts() const;

 /** Total number of cuts in the collection. */
 inline int sizeCuts() const;

 /** Print all cuts in the collection.*/
 void printCuts() const;


 /** Access to a quadratic cut by pointer.*/
 inline QuadCut * quadCutPtr(int i);

 /** Access to a quadratic cut by const pointer.*/
 inline const QuadCut * quadCutPtr(int i) const;

 /** Access to a quadratic cut by reference.*/
 inline QuadCut& quadCut(int i);


 /** Access to a quadratic cut by reference.*/
 inline const QuadCut& quadCut(int i) const;

 /** Erase quadratic cut from the collection.*/
 inline void eraseQuadCut(int i);

 private:
   QuadCutPtrStorage quadCuts_;
};

void
Cuts::insert(const QuadCut &c){
  quadCuts_.push_back(new QuadCut(c));
}

void
Cuts::insert(QuadCut * &c){
  quadCuts_.push_back(c);
  c = NULL;
}

void 
Cuts::insert(const Cuts & cs){
  OsiCuts::insert(cs);
  for(unsigned int i = 0 ; i < cs.quadCuts_.size() ; i++){
    quadCuts_.push_back(new QuadCut(*cs.quadCuts_[i]));
  }
}

int 
Cuts::sizeQuadCuts() const {
  return static_cast<int>(quadCuts_.size());
}

int
Cuts::sizeCuts() const {
  return static_cast<int>(quadCuts_.size()) + OsiCuts::sizeCuts();
}

QuadCut *
Cuts::quadCutPtr(int i) {
  return quadCuts_[i];
}

const QuadCut *
Cuts::quadCutPtr(int i) const {
  return quadCuts_[i];
}

QuadCut &
Cuts::quadCut(int i) {
  return *quadCuts_[i];
}

const QuadCut &
Cuts::quadCut(int i) const {
  return *quadCuts_[i];
}

void
Cuts::eraseQuadCut(int i){
  delete quadCuts_[i];
  quadCuts_.erase(quadCuts_.begin() + i);
}
typedef std::list<QuadCut*> list_QuadCut; 

}// Ends Bonmin namespace
#endif