Scippy

GCG

Branch-and-Price & Column Generation for Everyone

clscons_nnonzeros.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_nnonzeros.cpp
30  * @ingroup CLASSIFIERS
31  * @brief classifies constraints according to their nonzero entries
32  */
33 
34 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
35 
36 #include "clscons_nnonzeros.h"
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 "nnonzeros" /**< name of classifier */
50 #define DEC_DESC "nnonezero entries" /**< short description of classification*/
51 #define DEC_PRIORITY 0
52 
53 #define DEC_ENABLED TRUE
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 static
81 DEC_DECL_CONSCLASSIFY(classifierClassify)
82 {
83  gcg::DETPROBDATA* detprobdata;
84  if( transformed )
85  {
86  detprobdata = GCGconshdlrDecompGetDetprobdataPresolved(scip);
87  }
88  else
89  {
90  detprobdata = GCGconshdlrDecompGetDetprobdataOrig(scip);
91  }
92 
93  std::vector<int> nconssforclass( 0 );
94  std::vector<int> differentNNonzeros( 0 );
95  std::vector<int> classForCons( detprobdata->getNConss(), - 1 );
96  int counterClasses = 0;
97 
98  /* firstly, assign all constraints to classindices */
99  for( int i = 0; i < detprobdata->getNConss(); ++ i )
100  {
101  int consnnonzeros = detprobdata->getNVarsForCons( i );
102  bool nzalreadyfound = false;
103 
104  /* check if number of nonzeros belongs to an existing class index */
105  for( size_t nzid = 0; nzid < differentNNonzeros.size(); ++ nzid )
106  {
107  if( consnnonzeros == differentNNonzeros[nzid] )
108  {
109  nzalreadyfound = true;
110  classForCons[i] = nzid;
111  ++ nconssforclass[nzid];
112  break;
113  }
114  }
115 
116  /* if not, create a new class index */
117  if( ! nzalreadyfound )
118  {
119  classForCons[i] = counterClasses;
120  ++ counterClasses;
121  differentNNonzeros.push_back( consnnonzeros );
122  nconssforclass.push_back( 1 );
123  }
124  }
125 
126  /* secondly, use these information to create a ConsPartition */
127  gcg::ConsPartition* classifier = new gcg::ConsPartition(scip, "nonzeros", (int) differentNNonzeros.size(), detprobdata->getNConss() );
128 
129  /* set class names and descriptions of every class */
130  for( int c = 0; c < classifier->getNClasses(); ++ c )
131  {
132  std::stringstream text;
133  text << differentNNonzeros[c];
134  classifier->setClassName( c, text.str().c_str() );
135  text.str( "" );
136  text.clear();
137  text << "This class contains all constraints with " << differentNNonzeros[c] << " nonzero coefficients.";
138  classifier->setClassDescription( c, text.str().c_str() );
139  }
140 
141  /* copy the constraint assignment information found in first step */
142  for( int i = 0; i < classifier->getNConss(); ++ i )
143  {
144  classifier->assignConsToClass( i, classForCons[i] );
145  }
146  SCIPverbMessage(scip, SCIP_VERBLEVEL_HIGH, NULL, " Consclassifier \"%s\" yields a classification with %d different constraint classes \n", classifier->getName(), classifier->getNClasses() );
147 
148  detprobdata->addConsPartition(classifier);
149  return SCIP_OKAY;
150 }
151 
152 /*
153  * classifier specific interface methods
154  */
155 
156 /** creates the handler for classifier for Nnonzeros and includes it in SCIP */
158  SCIP* scip /**< SCIP data structure */
159 )
160 {
161  DEC_CLASSIFIERDATA* classifierdata = NULL;
162 
163  SCIP_CALL(
165  classifierFree, classifierClassify) );
166 
167  return SCIP_OKAY;
168 }
int getNConss()
returns the number of variables considered in the detprobdata
void setClassDescription(int classindex, const char *desc)
constraint handler for structure detection
void assignConsToClass(int consindex, int classindex)
classifies constraints according to their nonzero entries
class representing a partition of a set of constraints
DETPROBDATA * GCGconshdlrDecompGetDetprobdataOrig(SCIP *scip)
help method to access detprobdata for unpresolved problem
various SCIP helper methods
#define DEC_PRIORITY
#define DEC_DESC
void setClassName(int classindex, const char *name)
C++ interface of cons_decomp.
static DEC_DECL_CONSCLASSIFY(classifierClassify)
SCIP_RETCODE SCIPincludeConsClassifierNNonzeros(SCIP *scip)
int getNVarsForCons(int consIndex)
returns the number of variables for a given constraint
#define DEC_CLASSIFIERNAME
DETPROBDATA * GCGconshdlrDecompGetDetprobdataPresolved(SCIP *scip)
help method to access detprobdata for transformed problem
#define classifierFree
#define DEC_ENABLED
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