GCG 4.1.1 (dev)

Generic Column Generation

Loading...
Searching...
No Matches
class_indexpartition.cpp
Go to the documentation of this file.
1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2/* */
3/* This file is part of the program and library */
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-2026 Operations Research, RWTH Aachen University */
10/* Zuse Institute Berlin (ZIB) */
11/* */
12/* Licensed under the Apache License, Version 2.0 (the "License"); */
13/* you may not use this file except in compliance with the License. */
14/* You may obtain a copy of the License at */
15/* */
16/* http://www.apache.org/licenses/LICENSE-2.0 */
17/* */
18/* Unless required by applicable law or agreed to in writing, software */
19/* distributed under the License is distributed on an "AS IS" BASIS, */
20/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
21/* See the License for the specific language governing permissions and */
22/* limitations under the License. */
23/* */
24/* You should have received a copy of the Apache-2.0 license */
25/* along with GCG; see the file LICENSE. If not visit gcg.or.rwth-aachen.de.*/
26/* */
27/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
28
29/**@file class_indexpartition.cpp
30 * @brief generalization of ConsPartition and VarPartition
31 * @author Julius Hense
32 *
33 */
34
35/*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
36
38
39#include <cassert>
40#include <sstream>
41#include <algorithm>
42
43namespace gcg {
44
45/** local methods */
46
47struct sort_pred {
48 bool operator()(const std::pair<int,int> &left, const std::pair<int,int> &right) {
49 return left.second < right.second;
50 }
51};
52
53/** constructor */
55 GCG* gcgstruct,
56 const char* givenName,
57 int givenNClasses,
58 int givenNIndices
59) :
60 gcg(gcgstruct), name(std::string(givenName)), nClasses(givenNClasses), nIndices(givenNIndices), indicesToClasses(givenNIndices, -1),
61 classNames(givenNClasses, ""),
62 classDescriptions(givenNClasses, ""),
63 classDecompInfo( nClasses, 0 )
64{
65}
66
67/** copy constructor */
69{
70 assert( toCopy != NULL );
71 gcg = toCopy->gcg;
72 name = toCopy->name;
73 nClasses = toCopy->nClasses;
74 nIndices = toCopy->nIndices;
75 indicesToClasses = toCopy->indicesToClasses;
76 classNames.assign(nClasses, "");
77 classDescriptions.assign(nClasses, "");
78 classDecompInfo.assign(nClasses, 0);
79 for ( int i = 0; i < nClasses; ++i )
80 {
81 classNames[i] = toCopy->classNames[i];
82 classDescriptions[i] = toCopy->classDescriptions[i];
83 classDecompInfo[i] = toCopy->classDecompInfo[i];
84 }
85}
86
87/** destructor */
91
92/** creates a new class, returns index of the class */
93int IndexPartition::addClass(const char* givenName, const char* givenDesc)
94{
95 assert((int) classNames.size() == nClasses);
96 assert((int) classDescriptions.size() == nClasses);
97 assert((int) classDecompInfo.size() == nClasses);
98
99 std::string givenClassName = std::string(givenName);
100
101 classNames.push_back(givenClassName);
102 classDescriptions.push_back(givenDesc);
103 classDecompInfo.push_back(0);
104
105 ++nClasses;
106
107 return nClasses - 1;
108}
109
110/** assigns an index to a class */
111void IndexPartition::assignIndexToClass(int givenIndex, int givenClassindex)
112{
113 assert(0 <= givenIndex && givenIndex < nIndices);
114 assert(-1 <= givenClassindex && givenClassindex < nClasses);
115
116 indicesToClasses[givenIndex] = givenClassindex;
117}
118
119/** returns true if the other partition has an equivalent index structure,
120 * meaning that the partition of the set of constraints is the same ignoring the concrete classindices, classnames, etc. */
122{
123 std::vector<int> classMapping ( getNClasses(), -1 );
124
125 /* check whether number of indices and classes is the same */
126 assert(getNIndices() == otherPartition->getNIndices() );
127 if( getNClasses() != otherPartition->getNClasses() )
128 return false;
129
130 /* check whether index classes in this partition are subsets of classes in current partition */
131 for( int i = 0; i < getNIndices(); ++i )
132 {
133 if( isIndexClassified(i) )
134 {
135 int compClass = getClassOfIndex(i);
136
137 if( classMapping[compClass] == -1 )
138 classMapping[compClass] = otherPartition->getClassOfIndex(i);
139 else if( classMapping[compClass] != otherPartition->getClassOfIndex(i) )
140 return false;
141 }
142 else if( otherPartition->isIndexClassified(i) )
143 {
144 return false;
145 }
146 }
147
148 /* check whether index classes in this partition are strict subsets of classes in current partition */
149 for( size_t c = 0; c < classMapping.size(); ++c )
150 {
151 if( classMapping[c] != -1 )
152 {
153 for( size_t j = c + 1; j < classMapping.size(); ++j )
154 {
155 if( classMapping[c] == classMapping[j] )
156 return false;
157 }
158 }
159 }
160
161 return true;
162}
163
164/** returns a vector containing all possible subsets of the given classindices */
165std::vector<std::vector<int>> IndexPartition::getAllSubsets(std::vector<int>& givenClassindices)
166{
167 std::vector<std::vector<int>> subsets;
168 std::vector<int> empty;
169 subsets.push_back(empty);
170
171 for( int& givenClassindice : givenClassindices )
172 {
173 std::vector< std::vector<int> > subsetTemp = subsets;
174
175 for( auto& j : subsetTemp )
176 j.push_back(givenClassindice);
177
178 for( auto& j : subsetTemp )
179 subsets.push_back(j);
180 }
181 return subsets;
182}
183
184/** returns the decomposition info of the a class */
186{
187 assert(0 <= givenClassindex && givenClassindex < nClasses);
188
189 return classDecompInfo[givenClassindex];
190}
191
192/** returns the information text of a class */
193const char* IndexPartition::getClassDescription(int givenClassindex)
194{
195 assert(0 <= givenClassindex && givenClassindex < nClasses);
196
197 return classDescriptions[givenClassindex].c_str();
198}
199
200/** returns the name of a class */
201const char* IndexPartition::getClassName(int givenClassindex)
202{
203 assert(0 <= givenClassindex && givenClassindex < nClasses);
204
205 return classNames[givenClassindex].c_str();
206}
207
208/** returns the name of the class an index is assigned to */
209const char* IndexPartition::getClassNameOfIndex(int givenIndex)
210{
211 assert(0 <= givenIndex && givenIndex < nIndices);
212 assert(0 <= indicesToClasses[givenIndex] && indicesToClasses[givenIndex] < nClasses);
213
214 return classNames[indicesToClasses[givenIndex]].c_str();
215}
216
217
218/** returns the index of the class an index is assigned to */
220{
221 assert(0 <= givenIndex && givenIndex < nIndices);
222
223 return indicesToClasses[givenIndex];
224}
225
226/** returns vector containing the assigned class of each index */
228{
229 return indicesToClasses;
230}
231
232
233/** returns the name of the partition */
235{
236 return name.c_str();
237}
238
239
240/** returns the number of classes the partition provides */
242{
243 return nClasses;
244}
245
246/** returns the number of indices */
248{
249 return nIndices;
250}
251
252/** returns a vector with the numbers of indices that are assigned to the classes */
254{
255 std::vector<int> nIndicesOfClasses( nClasses, 0 );
256
257 if ( nClasses == 0 )
258 return nIndicesOfClasses;
259
260 for ( int i = 0; i < nIndices; ++i)
261 {
262 if ( indicesToClasses[i] != -1 )
263 ++nIndicesOfClasses[indicesToClasses[i]];
264 }
265 return nIndicesOfClasses;
266}
267
268/** returns whether an index is already assigned to a class */
270{
271 assert(0 <= givenIndex && givenIndex < nIndices);
272
273 return indicesToClasses[givenIndex] != -1;
274}
275
276/** returns a class index mapping for creating a new partition */
277std::vector<int> IndexPartition::reduceClasses(int givenMaxNumber)
278{
279 assert( givenMaxNumber > 0 );
280
281 if ( getNClasses() <= givenMaxNumber || nClasses >= 2*givenMaxNumber)
282 return std::vector<int>(0);
283
284 std::vector<int> classindexmapping(nClasses, 0);
285 int enlargedclass = nClasses - givenMaxNumber;
286
287 /* count number of indices per class */
288 std::vector<std::pair<int,int>> nmembers( nClasses, std::pair<int,int>(0,0) );
289 for( int i = 0; i < nClasses; ++i )
290 {
291 nmembers[i].first = i;
292 }
293
294 std::vector<int>::const_iterator iter = indicesToClasses.begin();
295 std::vector<int>::const_iterator iterend = indicesToClasses.end();
296 for( ; iter < iterend; ++iter )
297 {
298 if ( *iter != -1 )
299 nmembers[*iter].second++;
300 }
301
302 /* map the classes with high numbers of assigned indices to new class indices */
303 std::sort( nmembers.begin(), nmembers.end(), sort_pred());
304 for( int i = 1; i < givenMaxNumber; ++i )
305 {
306 classindexmapping[nmembers[enlargedclass + i].first] = i;
307 }
308
309 return classindexmapping;
310}
311
312/** removes all classes which do not have any assigned indices (classindices may change)
313 * returns number of removed classes */
315{
316 if ( nClasses == 0 )
317 return 0;
318
319 /* firstly, find empty classes */
320 std::vector<int> toDelete(0);
321 std::vector<int> nIndicesPerClasses(nClasses, 0);
322
323 for ( int i = 0; i < nIndices; ++i )
324 {
325 if ( indicesToClasses[i] != -1 )
326 ++nIndicesPerClasses[indicesToClasses[i]];
327 }
328
329 for ( int i = 0; i < nClasses; ++i )
330 {
331 if ( nIndicesPerClasses[i] == 0 )
332 {
333 toDelete.push_back(i);
334 }
335 }
336
337 /* secondly, update data */
338 for ( size_t i = 0; i < toDelete.size(); ++i )
339 {
340 int classindex = toDelete[toDelete.size() - 1 - i];
341
342 for ( int j = 0; j < nIndices; ++j )
343 {
344 assert( indicesToClasses[j] != classindex );
345 if ( indicesToClasses[j] > classindex )
346 --indicesToClasses[j];
347 }
348 classNames.erase(classNames.begin() + classindex);
349 classDescriptions.erase(classDescriptions.begin() + classindex);
350 classDecompInfo.erase(classDecompInfo.begin() + classindex);
351 --nClasses;
352 }
353
354 return (int) toDelete.size();
355}
356
357/** sets the decomposition info of the a class */
358void IndexPartition::setClassDecompInfo(int givenClassindex, int givenDecompInfo)
359{
360 assert(0 <= givenClassindex && givenClassindex < nClasses);
361
362 classDecompInfo[givenClassindex] = givenDecompInfo;
363}
364
365/** sets the information text of a class */
366void IndexPartition::setClassDescription(int givenClassindex, const char* givenDesc)
367{
368 assert(0 <= givenClassindex && givenClassindex < nClasses);
369
370 classDescriptions[givenClassindex] = std::string(givenDesc);
371}
372
373/** sets the name of a class */
374void IndexPartition::setClassName(int givenClassindex, const char* givenName)
375{
376 assert(0 <= givenClassindex && givenClassindex < nClasses);
377
378 classNames[givenClassindex] = std::string(givenName);
379}
380
381} /* namespace gcg */
382
generalization of ConsPartition and VarPartition
GCG_EXPORT std::vector< int > reduceClasses(int maxNumberOfClasses)
GCG_EXPORT const char * getClassName(int classindex)
std::vector< int > getNIndicesOfClasses()
GCG_EXPORT const char * getName()
std::vector< int > & getIndicesToClasses()
IndexPartition(GCG *gcgstruct, const char *name, int nClasses, int nIndices)
GCG_EXPORT void setClassDescription(int classindex, const char *desc)
void assignIndexToClass(int index, int classindex)
int getClassDecompInfo(int classindex)
GCG_EXPORT bool isDuplicateOf(IndexPartition *otherPartition)
bool isIndexClassified(int index)
GCG_EXPORT const char * getClassDescription(int classindex)
GCG_EXPORT int removeEmptyClasses()
int addClass(const char *name, const char *desc)
const char * getClassNameOfIndex(int index)
GCG_EXPORT void setClassName(int classindex, const char *name)
std::vector< std::vector< int > > getAllSubsets(std::vector< int > &classindices)
void setClassDecompInfo(int classindex, int decompInfo)
static assert(heur !=NULL)
int j
int i
#define NULL
bool operator()(const std::pair< int, int > &left, const std::pair< int, int > &right)
struct Gcg GCG
Definition type_gcg.h:44