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