reader_cls.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-2018 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 
34 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
35 
36 #include <assert.h>
37 #include <string.h>
38 #if defined(_WIN32) || defined(_WIN64)
39 #else
40 #include <strings.h> /*lint --e{766}*/ /* needed for strcasecmp() */
41 #endif
42 #include <ctype.h>
43 
44 #include "reader_cls.h"
45 #include "cons_decomp.h"
46 #include "class_seeedpool.h"
47 #include "class_consclassifier.h"
48 #include "class_varclassifier.h"
49 
50 
51 
52 #define READER_NAME "clsreader"
53 #define READER_DESC "reader for writing classifier data"
54 #define READER_EXTENSION "cls"
55 #define DEFAULT_USETRANSFORM TRUE
56 
57 struct SCIP_ConshdlrData
58 {
61 };
62 
63 
64 
66 struct SCIP_ReaderData
67 {
68  SCIP_Bool usetransform;
69 };
70 
71 /*
72  * Local methods
73  */
74 
75 
77 SCIP_RETCODE GCGwriteCls(
78  SCIP* scip,
79  FILE* file
80  )
81 {
82  assert(scip != NULL);
83 
84  /* TODO
85  * format description:
86  * a) <number of classifiers>
87  * for each classifier:
88  * b1) VAR or CONS
89  * b2) <name of classifier>>
90  * b3) <number of classes>
91  * b4) for each class:
92  * c1) <name of class>: <description of class>
93  * c2) <number of class elements>
94  * c3) for each element of class:
95  * d1) <name of element> (e.g. variable or constraint name, concerning transformed [default] or original problem)
96  *
97  *
98  * 1. Write cons_decomp method(s) to get all relevant data, i.e.
99  * - for all cons and var classifier in both presolved and unpresolved seeedpool:
100  * - Class to index mapping, class names, class descriptions (and maybe class decomp info)
101  * - Cons/var to classes mapping
102  * - index to SCIPcons/SCIPvar mapping from unpresolved seeedpool (and maybe seeedpool)
103  *
104  * 2. Write an output method to write these information into a file
105  *
106  */
107  SCIP_Bool transformed;
109 
110 
111  assert(scip != NULL);
112 
113  SCIP_CALL( SCIPgetBoolParam(scip,
114  "reading/clsreader/usetransform", &transformed));
115 
116 
117  if( SCIPgetStage(scip) < SCIP_STAGE_TRANSFORMED )
118  transformed = FALSE;
119 
120  if( !transformed && SCIPconshdlrDecompGetSeeedpoolUnpresolvedExtern(scip) == NULL )
122 
123  if( transformed && SCIPconshdlrDecompGetSeeedpoolExtern(scip) == NULL )
125 
127 
128 
130 
131  if( seeedpool->consclassescollection.size() == 0 )
132  seeedpool->calcClassifierAndNBlockCandidates(scip);
133 
134  SCIPinfoMessage(scip, file, "# a1) <number of classifiers>\n" );
135  SCIPinfoMessage(scip, file, "# a2) for each classifier:\n" );
136  SCIPinfoMessage(scip, file, "# b1) VAR or CONS\n" );
137  SCIPinfoMessage(scip, file, "# b2) <name of classifier>\n" );
138  SCIPinfoMessage(scip, file, "# b3) <number of classes>\n" );
139  SCIPinfoMessage(scip, file, "# b4) for each class:\n" );
140  SCIPinfoMessage(scip, file, "# c1) <name of class>: <description of class>\n" );
141  SCIPinfoMessage(scip, file, "# c2) <number of class elements>\n" );
142  SCIPinfoMessage(scip, file, "# c3) for each element of class:\n" );
143  SCIPinfoMessage(scip, file, "# d1) <name of element> (e.g. variable or constraint name, concerning transformed [default] or original problem)\n" );
144  SCIPinfoMessage(scip, file, "###########################################\n" );
145 
146 
148  SCIPinfoMessage(scip, file, "%d\n", (int) seeedpool->consclassescollection.size() + (int) seeedpool->varclassescollection.size() );
149 
150  for( size_t c = 0; c < seeedpool->consclassescollection.size() ; ++c )
151  {
152  gcg::ConsClassifier* classifier = seeedpool->consclassescollection[c];
153 
154  std::vector<std::vector<int> > conssofclasses = std::vector<std::vector<int> >(classifier->getNClasses()) ;
155  for( int cons = 0; cons < seeedpool->getNConss(); ++cons )
156  conssofclasses[classifier->getClassOfCons(cons)].push_back(cons);
157 
159  SCIPinfoMessage(scip, file, "CONS\n" );
161  SCIPinfoMessage(scip, file, "%s \n", classifier->getName());
163  SCIPinfoMessage(scip, file, "%d\n", classifier->getNClasses() );
164  for( int cl = 0; cl < classifier->getNClasses(); ++cl )
165  {
167  SCIPinfoMessage(scip, file, "%s: %s\n", classifier->getClassName(cl), classifier->getClassDescription(cl) );
169  SCIPinfoMessage(scip, file, "%d\n", conssofclasses[cl].size() );
171  for( size_t clm = 0; clm < conssofclasses[cl].size(); ++clm )
172  {
173  SCIPinfoMessage(scip, file, "%s\n", SCIPconsGetName( seeedpool->getConsForIndex( conssofclasses[cl][clm])) );
174  }
175  }
176  }
177 
178 
179  for( size_t c = 0; c < seeedpool->varclassescollection.size() ; ++c )
180  {
181  gcg::VarClassifier* classifier = seeedpool->varclassescollection[c];
182 
183  std::vector<std::vector<int> > varsofclasses = std::vector<std::vector<int> >(classifier->getNClasses()) ;
184  for( int var = 0; var < seeedpool->getNVars(); ++var )
185  varsofclasses[classifier->getClassOfVar(var)].push_back(var);
186 
188  SCIPinfoMessage(scip, file, "VAR\n" );
190  SCIPinfoMessage(scip, file, "%s \n", classifier->getName());
192  SCIPinfoMessage(scip, file, "%d\n", classifier->getNClasses() );
193  for( int cl = 0; cl < classifier->getNClasses(); ++cl )
194  {
196  SCIPinfoMessage(scip, file, "%s: %s\n", classifier->getClassName(cl), classifier->getClassDescription(cl) );
198  SCIPinfoMessage(scip, file, "%d\n", classifier->getNVarsOfClasses()[cl] );
200  for( size_t clm = 0; clm <varsofclasses[cl].size(); ++clm )
201  {
202  SCIPinfoMessage(scip, file, "%s\n", SCIPvarGetName( seeedpool->getVarForIndex( varsofclasses[cl][clm])) );
203  }
204  }
205  }
206 
207 
208  return SCIP_OKAY;
209 }
210 
211 
212 /*
213  * Callback methods of reader
214  */
215 
216 #define readerCopyCls NULL
217 
219 static
220 SCIP_DECL_READERFREE(readerFreeCls)
221 {
222  SCIP_READERDATA* readerdata;
223 
224  readerdata = SCIPreaderGetData( reader );
225  assert( readerdata != NULL );
226 
227  SCIPfreeMemory( scip, &readerdata );
228 
229  assert( strcmp( SCIPreaderGetName( reader ), READER_NAME ) == 0);
230  return SCIP_OKAY;
231 }
232 
234 #define readerReadCls NULL
235 
237 static
238 SCIP_DECL_READERWRITE(readerWriteCls)
239 {
240  /*lint --e{715}*/
241  SCIP_CALL( GCGwriteCls( scip, file ) );
242 
243  *result = SCIP_SUCCESS;
244  return SCIP_OKAY;
245 }
246 
247 
249 SCIP_RETCODE SCIPincludeReaderCls(
250  SCIP* scip
251  )
252 {
253  SCIP_READERDATA* readerdata;
254 
255  /* create cls reader data */
256  SCIP_CALL( SCIPallocMemory(scip, &readerdata) );
257 
258  /* include cls reader */
259  SCIP_CALL( SCIPincludeReader(scip, READER_NAME, READER_DESC, READER_EXTENSION,
260  readerCopyCls, readerFreeCls, readerReadCls, readerWriteCls, readerdata) );
261 
262  SCIP_CALL( SCIPaddBoolParam(scip,
263  "reading/clsreader/usetransform",
264  "should the transformed (and possibly presolved problem) be use or original one",
265  &readerdata->usetransform, FALSE, DEFAULT_USETRANSFORM, NULL, NULL) );
266 
267 
268  return SCIP_OKAY;
269 }
std::vector< int > getNVarsOfClasses()
SCIP_RETCODE SCIPincludeReaderCls(SCIP *scip)
Definition: reader_cls.cpp:249
SCIP_RETCODE GCGwriteCls(SCIP *scip, FILE *file)
Definition: reader_cls.cpp:77
SCIP_RETCODE calcClassifierAndNBlockCandidates(SCIP *givenScip)
creates constraint and variable classifiers, and deduces block number candidates
int getClassOfCons(int consindex)
CLS reader for writing files containing classifier data.
const char * getClassName(int classindex)
static SCIP_DECL_READERWRITE(readerWriteCls)
Definition: reader_cls.cpp:238
gcg::Seeedpool * seeedpoolunpresolved
class for classifying constraints
SCIP_VAR * getVarForIndex(int varIndex)
returns SCIP variable related to a variable index
#define READER_DESC
Definition: reader_cls.cpp:53
gcg::Seeedpool * seeedpool
int getNConss()
returns the number of variables considered in the seeedpool
int getClassOfVar(int varindex)
SEEEDPOOL_WRAPPER * SCIPconshdlrDecompGetSeeedpoolExtern(SCIP *scip)
help method to access seeedpool for transformed problem : consider deleting this method will be delet...
std::vector< VarClassifier * > varclassescollection
std::vector< ConsClassifier * > consclassescollection
SCIP_Bool usetransform
Definition: reader_cls.cpp:68
SCIP_RETCODE SCIPconshdlrDecompCreateSeeedpool(SCIP *scip)
creates the seeedpool for the presolved problem
const char * getClassDescription(int classindex)
#define DEFAULT_USETRANSFORM
Definition: reader_cls.cpp:55
SEEEDPOOL_WRAPPER * SCIPconshdlrDecompGetSeeedpoolUnpresolvedExtern(SCIP *scip)
help method to access seeedpool for unpresolved problem : consider deleting this method will be delet...
#define READER_EXTENSION
Definition: reader_cls.cpp:54
#define readerReadCls
Definition: reader_cls.cpp:234
#define readerCopyCls
Definition: reader_cls.cpp:216
class for classifying variables
SCIP_RETCODE SCIPconshdlrDecompCreateSeeedpoolUnpresolved(SCIP *scip)
creates the seeedpool for the unpresolved problem
#define READER_NAME
Definition: reader_cls.cpp:52
class with functions for seeed pool where a seeed is a (potentially incomplete) description of a deco...
static SCIP_DECL_READERFREE(readerFreeCls)
Definition: reader_cls.cpp:220
constraint handler for structure detection
SCIP_CONS * getConsForIndex(int consIndex)
returns the SCIP constraint related to a constraint index
int getNVars()
return the number of variables considered in the seeedpool