Cbc  2.9.9
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
OsiCuts.hpp
Go to the documentation of this file.
1 // Copyright (C) 2000, International Business Machines
2 // Corporation and others. All Rights Reserved.
3 // This code is licensed under the terms of the Eclipse Public License (EPL).
4 
5 #ifndef OsiCuts_H
6 #define OsiCuts_H
7 
8 #include "CoinPragma.hpp"
9 
10 #include <cmath>
11 #include <cfloat>
12 #include "OsiCollections.hpp"
13 #include "OsiRowCut.hpp"
14 #include "OsiColCut.hpp"
15 #include "CoinFloatEqual.hpp"
16 
19 class OsiCuts {
20  friend void OsiCutsUnitTest();
21 
22 public:
30  class iterator {
31  friend class OsiCuts;
32  public:
33  iterator(OsiCuts& cuts);
34  iterator(const iterator & src);
35  iterator & operator=( const iterator& rhs);
36  ~iterator ();
37  OsiCut* operator*() const { return cutP_; }
39 
41  {
42  iterator temp = *this;
43  ++*this;
44  return temp;
45  }
46 
47  bool operator==(const iterator& it) const {
49  }
50 
51  bool operator!=(const iterator& it) const {
52  return !((*this)==it);
53  }
54 
55  bool operator<(const iterator& it) const {
57  }
58 
59  private:
60  iterator();
61  // *THINK* : how to inline these without sticking the code here (ugly...)
62  iterator begin();
63  iterator end();
68  };
69 
75  friend class OsiCuts;
76  public:
77  typedef std::forward_iterator_tag iterator_category;
78  typedef OsiCut* value_type;
79  typedef size_t difference_type;
80  typedef OsiCut ** pointer;
81  typedef OsiCut *& reference;
82 
83  public:
84  const_iterator(const OsiCuts& cuts);
85  const_iterator(const const_iterator & src);
86  const_iterator & operator=( const const_iterator& rhs);
87  ~const_iterator ();
88  const OsiCut* operator*() const { return cutP_; }
89 
91 
93  {
94  const_iterator temp = *this;
95  ++*this;
96  return temp;
97  }
98 
99  bool operator==(const const_iterator& it) const {
101  }
102 
103  bool operator!=(const const_iterator& it) const {
104  return !((*this)==it);
105  }
106 
107  bool operator<(const const_iterator& it) const {
109  }
110  private:
111  inline const_iterator();
112  // *THINK* : how to inline these without sticking the code here (ugly...)
115  const OsiCuts * cutsPtr_;
118  const OsiCut * cutP_;
119  };
121 
122  //-------------------------------------------------------------------
123  //
124  // Cuts class definition begins here:
125  //
126  //-------------------------------------------------------------------
127 
131  inline void insert( const OsiRowCut & rc );
134  void insertIfNotDuplicate( OsiRowCut & rc , CoinAbsFltEq treatAsSame=CoinAbsFltEq(1.0e-12) );
137  void insertIfNotDuplicate( OsiRowCut & rc , CoinRelFltEq treatAsSame );
139  inline void insert( const OsiColCut & cc );
140 
146  inline void insert( OsiRowCut * & rcPtr );
152  inline void insert( OsiColCut * & ccPtr );
153 #if 0
154  inline void insert( OsiCut * & cPtr );
155 #endif
156 
158  inline void insert(const OsiCuts & cs);
159 
161 
164  inline int sizeRowCuts() const;
167  inline int sizeColCuts() const;
169  inline int sizeCuts() const;
171 
174  inline void printCuts() const;
177 
180  inline OsiRowCut * rowCutPtr(int i);
183  inline const OsiRowCut * rowCutPtr(int i) const;
185  inline OsiColCut * colCutPtr(int i);
187  inline const OsiColCut * colCutPtr(int i) const;
188 
190  inline OsiRowCut & rowCut(int i);
192  inline const OsiRowCut & rowCut(int i) const;
194  inline OsiColCut & colCut(int i);
196  inline const OsiColCut & colCut(int i) const;
197 
199  inline const OsiCut * mostEffectiveCutPtr() const;
201  inline OsiCut * mostEffectiveCutPtr();
203 
206  inline void eraseRowCut(int i);
209  inline void eraseColCut(int i);
211  inline OsiRowCut * rowCutPtrAndZap(int i);
218  inline void dumpCuts() ;
226  inline void eraseAndDumpCuts(const std::vector<int> to_erase) ;
228 
231  inline void sort();
234 
235 
246  inline iterator begin() { iterator it(*this); it.begin(); return it; }
249  inline const_iterator begin() const { const_iterator it(*this); it.begin(); return it; }
251  inline iterator end() { iterator it(*this); it.end(); return it; }
253  inline const_iterator end() const { const_iterator it(*this); it.end(); return it; }
255 
256 
259  OsiCuts ();
261 
263  OsiCuts ( const OsiCuts &);
264 
266  OsiCuts & operator=( const OsiCuts& rhs);
267 
269  virtual ~OsiCuts ();
271 
272 private:
273  //*@name Function operator for sorting cuts by efectiveness */
276  {
277  public:
279  inline bool operator()(const OsiCut * c1P,const OsiCut * c2P)
280  { return c1P->effectiveness() > c2P->effectiveness(); }
281  };
283 
286  void gutsOfCopy( const OsiCuts & source );
289  void gutsOfDestructor();
291 
299 
300 };
301 
302 
303 //-------------------------------------------------------------------
304 // insert cuts into collection
305 //-------------------------------------------------------------------
306 void OsiCuts::insert( const OsiRowCut & rc )
307 {
308  OsiRowCut * newCutPtr = rc.clone();
309  //assert(dynamic_cast<OsiRowCut*>(newCutPtr) != NULL );
310  rowCutPtrs_.push_back(static_cast<OsiRowCut*>(newCutPtr));
311 }
312 void OsiCuts::insert( const OsiColCut & cc )
313 {
314  OsiColCut * newCutPtr = cc.clone();
315  //assert(dynamic_cast<OsiColCut*>(newCutPtr) != NULL );
316  colCutPtrs_.push_back(static_cast<OsiColCut*>(newCutPtr));
317 }
318 
319 void OsiCuts::insert( OsiRowCut* & rcPtr )
320 {
321  rowCutPtrs_.push_back(rcPtr);
322  rcPtr = NULL;
323 }
324 void OsiCuts::insert( OsiColCut* &ccPtr )
325 {
326  colCutPtrs_.push_back(ccPtr);
327  ccPtr = NULL;
328 }
329 #if 0
330 void OsiCuts::insert( OsiCut* & cPtr )
331 {
332  OsiRowCut * rcPtr = dynamic_cast<OsiRowCut*>(cPtr);
333  if ( rcPtr != NULL ) {
334  insert( rcPtr );
335  cPtr = rcPtr;
336  }
337  else {
338  OsiColCut * ccPtr = dynamic_cast<OsiColCut*>(cPtr);
339  assert( ccPtr != NULL );
340  insert( ccPtr );
341  cPtr = ccPtr;
342  }
343 }
344 #endif
345 
346 // LANNEZ SEBASTIEN added Thu May 25 01:22:51 EDT 2006
347 void OsiCuts::insert(const OsiCuts & cs)
348 {
349  for (OsiCuts::const_iterator it = cs.begin (); it != cs.end (); it++)
350  {
351  const OsiRowCut * rCut = dynamic_cast <const OsiRowCut * >(*it);
352  const OsiColCut * cCut = dynamic_cast <const OsiColCut * >(*it);
353  assert (rCut || cCut);
354  if (rCut)
355  insert (*rCut);
356  else
357  insert (*cCut);
358  }
359 }
360 
361 //-------------------------------------------------------------------
362 // sort
363 //-------------------------------------------------------------------
365 {
366  std::sort(colCutPtrs_.begin(),colCutPtrs_.end(),OsiCutCompare());
367  std::sort(rowCutPtrs_.begin(),rowCutPtrs_.end(),OsiCutCompare());
368 }
369 
370 
371 //-------------------------------------------------------------------
372 // Get number of in collections
373 //-------------------------------------------------------------------
374 int OsiCuts::sizeRowCuts() const {
375  return static_cast<int>(rowCutPtrs_.size()); }
376 int OsiCuts::sizeColCuts() const {
377  return static_cast<int>(colCutPtrs_.size()); }
378 int OsiCuts::sizeCuts() const {
379  return static_cast<int>(sizeRowCuts()+sizeColCuts()); }
380 
381 //----------------------------------------------------------------
382 // Get i'th cut from the collection
383 //----------------------------------------------------------------
384 const OsiRowCut * OsiCuts::rowCutPtr(int i) const { return rowCutPtrs_[i]; }
385 const OsiColCut * OsiCuts::colCutPtr(int i) const { return colCutPtrs_[i]; }
386 OsiRowCut * OsiCuts::rowCutPtr(int i) { return rowCutPtrs_[i]; }
387 OsiColCut * OsiCuts::colCutPtr(int i) { return colCutPtrs_[i]; }
388 
389 const OsiRowCut & OsiCuts::rowCut(int i) const { return *rowCutPtr(i); }
390 const OsiColCut & OsiCuts::colCut(int i) const { return *colCutPtr(i); }
391 OsiRowCut & OsiCuts::rowCut(int i) { return *rowCutPtr(i); }
392 OsiColCut & OsiCuts::colCut(int i) { return *colCutPtr(i); }
393 
394 //----------------------------------------------------------------
395 // Get most effective cut from collection
396 //----------------------------------------------------------------
398 {
399  const_iterator b=begin();
400  const_iterator e=end();
401  return *(std::min_element(b,e,OsiCutCompare()));
402 }
404 {
405  iterator b=begin();
406  iterator e=end();
407  //return *(std::min_element(b,e,OsiCutCompare()));
408  OsiCut * retVal = NULL;
409  double maxEff = COIN_DBL_MIN;
410  for ( OsiCuts::iterator it=b; it!=e; ++it ) {
411  if (maxEff < (*it)->effectiveness() ) {
412  maxEff = (*it)->effectiveness();
413  retVal = *it;
414  }
415  }
416  return retVal;
417 }
418 
419 //----------------------------------------------------------------
420 // Print all cuts
421 //----------------------------------------------------------------
422 void
424 {
425  // do all column cuts first
426  int i;
427  int numberColCuts=sizeColCuts();
428  for (i=0;i<numberColCuts;i++) {
429  const OsiColCut * cut = colCutPtr(i);
430  cut->print();
431  }
432  int numberRowCuts=sizeRowCuts();
433  for (i=0;i<numberRowCuts;i++) {
434  const OsiRowCut * cut = rowCutPtr(i);
435  cut->print();
436  }
437 }
438 
439 //----------------------------------------------------------------
440 // Erase i'th cut from the collection
441 //----------------------------------------------------------------
443 {
444  delete rowCutPtrs_[i];
445  rowCutPtrs_.erase( rowCutPtrs_.begin()+i );
446 }
448 {
449  delete colCutPtrs_[i];
450  colCutPtrs_.erase( colCutPtrs_.begin()+i );
451 }
453 OsiRowCut *
455 {
456  OsiRowCut * cut = rowCutPtrs_[i];
457  rowCutPtrs_[i]=NULL;
458  rowCutPtrs_.erase( rowCutPtrs_.begin()+i );
459  return cut;
460 }
462 {
463  rowCutPtrs_.clear() ;
464 }
465 void OsiCuts::eraseAndDumpCuts(const std::vector<int> to_erase)
466 {
467  for (unsigned i=0; i<to_erase.size(); i++) {
468  delete rowCutPtrs_[to_erase[i]];
469  }
470  rowCutPtrs_.clear();
471 }
472 
473 
474 #endif
void gutsOfDestructor()
Delete internal data.
OsiVectorColCutPtr colCutPtrs_
Vector of column cuts pointers.
Definition: OsiCuts.hpp:297
const_iterator operator++(int)
Definition: OsiCuts.hpp:92
std::vector< OsiRowCut * > OsiVectorRowCutPtr
Vector of OsiRowCut pointers.
void dumpCuts()
Clear all row cuts without deleting them.
Definition: OsiCuts.hpp:461
virtual ~OsiCuts()
Destructor.
virtual OsiColCut * clone() const
Clone.
std::vector< OsiColCut * > OsiVectorColCutPtr
Vector of OsiColCut pointers.
iterator begin()
Get iterator to beginning of collection.
Definition: OsiCuts.hpp:247
OsiRowCut * rowCutPtrAndZap(int i)
Get pointer to i&#39;th row cut and remove ptr from collection.
Definition: OsiCuts.hpp:454
void insertIfNotDuplicate(OsiRowCut &rc, CoinAbsFltEq treatAsSame=CoinAbsFltEq(1.0e-12))
Insert a row cut unless it is a duplicate - cut may get sorted.
const_iterator operator++()
iterator begin()
double effectiveness() const
Get effectiveness.
Definition: OsiCut.hpp:210
OsiRowCut * rowCutPtr(int i)
Get pointer to i&#39;th row cut.
Definition: OsiCuts.hpp:386
const_iterator begin() const
Get const iterator to beginning of collection.
Definition: OsiCuts.hpp:249
void eraseColCut(int i)
Remove i&#39;th column cut from collection.
Definition: OsiCuts.hpp:447
OsiCuts & cuts_
Definition: OsiCuts.hpp:64
OsiCut * operator*() const
Definition: OsiCuts.hpp:37
Collections of row cuts and column cuts.
Definition: OsiCuts.hpp:19
Column Cut Class.
Definition: OsiColCut.hpp:23
Const Iterator.
Definition: OsiCuts.hpp:74
iterator operator++()
virtual OsiRowCut * clone() const
Clone.
OsiCut * cutP_
Definition: OsiCuts.hpp:67
const OsiCuts * cutsPtr_
Definition: OsiCuts.hpp:115
virtual void print() const
Print cuts in collection.
virtual void print() const
Print cuts in collection.
bool operator<(const const_iterator &it) const
Definition: OsiCuts.hpp:107
const_iterator end() const
Get const iterator to end of collection.
Definition: OsiCuts.hpp:253
const double COIN_DBL_MIN
Definition: CoinFinite.hpp:17
OsiColCut * colCutPtr(int i)
Get pointer to i&#39;th column cut.
Definition: OsiCuts.hpp:387
const OsiCut * mostEffectiveCutPtr() const
Get const pointer to the most effective cut.
Definition: OsiCuts.hpp:397
const_iterator end()
const_iterator & operator=(const const_iterator &rhs)
const OsiCut * operator*() const
Definition: OsiCuts.hpp:88
const OsiCut * cutP_
Definition: OsiCuts.hpp:118
Iterator.
Definition: OsiCuts.hpp:30
Equality to a scaled tolerance.
bool operator!=(const iterator &it) const
Definition: OsiCuts.hpp:51
std::forward_iterator_tag iterator_category
Definition: OsiCuts.hpp:77
friend void OsiCutsUnitTest()
A function that tests the methods in the OsiCuts class.
iterator & operator=(const iterator &rhs)
OsiRowCut & rowCut(int i)
Get reference to i&#39;th row cut.
Definition: OsiCuts.hpp:391
void eraseRowCut(int i)
Remove i&#39;th row cut from collection.
Definition: OsiCuts.hpp:442
bool operator!=(const const_iterator &it) const
Definition: OsiCuts.hpp:103
bool operator==(const const_iterator &it) const
Definition: OsiCuts.hpp:99
Row Cut Class.
Definition: OsiRowCut.hpp:29
void printCuts() const
Print cuts in collection.
Definition: OsiCuts.hpp:423
void eraseAndDumpCuts(const std::vector< int > to_erase)
Selective delete and clear for row cuts.
Definition: OsiCuts.hpp:465
Equality to an absolute tolerance.
void insert(const OsiRowCut &rc)
Insert a row cut.
Definition: OsiCuts.hpp:306
Function objects for testing equality of real numbers.
int sizeCuts() const
Number of cuts in collection.
Definition: OsiCuts.hpp:378
void gutsOfCopy(const OsiCuts &source)
Copy internal data.
const_iterator begin()
OsiColCut & colCut(int i)
Get reference to i&#39;th column cut.
Definition: OsiCuts.hpp:392
void sort()
Cuts with greatest effectiveness are first.
Definition: OsiCuts.hpp:364
iterator operator++(int)
Definition: OsiCuts.hpp:40
OsiCuts & operator=(const OsiCuts &rhs)
Assignment operator.
bool operator<(const iterator &it) const
Definition: OsiCuts.hpp:55
iterator end()
Get iterator to end of collection.
Definition: OsiCuts.hpp:251
bool operator==(const iterator &it) const
Definition: OsiCuts.hpp:47
OsiCuts()
Default constructor.
OsiVectorRowCutPtr rowCutPtrs_
Vector of row cuts pointers.
Definition: OsiCuts.hpp:295
int sizeRowCuts() const
Number of row cuts in collection.
Definition: OsiCuts.hpp:374
bool operator()(const OsiCut *c1P, const OsiCut *c2P)
Function for sorting cuts by effectiveness.
Definition: OsiCuts.hpp:279
int sizeColCuts() const
Number of column cuts in collection.
Definition: OsiCuts.hpp:376