SCIP

    Solving Constraint Integer Programs

    reader_rcp.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_rcp.c
    26 * @brief file reader for "pack" scheduling instances
    27 * @author Stefan Heinz
    28 */
    29
    30/*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
    31
    32#include <ctype.h>
    33
    34#include "reader_rcp.h"
    35#include "reader_sm.h"
    36
    37/**@name Reader properties
    38 *
    39 * @{
    40 */
    41
    42#define READER_NAME "rcpreader"
    43#define READER_DESC "reader for \"pack\" scheduling instances"
    44#define READER_EXTENSION "rcp"
    45
    46/**@} */
    47
    48
    49/**@name Local methods
    50 *
    51 * @{
    52 */
    53
    54/** parse job and capacities details */
    55static
    57 SCIP* scip, /**< SCIP data structure */
    58 SCIP_FILE* file, /**< file to parse */
    59 int* lineno, /**< pointer to store line number of the file */
    60 int** demands, /**< demand matrix resource job demand */
    61 SCIP_DIGRAPH* precedencegraph, /**< direct graph to store the precedence conditions */
    62 int* durations, /**< array to store the processing for each job */
    63 int* capacities, /**< array to store the different capacities */
    64 int njobs, /**< number of jobs to be parsed */
    65 int nresources /**< number of capacities to be parsed */
    66 )
    67{
    68 char buf[SCIP_MAXSTRLEN];
    69 char* endptr;
    70 int j;
    71
    72 /* get resources capacities */
    73 if( nresources > 0 && NULL != SCIPfgets(buf, (int) sizeof(buf), file) )
    74 {
    75 int r;
    76
    77 SCIPdebugMessage("line %d %s", *lineno, buf);
    78
    79 if( !SCIPstrToIntValue(buf, &capacities[0], &endptr) )
    80 return SCIP_READERROR;
    81
    82 SCIPdebugMessage("paresed capacities: <%d>", capacities[0]);
    83
    84 for( r = 1; r < nresources; ++r )
    85 {
    86 if( !SCIPstrToIntValue(endptr, &capacities[r], &endptr) )
    87 return SCIP_READERROR;
    88
    89 SCIPdebugPrintf(", <%d>", capacities[r]);
    90 }
    91
    92 SCIPdebugPrintf("\n");
    93
    94 (*lineno)++;
    95 }
    96 else
    97 return SCIP_READERROR;
    98
    99 /* get job details */
    100 for( j = 0; j < njobs; ++j )
    101 {
    102 if( NULL != SCIPfgets(buf, (int) sizeof(buf), file) )
    103 {
    104 int nsuccessors;
    105 int r;
    106 int s;
    107
    108 /* get job duration */
    109 if( !SCIPstrToIntValue(buf, &durations[j], &endptr) )
    110 return SCIP_READERROR;
    111
    112 SCIPdebugMessage("job %d: duration %d, demands (", j, durations[j]);
    113
    114 /* parse resources demands */
    115 for( r = 0; r < nresources; ++r )
    116 {
    117 if( !SCIPstrToIntValue(endptr, &demands[j][r], &endptr) )
    118 return SCIP_READERROR;
    119
    120 SCIPdebugPrintf(" %d ", demands[j][r]);
    121 }
    122
    123 /* get number of successors */
    124 if( !SCIPstrToIntValue(endptr, &nsuccessors, &endptr) )
    125 return SCIP_READERROR;
    126
    127 SCIPdebugPrintf("), successors %d:", nsuccessors);
    128
    129 /* parse successor job ids */
    130 for( s = 0; s < nsuccessors; ++s )
    131 {
    132 int successor;
    133
    134 if( !SCIPstrToIntValue(endptr, &successor, &endptr) )
    135 return SCIP_READERROR;
    136
    137 /* add precedence to precedence graph */
    138 SCIP_CALL( SCIPdigraphAddArc(precedencegraph, j, successor-1, (void*)(size_t)INT_MAX) );
    139
    140 SCIPdebugPrintf(" %d ", successor);
    141 }
    142
    143 SCIPdebugPrintf("\n");
    144 }
    145 else
    146 return SCIP_READERROR;
    147
    148 (*lineno)++;
    149 }
    150
    151 return SCIP_OKAY;
    152}
    153
    154/** read file and create problem */
    155static
    157 SCIP* scip, /**< SCIP data structure */
    158 SCIP_FILE* file, /**< file to pares */
    159 const char* filename /**< name of input file */
    160 )
    161{
    162 SCIP_RETCODE retcode;
    163 char buf[SCIP_MAXSTRLEN];
    164 SCIP_DIGRAPH* precedencegraph;
    165 int** demands;
    166 int* durations;
    167 int* capacities;
    168 int lineno;
    169 int njobs;
    170 int nresources;
    171 int j;
    172
    173 assert(scip != NULL);
    174 assert(file != NULL);
    175 assert(filename != NULL);
    176
    177 lineno = 0;
    178
    179 /* get number of jobs and resources */
    180 if( NULL != SCIPfgets(buf, (int) sizeof(buf), file) )
    181 {
    182 char* endptr;
    183
    184 lineno++;
    185
    186 /* get number of jobs */
    187 if( !SCIPstrToIntValue(buf, &njobs, &endptr) )
    188 return SCIP_READERROR;
    189
    190 /* get number of resources */
    191 if( !SCIPstrToIntValue(endptr, &nresources, &endptr) )
    192 return SCIP_READERROR;
    193 }
    194 else
    195 return SCIP_READERROR;
    196
    197 SCIP_CALL( SCIPallocBufferArray(scip, &capacities, nresources) );
    198 SCIP_CALL( SCIPallocBufferArray(scip, &durations, njobs) );
    199 SCIP_CALL( SCIPallocBufferArray(scip, &demands, njobs) );
    200
    201 for( j = 0; j < njobs; ++j )
    202 {
    203 SCIP_CALL( SCIPallocBufferArray(scip, &demands[j], nresources) ); /*lint !e866*/
    204 BMSclearMemoryArray(demands[j], nresources); /*lint !e866*/
    205 }
    206
    207 SCIP_CALL( SCIPcreateDigraph(scip, &precedencegraph, njobs) );
    208
    209 SCIPdebugMessage("problem has <%d> jobs and <%d> resources\n", njobs, nresources);
    210
    211 retcode = parseDetails(scip, file, &lineno, demands, precedencegraph, durations, capacities, njobs, nresources);
    212
    213 /* create problem */
    214 if( retcode == SCIP_OKAY )
    215 {
    216 SCIP_CALL( SCIPcreateSchedulingProblem(scip, filename, NULL, NULL, demands,
    217 precedencegraph, durations, capacities, njobs, nresources, TRUE) );
    218 }
    219
    220 /* free the precedence graph */
    221 SCIPdigraphFree(&precedencegraph);
    222
    223 /* free buffer before evaluating the retcode */
    224 for( j = njobs - 1; j >= 0; --j )
    225 {
    226 SCIPfreeBufferArray(scip, &demands[j]);
    227 }
    228 SCIPfreeBufferArray(scip, &demands);
    229 SCIPfreeBufferArray(scip, &durations);
    230 SCIPfreeBufferArray(scip, &capacities);
    231
    232 SCIP_CALL( retcode );
    233
    234 return SCIP_OKAY;
    235}
    236
    237/**@} */
    238
    239/**@name Callback methods of reader
    240 *
    241 * @{
    242 */
    243
    244/** copy method for reader plugins (called when SCIP copies plugins) */
    245static
    247{ /*lint --e{715}*/
    248 assert(scip != NULL);
    249 assert(reader != NULL);
    250
    252
    253 /* call inclusion method of reader handler */
    255
    256 return SCIP_OKAY;
    257}
    258
    259/** destructor of reader to free user data (called when SCIP is exiting) */
    260#define readerFreeSch NULL
    261
    262
    263/** problem reading method of reader */
    264static
    266{ /*lint --e{715}*/
    267 SCIP_FILE* file;
    268 SCIP_RETCODE retcode;
    269
    270 if( NULL == (file = SCIPfopen(filename, "r")) )
    271 {
    272 SCIPerrorMessage("cannot open file <%s> for reading\n", filename);
    273 SCIPprintSysError(filename);
    274 return SCIP_NOFILE;
    275 }
    276
    277 /* read file and create problem */
    278 retcode = readFile(scip, file, filename);
    279
    280 /* close file */
    281 SCIPfclose(file);
    282
    283 /* check retcode after the file was closed */
    284 SCIP_CALL( retcode );
    285
    286 (*result) = SCIP_SUCCESS;
    287
    288 return SCIP_OKAY;
    289}
    290
    291
    292/** problem writing method of reader */
    293#define readerWriteSch NULL
    294
    295
    296/**@} */
    297
    298/**@name Interface methods
    299 *
    300 * @{
    301 */
    302
    303/*
    304 * reader specific interface methods
    305 */
    306
    307/** includes the rcp file reader into SCIP */
    309 SCIP* scip /**< SCIP data structure */
    310 )
    311{
    312 /* include sch reader */
    314 readerCopyRcp, readerFreeSch, readerReadRcp, readerWriteSch, NULL) );
    315
    316 return SCIP_OKAY;
    317}
    318
    319/**@} */
    SCIP_Real * r
    Definition: circlepacking.c:59
    #define NULL
    Definition: def.h:257
    #define SCIP_MAXSTRLEN
    Definition: def.h:278
    #define SCIP_STRINGEQ(name, reference, retcode)
    Definition: def.h:454
    #define TRUE
    Definition: def.h:102
    #define SCIP_CALL(x)
    Definition: def.h:364
    SCIP_FILE * SCIPfopen(const char *path, const char *mode)
    Definition: fileio.c:153
    int SCIPfclose(SCIP_FILE *fp)
    Definition: fileio.c:232
    char * SCIPfgets(char *s, int size, SCIP_FILE *stream)
    Definition: fileio.c:200
    SCIP_RETCODE SCIPdigraphAddArc(SCIP_DIGRAPH *digraph, int startnode, int endnode, void *data)
    Definition: misc.c:7739
    void SCIPdigraphFree(SCIP_DIGRAPH **digraph)
    Definition: misc.c:7645
    SCIP_RETCODE SCIPcreateDigraph(SCIP *scip, SCIP_DIGRAPH **digraph, int nnodes)
    #define SCIPallocBufferArray(scip, ptr, num)
    Definition: scip_mem.h:124
    #define SCIPfreeBufferArray(scip, ptr)
    Definition: scip_mem.h:136
    SCIP_RETCODE SCIPincludeReader(SCIP *scip, const char *name, const char *desc, const char *extension, SCIP_DECL_READERCOPY((*readercopy)), SCIP_DECL_READERFREE((*readerfree)), SCIP_DECL_READERREAD((*readerread)), SCIP_DECL_READERWRITE((*readerwrite)), SCIP_READERDATA *readerdata)
    Definition: scip_reader.c:66
    const char * SCIPreaderGetName(SCIP_READER *reader)
    Definition: reader.c:700
    SCIP_Bool SCIPstrToIntValue(const char *str, int *value, char **endptr)
    Definition: misc.c:10924
    void SCIPprintSysError(const char *message)
    Definition: misc.c:10719
    #define BMSclearMemoryArray(ptr, num)
    Definition: memory.h:130
    struct SCIP_File SCIP_FILE
    Definition: pub_fileio.h:43
    #define SCIPerrorMessage
    Definition: pub_message.h:64
    #define SCIPdebugMessage
    Definition: pub_message.h:96
    #define SCIPdebugPrintf
    Definition: pub_message.h:99
    SCIP_RETCODE SCIPincludeReaderRcp(SCIP *scip)
    Definition: reader_rcp.c:308
    static SCIP_DECL_READERCOPY(readerCopyRcp)
    Definition: reader_rcp.c:246
    #define READER_DESC
    Definition: reader_rcp.c:43
    static SCIP_RETCODE readFile(SCIP *scip, SCIP_FILE *file, const char *filename)
    Definition: reader_rcp.c:156
    #define readerFreeSch
    Definition: reader_rcp.c:260
    #define READER_EXTENSION
    Definition: reader_rcp.c:44
    static SCIP_RETCODE parseDetails(SCIP *scip, SCIP_FILE *file, int *lineno, int **demands, SCIP_DIGRAPH *precedencegraph, int *durations, int *capacities, int njobs, int nresources)
    Definition: reader_rcp.c:56
    #define READER_NAME
    Definition: reader_rcp.c:42
    #define readerWriteSch
    Definition: reader_rcp.c:293
    static SCIP_DECL_READERREAD(readerReadRcp)
    Definition: reader_rcp.c:265
    file reader for "pack" scheduling instances
    SCIP_RETCODE SCIPcreateSchedulingProblem(SCIP *scip, const char *problemname, const char **jobnames, const char **resourcenames, int **demands, SCIP_DIGRAPH *precedencegraph, int *durations, int *capacities, int njobs, int nresources, SCIP_Bool initialize)
    Definition: reader_sm.c:746
    scheduling problem file reader for RCPSP format
    @ SCIP_SUCCESS
    Definition: type_result.h:58
    @ SCIP_NOFILE
    Definition: type_retcode.h:47
    @ SCIP_READERROR
    Definition: type_retcode.h:45
    @ SCIP_OKAY
    Definition: type_retcode.h:42
    @ SCIP_INVALIDCALL
    Definition: type_retcode.h:51
    enum SCIP_Retcode SCIP_RETCODE
    Definition: type_retcode.h:63