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 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_conspartition.cpp
30 * @brief class representing a partition of a set of constraints
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
43
44namespace gcg {
45
46
47/** constructor */
49 GCG* gcgstruct,
50 const char* givenName,
51 int givenNClasses,
52 int givenNCons
53) :
54 IndexPartition(gcgstruct, givenName, givenNClasses, givenNCons)
55{
56
57}
58
59/** copy constructor */
61 const ConsPartition* toCopy
62) : IndexPartition(toCopy )
63{
64}
65
66/** destructor */
68{
69}
70
71/** creates a new class, returns index of the class */
72int ConsPartition::addClass(const char* givenName, const char* givenDesc, CONS_DECOMPINFO givenDecompInfo)
73{
74 int classindex = IndexPartition::addClass(givenName, givenDesc);
75 setClassDecompInfo(classindex, givenDecompInfo);
76
77 return classindex;
78}
79
80/** assigns a constraint to a class */
81void ConsPartition::assignConsToClass(int givenConsindex, int givenClassindex)
82{
83 IndexPartition::assignIndexToClass(givenConsindex, givenClassindex );
84}
85
86/** returns a vector containing all possible subsets of the chosen classindices */
87std::vector<std::vector<int>> ConsPartition::getAllSubsets(bool both, bool only_master, bool only_pricing )
88{
89 std::vector<int> classindices;
90 for( int i = 0; i < getNClasses(); ++i )
91 {
92 if( ( both && getClassDecompInfo( i ) == BOTH ) || ( only_master && getClassDecompInfo( i ) == ONLY_MASTER )
93 || ( only_pricing && getClassDecompInfo( i ) == ONLY_PRICING ) )
94 classindices.push_back( i );
95 }
96 return IndexPartition::getAllSubsets(classindices );
97}
98
99/** returns the decomposition code of a class */
101{
102 int decompInfo = IndexPartition::getClassDecompInfo(givenClassindex);
103 CONS_DECOMPINFO interp;
104
105 assert( 0 <= decompInfo && decompInfo <= 2);
106
107 switch ( decompInfo )
108 {
109 case 0:
110 interp = BOTH;
111 break;
112 case 1:
113 interp = ONLY_MASTER;
114 break;
115 case 2:
116 interp = ONLY_PRICING;
117 break;
118 default:
119 interp = BOTH;
120 break;
121 }
122
123 return interp;
124}
125
126/** returns the name of the class a constraint is assigned to */
127const char* ConsPartition::getClassNameOfCons(int givenConsindex)
128{
129 return IndexPartition::getClassNameOfIndex(givenConsindex);
130}
131
132/** returns the index of the class a constraint is assigned to */
133int ConsPartition::getClassOfCons(int givenConsindex)
134{
135 return IndexPartition::getClassOfIndex(givenConsindex);
136}
137
138/** returns vector containing the assigned class of each constraint */
140{
141 std::vector<int>& conssToClasses = IndexPartition::getIndicesToClasses();
142 if( !conssToClasses.empty() )
143 return &conssToClasses[0];
144 else
145 return NULL;
146}
147
148/** returns the number of constraints */
150{
152}
153
154/** returns a vector with the numbers of constraints that are assigned to the classes */
156{
158}
159
160
161/** returns whether a constraint is already assigned to a class */
162bool ConsPartition::isConsClassified(int givenConsindex)
163{
164 return IndexPartition::isIndexClassified(givenConsindex);
165}
166
167/** returns partition with reduced number of classes */
169{
170 std::vector<int> classindexmapping = IndexPartition::reduceClasses(givenMaxNumber);
171 ConsPartition* newPartition;
172 std::stringstream newName;
173 std::stringstream newClassdesc;
174
175 if( classindexmapping.empty() )
176 return NULL;
177
178 /* create new ConsPartition */
179 newName << getName() << "-red-to-" << givenMaxNumber;
180 newPartition = new ConsPartition(gcg, newName.str().c_str(), givenMaxNumber, getNConss());
181
182 /* reassign conss */
183 for( int i = 0; i < newPartition->getNConss(); ++i)
184 {
185 if( getClassOfCons(i) != -1 )
186 {
187 newPartition->assignConsToClass(i, classindexmapping[getClassOfCons(i)]);
188 }
189 }
190
191 /* set new class names and descriptions (enlarged class has index 0) */
192 newPartition->setClassName(0, "merged");
193 newPartition->setClassDecompInfo(0, BOTH);
194
195 for( int i = 0; i < getNClasses(); ++i )
196 {
197 if( classindexmapping[i] == 0 )
198 {
199 newClassdesc << getClassDescription( i ) << " - ";
200 }
201 else
202 {
203 newPartition->setClassName(classindexmapping[i], getClassName(i));
204 newPartition->setClassDescription(classindexmapping[i], getClassDescription(i));
205 newPartition->setClassDecompInfo(classindexmapping[i], getClassDecompInfo(i));
206 }
207 }
208
209 newPartition->setClassDescription(0, newClassdesc.str().c_str());
210
211 return newPartition;
212}
213
214/** sets the decomposition code of a class */
215void ConsPartition::setClassDecompInfo(int givenClassindex, CONS_DECOMPINFO givenDecompInfo)
216{
217 assert(givenDecompInfo == BOTH || givenDecompInfo == ONLY_MASTER || givenDecompInfo == ONLY_PRICING );
218
219 IndexPartition::setClassDecompInfo(givenClassindex, (int) givenDecompInfo );
220}
221
222} /* namespace gcg */
class representing a partition of a set of constraints
GCG_EXPORT void assignConsToClass(int consindex, int classindex)
GCG_EXPORT const int * getConssToClasses()
GCG_EXPORT void setClassDecompInfo(int classindex, CONS_DECOMPINFO decompInfo)
GCG_EXPORT ConsPartition(GCG *gcgstruct, const char *name, int nClasses, int nConss)
GCG_EXPORT int addClass(const char *name, const char *desc, CONS_DECOMPINFO decompInfo)
GCG_EXPORT std::vector< int > getNConssOfClasses()
GCG_EXPORT int getClassOfCons(int consindex)
GCG_EXPORT ConsPartition * reduceClasses(int maxNumberOfClasses)
GCG_EXPORT const char * getClassNameOfCons(int consindex)
GCG_EXPORT bool isConsClassified(int consindex)
GCG_EXPORT CONS_DECOMPINFO getClassDecompInfo(int classindex)
GCG_EXPORT std::vector< std::vector< int > > getAllSubsets(bool both, bool only_master, bool only_pricing)
GCG_EXPORT int getNConss()
GCG_EXPORT int getNClasses()
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()
GCG_EXPORT void setClassDescription(int classindex, const char *desc)
void assignIndexToClass(int index, int classindex)
int getClassDecompInfo(int classindex)
bool isIndexClassified(int index)
GCG_EXPORT const char * getClassDescription(int classindex)
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)
enum ConsClassDecompInfo CONS_DECOMPINFO
#define NULL
Definition: struct_gcg.h:55