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-2025 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#include <string.h>
    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 assert(strcmp(SCIPreaderGetName(reader), READER_NAME) == 0);
    129
    130 /* call inclusion method of reader */
    132
    133 return SCIP_OKAY;
    134}
    135
    136
    137/** destructor of reader to free user data (called when SCIP is exiting) */
    138/**! [SnippetReaderFreeCor] */
    139static
    141{
    142 SCIP_READERDATA* readerdata;
    143
    144 assert(strcmp(SCIPreaderGetName(reader), READER_NAME) == 0);
    145 readerdata = SCIPreaderGetData(reader);
    146 assert(readerdata != NULL);
    147
    148 SCIP_CALL( freeReaderdata(scip, readerdata) );
    149
    150 SCIPfreeBlockMemory(scip, &readerdata);
    151
    152 return SCIP_OKAY;
    153}
    154/**! [SnippetReaderFreeCor] */
    155
    156
    157/** problem reading method of reader */
    158static
    160{ /*lint --e{715}*/
    161
    162 SCIP_CALL( SCIPreadCor(scip, filename, result) );
    163
    164 return SCIP_OKAY;
    165}
    166
    167
    168/*
    169 * reader specific interface methods
    170 */
    171
    172/** includes the cor file reader in SCIP */
    174 SCIP* scip /**< SCIP data structure */
    175 )
    176{
    177 SCIP_READERDATA* readerdata;
    178 SCIP_READER* reader;
    179
    180 /* create reader data */
    181 SCIP_CALL( SCIPallocBlockMemory(scip, &readerdata) );
    182 readerdata->created = FALSE;
    183
    184 /* include reader */
    186
    187 assert(reader != NULL);
    188
    189 /* set non fundamental callbacks via setter functions */
    190 SCIP_CALL( SCIPsetReaderCopy(scip, reader, readerCopyCor) );
    191 SCIP_CALL( SCIPsetReaderFree(scip, reader, readerFreeCor) );
    192 SCIP_CALL( SCIPsetReaderRead(scip, reader, readerReadCor) );
    193
    194 return SCIP_OKAY;
    195}
    196
    197
    198
    199/** reads problem from file */
    201 SCIP* scip, /**< SCIP data structure */
    202 const char* filename, /**< full path and name of file to read, or NULL if stdin should be used */
    203 SCIP_RESULT* result /**< pointer to store the result of the file reading call */
    204 )
    205{
    206 SCIP_READER* reader;
    207 SCIP_READERDATA* readerdata;
    208
    209 assert(result != NULL);
    210
    211 *result = SCIP_DIDNOTRUN;
    212
    214 assert(reader != NULL);
    215
    216 readerdata = SCIPreaderGetData(reader);
    217 assert(readerdata != NULL);
    218
    219 /* when the COR file is read, it is necessary to free the reader data from the COR, TIM and STO readers. This is
    220 * because the COR file is the base file for the TIM and STO files. For most readers, there is no problem data stored
    221 * in the reader data, and hence the data doesn't need to be freed.
    222 */
    227
    228 /* creating the reader data at the start of the instance read */
    229 SCIP_CALL( createReaderdata(scip, readerdata) );
    230
    231 SCIP_CALL( SCIPreadMps(scip, reader, filename, result, &readerdata->varnames, &readerdata->consnames,
    232 &readerdata->varnamessize, &readerdata->consnamessize, &readerdata->nvarnames, &readerdata->nconsnames) );
    233
    234 if( (*result) == SCIP_SUCCESS )
    235 readerdata->read = TRUE;
    236
    237 return SCIP_OKAY;
    238}
    239
    240/** frees the COR reader data */
    242 SCIP* scip /**< the SCIP data structure */
    243 )
    244{
    245 SCIP_READER* reader;
    246 SCIP_READERDATA* readerdata;
    247
    248 assert(scip != NULL);
    249
    251 assert(reader != NULL);
    252
    253 readerdata = SCIPreaderGetData(reader);
    254 assert(readerdata != NULL);
    255
    256 SCIP_CALL( freeReaderdata(scip, readerdata) );
    257
    258 return SCIP_OKAY;
    259}
    260
    261/*
    262 * Interface method for the tim and sto readers
    263 */
    264
    265
    266/** returns whether the COR file has been successfully read. This is used by the TIM and STO readers. */
    268 SCIP_READER* reader /**< the file reader itself */
    269 )
    270{
    271 SCIP_READERDATA* readerdata;
    272
    273 assert(reader != NULL);
    274 assert(strcmp(SCIPreaderGetName(reader), READER_NAME) == 0);
    275
    276 readerdata = SCIPreaderGetData(reader);
    277 assert(readerdata != NULL);
    278
    279 return readerdata->read;
    280}
    281
    282/** returns the number of variable names in the COR problem */
    284 SCIP_READER* reader /**< the file reader itself */
    285 )
    286{
    287 SCIP_READERDATA* readerdata;
    288
    289 assert(reader != NULL);
    290 assert(strcmp(SCIPreaderGetName(reader), READER_NAME) == 0);
    291
    292 readerdata = SCIPreaderGetData(reader);
    293 assert(readerdata != NULL);
    294
    295 return readerdata->nvarnames;
    296}
    297
    298/** returns the number of constraint names in the COR problem */
    300 SCIP_READER* reader /**< the file reader itself */
    301 )
    302{
    303 SCIP_READERDATA* readerdata;
    304
    305 assert(reader != NULL);
    306 assert(strcmp(SCIPreaderGetName(reader), READER_NAME) == 0);
    307
    308 readerdata = SCIPreaderGetData(reader);
    309 assert(readerdata != NULL);
    310
    311 return readerdata->nconsnames;
    312}
    313
    314/** returns the variable name for the given index */
    316 SCIP_READER* reader, /**< the file reader itself */
    317 int i /**< the index of the variable that is requested */
    318 )
    319{
    320 SCIP_READERDATA* readerdata;
    321
    322 assert(reader != NULL);
    323 assert(strcmp(SCIPreaderGetName(reader), READER_NAME) == 0);
    324
    325 readerdata = SCIPreaderGetData(reader);
    326 assert(readerdata != NULL);
    327 assert(i >= 0 && i < readerdata->nvarnames);
    328
    329 return readerdata->varnames[i];
    330}
    331
    332/** returns the constraint name for the given index */
    334 SCIP_READER* reader, /**< the file reader itself */
    335 int i /**< the index of the constraint that is requested */
    336 )
    337{
    338 SCIP_READERDATA* readerdata;
    339
    340 assert(reader != NULL);
    341 assert(strcmp(SCIPreaderGetName(reader), READER_NAME) == 0);
    342
    343 readerdata = SCIPreaderGetData(reader);
    344 assert(readerdata != NULL);
    345 assert(i >= 0 && i < readerdata->nconsnames);
    346
    347 return readerdata->consnames[i];
    348}
    #define NULL
    Definition: def.h:248
    #define SCIP_Bool
    Definition: def.h:91
    #define TRUE
    Definition: def.h:93
    #define FALSE
    Definition: def.h:94
    #define SCIP_CALL(x)
    Definition: def.h:355
    SCIP_RETCODE SCIPreadCor(SCIP *scip, const char *filename, SCIP_RESULT *result)
    Definition: reader_cor.c:200
    const char * SCIPcorGetVarName(SCIP_READER *reader, int i)
    Definition: reader_cor.c:315
    SCIP_Bool SCIPcorHasRead(SCIP_READER *reader)
    Definition: reader_cor.c:267
    const char * SCIPcorGetConsName(SCIP_READER *reader, int i)
    Definition: reader_cor.c:333
    SCIP_RETCODE SCIPfreeReaderdataCor(SCIP *scip)
    Definition: reader_cor.c:241
    SCIP_RETCODE SCIPfreeReaderdataSto(SCIP *scip)
    Definition: reader_sto.c:2880
    int SCIPcorGetNConsNames(SCIP_READER *reader)
    Definition: reader_cor.c:299
    int SCIPcorGetNVarNames(SCIP_READER *reader)
    Definition: reader_cor.c:283
    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:4945
    SCIP_RETCODE SCIPincludeReaderCor(SCIP *scip)
    Definition: reader_cor.c:173
    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 SCIPincludeReaderBasic(SCIP *scip, SCIP_READER **readerptr, const char *name, const char *desc, const char *extension, SCIP_READERDATA *readerdata)
    Definition: scip_reader.c:109
    SCIP_RETCODE SCIPsetReaderCopy(SCIP *scip, SCIP_READER *reader, SCIP_DECL_READERCOPY((*readercopy)))
    Definition: scip_reader.c:147
    SCIP_READERDATA * SCIPreaderGetData(SCIP_READER *reader)
    Definition: reader.c:605
    SCIP_READER * SCIPfindReader(SCIP *scip, const char *name)
    Definition: scip_reader.c:235
    SCIP_RETCODE SCIPsetReaderFree(SCIP *scip, SCIP_READER *reader, SCIP_DECL_READERFREE((*readerfree)))
    Definition: scip_reader.c:171
    const char * SCIPreaderGetName(SCIP_READER *reader)
    Definition: reader.c:680
    SCIP_RETCODE SCIPsetReaderRead(SCIP *scip, SCIP_READER *reader, SCIP_DECL_READERREAD((*readerread)))
    Definition: scip_reader.c:195
    public methods for message output
    public methods for input file readers
    static SCIP_RETCODE createReaderdata(SCIP *scip, SCIP_READERDATA *readerdata)
    Definition: reader_cor.c:65
    static SCIP_DECL_READERCOPY(readerCopyCor)
    Definition: reader_cor.c:124
    static SCIP_DECL_READERFREE(readerFreeCor)
    Definition: reader_cor.c:140
    #define READER_DESC
    Definition: reader_cor.c:45
    #define READER_EXTENSION
    Definition: reader_cor.c:46
    #define SCIP_DEFAULT_ARRAYSIZE
    Definition: reader_cor.c:48
    static SCIP_DECL_READERREAD(readerReadCor)
    Definition: reader_cor.c:159
    #define READER_NAME
    Definition: reader_cor.c:44
    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:932
    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
    @ 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_OKAY
    Definition: type_retcode.h:42
    enum SCIP_Retcode SCIP_RETCODE
    Definition: type_retcode.h:63