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