SCIP Doxygen Documentation
Loading...
Searching...
No Matches
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-2026 Zuse Institute Berlin (ZIB) */
7/* */
8/* Licensed under the Apache License, Version 2.0 (the "License"); */
9/* you may not use this file except in compliance with the License. */
10/* You may obtain a copy of the License at */
11/* */
12/* http://www.apache.org/licenses/LICENSE-2.0 */
13/* */
14/* Unless required by applicable law or agreed to in writing, software */
15/* distributed under the License is distributed on an "AS IS" BASIS, */
16/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
17/* See the License for the specific language governing permissions and */
18/* limitations under the License. */
19/* */
20/* You should have received a copy of the Apache-2.0 license */
21/* along with SCIP; see the file LICENSE. If not visit scipopt.org. */
22/* */
23/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
24
25/**@file reader_cor.c
26 * @ingroup DEFPLUGINS_READER
27 * @brief COR file reader (MPS format of the core problem for stochastic programs)
28 * @author Stephen J. Maher
29 */
30
31/*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
32
33#include "scip/pub_message.h"
34#include "scip/pub_reader.h"
35#include "scip/reader_cor.h"
36#include "scip/reader_mps.h"
37#include "scip/reader_tim.h"
38#include "scip/reader_sto.h"
39#include "scip/scip_mem.h"
40#include "scip/scip_reader.h"
41#include "scip/scip_prob.h"
42
43
44#define READER_NAME "correader"
45#define READER_DESC "file reader for CORE problem of stochastic programs in the SMPS file format"
46#define READER_EXTENSION "cor"
47
48#define SCIP_DEFAULT_ARRAYSIZE 100
49
50/** COR reading data */
51struct SCIP_ReaderData
52{
53 const char** varnames;
54 const char** consnames;
55 int varnamessize;
56 int consnamessize;
57 int nvarnames;
58 int nconsnames;
59 SCIP_Bool created;
60 SCIP_Bool read;
61};
62
63/** creates the reader data */
64static
66 SCIP* scip, /**< SCIP data structure */
67 SCIP_READERDATA* readerdata /**< the reader data structure */
68 )
69{
70 assert(scip != NULL);
71 assert(readerdata != NULL);
72
73 if( !readerdata->created )
74 {
75 readerdata->created = TRUE;
76 readerdata->read = FALSE;
77 readerdata->nvarnames = 0;
78 readerdata->nconsnames = 0;
79 readerdata->varnamessize = SCIP_DEFAULT_ARRAYSIZE;
80 readerdata->consnamessize = SCIP_DEFAULT_ARRAYSIZE;
81
82 SCIP_CALL( SCIPallocBlockMemoryArray(scip, &readerdata->varnames, readerdata->varnamessize) );
83 SCIP_CALL( SCIPallocBlockMemoryArray(scip, &readerdata->consnames, readerdata->consnamessize) );
84 }
85
86 return SCIP_OKAY;
87}
88
89/** creates the reader data */
90static
92 SCIP* scip, /**< SCIP data structure */
93 SCIP_READERDATA* readerdata /**< the reader data structure */
94 )
95{
96 int i;
97
98 assert(scip != NULL);
99 assert(readerdata != NULL);
100
101 if( readerdata->created )
102 {
103 for( i = readerdata->nvarnames - 1; i >= 0; i-- )
104 SCIPfreeBlockMemoryArray(scip, &readerdata->varnames[i], strlen(readerdata->varnames[i]) + 1);
105
106 for( i = readerdata->nconsnames - 1; i >= 0; i-- )
107 SCIPfreeBlockMemoryArray(scip, &readerdata->consnames[i], strlen(readerdata->consnames[i]) + 1);
108
109 SCIPfreeBlockMemoryArray(scip, &readerdata->consnames, readerdata->consnamessize);
110 SCIPfreeBlockMemoryArray(scip, &readerdata->varnames, readerdata->varnamessize);
111 }
112
113 readerdata->created = FALSE;
114
115 return SCIP_OKAY;
116}
117
118/*
119 * Callback methods of reader
120 */
121
122/** copy method for reader plugins (called when SCIP copies plugins) */
123static
125{ /*lint --e{715}*/
126 assert(scip != NULL);
127 assert(reader != NULL);
128
130
131 /* call inclusion method of reader */
133
134 return SCIP_OKAY;
135}
136
137
138/** destructor of reader to free user data (called when SCIP is exiting) */
139/**! [SnippetReaderFreeCor] */
140static
142{
143 SCIP_READERDATA* readerdata;
144
146
147 readerdata = SCIPreaderGetData(reader);
148 assert(readerdata != NULL);
149
150 SCIP_CALL( freeReaderdata(scip, readerdata) );
151
152 SCIPfreeBlockMemory(scip, &readerdata);
153
154 return SCIP_OKAY;
155}
156/**! [SnippetReaderFreeCor] */
157
158
159/** problem reading method of reader */
160static
162{ /*lint --e{715}*/
163
164 SCIP_CALL( SCIPreadCor(scip, filename, result) );
165
166 return SCIP_OKAY;
167}
168
169
170/*
171 * reader specific interface methods
172 */
173
174/** includes the cor file reader in SCIP */
176 SCIP* scip /**< SCIP data structure */
177 )
178{
179 SCIP_READERDATA* readerdata;
180 SCIP_READER* reader;
181
182 /* create reader data */
183 SCIP_CALL( SCIPallocBlockMemory(scip, &readerdata) );
184 readerdata->created = FALSE;
185
186 /* include reader */
188
189 assert(reader != NULL);
190
191 /* set non fundamental callbacks via setter functions */
192 SCIP_CALL( SCIPsetReaderCopy(scip, reader, readerCopyCor) );
193 SCIP_CALL( SCIPsetReaderFree(scip, reader, readerFreeCor) );
194 SCIP_CALL( SCIPsetReaderRead(scip, reader, readerReadCor) );
195
196 return SCIP_OKAY;
197}
198
199
200
201/** reads problem from file */
203 SCIP* scip, /**< SCIP data structure */
204 const char* filename, /**< full path and name of file to read, or NULL if stdin should be used */
205 SCIP_RESULT* result /**< pointer to store the result of the file reading call */
206 )
207{
208 SCIP_READER* reader;
209 SCIP_READERDATA* readerdata;
210
211 assert(result != NULL);
212
214
216 assert(reader != NULL);
217
218 readerdata = SCIPreaderGetData(reader);
219 assert(readerdata != NULL);
220
221 /* when the COR file is read, it is necessary to free the reader data from the COR, TIM and STO readers. This is
222 * because the COR file is the base file for the TIM and STO files. For most readers, there is no problem data stored
223 * in the reader data, and hence the data doesn't need to be freed.
224 */
229
230 /* creating the reader data at the start of the instance read */
231 SCIP_CALL( createReaderdata(scip, readerdata) );
232
233 SCIP_CALL( SCIPreadMps(scip, reader, filename, result, &readerdata->varnames, &readerdata->consnames,
234 &readerdata->varnamessize, &readerdata->consnamessize, &readerdata->nvarnames, &readerdata->nconsnames) );
235
236 if( (*result) == SCIP_SUCCESS )
237 readerdata->read = TRUE;
238
239 return SCIP_OKAY;
240}
241
242/** frees the COR reader data */
244 SCIP* scip /**< the SCIP data structure */
245 )
246{
247 SCIP_READER* reader;
248 SCIP_READERDATA* readerdata;
249
250 assert(scip != NULL);
251
253 assert(reader != NULL);
254
255 readerdata = SCIPreaderGetData(reader);
256 assert(readerdata != NULL);
257
258 SCIP_CALL( freeReaderdata(scip, readerdata) );
259
260 return SCIP_OKAY;
261}
262
263/*
264 * Interface method for the tim and sto readers
265 */
266
267
268/** returns whether the COR file has been successfully read. This is used by the TIM and STO readers. */
270 SCIP_READER* reader /**< the file reader itself */
271 )
272{
273 SCIP_READERDATA* readerdata;
274
275 assert(reader != NULL);
276
278
279 readerdata = SCIPreaderGetData(reader);
280 assert(readerdata != NULL);
281
282 return readerdata->read;
283}
284
285/** returns the number of variable names in the COR problem */
287 SCIP_READER* reader /**< the file reader itself */
288 )
289{
290 SCIP_READERDATA* readerdata;
291
292 assert(reader != NULL);
293
295
296 readerdata = SCIPreaderGetData(reader);
297 assert(readerdata != NULL);
298
299 return readerdata->nvarnames;
300}
301
302/** returns the number of constraint names in the COR problem */
304 SCIP_READER* reader /**< the file reader itself */
305 )
306{
307 SCIP_READERDATA* readerdata;
308
309 assert(reader != NULL);
310
312
313 readerdata = SCIPreaderGetData(reader);
314 assert(readerdata != NULL);
315
316 return readerdata->nconsnames;
317}
318
319/** returns the variable name for the given index */
321 SCIP_READER* reader, /**< the file reader itself */
322 int i /**< the index of the variable that is requested */
323 )
324{
325 SCIP_READERDATA* readerdata;
326
327 assert(reader != NULL);
328
330
331 readerdata = SCIPreaderGetData(reader);
332 assert(readerdata != NULL);
333 assert(i >= 0 && i < readerdata->nvarnames);
334
335 return readerdata->varnames[i];
336}
337
338/** returns the constraint name for the given index */
340 SCIP_READER* reader, /**< the file reader itself */
341 int i /**< the index of the constraint that is requested */
342 )
343{
344 SCIP_READERDATA* readerdata;
345
346 assert(reader != NULL);
347
349
350 readerdata = SCIPreaderGetData(reader);
351 assert(readerdata != NULL);
352 assert(i >= 0 && i < readerdata->nconsnames);
353
354 return readerdata->consnames[i];
355}
#define NULL
Definition def.h:257
#define SCIP_Bool
Definition def.h:100
#define SCIP_STRINGEQ(name, reference, retcode)
Definition def.h:454
#define TRUE
Definition def.h:102
#define FALSE
Definition def.h:103
#define SCIP_CALL(x)
Definition def.h:364
SCIP_RETCODE SCIPreadCor(SCIP *scip, const char *filename, SCIP_RESULT *result)
Definition reader_cor.c:202
const char * SCIPcorGetVarName(SCIP_READER *reader, int i)
Definition reader_cor.c:320
SCIP_Bool SCIPcorHasRead(SCIP_READER *reader)
Definition reader_cor.c:269
const char * SCIPcorGetConsName(SCIP_READER *reader, int i)
Definition reader_cor.c:339
SCIP_RETCODE SCIPfreeReaderdataCor(SCIP *scip)
Definition reader_cor.c:243
SCIP_RETCODE SCIPfreeReaderdataSto(SCIP *scip)
int SCIPcorGetNConsNames(SCIP_READER *reader)
Definition reader_cor.c:303
int SCIPcorGetNVarNames(SCIP_READER *reader)
Definition reader_cor.c:286
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)
SCIP_RETCODE SCIPincludeReaderCor(SCIP *scip)
Definition reader_cor.c:175
SCIP_RETCODE SCIPfreeProb(SCIP *scip)
Definition scip_prob.c:835
#define SCIPfreeBlockMemoryArray(scip, ptr, num)
Definition scip_mem.h:110
#define SCIPallocBlockMemoryArray(scip, ptr, num)
Definition scip_mem.h:93
#define SCIPfreeBlockMemory(scip, ptr)
Definition scip_mem.h:108
#define SCIPallocBlockMemory(scip, ptr)
Definition scip_mem.h:89
SCIP_RETCODE SCIPsetReaderCopy(SCIP *scip, SCIP_READER *reader,)
SCIP_RETCODE SCIPincludeReaderBasic(SCIP *scip, SCIP_READER **readerptr, const char *name, const char *desc, const char *extension, SCIP_READERDATA *readerdata)
SCIP_READERDATA * SCIPreaderGetData(SCIP_READER *reader)
Definition reader.c:625
SCIP_READER * SCIPfindReader(SCIP *scip, const char *name)
SCIP_RETCODE SCIPsetReaderRead(SCIP *scip, SCIP_READER *reader,)
const char * SCIPreaderGetName(SCIP_READER *reader)
Definition reader.c:700
SCIP_RETCODE SCIPsetReaderFree(SCIP *scip, SCIP_READER *reader,)
return SCIP_OKAY
assert(minobj< SCIPgetCutoffbound(scip))
public methods for message output
public methods for input file readers
#define READER_DESC
Definition reader_bnd.c:62
#define READER_EXTENSION
Definition reader_bnd.c:63
#define READER_NAME
Definition reader_bnd.c:61
static SCIP_RETCODE createReaderdata(SCIP *scip, SCIP_READERDATA *readerdata)
Definition reader_cor.c:65
#define SCIP_DEFAULT_ARRAYSIZE
Definition reader_cor.c:48
static SCIP_RETCODE freeReaderdata(SCIP *scip, SCIP_READERDATA *readerdata)
Definition reader_cor.c:91
COR file reader (MPS format of the core problem for stochastic programs).
(extended) MPS file reader
STO file reader - the stochastic information of an instance in SMPS format.
SCIP_RETCODE SCIPfreeReaderdataTim(SCIP *scip)
Definition reader_tim.c:934
TIM file reader - the stage information for a stochastic programming instance in SMPS format.
public methods for memory management
public methods for global and local (sub)problems
public methods for reader plugins
struct SCIP_ReaderData SCIP_READERDATA
Definition type_reader.h:54
struct SCIP_Reader SCIP_READER
Definition type_reader.h:53
#define SCIP_DECL_READERREAD(x)
Definition type_reader.h:88
#define SCIP_DECL_READERCOPY(x)
Definition type_reader.h:63
#define SCIP_DECL_READERFREE(x)
Definition type_reader.h:72
@ SCIP_DIDNOTRUN
Definition type_result.h:42
@ SCIP_SUCCESS
Definition type_result.h:58
enum SCIP_Result SCIP_RESULT
Definition type_result.h:61
@ SCIP_INVALIDCALL
enum SCIP_Retcode SCIP_RETCODE
struct Scip SCIP
Definition type_scip.h:39