OsiColCut.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 OsiColCut_H
6 #define OsiColCut_H
7 
8 #include <string>
9 
10 #include "CoinPackedVector.hpp"
11 
12 #include "OsiCollections.hpp"
13 #include "OsiCut.hpp"
14 
23 class OsiColCut : public OsiCut {
24  friend void OsiColCutUnitTest(const OsiSolverInterface * baseSiP,
25  const std::string & mpsDir);
26 
27 public:
28 
29  //----------------------------------------------------------------
30 
33  inline void setLbs(
35  int nElements,
36  const int * colIndices,
37  const double * lbElements );
38 
40  inline void setLbs( const CoinPackedVector & lbs );
41 
43  inline void setUbs(
44  int nElements,
45  const int * colIndices,
46  const double * ubElements );
47 
49  inline void setUbs( const CoinPackedVector & ubs );
51 
52  //----------------------------------------------------------------
53 
56  inline const CoinPackedVector & lbs() const;
59  inline const CoinPackedVector & ubs() const;
61 
64 #if __GNUC__ != 2
65  using OsiCut::operator== ;
66 #endif
67 
70  inline virtual bool operator==(const OsiColCut& rhs) const;
71 
72 #if __GNUC__ != 2
73  using OsiCut::operator!= ;
74 #endif
75  inline virtual bool operator!=(const OsiColCut& rhs) const;
78 
79 
80  //----------------------------------------------------------------
81 
91  inline virtual bool consistent() const;
92 
100  inline virtual bool consistent(const OsiSolverInterface& im) const;
101 
110  inline virtual bool infeasible(const OsiSolverInterface &im) const;
115  virtual double violated(const double * solution) const;
117 
118  //----------------------------------------------------------------
119 
122  OsiColCut & operator=( const OsiColCut& rhs);
124 
126  OsiColCut ( const OsiColCut &);
127 
129  OsiColCut ();
130 
132  virtual OsiColCut * clone() const;
133 
135  virtual ~OsiColCut ();
137 
140  virtual void print() const;
143 
144 private:
145 
153 
154 };
155 
156 
157 
158 //-------------------------------------------------------------------
159 // Set lower & upper bound vectors
160 //-------------------------------------------------------------------
162  int size,
163  const int * colIndices,
164  const double * lbElements )
165 {
166  lbs_.setVector(size,colIndices,lbElements);
167 }
168 //
170  int size,
171  const int * colIndices,
172  const double * ubElements )
173 {
174  ubs_.setVector(size,colIndices,ubElements);
175 }
176 //
178 {
179  lbs_ = lbs;
180 }
181 //
183 {
184  ubs_ = ubs;
185 }
186 
187 //-------------------------------------------------------------------
188 // Get Column Lower Bounds and Column Upper Bounds
189 //-------------------------------------------------------------------
191 {
192  return lbs_;
193 }
194 //
196 {
197  return ubs_;
198 }
199 
200 //----------------------------------------------------------------
201 // == operator
202 //-------------------------------------------------------------------
203 bool
205  const OsiColCut& rhs) const
206 {
207  if ( this->OsiCut::operator!=(rhs) )
208  return false;
209  if ( lbs() != rhs.lbs() )
210  return false;
211  if ( ubs() != rhs.ubs() )
212  return false;
213  return true;
214 }
215 //
216 bool
218  const OsiColCut& rhs) const
219 {
220  return !( (*this)==rhs );
221 }
222 
223 //----------------------------------------------------------------
224 // consistent & infeasible
225 //-------------------------------------------------------------------
227 {
228  const CoinPackedVector & lb = lbs();
229  const CoinPackedVector & ub = ubs();
230  // Test for consistent cut.
231  // Are packed vectors consistent?
232  lb.duplicateIndex("consistent", "OsiColCut");
233  ub.duplicateIndex("consistent", "OsiColCut");
234  if ( lb.getMinIndex() < 0 ) return false;
235  if ( ub.getMinIndex() < 0 ) return false;
236  return true;
237 }
238 //
240 {
241  const CoinPackedVector & lb = lbs();
242  const CoinPackedVector & ub = ubs();
243 
244  // Test for consistent cut.
245  if ( lb.getMaxIndex() >= im.getNumCols() ) return false;
246  if ( ub.getMaxIndex() >= im.getNumCols() ) return false;
247 
248  return true;
249 }
250 
251 #if 0
252 bool OsiColCut::feasible(const OsiSolverInterface &im) const
253 {
254  const double * oldColLb = im.getColLower();
255  const double * oldColUb = im.getColUpper();
256  const CoinPackedVector & cutLbs = lbs();
257  const CoinPackedVector & cutUbs = ubs();
258  int i;
259 
260  for ( i=0; i<cutLbs.size(); i++ ) {
261  int colIndx = cutLbs.indices()[i];
262  double newLb;
263  if ( cutLbs.elements()[i] > oldColLb[colIndx] )
264  newLb = cutLbs.elements()[i];
265  else
266  newLb = oldColLb[colIndx];
267 
268  double newUb = oldColUb[colIndx];
269  if ( cutUbs.indexExists(colIndx) )
270  if ( cutUbs[colIndx] < newUb ) newUb = cutUbs[colIndx];
271  if ( newLb > newUb )
272  return false;
273  }
274 
275  for ( i=0; i<cutUbs.size(); i++ ) {
276  int colIndx = cutUbs.indices()[i];
277  double newUb = cutUbs.elements()[i] < oldColUb[colIndx] ? cutUbs.elements()[i] : oldColUb[colIndx];
278  double newLb = oldColLb[colIndx];
279  if ( cutLbs.indexExists(colIndx) )
280  if ( cutLbs[colIndx] > newLb ) newLb = cutLbs[colIndx];
281  if ( newUb < newLb )
282  return false;
283  }
284 
285  return true;
286 }
287 #endif
288 
289 
291 {
292  const double * oldColLb = im.getColLower();
293  const double * oldColUb = im.getColUpper();
294  const CoinPackedVector & cutLbs = lbs();
295  const CoinPackedVector & cutUbs = ubs();
296  int i;
297 
298  for ( i=0; i<cutLbs.getNumElements(); i++ ) {
299  int colIndx = cutLbs.getIndices()[i];
300  double newLb= cutLbs.getElements()[i] > oldColLb[colIndx] ?
301  cutLbs.getElements()[i] : oldColLb[colIndx];
302 
303  double newUb = oldColUb[colIndx];
304  if ( cutUbs.isExistingIndex(colIndx) )
305  if ( cutUbs[colIndx] < newUb ) newUb = cutUbs[colIndx];
306  if ( newLb > newUb )
307  return true;
308  }
309 
310  for ( i=0; i<cutUbs.getNumElements(); i++ ) {
311  int colIndx = cutUbs.getIndices()[i];
312  double newUb = cutUbs.getElements()[i] < oldColUb[colIndx] ?
313  cutUbs.getElements()[i] : oldColUb[colIndx];
314  double newLb = oldColLb[colIndx];
315  if ( cutLbs.isExistingIndex(colIndx) )
316  if ( cutLbs[colIndx] > newLb ) newLb = cutLbs[colIndx];
317  if ( newUb < newLb )
318  return true;
319  }
320 
321  return false;
322 }
323 
324 #endif
virtual ~OsiColCut()
Destructor.
virtual bool operator!=(const OsiColCut &rhs) const
not equal
Definition: OsiColCut.hpp:217
void setLbs(int nElements, const int *colIndices, const double *lbElements)
Set column lower bounds.
Definition: OsiColCut.hpp:161
const CoinPackedVector & lbs() const
Get column lower bounds.
Definition: OsiColCut.hpp:190
int getMaxIndex() const
Get value of maximum index.
Column Cut Class.
Definition: OsiColCut.hpp:23
OsiColCut()
Default Constructor.
virtual const int * getIndices() const
Get indices of elements.
virtual const double * getElements() const
Get element values.
void setVector(int size, const int *inds, const double *elems, bool testForDuplicateIndex=COIN_DEFAULT_VALUE_FOR_DUPLICATE)
Set vector size, indices, and elements.
virtual const double * getColUpper() const =0
Get a pointer to an array[getNumCols()] of column upper bounds.
virtual int getNumCols() const =0
Get the number of columns.
virtual OsiColCut * clone() const
Clone.
void setUbs(int nElements, const int *colIndices, const double *ubElements)
Set column upper bounds.
Definition: OsiColCut.hpp:169
OsiColCut & operator=(const OsiColCut &rhs)
Assignment operator.
virtual double violated(const double *solution) const
Returns infeasibility of the cut with respect to solution passed in i.e.
int getMinIndex() const
Get value of minimum index.
friend void OsiColCutUnitTest(const OsiSolverInterface *baseSiP, const std::string &mpsDir)
A function that tests the methods in the OsiColCut class.
virtual int getNumElements() const
Get the size.
CoinPackedVector ubs_
Upper bounds.
Definition: OsiColCut.hpp:151
virtual void print() const
Print cuts in collection.
virtual const double * getColLower() const =0
Get a pointer to an array[getNumCols()] of column lower bounds.
bool isExistingIndex(int i) const
Return true if the i&#39;th element of the full storage vector exists in the packed storage vector...
CoinPackedVector lbs_
Lower bounds.
Definition: OsiColCut.hpp:149
virtual bool operator==(const OsiColCut &rhs) const
equal - true if lower bounds, upper bounds, and OsiCut are equal.
Definition: OsiColCut.hpp:204
Abstract Base Class for describing an interface to a solver.
const CoinPackedVector & ubs() const
Get column upper bounds.
Definition: OsiColCut.hpp:195
virtual bool consistent() const
Returns true if the cut is consistent with respect to itself.
Definition: OsiColCut.hpp:226
Sparse Vector.
virtual bool infeasible(const OsiSolverInterface &im) const
Returns true if the cut is infeasible with respect to its bounds and the column bounds in the solve...
Definition: OsiColCut.hpp:290
void duplicateIndex(const char *methodName=NULL, const char *className=NULL) const
Throw an exception if there are duplicate indices.