Scippy

GCG

Branch-and-Price & Column Generation for Everyone

class_varpartition.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-2021 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 
28 /**@file class_varpartition.cpp
29  * @brief class representing a partition of a set of variables
30  * @author Julius Hense
31  *
32  */
33 
34 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
35 
36 #include "class_varpartition.h"
37 
38 #include <cassert>
39 #include <sstream>
40 #include <algorithm>
41 
42 
43 namespace gcg {
44 
45 
46 /** constructor */
48  SCIP* _scip,
49  const char* givenName,
50  int givenNClasses,
51  int givenNVars
52 ) :
53  IndexPartition(_scip, givenName, givenNClasses, givenNVars)
54 {
55 
56 }
57 
58 /** copy constructor */
60  const VarPartition* toCopy
61 ) : IndexPartition(toCopy)
62 {
63 }
64 
65 /** destructor */
67 {
68 }
69 
70 /** creates a new class, returns index of the class */
71 int VarPartition::addClass(const char* givenName, const char* givenDesc, VAR_DECOMPINFO givenDecompInfo)
72 {
73  int classindex = IndexPartition::addClass(givenName, givenDesc);
74  setClassDecompInfo(classindex, givenDecompInfo);
75 
76  return classindex;
77 }
78 
79 /** assigns a variable to a class */
80 void VarPartition::assignVarToClass(int givenVarindex, int givenClassindex)
81 {
82  IndexPartition::assignIndexToClass(givenVarindex, givenClassindex);
83 }
84 
85 /** returns a vector containing all possible subsets of the chosen classindices */
86 std::vector<std::vector<int>> VarPartition::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 IndexPartition::getAllSubsets(classindices);
99 }
100 
101 /** returns the decomposition code of a class */
103 {
104  int decompInfo = IndexPartition::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 
131 
132 /** returns the name of the class a variable is assigned to */
133 const char* VarPartition::getClassNameOfVar(int givenVarindex)
134 {
135  return IndexPartition::getClassNameOfIndex(givenVarindex );
136 }
137 
138 /** returns the index of the class a variable is assigned to */
139 int VarPartition::getClassOfVar(int givenVarindex)
140 {
141  return IndexPartition::getClassOfIndex(givenVarindex);
142 }
143 
144 /** returns vector containing the assigned class of each variable */
146 {
147  std::vector<int>& varsToClasses = IndexPartition::getIndicesToClasses();
148  if( !varsToClasses.empty() )
149  return &varsToClasses[0];
150  else
151  return NULL;
152 }
153 
154 /** returns the number of variables */
156 {
158 }
159 
160 /** returns a vector with the numbers of variables that are assigned to the classes */
162 {
164 }
165 
166 
167 /** returns whether a variable is already assigned to a class */
168 bool VarPartition::isVarClassified(int givenVarindex)
169 {
170  return IndexPartition::isIndexClassified(givenVarindex);
171 }
172 
173 /** returns partition with reduced number of classes */
175 {
176  std::vector<int> classindexmapping = IndexPartition::reduceClasses(givenMaxNumber);
177  VarPartition* newPartition;
178  std::stringstream newName;
179  std::stringstream newClassdesc;
180 
181  if( classindexmapping.empty() )
182  return NULL;
183 
184  /* create new VarPartition */
185  newName << getName() << "-red-to-" << givenMaxNumber;
186  newPartition = new VarPartition(scip, newName.str().c_str(), givenMaxNumber, getNVars());
187 
188  /* reassign vars */
189  for( int i = 0; i < newPartition->getNVars(); ++i)
190  {
191  if( getClassOfVar(i) != -1 )
192  {
193  newPartition->assignVarToClass(i, classindexmapping[getClassOfVar(i)]);
194  }
195  }
196 
197  /* set new class names and descriptions (enlarged class has index 0) */
198  newPartition->setClassName(0, "merged");
199  newPartition->setClassDecompInfo(0, ALL);
200 
201  for( int i = 0; i < getNClasses(); ++i )
202  {
203  if( classindexmapping[i] == 0 )
204  {
205  newClassdesc << getClassDescription( i ) << " - ";
206  }
207  else
208  {
209  newPartition->setClassName(classindexmapping[i], getClassName(i));
210  newPartition->setClassDescription(classindexmapping[i], getClassDescription(i));
211  newPartition->setClassDecompInfo(classindexmapping[i], getClassDecompInfo(i));
212  }
213  }
214 
215  newPartition->setClassDescription(0, newClassdesc.str().c_str());
216 
217  return newPartition;
218 }
219 
220 /** sets the decomposition code of a class */
221 void VarPartition::setClassDecompInfo(int givenClassindex, VAR_DECOMPINFO givenDecompInfo)
222 {
223  assert( givenDecompInfo == ALL || givenDecompInfo == LINKING || givenDecompInfo == MASTER || givenDecompInfo == BLOCK );
224 
225  IndexPartition::setClassDecompInfo(givenClassindex, (int) givenDecompInfo);
226 }
227 
228 } /* namespace gcg */
void setClassDescription(int classindex, const char *desc)
int addClass(const char *name, const char *desc)
class representing a partition of a set of variables
int addClass(const char *name, const char *desc, VAR_DECOMPINFO decompInfo)
int getClassOfVar(int varindex)
std::vector< int > & getIndicesToClasses()
bool isVarClassified(int varindex)
void setClassDecompInfo(int classindex, int decompInfo)
VarPartition(SCIP *scip, const char *name, int nClasses, int nVars)
enum VarClassDecompInfo VAR_DECOMPINFO
VarPartition * reduceClasses(int maxNumberOfClasses)
const char * getClassName(int classindex)
std::vector< std::vector< int > > getAllSubsets(std::vector< int > &classindices)
void setClassName(int classindex, const char *name)
void assignVarToClass(int varindex, int classindex)
const char * getClassNameOfVar(int varindex)
bool isIndexClassified(int index)
const int * getVarsToClasses()
std::vector< int > getNIndicesOfClasses()
void setClassDecompInfo(int classindex, VAR_DECOMPINFO decompInfo)
std::vector< std::vector< int > > getAllSubsets(bool all, bool linking, bool master, bool block)
const char * getClassNameOfIndex(int index)
std::vector< int > reduceClasses(int maxNumberOfClasses)
void assignIndexToClass(int index, int classindex)
std::vector< int > getNVarsOfClasses()
VAR_DECOMPINFO getClassDecompInfo(int classindex)
int getClassDecompInfo(int classindex)
const char * getClassDescription(int classindex)