Scippy

GCG

Branch-and-Price & Column Generation for Everyone

class_conspartition.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_conspartition.cpp
29  * @brief class representing a partition of a set of constraints
30  * @author Julius Hense
31  *
32  */
33 
34 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
35 
36 #include "class_conspartition.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 givenNCons
52 ) :
53  IndexPartition(_scip, givenName, givenNClasses, givenNCons)
54 {
55 
56 }
57 
58 /** copy constructor */
60  const ConsPartition* toCopy
61 ) : IndexPartition(toCopy )
62 {
63 }
64 
65 /** destructor */
67 {
68 }
69 
70 /** creates a new class, returns index of the class */
71 int ConsPartition::addClass(const char* givenName, const char* givenDesc, CONS_DECOMPINFO givenDecompInfo)
72 {
73  int classindex = IndexPartition::addClass(givenName, givenDesc);
74  setClassDecompInfo(classindex, givenDecompInfo);
75 
76  return classindex;
77 }
78 
79 /** assigns a constraint to a class */
80 void ConsPartition::assignConsToClass(int givenConsindex, int givenClassindex)
81 {
82  IndexPartition::assignIndexToClass(givenConsindex, givenClassindex );
83 }
84 
85 /** returns a vector containing all possible subsets of the chosen classindices */
86 std::vector<std::vector<int>> ConsPartition::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 IndexPartition::getAllSubsets(classindices );
96 }
97 
98 /** returns the decomposition code of a class */
100 {
101  int decompInfo = IndexPartition::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 
125 /** returns the name of the class a constraint is assigned to */
126 const char* ConsPartition::getClassNameOfCons(int givenConsindex)
127 {
128  return IndexPartition::getClassNameOfIndex(givenConsindex);
129 }
130 
131 /** returns the index of the class a constraint is assigned to */
132 int ConsPartition::getClassOfCons(int givenConsindex)
133 {
134  return IndexPartition::getClassOfIndex(givenConsindex);
135 }
136 
137 /** returns vector containing the assigned class of each constraint */
139 {
140  std::vector<int>& conssToClasses = IndexPartition::getIndicesToClasses();
141  if( !conssToClasses.empty() )
142  return &conssToClasses[0];
143  else
144  return NULL;
145 }
146 
147 /** returns the number of constraints */
149 {
151 }
152 
153 /** returns a vector with the numbers of constraints that are assigned to the classes */
155 {
157 }
158 
159 
160 /** returns whether a constraint is already assigned to a class */
161 bool ConsPartition::isConsClassified(int givenConsindex)
162 {
163  return IndexPartition::isIndexClassified(givenConsindex);
164 }
165 
166 /** returns partition with reduced number of classes */
168 {
169  std::vector<int> classindexmapping = IndexPartition::reduceClasses(givenMaxNumber);
170  ConsPartition* newPartition;
171  std::stringstream newName;
172  std::stringstream newClassdesc;
173 
174  if( classindexmapping.empty() )
175  return NULL;
176 
177  /* create new ConsPartition */
178  newName << getName() << "-red-to-" << givenMaxNumber;
179  newPartition = new ConsPartition(scip, newName.str().c_str(), givenMaxNumber, getNConss());
180 
181  /* reassign conss */
182  for( int i = 0; i < newPartition->getNConss(); ++i)
183  {
184  if( getClassOfCons(i) != -1 )
185  {
186  newPartition->assignConsToClass(i, classindexmapping[getClassOfCons(i)]);
187  }
188  }
189 
190  /* set new class names and descriptions (enlarged class has index 0) */
191  newPartition->setClassName(0, "merged");
192  newPartition->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  newPartition->setClassName(classindexmapping[i], getClassName(i));
203  newPartition->setClassDescription(classindexmapping[i], getClassDescription(i));
204  newPartition->setClassDecompInfo(classindexmapping[i], getClassDecompInfo(i));
205  }
206  }
207 
208  newPartition->setClassDescription(0, newClassdesc.str().c_str());
209 
210  return newPartition;
211 }
212 
213 /** sets the decomposition code of a class */
214 void ConsPartition::setClassDecompInfo(int givenClassindex, CONS_DECOMPINFO givenDecompInfo)
215 {
216  assert(givenDecompInfo == BOTH || givenDecompInfo == ONLY_MASTER || givenDecompInfo == ONLY_PRICING );
217 
218  IndexPartition::setClassDecompInfo(givenClassindex, (int) givenDecompInfo );
219 }
220 
221 } /* namespace gcg */
bool isConsClassified(int consindex)
void setClassDescription(int classindex, const char *desc)
int addClass(const char *name, const char *desc)
enum ConsClassDecompInfo CONS_DECOMPINFO
std::vector< int > & getIndicesToClasses()
void assignConsToClass(int consindex, int classindex)
class representing a partition of a set of constraints
void setClassDecompInfo(int classindex, CONS_DECOMPINFO decompInfo)
const int * getConssToClasses()
std::vector< int > getNConssOfClasses()
void setClassDecompInfo(int classindex, int decompInfo)
CONS_DECOMPINFO getClassDecompInfo(int classindex)
const char * getClassName(int classindex)
std::vector< std::vector< int > > getAllSubsets(std::vector< int > &classindices)
void setClassName(int classindex, const char *name)
int addClass(const char *name, const char *desc, CONS_DECOMPINFO decompInfo)
int getClassOfCons(int consindex)
const char * getClassNameOfCons(int consindex)
bool isIndexClassified(int index)
ConsPartition * reduceClasses(int maxNumberOfClasses)
std::vector< int > getNIndicesOfClasses()
const char * getClassNameOfIndex(int index)
ConsPartition(SCIP *scip, const char *name, int nClasses, int nConss)
std::vector< int > reduceClasses(int maxNumberOfClasses)
std::vector< std::vector< int > > getAllSubsets(bool both, bool only_master, bool only_pricing)
void assignIndexToClass(int index, int classindex)
int getClassDecompInfo(int classindex)
const char * getClassDescription(int classindex)