class_varclassifier.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_varclassifier.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 givenNVars
52 ) :
53  IndexClassifier(_scip, givenName, givenNClasses, givenNVars)
54 {
55 
56 }
57 
60  const VarClassifier* toCopy
61 ) : IndexClassifier( toCopy )
62 {
63 }
64 
67 {
68 }
69 
71 int VarClassifier::addClass( const char* givenName, const char* givenDesc, VAR_DECOMPINFO givenDecompInfo )
72 {
73  int classindex = IndexClassifier::addClass( givenName, givenDesc );
74  setClassDecompInfo( classindex, givenDecompInfo );
75 
76  return classindex;
77 }
78 
80 void VarClassifier::assignVarToClass( int givenVarindex, int givenClassindex )
81 {
82  IndexClassifier::assignIndexToClass( givenVarindex, givenClassindex );
83 }
84 
86 std::vector<std::vector<int>> VarClassifier::getAllSubsets( bool all, bool linking, bool master, bool block )
87 {
88  std::vector<int> classindices;
89  for ( int i = 0; i < getNClasses(); ++i )
90  {
91  if ( ( all && getClassDecompInfo( i ) == ALL ) ||
92  ( linking && getClassDecompInfo( i ) == LINKING ) ||
93  ( master && getClassDecompInfo( i ) == MASTER ) ||
94  ( block && getClassDecompInfo( i ) == BLOCK )
95  )
96  classindices.push_back( i );
97  }
98  return IndexClassifier::getAllSubsets( classindices );
99 }
100 
103 {
104  int decompInfo = IndexClassifier::getClassDecompInfo( givenClassindex );
105  VAR_DECOMPINFO interp;
106 
107  assert( 0 <= decompInfo && decompInfo <= 3);
108 
109  switch ( decompInfo )
110  {
111  case 0:
112  interp = ALL;
113  break;
114  case 1:
115  interp = LINKING;
116  break;
117  case 2:
118  interp = MASTER;
119  break;
120  case 3:
121  interp = BLOCK;
122  break;
123  default:
124  interp = ALL;
125  break;
126  }
127 
128  return interp;
129 }
130 
132 const char* VarClassifier::getClassNameOfVar( int givenVarindex )
133 {
134  return IndexClassifier::getClassNameOfIndex( givenVarindex );
135 }
136 
138 int VarClassifier::getClassOfVar( int givenVarindex )
139 {
140  return IndexClassifier::getClassOfIndex( givenVarindex );
141 }
142 
145 {
146  std::vector<int> varsToClasses = IndexClassifier::getIndicesToClasses();
147  if ( varsToClasses.size() > 0 )
148  return &varsToClasses[0];
149  else
150  return NULL;
151 }
152 
155 {
157 }
158 
161 {
163 }
164 
165 
167 bool VarClassifier::isVarClassified( int givenVarindex )
168 {
169  return IndexClassifier::isIndexClassified( givenVarindex );
170 }
171 
174 {
175  std::vector<int> classindexmapping = IndexClassifier::reduceClasses( givenMaxNumber );
176  VarClassifier* newClassifier;
177  std::stringstream newName;
178  std::stringstream newClassdesc;
179 
180  if ( classindexmapping.empty() )
181  return NULL;
182 
184  newName << getName() << "-red-to-" << givenMaxNumber;
185  newClassifier = new VarClassifier( scip, newName.str().c_str(), givenMaxNumber, getNVars() );
186 
188  for( int i = 0; i < newClassifier->getNVars(); ++i)
189  {
190  if ( getClassOfVar(i) != -1 )
191  {
192  newClassifier->assignVarToClass( i, classindexmapping[getClassOfVar(i)] );
193  }
194  }
195 
197  newClassifier->setClassName( 0, "merged" );
198  newClassifier->setClassDecompInfo( 0, ALL );
199 
200  for ( int i = 0; i < getNClasses(); ++i )
201  {
202  if ( classindexmapping[i] == 0 )
203  {
204  newClassdesc << getClassDescription( i ) << " - ";
205  }
206  else
207  {
208  newClassifier->setClassName( classindexmapping[i], getClassName(i) );
209  newClassifier->setClassDescription( classindexmapping[i], getClassDescription(i) );
210  newClassifier->setClassDecompInfo( classindexmapping[i], getClassDecompInfo(i) );
211  }
212  }
213 
214  newClassifier->setClassDescription( 0, newClassdesc.str().c_str() );
215 
216 
217  return newClassifier;
218 }
219 
221 void VarClassifier::setClassDecompInfo( int givenClassindex, VAR_DECOMPINFO givenDecompInfo )
222 {
223  assert( givenDecompInfo == ALL || givenDecompInfo == LINKING || givenDecompInfo == MASTER || givenDecompInfo == BLOCK );
224 
225  IndexClassifier::setClassDecompInfo( givenClassindex, (int) givenDecompInfo );
226 }
227 
228 } /* namespace gcg */
std::vector< int > getNVarsOfClasses()
const char * getClassNameOfVar(int varindex)
VarClassifier(SCIP *scip, const char *name, int nClasses, int nVars)
VarClassifier * reduceClasses(int maxNumberOfClasses)
void setClassName(int classindex, const char *name)
void setClassDecompInfo(int classindex, int decompInfo)
const char * getClassName(int classindex)
std::vector< int > getIndicesToClasses()
const char * getClassNameOfIndex(int index)
int addClass(const char *name, const char *desc)
void setClassDecompInfo(int classindex, VAR_DECOMPINFO decompInfo)
int getClassDecompInfo(int classindex)
int getClassOfVar(int varindex)
std::vector< std::vector< int > > getAllSubsets(bool all, bool linking, bool master, bool block)
enum VarClassDecompInfo VAR_DECOMPINFO
VAR_DECOMPINFO getClassDecompInfo(int classindex)
void assignIndexToClass(int index, int classindex)
std::vector< int > getNIndicesOfClasses()
const char * getClassDescription(int classindex)
const int * getVarsToClasses()
void setClassDescription(int classindex, const char *desc)
bool isVarClassified(int varindex)
std::vector< std::vector< int > > getAllSubsets(std::vector< int > &classindices)
class for classifying variables
std::vector< int > reduceClasses(int maxNumberOfClasses)
int addClass(const char *name, const char *desc, VAR_DECOMPINFO decompInfo)
void assignVarToClass(int varindex, int classindex)