Scippy

GCG

Branch-and-Price & Column Generation for Everyone

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-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 reader_cls.cpp
29  * @brief CLS reader for writing files containing classification data
30  * @author Michael Bastubbe
31  * @author Julius Hense
32  */
33 
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 "cons_decomp.hpp"
47 #include "class_detprobdata.h"
48 #include "class_conspartition.h"
49 #include "class_varpartition.h"
50 
51 
52 #define READER_NAME "clsreader"
53 #define READER_DESC "reader for writing classification data"
54 #define READER_EXTENSION "cls"
55 #define DEFAULT_USETRANSFORM TRUE
56 
57 struct SCIP_ConshdlrData
58 {
59 };
60 
61 
62 
63 /** data for dec reader */
64 struct SCIP_ReaderData
65 {
66  SCIP_Bool usetransform;
67 };
68 
69 /*
70  * Local methods
71  */
72 
73 
74 /** write classification data */
75 SCIP_RETCODE GCGwriteCls(
76  SCIP* scip, /**< SCIP data structure */
77  FILE* file /**< File pointer to write to */
78  )
79 {
80  SCIP_Bool transformed;
81  gcg::DETPROBDATA* detprobdata;
82 
83  assert(scip != NULL);
84 
85  SCIP_CALL( SCIPgetBoolParam(scip,
86  "reading/clsreader/usetransform", &transformed));
87 
88  if( SCIPgetStage(scip) < SCIP_STAGE_TRANSFORMED )
89  transformed = FALSE;
90 
91  if( !transformed )
92  detprobdata = GCGconshdlrDecompGetDetprobdataOrig(scip);
93  else
94  detprobdata = GCGconshdlrDecompGetDetprobdataPresolved(scip);
95 
96  if( detprobdata->conspartitioncollection.empty() )
97  {
98  GCGconshdlrDecompClassify(scip, !detprobdata->isAssignedToOrigProb());
100  }
101 
102  SCIPinfoMessage(scip, file, "# a1) <number of partitions>\n" );
103  SCIPinfoMessage(scip, file, "# a2) for each partition:\n" );
104  SCIPinfoMessage(scip, file, "# b1) VAR or CONS\n" );
105  SCIPinfoMessage(scip, file, "# b2) <name of partition>\n" );
106  SCIPinfoMessage(scip, file, "# b3) <number of classes>\n" );
107  SCIPinfoMessage(scip, file, "# b4) for each class:\n" );
108  SCIPinfoMessage(scip, file, "# c1) <name of class>: <description of class>\n" );
109  SCIPinfoMessage(scip, file, "# c2) <number of class elements>\n" );
110  SCIPinfoMessage(scip, file, "# c3) for each element of class:\n" );
111  SCIPinfoMessage(scip, file, "# d1) <name of element> (e.g. variable or constraint name, concerning transformed [default] or original problem)\n" );
112  SCIPinfoMessage(scip, file, "###########################################\n" );
113 
114  /* a */
115  SCIPinfoMessage(scip, file, "%d\n", (int) detprobdata->conspartitioncollection.size() + (int) detprobdata->varpartitioncollection.size() );
116 
117  for( size_t c = 0; c < detprobdata->conspartitioncollection.size() ; ++c )
118  {
120 
121  std::vector<std::vector<int> > conssofclasses = std::vector<std::vector<int> >(partition->getNClasses()) ;
122  for( int cons = 0; cons < detprobdata->getNConss(); ++cons )
123  conssofclasses[partition->getClassOfCons(cons)].push_back(cons);
124 
125  /* b1 */
126  SCIPinfoMessage(scip, file, "CONS\n" );
127  /* b2 */
128  SCIPinfoMessage(scip, file, "%s \n", partition->getName());
129  /* b3 */
130  SCIPinfoMessage(scip, file, "%d\n", partition->getNClasses());
131  for( int cl = 0; cl < partition->getNClasses(); ++cl )
132  {
133  /* c1 */
134  SCIPinfoMessage(scip, file, "%s: %s\n", partition->getClassName(cl), partition->getClassDescription(cl));
135  /* c2 */
136  SCIPinfoMessage(scip, file, "%ld\n", conssofclasses[cl].size());
137  /* c3 */
138  for( size_t clm = 0; clm < conssofclasses[cl].size(); ++clm )
139  {
140  SCIPinfoMessage(scip, file, "%s\n", SCIPconsGetName(detprobdata->getCons(conssofclasses[cl][clm])));
141  }
142  }
143  }
144 
145 
146  for( size_t c = 0; c < detprobdata->varpartitioncollection.size() ; ++c )
147  {
149 
150  std::vector<std::vector<int> > varsofclasses = std::vector<std::vector<int> >(partition->getNClasses()) ;
151  for( int var = 0; var < detprobdata->getNVars(); ++var )
152  varsofclasses[partition->getClassOfVar(var)].push_back(var);
153 
154  /* b1 */
155  SCIPinfoMessage(scip, file, "VAR\n" );
156  /* b2 */
157  SCIPinfoMessage(scip, file, "%s \n", partition->getName());
158  /* b3 */
159  SCIPinfoMessage(scip, file, "%d\n", partition->getNClasses() );
160  for( int cl = 0; cl < partition->getNClasses(); ++cl )
161  {
162  /* c1 */
163  SCIPinfoMessage(scip, file, "%s: %s\n", partition->getClassName(cl), partition->getClassDescription(cl));
164  /* c2 */
165  SCIPinfoMessage(scip, file, "%d\n", partition->getNVarsOfClasses()[cl] );
166  /* c3 */
167  for( size_t clm = 0; clm <varsofclasses[cl].size(); ++clm )
168  {
169  SCIPinfoMessage(scip, file, "%s\n", SCIPvarGetName(detprobdata->getVar(varsofclasses[cl][clm])));
170  }
171  }
172  }
173 
174 
175  return SCIP_OKAY;
176 }
177 
178 
179 /*
180  * Callback methods of reader
181  */
182 
183 #define readerCopyCls NULL
184 
185 /** destructor of reader to free user data (called when SCIP is exiting) */
186 static
187 SCIP_DECL_READERFREE(readerFreeCls)
188 {
189  SCIP_READERDATA* readerdata;
190 
191  readerdata = SCIPreaderGetData( reader );
192  assert( readerdata != NULL );
193 
194  SCIPfreeMemory( scip, &readerdata );
195 
196  assert( strcmp( SCIPreaderGetName( reader ), READER_NAME ) == 0);
197  return SCIP_OKAY;
198 }
199 
200 /** problem reading method of reader */
201 #define readerReadCls NULL
202 
203 /** problem writing method of reader */
204 static
205 SCIP_DECL_READERWRITE(readerWriteCls)
206 {
207  /*lint --e{715}*/
208  SCIP_CALL( GCGwriteCls( scip, file ) );
209 
210  *result = SCIP_SUCCESS;
211  return SCIP_OKAY;
212 }
213 
214 
215 /** includes the cls reader into SCIP */
216 SCIP_RETCODE SCIPincludeReaderCls(
217  SCIP* scip /**< SCIP data structure */
218  )
219 {
220  SCIP_READERDATA* readerdata;
221 
222  /* create cls reader data */
223  SCIP_CALL( SCIPallocMemory(scip, &readerdata) );
224 
225  /* include cls reader */
226  SCIP_CALL( SCIPincludeReader(scip, READER_NAME, READER_DESC, READER_EXTENSION,
227  readerCopyCls, readerFreeCls, readerReadCls, readerWriteCls, readerdata) );
228 
229  SCIP_CALL( SCIPaddBoolParam(scip,
230  "reading/clsreader/usetransform",
231  "should the transformed (and possibly presolved problem) be use or original one",
232  &readerdata->usetransform, FALSE, DEFAULT_USETRANSFORM, NULL, NULL) );
233 
234 
235  return SCIP_OKAY;
236 }
int getNConss()
returns the number of variables considered in the detprobdata
SCIP_RETCODE SCIPincludeReaderCls(SCIP *scip)
Definition: reader_cls.cpp:216
SCIP_RETCODE GCGwriteCls(SCIP *scip, FILE *file)
Definition: reader_cls.cpp:75
class representing a partition of a set of variables
CLS reader for writing files containing classification data.
SCIP_Bool isAssignedToOrigProb()
returns true if the matrix structure corresponds to the presolved problem
constraint handler for structure detection
class representing a partition of a set of constraints
static SCIP_DECL_READERWRITE(readerWriteCls)
Definition: reader_cls.cpp:205
DETPROBDATA * GCGconshdlrDecompGetDetprobdataOrig(SCIP *scip)
help method to access detprobdata for unpresolved problem
#define READER_DESC
Definition: reader_cls.cpp:53
static SCIP_RETCODE partition(SCIP *scip, SCIP_VAR **J, int *Jsize, SCIP_Longint *priority, SCIP_VAR **F, int Fsize, SCIP_VAR **origvar, SCIP_Real *median)
SCIP_CONS * getCons(int consIndex)
returns the SCIP constraint related to a constraint index
int getNVars()
return the number of variables considered in the detprobdata
void GCGconshdlrDecompCalcCandidatesNBlocks(SCIP *scip, SCIP_Bool transformed)
calculates and adds block size candidates using constraint classifications and variable classificatio...
C++ interface of cons_decomp.
SCIP_Bool usetransform
Definition: reader_cls.cpp:66
#define DEFAULT_USETRANSFORM
Definition: reader_cls.cpp:55
#define readerReadCls
Definition: reader_cls.cpp:201
#define READER_EXTENSION
Definition: reader_cls.cpp:54
#define readerCopyCls
Definition: reader_cls.cpp:183
#define READER_NAME
Definition: reader_cls.cpp:52
std::vector< ConsPartition * > conspartitioncollection
DETPROBDATA * GCGconshdlrDecompGetDetprobdataPresolved(SCIP *scip)
help method to access detprobdata for transformed problem
SCIP_RETCODE GCGconshdlrDecompClassify(SCIP *scip, SCIP_Bool transformed)
run classification of vars and cons
std::vector< VarPartition * > varpartitioncollection
static SCIP_DECL_READERFREE(readerFreeCls)
Definition: reader_cls.cpp:187
SCIP_VAR * getVar(int varIndex)
returns SCIP variable related to a variable index
class storing partialdecs and the problem matrix