Scippy

GCG

Branch-and-Price & Column Generation for Everyone

clscons_consnamenonumbers.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 sh
23 ould have received a copy of the GNU Lesser General Public License */
24 /* along with this program; if not, write to the Free Software */
25 /* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.*/
26 /* */
27 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
28 
29 /**@file clscons_consnamenonumbers.cpp
30  * @ingroup CLASSIFIERS
31  * @brief classifies constraints according to names (without digits)
32  */
33 
34 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
35 
37 #include "cons_decomp.h"
38 #include "cons_decomp.hpp"
39 #include <vector>
40 #include <stdio.h>
41 #include <sstream>
42 
43 #include "class_detprobdata.h"
44 
45 #include "class_conspartition.h"
46 #include "scip_misc.h"
47 
48 /* classifier properties */
49 #define DEC_CLASSIFIERNAME "consnamenonumbers" /**< name of classifier */
50 #define DEC_DESC "constraint names (remove digits; check for identity)" /**< short description of classification*/
51 #define DEC_PRIORITY 0
52 
53 #define DEC_ENABLED FALSE
54 
55 
56 /*
57  * Data structures
58  */
59 
60 /** classifier handler data */
61 struct DEC_ClassifierData
62 {
63 };
64 
65 
66 /*
67  * Local methods
68  */
69 
70 /* put your local methods here, and declare them static */
71 
72 
73 /*
74  * classifier callback methods
75  */
76 
77 /** destructor of classifier to free user data (called when GCG is exiting) */
78 #define classifierFree NULL
79 
80 /** removes all digits from string str */
82  char *str,
83  int *nremoved
84 )
85 {
86  char digits[11] = "0123456789";
87  * nremoved = 0;
88 
89  for( int i = 0; i < 10; ++ i )
90  {
91  char digit = digits[i];
92  size_t j = 0;
93  while( j < strlen( str ) )
94  {
95  if( str[j] == digit )
96  {
97  * nremoved = * nremoved + 1;
98  for( size_t k = j; k < strlen( str ); ++ k )
99  {
100  str[k] = str[k + 1];
101  }
102  }
103  else
104  ++ j;
105  }
106  }
107 }
108 
109 
110 static
111 DEC_DECL_CONSCLASSIFY(classifierClassify) {
112  gcg::DETPROBDATA* detprobdata;
113  if( transformed )
114  {
115  detprobdata = GCGconshdlrDecompGetDetprobdataPresolved(scip);
116  }
117  else
118  {
119  detprobdata = GCGconshdlrDecompGetDetprobdataOrig(scip);
120  }
121 
122  std::vector < std::string > consnamesToCompare( detprobdata->getNConss(), "" );
123  std::vector<int> nConssConstype( 0 );
124  std::vector<int> classForCons = std::vector<int>( detprobdata->getNConss(), - 1 );
125  std::vector < std::string > nameClasses( 0 );
126  gcg::ConsPartition* classifier;
127 
128  /* firstly, remove all digits from the consnames */
129  for( int i = 0; i < detprobdata->getNConss(); ++ i )
130  {
131  int nremoved;
132  char consname[SCIP_MAXSTRLEN];
133  strcpy(consname, SCIPconsGetName(detprobdata->getCons(i)));
134 
135  removeDigits(consname, &nremoved);
136  consnamesToCompare[i] = std::string(consname);
137  }
138 
139  for( int i = 0; i < detprobdata->getNConss(); ++ i )
140  {
141  /* check if string belongs to an existing name class */
142  bool belongstoexistingclass = false;
143 
144  for( size_t j = 0; j < nameClasses.size(); ++ j )
145  {
146  if( nameClasses[j] == consnamesToCompare[i] )
147  {
148  belongstoexistingclass = true;
149  classForCons[i] = j;
150  nConssConstype[j] ++;
151  break;
152  }
153  }
154  /* if not, create a new class */
155  if( !belongstoexistingclass )
156  {
157  nameClasses.push_back(consnamesToCompare[i]);
158  nConssConstype.push_back(1);
159  classForCons[i] = nameClasses.size() - 1;
160  }
161  }
162 
163  /* secondly, use these information to create a ConsPartition */
164  classifier = new gcg::ConsPartition(scip, "consnames", (int) nameClasses.size(), detprobdata->getNConss());
165 
166  /* set all class names and descriptions */
167  for( int c = 0; c < classifier->getNClasses(); ++ c )
168  {
169  std::stringstream text;
170  classifier->setClassName(c, nameClasses[c].c_str());
171  text << "This class contains all constraints with name \"" << nameClasses[c] << "\".";
172  classifier->setClassDescription(c, text.str().c_str());
173  }
174 
175  /* copy the constraint assignment information found in first step */
176  for( int i = 0; i < classifier->getNConss(); ++ i )
177  {
178  classifier->assignConsToClass(i, classForCons[i]);
179  }
180 
181  SCIPverbMessage(scip, SCIP_VERBLEVEL_HIGH, NULL, " Consclassifier \"%s\" yields a classification with %d different constraint classes \n", classifier->getName(), classifier->getNClasses());
182 
183  detprobdata->addConsPartition(classifier);
184  return SCIP_OKAY;
185 }
186 
187 /*
188  * classifier specific interface methods
189  */
190 
192  SCIP *scip /**< SCIP data structure */
193 ) {
194  DEC_CLASSIFIERDATA* classifierdata = NULL;
195 
196  SCIP_CALL(
198  classifierFree, classifierClassify));
199 
200  return SCIP_OKAY;
201 }
int getNConss()
returns the number of variables considered in the detprobdata
#define DEC_DESC
void removeDigits(char *str, int *nremoved)
void setClassDescription(int classindex, const char *desc)
SCIP_RETCODE SCIPincludeConsClassifierForConsnamesDigitFreeIdentical(SCIP *scip)
classifies constraints according to names (without digits)
constraint handler for structure detection
void assignConsToClass(int consindex, int classindex)
static DEC_DECL_CONSCLASSIFY(classifierClassify)
class representing a partition of a set of constraints
DETPROBDATA * GCGconshdlrDecompGetDetprobdataOrig(SCIP *scip)
help method to access detprobdata for unpresolved problem
#define DEC_PRIORITY
various SCIP helper methods
SCIP_CONS * getCons(int consIndex)
returns the SCIP constraint related to a constraint index
void setClassName(int classindex, const char *name)
#define DEC_CLASSIFIERNAME
C++ interface of cons_decomp.
#define DEC_ENABLED
DETPROBDATA * GCGconshdlrDecompGetDetprobdataPresolved(SCIP *scip)
help method to access detprobdata for transformed problem
#define classifierFree
void addConsPartition(ConsPartition *partition)
adds a constraint partition if it is no duplicate of an existing constraint partition
SCIP_RETCODE DECincludeConsClassifier(SCIP *scip, const char *name, const char *description, int priority, SCIP_Bool enabled, DEC_CLASSIFIERDATA *classifierdata, DEC_DECL_FREECONSCLASSIFIER((*freeClassifier)),)
class storing partialdecs and the problem matrix