Scippy

GCG

Branch-and-Price & Column Generation for Everyone

clsvar_objvalues.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_objvalues.cpp
29  * @ingroup CLASSIFIERS
30  * @brief classifies variables according to their objective function values
31  */
32 
33 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
34 
35 #include "clsvar_objvalues.h"
36 #include "cons_decomp.h"
37 #include "cons_decomp.hpp"
38 #include <vector>
39 #include <stdio.h>
40 #include <sstream>
41 #include <iomanip>
42 
43 #include "class_detprobdata.h"
44 
45 #include "class_varpartition.h"
46 #include "scip_misc.h"
47 
48 /* classifier properties */
49 #define DEC_CLASSIFIERNAME "objectivevalues" /**< name of classifier */
50 #define DEC_DESC "objective function values" /**< 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_VARCLASSIFY(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  // CLASSIFICATION
94  std::vector<SCIP_Real> foundobjvals; /* all found objective function values */
95  std::vector<int> classforvars(detprobdata->getNVars(), -1); /* vector assigning a class index to each variable */
96  int curclassindex; /* stores a var's classindex if the objective value of a var has already been found for another var */
97  SCIP_Real curobjval;
98  gcg::VarPartition* classifier; /* new VarPartition */
99 
100  for( int v = 0; v < detprobdata->getNVars(); ++v )
101  {
102  assert( detprobdata->getVar(v) != NULL );
103  curobjval = SCIPvarGetObj(detprobdata->getVar(v));
104  curclassindex = -1;
105 
106  /* check whether current objective funtion value already exists */
107  for( size_t c = 0; c < foundobjvals.size(); ++c )
108  {
109  if( SCIPisEQ(scip, curobjval, foundobjvals[c]) )
110  {
111  curclassindex = c;
112  break;
113  }
114  }
115 
116  /* assign var to class and save objective function value, if it is new */
117  if( curclassindex == -1 )
118  {
119  foundobjvals.push_back(curobjval);
120  classforvars[v] = foundobjvals.size() - 1;
121  }
122  else
123  {
124  classforvars[v] = curclassindex;
125  }
126  }
127 
128  classifier = new gcg::VarPartition(scip, "varobjvals", (int) foundobjvals.size(), detprobdata->getNVars());
129 
130  /* set up class information */
131  for ( int c = 0; c < classifier->getNClasses(); ++c )
132  {
133  std::stringstream name;
134  std::stringstream text;
135 
136  name << std::setprecision( 5 ) << foundobjvals[c];
137  text << "This class contains all variables with objective function value " << name.str() << ".";
138 
139  classifier->setClassName(c, name.str().c_str());
140  classifier->setClassDescription(c, text.str().c_str());
141  }
142 
143  /* assign vars according to classforvars vactor */
144  for ( int v = 0; v < classifier->getNVars(); ++v )
145  {
146  classifier->assignVarToClass(v, classforvars[v]);
147  }
148 
149  SCIPverbMessage(scip, SCIP_VERBLEVEL_HIGH, NULL, " Varclassifier \"%s\" yields a classification with %d different variable classes\n", classifier->getName(), classifier->getNClasses()) ;
150 
151  detprobdata->addVarPartition(classifier);
152  return SCIP_OKAY;
153 }
154 
155 /*
156  * classifier specific interface methods
157  */
158 
160  SCIP* scip /**< SCIP data structure */
161 )
162 {
163  DEC_CLASSIFIERDATA* classifierdata = NULL;
164 
165  SCIP_CALL( DECincludeVarClassifier(scip, DEC_CLASSIFIERNAME, DEC_DESC, DEC_PRIORITY, DEC_ENABLED, classifierdata, classifierFree, classifierClassify) );
166 
167  return SCIP_OKAY;
168 }
void setClassDescription(int classindex, const char *desc)
class representing a partition of a set of variables
#define DEC_PRIORITY
constraint handler for structure detection
#define DEC_DESC
DETPROBDATA * GCGconshdlrDecompGetDetprobdataOrig(SCIP *scip)
help method to access detprobdata for unpresolved problem
#define classifierFree
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
int getNVars()
return the number of variables considered in the detprobdata
void setClassName(int classindex, const char *name)
void assignVarToClass(int varindex, int classindex)
#define DEC_CLASSIFIERNAME
#define DEC_ENABLED
C++ interface of cons_decomp.
void addVarPartition(VarPartition *partition)
adds a variable partition if it is no duplicate of an existing variable partition
static DEC_DECL_VARCLASSIFY(classifierClassify)
SCIP_RETCODE SCIPincludeVarClassifierObjValues(SCIP *scip)
classifies variables according to their objective function values
DETPROBDATA * GCGconshdlrDecompGetDetprobdataPresolved(SCIP *scip)
help method to access detprobdata for transformed problem
SCIP_VAR * getVar(int varIndex)
returns SCIP variable related to a variable index
class storing partialdecs and the problem matrix