SCIP

    Solving Constraint Integer Programs

    reader_cyc.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_cyc.c
    26 * @brief file reader for cycle clustering instances
    27 * @author Leon Eifler
    28 *
    29 * This file implements the reader for the cycle clustering problem. The data is read from a matrix, entries separated
    30 * by whitespace. The first line in the file has to be of the form "# p nstates ncluster",
    31 * where nstates is the size of the matrix and ncluster is the number of clusters that should be used.
    32 * The file has to have the ending ".cyc" to be recognized by the reader.
    33 */
    34
    35/*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
    36#include "reader_cyc.h"
    37
    38#include <ctype.h>
    39#include "probdata_cyc.h"
    40
    41#define READER_NAME "cycreader"
    42#define READER_DESC "file reader for a .cyc-file with a transition matrix for a cycle clustering problem"
    43#define READER_EXTENSION "cyc"
    44
    45#define COL_MAX_LINELEN 10000
    46
    47
    48/*
    49 * Local methods
    50 */
    51
    52/** get next number from string s */
    53static
    55 char** s /**< pointer to the pointer of the current position in the string */
    56 )
    57{
    58 SCIP_Real tmp;
    59
    60 /* skip whitespaces */
    61 while( isspace((unsigned char)**s) )
    62 ++(*s);
    63 /* read number */
    64 tmp = atof(*s);
    65 /* skip whitespaces */
    66 while( (**s != 0) && (!isspace((unsigned char)**s)) )
    67 ++(*s);
    68
    69 return tmp;
    70}
    71
    72/** read LP in Cyc File Format.
    73 * That means first line is "p edges nbins ncluster".
    74 * Then a matrix with whitespace-separated entries of size nbins x nbins
    75*/
    76static
    78 SCIP* scip, /**< SCIP data structure */
    79 const char* filename /**< name of the input file */
    80 )
    81{
    82 SCIP_FILE* fp; /* file-reader */
    83 char buf[COL_MAX_LINELEN]; /* maximal length of line */
    84 char* char_p; /* current char */
    85 SCIP_Real** cmatrix; /* transition matrix */
    86 int nbins; /* number of states */
    87 int ncluster; /* number of clusters */
    88 int i;
    89 int col;
    90
    91 assert(scip != NULL);
    92 assert(filename != NULL);
    93
    94 if( NULL == (fp = SCIPfopen(filename, "r")) )
    95 {
    96 SCIPerrorMessage("cannot open file <%s> for reading\n", filename);
    97 perror(filename);
    98 return SCIP_NOFILE;
    99 }
    100
    101 /* Get problem name from filename and save it */
    102 if( SCIPfgets(buf, (int) sizeof(buf), fp) == NULL )
    103 return SCIP_READERROR;
    104
    105 while( !SCIPfeof(fp) && (buf[0] != '#' || buf[2] != 'p') )
    106 {
    107 SCIPfgets(buf, (int) sizeof(buf), fp); /*lint !e534*/
    108 }
    109
    110 /* no graph information in file! */
    111 if( SCIPfeof(fp) )
    112 {
    113 SCIPerrorMessage("Error! Could not find line starting with 'p'.\n");
    114 return SCIP_READERROR;
    115 }
    116 char_p = &buf[3];
    117
    118 /* read out number of nodes and edges, the pointer char_p will be changed */
    119 nbins = (int) getNextNumber(&char_p);
    120 ncluster = (int) getNextNumber(&char_p);
    121
    122 if( nbins <= 0 )
    123 {
    124 SCIPerrorMessage("Number of bins must be positive!\n");
    125 return SCIP_READERROR;
    126 }
    127
    128 if( ncluster <= 0 || nbins <= ncluster )
    129 {
    130 SCIPerrorMessage("Number of cluster must be positive and smaller than number of bins!\n");
    131 return SCIP_READERROR;
    132 }
    133
    134 /* create cmatrix */
    135 SCIP_CALL( SCIPallocMemoryArray(scip, &cmatrix, nbins) );
    136 for( i = 0; i < nbins; i++ )
    137 {
    138 SCIP_CALL( SCIPallocMemoryArray(scip, &(cmatrix[i]), nbins) ); /*lint !e866*/
    139 }
    140
    141 /* fill array the cmatrix */
    142 i = 0;
    143 while( !SCIPfeof(fp) && i < nbins )
    144 {
    145 SCIPfgets(buf, (int) sizeof(buf), fp); /*lint !e534*/
    146 char_p = &buf[0];
    147 for( col = 0; col < nbins; ++col )
    148 {
    149 cmatrix[i][col] = (SCIP_Real) getNextNumber(&char_p);
    150 }
    151
    152 if( i >= nbins )
    153 {
    154 SCIPerrorMessage( "more lines than expected: expected %d many, but got already %d'th (non-duplicate) edge",
    155 nbins, i+1 );
    156
    157 return SCIP_READERROR;
    158 }
    159
    160 i++;
    161 }
    162
    163 /* create problem data */
    164 SCIP_CALL( SCIPcreateProbCyc(scip, filename, nbins, ncluster, cmatrix) );
    165
    166 SCIPinfoMessage(scip, NULL, "Original problem: \n");
    167
    168 for( i = nbins - 1; i >= 0; i-- )
    169 {
    170 SCIPfreeMemoryArray(scip, &(cmatrix[i]));
    171 }
    172
    173 SCIPfreeMemoryArray(scip, &cmatrix);
    174
    175 SCIPfclose(fp);
    176
    177 return SCIP_OKAY;
    178}
    179
    180/*
    181 * Callback methods of reader
    182 */
    183
    184/** copy method for reader plugins (called when SCIP copies plugins) */
    185static
    187{
    188 assert(scip != NULL);
    189 assert(reader != NULL);
    190
    192
    193 return SCIP_OKAY;
    194}
    195
    196/** problem reading method of reader */
    197static
    199{
    200 assert(reader != NULL);
    201 assert(scip != NULL);
    202 assert(result != NULL);
    203
    205
    206 SCIP_CALL( readCyc( scip, filename) );
    207
    208 *result = SCIP_SUCCESS;
    209
    210 return SCIP_OKAY;
    211}
    212
    213/*
    214 * cyc file reader specific interface methods
    215 */
    216
    217/** includes the cyc file reader in SCIP */
    219 SCIP* scip /**< SCIP data structure */
    220 )
    221{
    222 SCIP_READERDATA* readerdata;
    223 SCIP_READER* reader;
    224
    225 /* create cyc reader data */
    226 readerdata = NULL;
    227
    228 /* include cyc reader */
    230 readerdata) );
    231
    232 SCIP_CALL( SCIPsetReaderCopy( scip, reader, readerCopyCyc) );
    233 SCIP_CALL( SCIPsetReaderRead( scip, reader, readerReadCyc ) );
    234
    235 SCIP_CALL( SCIPaddRealParam(scip,"cycleclustering/scale_coherence",
    236 "factor to scale the cohrence in the target function", NULL, FALSE, 0.001, 0.0, 1.0, NULL, NULL ) );
    237 SCIP_CALL( SCIPaddCharParam(scip, "cycleclustering/model",
    238 "the model variant", NULL, FALSE, 's', "seqt", NULL, NULL) );
    239 SCIP_CALL( SCIPaddBoolParam(scip, "cycleclustering/usecutselection",
    240 "true if cut selection should be used in cyc-separators", NULL, FALSE, TRUE, NULL, NULL) );
    241 SCIP_CALL( SCIPaddRealParam(scip, "cycleclustering/goodscorefac", "used for cut-selection in cycle-clustering",
    242 NULL, FALSE, 0.8, 0.0, 1.0, NULL, NULL) );
    243 SCIP_CALL( SCIPaddRealParam(scip, "cycleclustering/badscorefac", "used for cut-selection in cycle-clustering",
    244 NULL, FALSE, 0.0, 0.0, 1.0, NULL, NULL) );
    245 SCIP_CALL( SCIPaddRealParam(scip, "cycleclustering/goodmaxparall", "used for cut-selection in cycle-clustering",
    246 NULL, FALSE, 0.1, 0.0, 1.0, NULL, NULL) );
    247 SCIP_CALL( SCIPaddRealParam(scip, "cycleclustering/maxparall", "used for cut-selection in cycle-clustering",
    248 NULL, FALSE, 0.5, 0.0, 1.0, NULL, NULL) );
    249 SCIP_CALL( SCIPaddRealParam(scip, "cycleclustering/dircutoffdist", "used for cut-selection in cycle-clustering",
    250 NULL, FALSE, 0.5, 0.0, 1.0, NULL, NULL) );
    251 SCIP_CALL( SCIPaddRealParam(scip, "cycleclustering/efficacyweight", "used for cut-selection in cycle-clustering",
    252 NULL, FALSE, 0.4, 0.0, 1.0, NULL, NULL) );
    253 SCIP_CALL( SCIPaddRealParam(scip, "cycleclustering/objparalweight", "used for cut-selection in cycle-clustering",
    254 NULL, FALSE, 0.1, 0.0, 1.0, NULL, NULL) );
    255 SCIP_CALL( SCIPaddRealParam(scip, "cycleclustering/intsuppweight", "used for cut-selection in cycle-clustering",
    256 NULL, FALSE, 0.3, 0.0, 1.0, NULL, NULL) );
    257
    258 return SCIP_OKAY;
    259}
    #define NULL
    Definition: def.h:257
    #define SCIP_STRINGEQ(name, reference, retcode)
    Definition: def.h:454
    #define SCIP_Real
    Definition: def.h:165
    #define TRUE
    Definition: def.h:102
    #define FALSE
    Definition: def.h:103
    #define SCIP_CALL(x)
    Definition: def.h:364
    SCIP_FILE * SCIPfopen(const char *path, const char *mode)
    Definition: fileio.c:153
    int SCIPfeof(SCIP_FILE *stream)
    Definition: fileio.c:227
    int SCIPfclose(SCIP_FILE *fp)
    Definition: fileio.c:232
    char * SCIPfgets(char *s, int size, SCIP_FILE *stream)
    Definition: fileio.c:200
    void SCIPinfoMessage(SCIP *scip, FILE *file, const char *formatstr,...)
    Definition: scip_message.c:208
    SCIP_RETCODE SCIPaddCharParam(SCIP *scip, const char *name, const char *desc, char *valueptr, SCIP_Bool isadvanced, char defaultvalue, const char *allowedvalues, SCIP_DECL_PARAMCHGD((*paramchgd)), SCIP_PARAMDATA *paramdata)
    Definition: scip_param.c:167
    SCIP_RETCODE SCIPaddRealParam(SCIP *scip, const char *name, const char *desc, SCIP_Real *valueptr, SCIP_Bool isadvanced, SCIP_Real defaultvalue, SCIP_Real minvalue, SCIP_Real maxvalue, SCIP_DECL_PARAMCHGD((*paramchgd)), SCIP_PARAMDATA *paramdata)
    Definition: scip_param.c:139
    SCIP_RETCODE SCIPaddBoolParam(SCIP *scip, const char *name, const char *desc, SCIP_Bool *valueptr, SCIP_Bool isadvanced, SCIP_Bool defaultvalue, SCIP_DECL_PARAMCHGD((*paramchgd)), SCIP_PARAMDATA *paramdata)
    Definition: scip_param.c:57
    #define SCIPallocMemoryArray(scip, ptr, num)
    Definition: scip_mem.h:64
    #define SCIPfreeMemoryArray(scip, ptr)
    Definition: scip_mem.h:80
    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
    const char * SCIPreaderGetName(SCIP_READER *reader)
    Definition: reader.c:700
    SCIP_RETCODE SCIPsetReaderRead(SCIP *scip, SCIP_READER *reader, SCIP_DECL_READERREAD((*readerread)))
    Definition: scip_reader.c:195
    SCIP_RETCODE SCIPcreateProbCyc(SCIP *scip, const char *name, int nbins, int ncluster, SCIP_Real **cmatrix)
    problem data for cycle clustering problem
    struct SCIP_File SCIP_FILE
    Definition: pub_fileio.h:43
    #define SCIPerrorMessage
    Definition: pub_message.h:64
    static SCIP_DECL_READERREAD(readerReadCyc)
    Definition: reader_cyc.c:198
    SCIP_RETCODE SCIPincludeReaderCyc(SCIP *scip)
    Definition: reader_cyc.c:218
    #define READER_DESC
    Definition: reader_cyc.c:42
    static SCIP_Real getNextNumber(char **s)
    Definition: reader_cyc.c:54
    #define COL_MAX_LINELEN
    Definition: reader_cyc.c:45
    #define READER_EXTENSION
    Definition: reader_cyc.c:43
    static SCIP_RETCODE readCyc(SCIP *scip, const char *filename)
    Definition: reader_cyc.c:77
    static SCIP_DECL_READERCOPY(readerCopyCyc)
    Definition: reader_cyc.c:186
    #define READER_NAME
    Definition: reader_cyc.c:41
    file reader for cycle clustering instances
    struct SCIP_ReaderData SCIP_READERDATA
    Definition: type_reader.h:54
    @ 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