SCIP Doxygen Documentation
Loading...
Searching...
No Matches
reader_col.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_col.c
26 * @brief file reader for vertex coloring instances
27 * @author Gerald Gamrath
28 *
29 * This file implements the reader for vertex coloring problems in DIMACS standard format.
30 *
31 */
32
33/*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
34#include <assert.h>
35#include <string.h>
36#include <ctype.h>
37#include <stdlib.h>
38
39#include "reader_col.h"
40
41
42#define READER_NAME "colreader"
43#define READER_DESC "file reader for a .col-file representing a graph that should be colored"
44#define READER_EXTENSION "col"
45
46#define COL_MAX_LINELEN 1024
47
48
49
50/*
51 * Local methods
52 */
53
54/** get next number from string s */
55static
57 char** s /**< pointer to the pointer of the current position in the string */
58 )
59{
60 long tmp;
61 /* skip whitespaces */
62 while ( isspace((unsigned char)**s) )
63 ++(*s);
64 /* read number */
65 tmp = atol(*s);
66 /* skip whitespaces */
67 while ( (**s != 0) && (!isspace((unsigned char)**s)) )
68 ++(*s);
69 return tmp;
70}
71
72/** read LP in "COL File Format" */
73static
75 SCIP* scip, /**< SCIP data structure */
76 const char* filename /**< name of the input file */
77 )
78{
79 SCIP_FILE* fp; /* file-reader */
80 char buf[COL_MAX_LINELEN]; /* maximal length of line */
81 int nedges;
82 int nnodes;
83 char* char_p;
84 char* probname;
85 int** edges;
86 int i;
87 int j;
88 int begin;
89 int end;
90 int nduplicateedges;
91 SCIP_Bool duplicateedge;
92
93
94 assert(scip != NULL);
95 assert(filename != NULL);
96
97 if (NULL == (fp = SCIPfopen(filename, "r")))
98 {
99 SCIPerrorMessage("cannot open file <%s> for reading\n", filename);
100 perror(filename);
101 return SCIP_NOFILE;
102 }
103
104 /* Get problem name from filename and save it */
105 if( SCIPfgets(buf, (int) sizeof(buf), fp) == NULL)
106 return SCIP_READERROR;
107
108 i = 1;
109 while ( (filename[i] != '/') && (filename[i] != '\0') )
110 {
111 i++;
112 }
113 if ( filename[i] != '/' )
114 {
115 j = i;
116 i = -1;
117 }
118 else
119 {
120 j = i+1;
121 while ( filename[i] == '/' && filename[j] != '\0' )
122 {
123 j = i+1;
124 while ( filename[j] != '\0' )
125 {
126 j++;
127 if ( filename[j] == '/' )
128 {
129 i = j;
130 break;
131 }
132 }
133 }
134 }
135
136 if( j-i-4 <= 0 )
137 return SCIP_READERROR;
138
139 SCIP_CALL( SCIPallocBufferArray(scip, &probname, j-i-4) );
140 (void) SCIPstrncpy(probname, filename + (i+1), j-i-4);
141
142 /* Read until information about graph starts */
143 while( !SCIPfeof(fp) && (buf[0] != 'p') )
144 {
145 SCIPfgets(buf, (int) sizeof(buf), fp); /*lint !e534*/
146 }
147
148 /* no graph information in file! */
149 if ( SCIPfeof(fp) )
150 {
151 SCIPerrorMessage("Error! Could not find line starting with 'p'.\n");
152 return SCIP_READERROR;
153 }
154
155 /* wrong format of the line containig number of nodes and edges */
156 if ( buf[2] != 'e' || buf[3] != 'd' || buf[4] != 'g' || buf[5] != 'e' )
157 {
158 SCIPerrorMessage("Line starting with 'p' must continue with 'edge'!\n");
159 return SCIP_READERROR;
160 }
161 char_p = &buf[6];
162
163 /* if line reads 'edges' (non-standard!), instead of 'edge'. */
164 if ( *char_p == 's' )
165 ++(char_p);
166
167 /* read out number of nodes and edges, the pointer char_p will be changed */
168 nduplicateedges = 0;
169 nnodes = (int) getNextNumber(&char_p);
170 nedges = (int) getNextNumber(&char_p);
171
172 if ( nnodes <= 0 )
173 {
174 SCIPerrorMessage("Number of vertices must be positive!\n");
175 return SCIP_READERROR;
176 }
177
178 if ( nedges < 0 )
179 {
180 SCIPerrorMessage("Number of edges must be nonnegative!\n");
181 return SCIP_READERROR;
182 }
183
184 /* create array for edges */
185 SCIP_CALL( SCIPallocBufferArray(scip, &edges, nedges) );
186 for( i = 0; i < nedges; i++)
187 {
188 SCIP_CALL( SCIPallocBufferArray(scip, &(edges[i]), 2) ); /*lint !e866*/
189 }
190
191 /* fill array for edges */
192 i = 0;
193 while ( !SCIPfeof(fp) )
194 {
195 SCIPfgets(buf, (int) sizeof(buf), fp); /*lint !e534*/
196 if ( buf[0] == 'e')
197 {
198 duplicateedge = FALSE;
199 char_p = &buf[2];
200
201 begin = (int) getNextNumber(&char_p);
202 end = (int) getNextNumber(&char_p);
203 for ( j = 0; j < i; j++)
204 {
205 if ( ((edges[j][0] == begin) && (edges[j][1] == end))
206 || ((edges[j][1] == begin) && (edges[j][0] == end)) )
207 {
208 duplicateedge = TRUE;
209 nduplicateedges++;
210 break;
211 }
212 }
213 if ( !duplicateedge )
214 {
215 if( i >= nedges )
216 {
217 SCIPerrorMessage("more edges than expected: expected %d many, but got already %d'th (non-duplicate) edge", nedges, i+1);
218 return SCIP_READERROR;
219 }
220 edges[i][0] = begin;
221 edges[i][1] = end;
222 assert((edges[i][0] > 0) && (edges[i][0] <= nnodes));
223 assert((edges[i][1] > 0) && (edges[i][1] <= nnodes));
224 i++;
225 }
226 }
227 }
228 if( i + nduplicateedges != nedges ) /*lint !e845*/
229 {
230 SCIPerrorMessage("incorrect number of edges: expected %d many, but got %d many\n", nedges, i + nduplicateedges); /*lint !e845*/
231 return SCIP_ERROR;
232 }
233
234 printf("Read graph: %d nodes, %d edges (%d duplicates)\n", nnodes, nedges, nduplicateedges); /*lint !e845*/
235
236 /* create problem data */
237 SCIP_CALL( SCIPcreateProbColoring(scip, probname, nnodes, nedges-nduplicateedges, edges) );
238
239 /* create LP */
240 SCIPdebugMessage("Create LP...\n");
242
243 /* activate the pricer */
246 for ( i = nedges-1; i >= 0; i--)
247 {
248 SCIPfreeBufferArray(scip, &(edges[i]));
249 }
250 SCIPfreeBufferArray(scip, &edges);
251 SCIPfreeBufferArray(scip, &probname);
252 SCIPfclose(fp);
253
254 return SCIP_OKAY;
255}
256
257
258
259
260/*
261 * Callback methods of reader
262 */
263
264/** copy method for reader plugins (called when SCIP copies plugins) */
265static
267{ /*lint --e{715}*/
268 assert(scip != NULL);
269 assert(reader != NULL);
270 assert(strcmp(SCIPreaderGetName(reader), READER_NAME) == 0);
271
272 return SCIP_OKAY;
273}
274
275/** problem reading method of reader */
276static
278{ /*lint --e{715}*/
279 assert(reader != NULL);
280 assert(strcmp(SCIPreaderGetName(reader), READER_NAME) == 0);
281 assert(scip != NULL);
282 assert(result != NULL);
283
284 SCIP_CALL( readCol(scip, filename) );
285
287
288 return SCIP_OKAY;
289}
290
291
292
293
294/*
295 * col file reader specific interface methods
296 */
297
298/** includes the col file reader in SCIP */
300 SCIP* scip /**< SCIP data structure */
301 )
302{
303 SCIP_READERDATA* readerdata;
304 SCIP_READER* reader;
305
306 /* create col reader data */
307 readerdata = NULL;
308
309 /* include col reader */
311
312 SCIP_CALL( SCIPsetReaderCopy(scip, reader, readerCopyCol) );
313 SCIP_CALL( SCIPsetReaderRead(scip, reader, readerReadCol) );
314
315 return SCIP_OKAY;
316}
#define NULL
Definition def.h:255
#define SCIP_Bool
Definition def.h:98
#define TRUE
Definition def.h:100
#define FALSE
Definition def.h:101
#define SCIP_CALL(x)
Definition def.h:362
SCIP_FILE * SCIPfopen(const char *path, const char *mode)
Definition fileio.c:154
int SCIPfeof(SCIP_FILE *stream)
Definition fileio.c:228
int SCIPfclose(SCIP_FILE *fp)
Definition fileio.c:233
char * SCIPfgets(char *s, int size, SCIP_FILE *stream)
Definition fileio.c:201
#define nnodes
Definition gastrans.c:74
SCIP_RETCODE SCIPsetObjIntegral(SCIP *scip)
Definition scip_prob.c:1758
#define SCIPallocBufferArray(scip, ptr, num)
Definition scip_mem.h:124
#define SCIPfreeBufferArray(scip, ptr)
Definition scip_mem.h:136
SCIP_PRICER * SCIPfindPricer(SCIP *scip, const char *name)
SCIP_RETCODE SCIPactivatePricer(SCIP *scip, SCIP_PRICER *pricer)
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_RETCODE SCIPsetReaderRead(SCIP *scip, SCIP_READER *reader,)
const char * SCIPreaderGetName(SCIP_READER *reader)
Definition reader.c:680
int SCIPstrncpy(char *t, const char *s, int size)
Definition misc.c:10897
return SCIP_OKAY
assert(minobj< SCIPgetCutoffbound(scip))
SCIP_RETCODE SCIPcreateProbColoring(SCIP *scip, const char *name, int nnodes, int nedges, int **edges)
SCIP_RETCODE COLORprobSetUpArrayOfCons(SCIP *scip)
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 READER_DESC
Definition reader_bnd.c:62
#define READER_EXTENSION
Definition reader_bnd.c:63
#define READER_NAME
Definition reader_bnd.c:61
SCIP_RETCODE SCIPincludeReaderCol(SCIP *scip)
Definition reader_col.c:299
static SCIP_RETCODE readCol(SCIP *scip, const char *filename)
Definition reader_col.c:74
#define COL_MAX_LINELEN
Definition reader_col.c:46
static long getNextNumber(char **s)
Definition reader_col.c:56
file reader for vertex coloring instances
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
@ SCIP_SUCCESS
Definition type_result.h:58
@ SCIP_NOFILE
@ SCIP_READERROR
@ SCIP_ERROR
enum SCIP_Retcode SCIP_RETCODE
struct Scip SCIP
Definition type_scip.h:39