summaryrefslogtreecommitdiff
path: root/thirdparty/linux/include/coin/OsiColCut.hpp
blob: c98eb5cc9ee0c8a28fd3b041f4c77a0a26ef3044 (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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
// Copyright (C) 2000, International Business Machines
// Corporation and others.  All Rights Reserved.
// This code is licensed under the terms of the Eclipse Public License (EPL).

#ifndef OsiColCut_H
#define OsiColCut_H

#include <string>

#include "CoinPackedVector.hpp"

#include "OsiCollections.hpp"
#include "OsiCut.hpp"

/** Column Cut Class

Column Cut Class has:
  <ul>
  <li>a sparse vector of column lower bounds
  <li>a sparse vector of column upper bounds
  </ul>
*/
class OsiColCut : public OsiCut {
   friend void OsiColCutUnitTest(const OsiSolverInterface * baseSiP, 
				 const std::string & mpsDir);
  
public:
  
  //----------------------------------------------------------------
  
  /**@name Setting column bounds */
  //@{
  /// Set column lower bounds 
  inline void setLbs( 
    int nElements, 
    const int * colIndices, 
    const double * lbElements );   
  
  /// Set column lower bounds from a packed vector
  inline void setLbs( const CoinPackedVector & lbs );
  
  /// Set column upper bounds 
  inline void setUbs( 
    int nElements, 
    const int * colIndices, 
    const double * ubElements );
  
  /// Set column upper bounds from a packed vector
  inline void setUbs( const CoinPackedVector & ubs );
  //@}
  
  //----------------------------------------------------------------
  
  /**@name Getting column bounds */
  //@{
  /// Get column lower bounds
  inline const CoinPackedVector & lbs() const;
  /// Get column upper bounds
  inline const CoinPackedVector & ubs() const;
  //@}
  
  /**@name Comparison operators  */
  //@{
#if __GNUC__ != 2 
  using OsiCut::operator== ;
#endif
  /** equal - true if lower bounds, upper bounds, 
  and OsiCut are equal.
  */
  inline virtual bool operator==(const OsiColCut& rhs) const; 

#if __GNUC__ != 2 
  using OsiCut::operator!= ;
#endif
  /// not equal
  inline virtual bool operator!=(const OsiColCut& rhs) const; 
  //@}
  
  
  //----------------------------------------------------------------
  
  /**@name Sanity checks on cut */
  //@{
  /** Returns true if the cut is consistent with respect to itself.
  This checks to ensure that:
  <ul>
  <li>The bound vectors do not have duplicate indices,
  <li>The bound vectors indices are >=0
  </ul>
  */
  inline virtual bool consistent() const; 
  
  /** Returns true if cut is consistent with respect to the solver  
  interface's model. This checks to ensure that
  the lower & upperbound packed vectors:
  <ul>
  <li>do not have an index >= the number of column is the model.
  </ul>
  */
  inline virtual bool consistent(const OsiSolverInterface& im) const;
  
  /** Returns true if the cut is infeasible with respect to its bounds and the 
  column bounds in the solver interface's models.
  This checks whether:
  <ul>
  <li>the maximum of the new and existing lower bounds is strictly 
  greater than the minimum of the new and existing upper bounds.
</ul>
  */
  inline virtual bool infeasible(const OsiSolverInterface &im) const;
  /** Returns infeasibility of the cut with respect to solution 
      passed in i.e. is positive if cuts off that solution.  
      solution is getNumCols() long..
  */
  virtual double violated(const double * solution) const;
  //@}
  
  //----------------------------------------------------------------
  
  /**@name Constructors and destructors */
  //@{
  /// Assignment operator 
  OsiColCut & operator=( const OsiColCut& rhs);
  
  /// Copy constructor 
  OsiColCut ( const OsiColCut &);
  
  /// Default Constructor 
  OsiColCut ();
  
  /// Clone
  virtual OsiColCut * clone() const;
  
  /// Destructor 
  virtual ~OsiColCut ();
  //@}
  
  /**@name Debug stuff */
  //@{
    /// Print cuts in collection
  virtual void print() const;
  //@}
   
private:
  
  /**@name Private member data */
  //@{
  /// Lower bounds
  CoinPackedVector lbs_;
  /// Upper bounds
  CoinPackedVector ubs_;
  //@}
  
};



//-------------------------------------------------------------------
// Set lower & upper bound vectors
//------------------------------------------------------------------- 
void OsiColCut::setLbs( 
                       int size, 
                       const int * colIndices, 
                       const double * lbElements )
{
  lbs_.setVector(size,colIndices,lbElements);
}
//
void OsiColCut::setUbs( 
                       int size, 
                       const int * colIndices, 
                       const double * ubElements )
{
  ubs_.setVector(size,colIndices,ubElements);
}
//
void OsiColCut::setLbs( const CoinPackedVector & lbs )
{
  lbs_ = lbs;
}
//
void OsiColCut::setUbs( const CoinPackedVector & ubs )
{
  ubs_ = ubs;
}

//-------------------------------------------------------------------
// Get Column Lower Bounds and Column Upper Bounds
//-------------------------------------------------------------------
const CoinPackedVector & OsiColCut::lbs() const 
{ 
  return lbs_; 
}
//
const CoinPackedVector & OsiColCut::ubs() const 
{ 
  return ubs_; 
}

//----------------------------------------------------------------
// == operator 
//-------------------------------------------------------------------
bool
OsiColCut::operator==(
                      const OsiColCut& rhs) const
{
  if ( this->OsiCut::operator!=(rhs) ) 
    return false;
  if ( lbs() != rhs.lbs() ) 
    return false;
  if ( ubs() != rhs.ubs() ) 
    return false;
  return true;
}
//
bool
OsiColCut::operator!=(
                      const OsiColCut& rhs) const
{
  return !( (*this)==rhs );
}

//----------------------------------------------------------------
// consistent & infeasible 
//-------------------------------------------------------------------
bool OsiColCut::consistent() const
{
  const CoinPackedVector & lb = lbs();
  const CoinPackedVector & ub = ubs();
  // Test for consistent cut.
  // Are packed vectors consistent?
  lb.duplicateIndex("consistent", "OsiColCut");
  ub.duplicateIndex("consistent", "OsiColCut");
  if ( lb.getMinIndex() < 0 ) return false;
  if ( ub.getMinIndex() < 0 ) return false;
  return true;
}
//
bool OsiColCut::consistent(const OsiSolverInterface& im) const
{  
  const CoinPackedVector & lb = lbs();
  const CoinPackedVector & ub = ubs();
  
  // Test for consistent cut.
  if ( lb.getMaxIndex() >= im.getNumCols() ) return false;
  if ( ub.getMaxIndex() >= im.getNumCols() ) return false;
  
  return true;
}

#if 0
bool OsiColCut::feasible(const OsiSolverInterface &im) const
{
  const double * oldColLb = im.getColLower();
  const double * oldColUb = im.getColUpper();
  const CoinPackedVector & cutLbs = lbs();
  const CoinPackedVector & cutUbs = ubs();
  int i;
  
  for ( i=0; i<cutLbs.size(); i++ ) {
    int colIndx = cutLbs.indices()[i];
    double newLb;
    if ( cutLbs.elements()[i] > oldColLb[colIndx] )
      newLb = cutLbs.elements()[i];
    else
      newLb = oldColLb[colIndx];

    double newUb = oldColUb[colIndx];
    if ( cutUbs.indexExists(colIndx) )
      if ( cutUbs[colIndx] < newUb ) newUb = cutUbs[colIndx];
      if ( newLb > newUb ) 
        return false;
  }
  
  for ( i=0; i<cutUbs.size(); i++ ) {
    int colIndx = cutUbs.indices()[i];
    double newUb = cutUbs.elements()[i] < oldColUb[colIndx] ? cutUbs.elements()[i] : oldColUb[colIndx];
    double newLb = oldColLb[colIndx];
    if ( cutLbs.indexExists(colIndx) )
      if ( cutLbs[colIndx] > newLb ) newLb = cutLbs[colIndx];
      if ( newUb < newLb ) 
        return false;
  }
  
  return true;
}
#endif 


bool OsiColCut::infeasible(const OsiSolverInterface &im) const
{
  const double * oldColLb = im.getColLower();
  const double * oldColUb = im.getColUpper();
  const CoinPackedVector & cutLbs = lbs();
  const CoinPackedVector & cutUbs = ubs();
  int i;
  
  for ( i=0; i<cutLbs.getNumElements(); i++ ) {
    int colIndx = cutLbs.getIndices()[i];
    double newLb= cutLbs.getElements()[i] > oldColLb[colIndx] ?
       cutLbs.getElements()[i] : oldColLb[colIndx];

    double newUb = oldColUb[colIndx];
    if ( cutUbs.isExistingIndex(colIndx) )
      if ( cutUbs[colIndx] < newUb ) newUb = cutUbs[colIndx];
      if ( newLb > newUb ) 
        return true;
  }
  
  for ( i=0; i<cutUbs.getNumElements(); i++ ) {
    int colIndx = cutUbs.getIndices()[i];
    double newUb = cutUbs.getElements()[i] < oldColUb[colIndx] ?
       cutUbs.getElements()[i] : oldColUb[colIndx];
    double newLb = oldColLb[colIndx];
    if ( cutLbs.isExistingIndex(colIndx) )
      if ( cutLbs[colIndx] > newLb ) newLb = cutLbs[colIndx];
      if ( newUb < newLb ) 
        return true;
  }
  
  return false;
}

#endif