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 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_varpartition.cpp
30 * @brief class representing a partition of a set of variables
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 givenNVars
53) :
54 IndexPartition(gcgstruct, givenName, givenNClasses, givenNVars)
55{
56
57}
58
59/** copy constructor */
61 const VarPartition* toCopy
62) : IndexPartition(toCopy)
63{
64}
65
66/** destructor */
68{
69}
70
71/** creates a new class, returns index of the class */
72int VarPartition::addClass(const char* givenName, const char* givenDesc, VAR_DECOMPINFO givenDecompInfo)
73{
74 int classindex = IndexPartition::addClass(givenName, givenDesc);
75 setClassDecompInfo(classindex, givenDecompInfo);
76
77 return classindex;
78}
79
80/** assigns a variable to a class */
81void VarPartition::assignVarToClass(int givenVarindex, int givenClassindex)
82{
83 IndexPartition::assignIndexToClass(givenVarindex, givenClassindex);
84}
85
86/** returns a vector containing all possible subsets of the chosen classindices */
87std::vector<std::vector<int>> VarPartition::getAllSubsets(bool all, bool linking, bool master, bool block)
88{
89 std::vector<int> classindices;
90 for ( int i = 0; i < getNClasses(); ++i )
91 {
92 if( ( all && getClassDecompInfo(i) == ALL ) ||
93 ( linking && getClassDecompInfo(i) == LINKING ) ||
94 ( master && getClassDecompInfo(i) == MASTER ) ||
95 ( block && getClassDecompInfo(i) == BLOCK )
96 )
97 classindices.push_back(i);
98 }
99 return IndexPartition::getAllSubsets(classindices);
100}
101
102/** returns the decomposition code of a class */
104{
105 int decompInfo = IndexPartition::getClassDecompInfo(givenClassindex);
106 VAR_DECOMPINFO interp;
107
108 assert( 0 <= decompInfo && decompInfo <= 3);
109
110 switch ( decompInfo )
111 {
112 case 0:
113 interp = ALL;
114 break;
115 case 1:
116 interp = LINKING;
117 break;
118 case 2:
119 interp = MASTER;
120 break;
121 case 3:
122 interp = BLOCK;
123 break;
124 default:
125 interp = ALL;
126 break;
127 }
128
129 return interp;
130}
131
132
133/** returns the name of the class a variable is assigned to */
134const char* VarPartition::getClassNameOfVar(int givenVarindex)
135{
136 return IndexPartition::getClassNameOfIndex(givenVarindex );
137}
138
139/** returns the index of the class a variable is assigned to */
140int VarPartition::getClassOfVar(int givenVarindex)
141{
142 return IndexPartition::getClassOfIndex(givenVarindex);
143}
144
145/** returns vector containing the assigned class of each variable */
147{
148 std::vector<int>& varsToClasses = IndexPartition::getIndicesToClasses();
149 if( !varsToClasses.empty() )
150 return &varsToClasses[0];
151 else
152 return NULL;
153}
154
155/** returns the number of variables */
157{
159}
160
161/** returns a vector with the numbers of variables that are assigned to the classes */
163{
165}
166
167
168/** returns whether a variable is already assigned to a class */
169bool VarPartition::isVarClassified(int givenVarindex)
170{
171 return IndexPartition::isIndexClassified(givenVarindex);
172}
173
174/** returns partition with reduced number of classes */
176{
177 std::vector<int> classindexmapping = IndexPartition::reduceClasses(givenMaxNumber);
178 VarPartition* newPartition;
179 std::stringstream newName;
180 std::stringstream newClassdesc;
181
182 if( classindexmapping.empty() )
183 return NULL;
184
185 /* create new VarPartition */
186 newName << getName() << "-red-to-" << givenMaxNumber;
187 newPartition = new VarPartition(gcg, newName.str().c_str(), givenMaxNumber, getNVars());
188
189 /* reassign vars */
190 for( int i = 0; i < newPartition->getNVars(); ++i)
191 {
192 if( getClassOfVar(i) != -1 )
193 {
194 newPartition->assignVarToClass(i, classindexmapping[getClassOfVar(i)]);
195 }
196 }
197
198 /* set new class names and descriptions (enlarged class has index 0) */
199 newPartition->setClassName(0, "merged");
200 newPartition->setClassDecompInfo(0, ALL);
201
202 for( int i = 0; i < getNClasses(); ++i )
203 {
204 if( classindexmapping[i] == 0 )
205 {
206 newClassdesc << getClassDescription( i ) << " - ";
207 }
208 else
209 {
210 newPartition->setClassName(classindexmapping[i], getClassName(i));
211 newPartition->setClassDescription(classindexmapping[i], getClassDescription(i));
212 newPartition->setClassDecompInfo(classindexmapping[i], getClassDecompInfo(i));
213 }
214 }
215
216 newPartition->setClassDescription(0, newClassdesc.str().c_str());
217
218 return newPartition;
219}
220
221/** sets the decomposition code of a class */
222void VarPartition::setClassDecompInfo(int givenClassindex, VAR_DECOMPINFO givenDecompInfo)
223{
224 assert( givenDecompInfo == ALL || givenDecompInfo == LINKING || givenDecompInfo == MASTER || givenDecompInfo == BLOCK );
225
226 IndexPartition::setClassDecompInfo(givenClassindex, (int) givenDecompInfo);
227}
228
229} /* namespace gcg */
class representing a partition of a set of variables
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)
GCG_EXPORT void setClassDecompInfo(int classindex, VAR_DECOMPINFO decompInfo)
GCG_EXPORT int getClassOfVar(int varindex)
GCG_EXPORT VarPartition * reduceClasses(int maxNumberOfClasses)
GCG_EXPORT bool isVarClassified(int varindex)
GCG_EXPORT void assignVarToClass(int varindex, int classindex)
GCG_EXPORT ~VarPartition()
GCG_EXPORT const char * getClassNameOfVar(int varindex)
GCG_EXPORT std::vector< int > getNVarsOfClasses()
GCG_EXPORT VAR_DECOMPINFO getClassDecompInfo(int classindex)
GCG_EXPORT VarPartition(GCG *gcgstruct, const char *name, int nClasses, int nVars)
GCG_EXPORT int getNVars()
GCG_EXPORT std::vector< std::vector< int > > getAllSubsets(bool all, bool linking, bool master, bool block)
GCG_EXPORT int addClass(const char *name, const char *desc, VAR_DECOMPINFO decompInfo)
GCG_EXPORT const int * getVarsToClasses()
enum VarClassDecompInfo VAR_DECOMPINFO
#define NULL
Definition: struct_gcg.h:55