Scippy

GCG

Branch-and-Price & Column Generation for Everyone

clsvar_scipvartypes.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 clsvar_scipvartypes.cpp
29  * @ingroup CLASSIFIERS
30  * @brief classifies variables according to their scip vartypes
31  */
32 
33 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
34 
35 #include "clsvar_scipvartypes.h"
36 #include "cons_decomp.h"
37 #include "cons_decomp.hpp"
38 #include <vector>
39 #include <stdio.h>
40 #include <sstream>
41 
42 #include "class_detprobdata.h"
43 
44 #include "class_varpartition.h"
45 #include "scip_misc.h"
46 
47 /* classifier properties */
48 #define DEC_CLASSIFIERNAME "scipvartype" /**< name of classifier */
49 #define DEC_DESC "scipvartypes" /**< short description of classification*/
50 #define DEC_PRIORITY 0
51 
52 #define DEC_ENABLED TRUE
53 
54 
55 /*
56  * Data structures
57  */
58 
59 /** classifier handler data */
60 struct DEC_ClassifierData
61 {
62 };
63 
64 
65 /*
66  * Local methods
67  */
68 
69 /* put your local methods here, and declare them static */
70 
71 
72 /*
73  * classifier callback methods
74  */
75 
76 /** destructor of classifier to free user data (called when GCG is exiting) */
77 #define classifierFree NULL
78 
79 static
80 DEC_DECL_VARCLASSIFY(classifierClassify)
81 {
82  gcg::DETPROBDATA* detprobdata;
83  if( transformed )
84  {
85  detprobdata = GCGconshdlrDecompGetDetprobdataPresolved(scip);
86  }
87  else
88  {
89  detprobdata = GCGconshdlrDecompGetDetprobdataOrig(scip);
90  }
91 
92  // CLASSIFICATION
93  std::vector < SCIP_VARTYPE > foundVartypes( 0 );
94  std::vector<int> classForVars = std::vector<int>( detprobdata->getNVars(), - 1 );
95  gcg::VarPartition* classifier;
96 
97  SCIP_Bool onlycontsub;
98  SCIP_Bool onlybinmaster;
99 
100  SCIPgetBoolParam(scip, "detection/benders/onlycontsubpr", &onlycontsub);
101  SCIPgetBoolParam(scip, "detection/benders/onlybinmaster", &onlybinmaster);
102 
103  /* firstly, assign all variables to classindices */
104  for( int i = 0; i < detprobdata->getNVars(); ++ i )
105  {
106  SCIP_VAR* var;
107  bool found = false;
108  var = detprobdata->getVar(i);
109  SCIP_VARTYPE vT = SCIPvarGetType( var );
110  size_t vartype;
111 
112  if( onlycontsub )
113  {
114  if ( vT == SCIP_VARTYPE_BINARY )
115  vT = SCIP_VARTYPE_INTEGER;
116  if( vT == SCIP_VARTYPE_IMPLINT )
117  vT = SCIP_VARTYPE_CONTINUOUS;
118  }
119 
120  /* check whether the variable's vartype is new */
121  for( vartype = 0; vartype < foundVartypes.size(); ++ vartype )
122  {
123  if( foundVartypes[vartype] == vT )
124  {
125  found = true;
126  break;
127  }
128  }
129  /* if it is new, create a new class index */
130  if( ! found )
131  {
132  foundVartypes.push_back( vT );
133  classForVars[i] = foundVartypes.size() - 1;
134  }
135  else
136  classForVars[i] = vartype;
137  }
138 
139  /* secondly, use these information to create a VarPartition */
140  classifier = new gcg::VarPartition(scip, "vartypes", (int) foundVartypes.size(), detprobdata->getNVars() );
141 
142  /* set class names and descriptions of every class */
143  for( int c = 0; c < classifier->getNClasses(); ++ c )
144  {
145  std::string name;
146  std::stringstream text;
147  switch( foundVartypes[c] )
148  {
149  case SCIP_VARTYPE_BINARY:
150  name = "bin";
151  if( onlybinmaster )
152  classifier->setClassDecompInfo(c, gcg::LINKING);
153  break;
154  case SCIP_VARTYPE_INTEGER:
155  name = "int";
156  if( onlycontsub )
157  classifier->setClassDecompInfo(c, gcg::LINKING);
158  if( onlybinmaster )
159  classifier->setClassDecompInfo(c, gcg::BLOCK);
160  break;
161  case SCIP_VARTYPE_IMPLINT:
162  name = "impl";
163  if( onlybinmaster )
164  classifier->setClassDecompInfo(c, gcg::BLOCK);
165  break;
166  case SCIP_VARTYPE_CONTINUOUS:
167  name = "cont";
168  if( onlycontsub )
169  classifier->setClassDecompInfo(c, gcg::BLOCK);
170  if( onlybinmaster )
171  classifier->setClassDecompInfo(c, gcg::BLOCK);
172  break;
173  default:
174  name = "newVartype";
175  break;
176  }
177  classifier->setClassName( c, name.c_str() );
178  text << "This class contains all variables that are of (SCIP) vartype \"" << name << "\".";
179  classifier->setClassDescription( c, text.str().c_str() );
180  }
181 
182  /* copy the variable assignment information found in first step */
183  for( int i = 0; i < classifier->getNVars(); ++ i )
184  {
185  classifier->assignVarToClass( i, classForVars[i] );
186  }
187 
188  SCIPverbMessage(scip, SCIP_VERBLEVEL_HIGH, NULL, " Varclassifier \"%s\" yields a classification with %d different variable classes\n", classifier->getName(), classifier->getNClasses() ) ;
189 
190  detprobdata->addVarPartition(classifier);
191  return SCIP_OKAY;
192 }
193 
194 /*
195  * classifier specific interface methods
196  */
197 
199  SCIP* scip /**< SCIP data structure */
200 )
201 {
202  DEC_CLASSIFIERDATA* classifierdata = NULL;
203 
204  SCIP_CALL( DECincludeVarClassifier(scip, DEC_CLASSIFIERNAME, DEC_DESC, DEC_PRIORITY, DEC_ENABLED, classifierdata, classifierFree, classifierClassify) );
205 
206  return SCIP_OKAY;
207 }
#define DEC_PRIORITY
void setClassDescription(int classindex, const char *desc)
class representing a partition of a set of variables
constraint handler for structure detection
#define DEC_CLASSIFIERNAME
classifies variables according to their scip vartypes
DETPROBDATA * GCGconshdlrDecompGetDetprobdataOrig(SCIP *scip)
help method to access detprobdata for unpresolved problem
static DEC_DECL_VARCLASSIFY(classifierClassify)
SCIP_RETCODE DECincludeVarClassifier(SCIP *scip, const char *name, const char *description, int priority, SCIP_Bool enabled, DEC_CLASSIFIERDATA *classifierdata, DEC_DECL_FREEVARCLASSIFIER((*freeClassifier)),)
various SCIP helper methods
#define DEC_DESC
int getNVars()
return the number of variables considered in the detprobdata
void setClassName(int classindex, const char *name)
void assignVarToClass(int varindex, int classindex)
C++ interface of cons_decomp.
void addVarPartition(VarPartition *partition)
adds a variable partition if it is no duplicate of an existing variable partition
#define DEC_ENABLED
void setClassDecompInfo(int classindex, VAR_DECOMPINFO decompInfo)
DETPROBDATA * GCGconshdlrDecompGetDetprobdataPresolved(SCIP *scip)
help method to access detprobdata for transformed problem
SCIP_RETCODE SCIPincludeVarClassifierScipVartypes(SCIP *scip)
SCIP_VAR * getVar(int varIndex)
returns SCIP variable related to a variable index
#define classifierFree
class storing partialdecs and the problem matrix