class_consclassifier.cpp
Go to the documentation of this file.
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2 /* */
3 /* This file is part of the program */
4 /* GCG --- Generic Column Generation */
5 /* a Dantzig-Wolfe decomposition based extension */
6 /* of the branch-cut-and-price framework */
7 /* SCIP --- Solving Constraint Integer Programs */
8 /* */
9 /* Copyright (C) 2010-2018 Operations Research, RWTH Aachen University */
10 /* Zuse Institute Berlin (ZIB) */
11 /* */
12 /* This program is free software; you can redistribute it and/or */
13 /* modify it under the terms of the GNU Lesser General Public License */
14 /* as published by the Free Software Foundation; either version 3 */
15 /* of the License, or (at your option) any later version. */
16 /* */
17 /* This program is distributed in the hope that it will be useful, */
18 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
19 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
20 /* GNU Lesser General Public License for more details. */
21 /* */
22 /* You should have received a copy of the GNU Lesser General Public License */
23 /* along with this program; if not, write to the Free Software */
24 /* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.*/
25 /* */
26 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
27 
34 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
35 
36 #include "class_consclassifier.h"
37 
38 #include <assert.h>
39 #include <sstream>
40 #include <algorithm>
41 
42 
43 namespace gcg {
44 
45 
48  SCIP* _scip,
49  const char* givenName,
50  int givenNClasses,
51  int givenNCons
52 ) :
53  IndexClassifier(_scip, givenName, givenNClasses, givenNCons)
54 {
55 
56 }
57 
60  const ConsClassifier* toCopy
61 ) : IndexClassifier( toCopy )
62 {
63 }
64 
67 {
68 }
69 
71 int ConsClassifier::addClass( const char* givenName, const char* givenDesc, CONS_DECOMPINFO givenDecompInfo )
72 {
73  int classindex = IndexClassifier::addClass( givenName, givenDesc );
74  setClassDecompInfo( classindex, givenDecompInfo );
75 
76  return classindex;
77 }
78 
80 void ConsClassifier::assignConsToClass( int givenConsindex, int givenClassindex )
81 {
82  IndexClassifier::assignIndexToClass( givenConsindex, givenClassindex );
83 }
84 
86 std::vector<std::vector<int>> ConsClassifier::getAllSubsets( bool both, bool only_master, bool only_pricing )
87 {
88  std::vector<int> classindices;
89  for ( int i = 0; i < getNClasses(); ++i )
90  {
91  if ( ( both && getClassDecompInfo( i ) == BOTH ) || ( only_master && getClassDecompInfo( i ) == ONLY_MASTER )
92  || ( only_pricing && getClassDecompInfo( i ) == ONLY_PRICING ) )
93  classindices.push_back( i );
94  }
95  return IndexClassifier::getAllSubsets( classindices );
96 }
97 
100 {
101  int decompInfo = IndexClassifier::getClassDecompInfo( givenClassindex );
102  CONS_DECOMPINFO interp;
103 
104  assert( 0 <= decompInfo && decompInfo <= 2);
105 
106  switch ( decompInfo )
107  {
108  case 0:
109  interp = BOTH;
110  break;
111  case 1:
112  interp = ONLY_MASTER;
113  break;
114  case 2:
115  interp = ONLY_PRICING;
116  break;
117  default:
118  interp = BOTH;
119  break;
120  }
121 
122  return interp;
123 }
124 
126 const char* ConsClassifier::getClassNameOfCons( int givenConsindex )
127 {
128  return IndexClassifier::getClassNameOfIndex( givenConsindex );
129 }
130 
132 int ConsClassifier::getClassOfCons( int givenConsindex )
133 {
134  return IndexClassifier::getClassOfIndex( givenConsindex );
135 }
136 
139 {
140  std::vector<int> conssToClasses = IndexClassifier::getIndicesToClasses();
141  if ( conssToClasses.size() > 0 )
142  return &conssToClasses[0];
143  else
144  return NULL;
145 }
146 
149 {
151 }
152 
155 {
157 }
158 
159 
161 bool ConsClassifier::isConsClassified( int givenConsindex )
162 {
163  return IndexClassifier::isIndexClassified( givenConsindex );
164 }
165 
168 {
169  std::vector<int> classindexmapping = IndexClassifier::reduceClasses( givenMaxNumber );
170  ConsClassifier* newClassifier;
171  std::stringstream newName;
172  std::stringstream newClassdesc;
173 
174  if ( classindexmapping.empty() )
175  return NULL;
176 
178  newName << getName() << "-red-to-" << givenMaxNumber;
179  newClassifier = new ConsClassifier( scip, newName.str().c_str(), givenMaxNumber, getNConss() );
180 
182  for( int i = 0; i < newClassifier->getNConss(); ++i)
183  {
184  if ( getClassOfCons(i) != -1 )
185  {
186  newClassifier->assignConsToClass( i, classindexmapping[getClassOfCons(i)] );
187  }
188  }
189 
191  newClassifier->setClassName( 0, "merged" );
192  newClassifier->setClassDecompInfo( 0, BOTH );
193 
194  for ( int i = 0; i < getNClasses(); ++i )
195  {
196  if ( classindexmapping[i] == 0 )
197  {
198  newClassdesc << getClassDescription( i ) << " - ";
199  }
200  else
201  {
202  newClassifier->setClassName( classindexmapping[i], getClassName(i) );
203  newClassifier->setClassDescription( classindexmapping[i], getClassDescription(i) );
204  newClassifier->setClassDecompInfo( classindexmapping[i], getClassDecompInfo(i) );
205  }
206  }
207 
208  newClassifier->setClassDescription( 0, newClassdesc.str().c_str() );
209 
210 
211  return newClassifier;
212 }
213 
215 void ConsClassifier::setClassDecompInfo( int givenClassindex, CONS_DECOMPINFO givenDecompInfo )
216 {
217  assert(givenDecompInfo == BOTH || givenDecompInfo == ONLY_MASTER || givenDecompInfo == ONLY_PRICING );
218 
219  IndexClassifier::setClassDecompInfo( givenClassindex, (int) givenDecompInfo );
220 }
221 
222 } /* namespace gcg */
void setClassName(int classindex, const char *name)
int getClassOfCons(int consindex)
void setClassDecompInfo(int classindex, int decompInfo)
const char * getClassName(int classindex)
std::vector< int > getIndicesToClasses()
const char * getClassNameOfIndex(int index)
class for classifying constraints
const char * getClassNameOfCons(int consindex)
std::vector< int > getNConssOfClasses()
int addClass(const char *name, const char *desc)
int getClassDecompInfo(int classindex)
void assignConsToClass(int consindex, int classindex)
CONS_DECOMPINFO getClassDecompInfo(int classindex)
enum ConsClassDecompInfo CONS_DECOMPINFO
void assignIndexToClass(int index, int classindex)
std::vector< int > getNIndicesOfClasses()
std::vector< std::vector< int > > getAllSubsets(bool both, bool only_master, bool only_pricing)
const char * getClassDescription(int classindex)
void setClassDescription(int classindex, const char *desc)
std::vector< std::vector< int > > getAllSubsets(std::vector< int > &classindices)
ConsClassifier(SCIP *scip, const char *name, int nClasses, int nConss)
void setClassDecompInfo(int classindex, CONS_DECOMPINFO decompInfo)
std::vector< int > reduceClasses(int maxNumberOfClasses)
int addClass(const char *name, const char *desc, CONS_DECOMPINFO decompInfo)
bool isConsClassified(int consindex)
ConsClassifier * reduceClasses(int maxNumberOfClasses)