Scippy

GCG

Branch-and-Price & Column Generation for Everyone

clscons_scipconstypes.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_scipconstypes.cpp
30  * @ingroup CLASSIFIERS
31  * @brief classifies constraints according to their scip constypes
32  */
33 
34 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
35 
36 #include "clscons_scipconstypes.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 "scipconstype" /**< name of classifier */
50 #define DEC_DESC "scip constypes" /**< 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  gcg::DETPROBDATA *detprobdata;
83  if( transformed )
84  {
85  detprobdata = GCGconshdlrDecompGetDetprobdataPresolved(scip);
86  }
87  else
88  {
89  detprobdata = GCGconshdlrDecompGetDetprobdataOrig(scip);
90  }
91 
92  std::vector<consType> foundConstypes(0);
93  std::vector<int> constypesIndices(0);
94  std::vector<int> classForCons = std::vector<int>(detprobdata->getNConss(), -1);
95  gcg::ConsPartition *classifier;
96 
97  /* firstly, assign all constraints to classindices */
98  for (int i = 0; i < detprobdata->getNConss(); ++i) {
99  SCIP_CONS *cons;
100  bool found = false;
101  cons = detprobdata->getCons(i);
102  consType cT = GCGconsGetType(scip, cons);
103  size_t constype;
104 
105  /* check whether the constraint's constype is new */
106  for (constype = 0; constype < foundConstypes.size(); ++constype) {
107  if (foundConstypes[constype] == cT) {
108  found = true;
109  break;
110  }
111  }
112  /* if it is new, create a new classindex */
113  if (!found) {
114  foundConstypes.push_back(GCGconsGetType(scip, cons));
115  classForCons[i] = foundConstypes.size() - 1;
116  } else
117  classForCons[i] = constype;
118  }
119 
120  /* secondly, use these information to create a ConsPartition */
121  classifier = new gcg::ConsPartition(scip, "constypes", (int) foundConstypes.size(), detprobdata->getNConss());
122 
123  /* set class names and descriptions of every class */
124  for (int c = 0; c < classifier->getNClasses(); ++c) {
125  std::string name;
126  std::stringstream text;
127  switch (foundConstypes[c]) {
128  case linear:
129  name = "linear";
130  break;
131  case knapsack:
132  name = "knapsack";
133  break;
134  case varbound:
135  name = "varbound";
136  break;
137  case setpacking:
138  name = "setpacking";
139  break;
140  case setcovering:
141  name = "setcovering";
142  break;
143  case setpartitioning:
144  name = "setpartitioning";
145  break;
146  case logicor:
147  name = "logicor";
148  break;
149  case sos1:
150  name = "sos1";
151  break;
152  case sos2:
153  name = "sos2";
154  break;
155  case unknown:
156  name = "unknown";
157  break;
158  case nconsTypeItems:
159  name = "nconsTypeItems";
160  break;
161  default:
162  name = "newConstype";
163  break;
164  }
165  classifier->setClassName(c, name.c_str());
166  text << "This class contains all constraints that are of (SCIP) constype \"" << name << "\".";
167  classifier->setClassDescription(c, text.str().c_str());
168  }
169 
170  /* copy the constraint assignment information found in first step */
171  for (int i = 0; i < classifier->getNConss(); ++i) {
172  classifier->assignConsToClass(i, classForCons[i]);
173  }
174 
175  SCIPverbMessage(scip, SCIP_VERBLEVEL_HIGH, NULL,
176  " Consclassifier \"%s\" yields a classification with %d different constraint classes \n",
177  classifier->getName(), (int) foundConstypes.size());
178 
179  detprobdata->addConsPartition(classifier);
180  return SCIP_OKAY;
181 }
182 
183 /*
184  * classifier specific interface methods
185  */
186 
187 /** creates the handler for XYZ classifier and includes it in SCIP */
189  SCIP *scip /**< SCIP data structure */
190 ) {
191  DEC_CLASSIFIERDATA* classifierdata = NULL;
192 
193  SCIP_CALL(
195  classifierFree, classifierClassify));
196 
197  return SCIP_OKAY;
198 }
int getNConss()
returns the number of variables considered in the detprobdata
SCIP_RETCODE SCIPincludeConsClassifierScipConstypes(SCIP *scip)
void setClassDescription(int classindex, const char *desc)
@ logicor
Definition: scip_misc.h:49
#define DEC_ENABLED
@ sos2
Definition: scip_misc.h:49
constraint handler for structure detection
void assignConsToClass(int consindex, int classindex)
@ knapsack
Definition: scip_misc.h:48
class representing a partition of a set of constraints
classifies constraints according to their scip constypes
DETPROBDATA * GCGconshdlrDecompGetDetprobdataOrig(SCIP *scip)
help method to access detprobdata for unpresolved problem
@ sos1
Definition: scip_misc.h:49
various SCIP helper methods
@ nconsTypeItems
Definition: scip_misc.h:49
SCIP_CONS * getCons(int consIndex)
returns the SCIP constraint related to a constraint index
#define DEC_CLASSIFIERNAME
void setClassName(int classindex, const char *name)
C++ interface of cons_decomp.
@ setpacking
Definition: scip_misc.h:48
@ setpartitioning
Definition: scip_misc.h:48
consType GCGconsGetType(SCIP *scip, SCIP_CONS *cons)
Definition: scip_misc.c:51
DETPROBDATA * GCGconshdlrDecompGetDetprobdataPresolved(SCIP *scip)
help method to access detprobdata for transformed problem
@ linear
Definition: scip_misc.h:48
#define DEC_PRIORITY
consType
Definition: scip_misc.h:47
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
@ setcovering
Definition: scip_misc.h:48
@ unknown
Definition: scip_misc.h:49
@ varbound
Definition: scip_misc.h:48
#define DEC_DESC
#define classifierFree
static DEC_DECL_CONSCLASSIFY(classifierClassify)