Scippy

SCIP

Solving Constraint Integer Programs

reader_cor.c
Go to the documentation of this file.
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2 /* */
3 /* This file is part of the program and library */
4 /* SCIP --- Solving Constraint Integer Programs */
5 /* */
6 /* Copyright (C) 2002-2020 Konrad-Zuse-Zentrum */
7 /* fuer Informationstechnik Berlin */
8 /* */
9 /* SCIP is distributed under the terms of the ZIB Academic License. */
10 /* */
11 /* You should have received a copy of the ZIB Academic License */
12 /* along with SCIP; see the file COPYING. If not visit scipopt.org. */
13 /* */
14 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
15 
16 /**@file reader_cor.c
17  * @ingroup DEFPLUGINS_READER
18  * @brief COR file reader (MPS format of the core problem for stochastic programs)
19  * @author Stephen J. Maher
20  */
21 
22 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
23 
24 #include "scip/pub_message.h"
25 #include "scip/pub_reader.h"
26 #include "scip/reader_cor.h"
27 #include "scip/reader_mps.h"
28 #include "scip/scip_mem.h"
29 #include "scip/scip_reader.h"
30 #include <string.h>
31 
32 #define READER_NAME "correader"
33 #define READER_DESC "file reader for CORE problem of stochastic programs in the SMPS file format"
34 #define READER_EXTENSION "cor"
35 
36 #define SCIP_DEFAULT_ARRAYSIZE 100
37 
38 /** COR reading data */
39 struct SCIP_ReaderData
40 {
41  const char** varnames;
42  const char** consnames;
43  int varnamessize;
44  int consnamessize;
45  int nvarnames;
46  int nconsnames;
47  SCIP_Bool read;
48 };
49 
50 /** creates the reader data */
51 static
53  SCIP* scip, /**< SCIP data structure */
54  SCIP_READERDATA* readerdata /**< the reader data structure */
55  )
56 {
57  assert(scip != NULL);
58  assert(readerdata != NULL);
59 
60  readerdata->read = FALSE;
61  readerdata->nvarnames = 0;
62  readerdata->nconsnames = 0;
63  readerdata->varnamessize = SCIP_DEFAULT_ARRAYSIZE;
64  readerdata->consnamessize = SCIP_DEFAULT_ARRAYSIZE;
65 
66  SCIP_CALL( SCIPallocBlockMemoryArray(scip, &readerdata->varnames, readerdata->varnamessize) );
67  SCIP_CALL( SCIPallocBlockMemoryArray(scip, &readerdata->consnames, readerdata->consnamessize) );
68 
69  return SCIP_OKAY;
70 }
71 
72 /** creates the reader data */
73 static
75  SCIP* scip, /**< SCIP data structure */
76  SCIP_READERDATA* readerdata /**< the reader data structure */
77  )
78 {
79  int i;
80 
81  assert(scip != NULL);
82  assert(readerdata != NULL);
83 
84  for( i = readerdata->nvarnames - 1; i >= 0; i-- )
85  SCIPfreeBlockMemoryArray(scip, &readerdata->varnames[i], strlen(readerdata->varnames[i]) + 1);
86 
87  for( i = readerdata->nconsnames - 1; i >= 0; i-- )
88  SCIPfreeBlockMemoryArray(scip, &readerdata->consnames[i], strlen(readerdata->consnames[i]) + 1);
89 
90  SCIPfreeBlockMemoryArray(scip, &readerdata->consnames, readerdata->consnamessize);
91  SCIPfreeBlockMemoryArray(scip, &readerdata->varnames, readerdata->varnamessize);
92 
93  return SCIP_OKAY;
94 }
95 
96 /*
97  * Callback methods of reader
98  */
99 
100 /** copy method for reader plugins (called when SCIP copies plugins) */
101 static
102 SCIP_DECL_READERCOPY(readerCopyCor)
103 { /*lint --e{715}*/
104  assert(scip != NULL);
105  assert(reader != NULL);
106  assert(strcmp(SCIPreaderGetName(reader), READER_NAME) == 0);
107 
108  /* call inclusion method of reader */
110 
111  return SCIP_OKAY;
112 }
113 
114 
115 /** destructor of reader to free user data (called when SCIP is exiting) */
116 /**! [SnippetReaderFreeCor] */
117 static
118 SCIP_DECL_READERFREE(readerFreeCor)
119 {
120  SCIP_READERDATA* readerdata;
121 
122  assert(strcmp(SCIPreaderGetName(reader), READER_NAME) == 0);
123  readerdata = SCIPreaderGetData(reader);
124  assert(readerdata != NULL);
125 
126  SCIP_CALL( freeReaderdata(scip, readerdata) );
127 
128  SCIPfreeBlockMemory(scip, &readerdata);
129 
130  return SCIP_OKAY;
131 }
132 /**! [SnippetReaderFreeCor] */
133 
134 
135 /** problem reading method of reader */
136 static
137 SCIP_DECL_READERREAD(readerReadCor)
138 { /*lint --e{715}*/
139 
140  SCIP_CALL( SCIPreadCor(scip, filename, result) );
141 
142  return SCIP_OKAY;
143 }
144 
145 
146 /*
147  * reader specific interface methods
148  */
149 
150 /** includes the cor file reader in SCIP */
152  SCIP* scip /**< SCIP data structure */
153  )
154 {
155  SCIP_READERDATA* readerdata;
156  SCIP_READER* reader;
157 
158  /* create reader data */
159  SCIP_CALL( SCIPallocBlockMemory(scip, &readerdata) );
160  SCIP_CALL( createReaderdata(scip, readerdata) );
161 
162  /* include reader */
164 
165  assert(reader != NULL);
166 
167  /* set non fundamental callbacks via setter functions */
168  SCIP_CALL( SCIPsetReaderCopy(scip, reader, readerCopyCor) );
169  SCIP_CALL( SCIPsetReaderFree(scip, reader, readerFreeCor) );
170  SCIP_CALL( SCIPsetReaderRead(scip, reader, readerReadCor) );
171 
172  return SCIP_OKAY;
173 }
174 
175 
176 
177 /** reads problem from file */
179  SCIP* scip, /**< SCIP data structure */
180  const char* filename, /**< full path and name of file to read, or NULL if stdin should be used */
181  SCIP_RESULT* result /**< pointer to store the result of the file reading call */
182  )
183 {
184  SCIP_READER* reader;
185  SCIP_READERDATA* readerdata;
186 
187  reader = SCIPfindReader(scip, READER_NAME);
188  assert(reader != NULL);
189 
190  readerdata = SCIPreaderGetData(reader);
191  assert(readerdata != NULL);
192 
193  SCIP_CALL( SCIPreadMps(scip, reader, filename, result, &readerdata->varnames, &readerdata->consnames,
194  &readerdata->varnamessize, &readerdata->consnamessize, &readerdata->nvarnames, &readerdata->nconsnames) );
195 
196  if( (*result) == SCIP_SUCCESS )
197  readerdata->read = TRUE;
198 
199  return SCIP_OKAY;
200 }
201 
202 /*
203  * Interface method for the tim and sto readers
204  */
205 
206 
207 /** returns whether the COR file has been successfully read. This is used by the TIM and STO readers. */
209  SCIP_READER* reader /**< the file reader itself */
210  )
211 {
212  SCIP_READERDATA* readerdata;
213 
214  assert(reader != NULL);
215  assert(strcmp(SCIPreaderGetName(reader), READER_NAME) == 0);
216 
217  readerdata = SCIPreaderGetData(reader);
218  assert(readerdata != NULL);
219 
220  return readerdata->read;
221 }
222 
223 /** returns the number of variable names in the COR problem */
225  SCIP_READER* reader /**< the file reader itself */
226  )
227 {
228  SCIP_READERDATA* readerdata;
229 
230  assert(reader != NULL);
231  assert(strcmp(SCIPreaderGetName(reader), READER_NAME) == 0);
232 
233  readerdata = SCIPreaderGetData(reader);
234  assert(readerdata != NULL);
235 
236  return readerdata->nvarnames;
237 }
238 
239 /** returns the number of constraint names in the COR problem */
241  SCIP_READER* reader /**< the file reader itself */
242  )
243 {
244  SCIP_READERDATA* readerdata;
245 
246  assert(reader != NULL);
247  assert(strcmp(SCIPreaderGetName(reader), READER_NAME) == 0);
248 
249  readerdata = SCIPreaderGetData(reader);
250  assert(readerdata != NULL);
251 
252  return readerdata->nconsnames;
253 }
254 
255 /** returns the variable name for the given index */
256 const char* SCIPcorGetVarName(
257  SCIP_READER* reader, /**< the file reader itself */
258  int i /**< the index of the variable that is requested */
259  )
260 {
261  SCIP_READERDATA* readerdata;
262 
263  assert(reader != NULL);
264  assert(strcmp(SCIPreaderGetName(reader), READER_NAME) == 0);
265 
266  readerdata = SCIPreaderGetData(reader);
267  assert(readerdata != NULL);
268  assert(i >= 0 && i < readerdata->nvarnames);
269 
270  return readerdata->varnames[i];
271 }
272 
273 /** returns the constraint name for the given index */
274 const char* SCIPcorGetConsName(
275  SCIP_READER* reader, /**< the file reader itself */
276  int i /**< the index of the constraint that is requested */
277  )
278 {
279  SCIP_READERDATA* readerdata;
280 
281  assert(reader != NULL);
282  assert(strcmp(SCIPreaderGetName(reader), READER_NAME) == 0);
283 
284  readerdata = SCIPreaderGetData(reader);
285  assert(readerdata != NULL);
286  assert(i >= 0 && i < readerdata->nconsnames);
287 
288  return readerdata->consnames[i];
289 }
enum SCIP_Result SCIP_RESULT
Definition: type_result.h:52
#define SCIPfreeBlockMemoryArray(scip, ptr, num)
Definition: scip_mem.h:97
SCIP_EXPORT const char * SCIPreaderGetName(SCIP_READER *reader)
Definition: reader.c:548
const char * SCIPcorGetVarName(SCIP_READER *reader, int i)
Definition: reader_cor.c:256
#define SCIPallocBlockMemoryArray(scip, ptr, num)
Definition: scip_mem.h:80
public methods for memory management
SCIP_EXPORT SCIP_READERDATA * SCIPreaderGetData(SCIP_READER *reader)
Definition: reader.c:483
COR file reader (MPS format of the core problem for stochastic programs)
const char * SCIPcorGetConsName(SCIP_READER *reader, int i)
Definition: reader_cor.c:274
SCIP_RETCODE SCIPsetReaderRead(SCIP *scip, SCIP_READER *reader, SCIP_DECL_READERREAD((*readerread)))
Definition: scip_reader.c:186
#define FALSE
Definition: def.h:73
SCIP_RETCODE SCIPsetReaderCopy(SCIP *scip, SCIP_READER *reader, SCIP_DECL_READERCOPY((*readercopy)))
Definition: scip_reader.c:138
#define TRUE
Definition: def.h:72
enum SCIP_Retcode SCIP_RETCODE
Definition: type_retcode.h:54
int SCIPcorGetNVarNames(SCIP_READER *reader)
Definition: reader_cor.c:224
#define SCIPfreeBlockMemory(scip, ptr)
Definition: scip_mem.h:95
SCIP_RETCODE SCIPincludeReaderCor(SCIP *scip)
Definition: reader_cor.c:151
#define SCIPallocBlockMemory(scip, ptr)
Definition: scip_mem.h:78
static SCIP_RETCODE createReaderdata(SCIP *scip, SCIP_READERDATA *readerdata)
Definition: reader_cor.c:52
int SCIPcorGetNConsNames(SCIP_READER *reader)
Definition: reader_cor.c:240
static SCIP_DECL_READERCOPY(readerCopyCor)
Definition: reader_cor.c:102
SCIP_RETCODE SCIPreadCor(SCIP *scip, const char *filename, SCIP_RESULT *result)
Definition: reader_cor.c:178
#define NULL
Definition: lpi_spx1.cpp:155
SCIP_RETCODE SCIPsetReaderFree(SCIP *scip, SCIP_READER *reader, SCIP_DECL_READERFREE((*readerfree)))
Definition: scip_reader.c:162
#define SCIP_CALL(x)
Definition: def.h:364
SCIP_READER * SCIPfindReader(SCIP *scip, const char *name)
Definition: scip_reader.c:226
#define READER_EXTENSION
Definition: reader_cor.c:34
struct SCIP_ReaderData SCIP_READERDATA
Definition: type_reader.h:44
#define SCIP_Bool
Definition: def.h:70
#define SCIP_DEFAULT_ARRAYSIZE
Definition: reader_cor.c:36
SCIP_RETCODE SCIPincludeReaderBasic(SCIP *scip, SCIP_READER **readerptr, const char *name, const char *desc, const char *extension, SCIP_READERDATA *readerdata)
Definition: scip_reader.c:100
#define READER_DESC
Definition: reader_cor.c:33
static SCIP_RETCODE freeReaderdata(SCIP *scip, SCIP_READERDATA *readerdata)
Definition: reader_cor.c:74
static SCIP_DECL_READERREAD(readerReadCor)
Definition: reader_cor.c:137
public methods for message output
#define READER_NAME
Definition: reader_cor.c:32
SCIP_Bool SCIPcorHasRead(SCIP_READER *reader)
Definition: reader_cor.c:208
public methods for input file readers
static SCIP_DECL_READERFREE(readerFreeCor)
Definition: reader_cor.c:118
public methods for reader plugins
SCIP_RETCODE SCIPreadMps(SCIP *scip, SCIP_READER *reader, const char *filename, SCIP_RESULT *result, const char ***varnames, const char ***consnames, int *varnamessize, int *consnamessize, int *nvarnames, int *nconsnames)
Definition: reader_mps.c:3816
(extended) MPS file reader