OsiColCut.hpp
Go to the documentation of this file.
1 // Copyright (C) 2000, International Business Machines
2 // Corporation and others. All Rights Reserved.
3 #ifndef OsiColCut_H
4 #define OsiColCut_H
5 
6 #include <string>
7 
8 #include "CoinPackedVector.hpp"
9 
10 #include "OsiCollections.hpp"
11 #include "OsiCut.hpp"
12 
21 class OsiColCut : public OsiCut {
22  friend void OsiColCutUnitTest(const OsiSolverInterface * baseSiP,
23  const std::string & mpsDir);
24 
25 public:
26 
27  //----------------------------------------------------------------
28 
31 
32  inline void setLbs(
33  int nElements,
34  const int * colIndices,
35  const double * lbElements );
36 
38  inline void setLbs( const CoinPackedVector & lbs );
39 
41  inline void setUbs(
42  int nElements,
43  const int * colIndices,
44  const double * ubElements );
45 
47  inline void setUbs( const CoinPackedVector & ubs );
49 
50  //----------------------------------------------------------------
51 
54 
55  inline const CoinPackedVector & lbs() const;
57  inline const CoinPackedVector & ubs() const;
59 
62 #if __GNUC__ != 2
63  using OsiCut::operator== ;
64 #endif
65 
68  inline virtual bool operator==(const OsiColCut& rhs) const;
69 
70 #if __GNUC__ != 2
71  using OsiCut::operator!= ;
72 #endif
73 
74  inline virtual bool operator!=(const OsiColCut& rhs) const;
76 
77 
78  //----------------------------------------------------------------
79 
89  inline virtual bool consistent() const;
90 
98  inline virtual bool consistent(const OsiSolverInterface& im) const;
99 
108  inline virtual bool infeasible(const OsiSolverInterface &im) const;
113  virtual double violated(const double * solution) const;
115 
116  //----------------------------------------------------------------
117 
120 
121  OsiColCut & operator=( const OsiColCut& rhs);
122 
124  OsiColCut ( const OsiColCut &);
125 
127  OsiColCut ();
128 
130  virtual OsiColCut * clone() const;
131 
133  virtual ~OsiColCut ();
135 
138 
139  virtual void print() const;
141 
142 private:
143 
146 
147  CoinPackedVector lbs_;
149  CoinPackedVector ubs_;
151 
152 };
153 
154 
155 
156 //-------------------------------------------------------------------
157 // Set lower & upper bound vectors
158 //-------------------------------------------------------------------
160  int size,
161  const int * colIndices,
162  const double * lbElements )
163 {
164  lbs_.setVector(size,colIndices,lbElements);
165 }
166 //
168  int size,
169  const int * colIndices,
170  const double * ubElements )
171 {
172  ubs_.setVector(size,colIndices,ubElements);
173 }
174 //
175 void OsiColCut::setLbs( const CoinPackedVector & lbs )
176 {
177  lbs_ = lbs;
178 }
179 //
180 void OsiColCut::setUbs( const CoinPackedVector & ubs )
181 {
182  ubs_ = ubs;
183 }
184 
185 //-------------------------------------------------------------------
186 // Get Column Lower Bounds and Column Upper Bounds
187 //-------------------------------------------------------------------
188 const CoinPackedVector & OsiColCut::lbs() const
189 {
190  return lbs_;
191 }
192 //
193 const CoinPackedVector & OsiColCut::ubs() const
194 {
195  return ubs_;
196 }
197 
198 //----------------------------------------------------------------
199 // == operator
200 //-------------------------------------------------------------------
201 bool
203  const OsiColCut& rhs) const
204 {
205  if ( this->OsiCut::operator!=(rhs) )
206  return false;
207  if ( lbs() != rhs.lbs() )
208  return false;
209  if ( ubs() != rhs.ubs() )
210  return false;
211  return true;
212 }
213 //
214 bool
216  const OsiColCut& rhs) const
217 {
218  return !( (*this)==rhs );
219 }
220 
221 //----------------------------------------------------------------
222 // consistent & infeasible
223 //-------------------------------------------------------------------
225 {
226  const CoinPackedVector & lb = lbs();
227  const CoinPackedVector & ub = ubs();
228  // Test for consistent cut.
229  // Are packed vectors consistent?
230  lb.duplicateIndex("consistent", "OsiColCut");
231  ub.duplicateIndex("consistent", "OsiColCut");
232  if ( lb.getMinIndex() < 0 ) return false;
233  if ( ub.getMinIndex() < 0 ) return false;
234  return true;
235 }
236 //
238 {
239  const CoinPackedVector & lb = lbs();
240  const CoinPackedVector & ub = ubs();
241 
242  // Test for consistent cut.
243  if ( lb.getMaxIndex() >= im.getNumCols() ) return false;
244  if ( ub.getMaxIndex() >= im.getNumCols() ) return false;
245 
246  return true;
247 }
248 
249 #if 0
250 bool OsiColCut::feasible(const OsiSolverInterface &im) const
251 {
252  const double * oldColLb = im.getColLower();
253  const double * oldColUb = im.getColUpper();
254  const CoinPackedVector & cutLbs = lbs();
255  const CoinPackedVector & cutUbs = ubs();
256  int i;
257 
258  for ( i=0; i<cutLbs.size(); i++ ) {
259  int colIndx = cutLbs.indices()[i];
260  double newLb;
261  if ( cutLbs.elements()[i] > oldColLb[colIndx] )
262  newLb = cutLbs.elements()[i];
263  else
264  newLb = oldColLb[colIndx];
265 
266  double newUb = oldColUb[colIndx];
267  if ( cutUbs.indexExists(colIndx) )
268  if ( cutUbs[colIndx] < newUb ) newUb = cutUbs[colIndx];
269  if ( newLb > newUb )
270  return false;
271  }
272 
273  for ( i=0; i<cutUbs.size(); i++ ) {
274  int colIndx = cutUbs.indices()[i];
275  double newUb = cutUbs.elements()[i] < oldColUb[colIndx] ? cutUbs.elements()[i] : oldColUb[colIndx];
276  double newLb = oldColLb[colIndx];
277  if ( cutLbs.indexExists(colIndx) )
278  if ( cutLbs[colIndx] > newLb ) newLb = cutLbs[colIndx];
279  if ( newUb < newLb )
280  return false;
281  }
282 
283  return true;
284 }
285 #endif
286 
287 
289 {
290  const double * oldColLb = im.getColLower();
291  const double * oldColUb = im.getColUpper();
292  const CoinPackedVector & cutLbs = lbs();
293  const CoinPackedVector & cutUbs = ubs();
294  int i;
295 
296  for ( i=0; i<cutLbs.getNumElements(); i++ ) {
297  int colIndx = cutLbs.getIndices()[i];
298  double newLb= cutLbs.getElements()[i] > oldColLb[colIndx] ?
299  cutLbs.getElements()[i] : oldColLb[colIndx];
300 
301  double newUb = oldColUb[colIndx];
302  if ( cutUbs.isExistingIndex(colIndx) )
303  if ( cutUbs[colIndx] < newUb ) newUb = cutUbs[colIndx];
304  if ( newLb > newUb )
305  return true;
306  }
307 
308  for ( i=0; i<cutUbs.getNumElements(); i++ ) {
309  int colIndx = cutUbs.getIndices()[i];
310  double newUb = cutUbs.getElements()[i] < oldColUb[colIndx] ?
311  cutUbs.getElements()[i] : oldColUb[colIndx];
312  double newLb = oldColLb[colIndx];
313  if ( cutLbs.isExistingIndex(colIndx) )
314  if ( cutLbs[colIndx] > newLb ) newLb = cutLbs[colIndx];
315  if ( newUb < newLb )
316  return true;
317  }
318 
319  return false;
320 }
321 
322 //#############################################################################
328 void
329 OsiColCutUnitTest(const OsiSolverInterface * baseSiP,
330  const std::string & mpsDir);
331 
332 #endif