Scippy

SCIP

Solving Constraint Integer Programs

reader_mst.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-2019 Konrad-Zuse-Zentrum */
7 /* fuer Informationstechnik Berlin */
8 /* */
9 /* SCIP is distributed under the terms of the ZIB Academic License. */
10 /* */
11 /* You should have received a copy of the ZIB Academic License */
12 /* along with SCIP; see the file COPYING. If not visit scip.zib.de. */
13 /* */
14 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
15 
16 /**@file reader_mst.c
17  * @brief file reader for partial primal solutions (like MIP-start of Cplex)
18  * @author Jakob Witzig
19  *
20  */
21 
22 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
23 
24 #include <ctype.h>
25 #include "scip/pub_fileio.h"
26 #include "scip/pub_message.h"
27 #include "scip/pub_misc.h"
28 #include "scip/pub_reader.h"
29 #include "scip/reader_mst.h"
30 #include "scip/scip_general.h"
31 #include "scip/scip_message.h"
32 #include "scip/scip_param.h"
33 #include "scip/scip_reader.h"
34 #include "scip/scip_sol.h"
35 #include <string.h>
36 
37 #define READER_NAME "mstreader"
38 #define READER_DESC "file reader for partial primal solutions"
39 #define READER_EXTENSION "mst"
40 
41 
42 /*
43  * Local methods of reader
44  */
45 
46 /** reads a given SCIP solution file, problem has to be transformed in advance */
47 static
49  SCIP* scip, /**< SCIP data structure */
50  const char* fname, /**< name of the input file */
51  SCIP_Bool xml /**< true, iff the given file is XML */
52  )
53 {
54  SCIP_SOL* sol;
55  SCIP_Bool error;
56  SCIP_Bool stored;
57  SCIP_Bool usevartable;
58 
59  assert(scip != NULL);
60  assert(fname != NULL);
61 
62  SCIP_CALL( SCIPgetBoolParam(scip, "misc/usevartable", &usevartable) );
63 
64  if( !usevartable )
65  {
66  SCIPerrorMessage("Cannot read solution file if vartable is disabled. Make sure parameter 'misc/usevartable' is set to TRUE.\n");
67  return SCIP_READERROR;
68  }
69 
70  /* create zero solution */
71  SCIP_CALL( SCIPcreatePartialSol(scip, &sol, NULL) );
72 
73  SCIP_CALL( SCIPreadSolFile(scip, fname, sol, xml, NULL, &error) );
74 
75  if( !error )
76  {
77  assert(!SCIPisTransformed(scip));
78 
79  /* add primal solution to solution candidate storage, frees the solution afterwards */
80  SCIP_CALL( SCIPaddSolFree(scip, &sol, &stored) );
81 
82  /* display result */
83  SCIPverbMessage(scip, SCIP_VERBLEVEL_NORMAL, NULL, "partial primal solution from solution file <%s> was accepted as candidate, will be completed and checked when solving starts\n",
84  fname);
85 
86  return SCIP_OKAY;
87  }
88  else
89  {
90  /* free solution */
91  SCIP_CALL( SCIPfreeSol(scip, &sol) );
92 
93  return SCIP_READERROR;
94  }
95 }
96 
97 /*
98  * Callback methods of reader
99  */
100 
101 /** copy method for reader plugins (called when SCIP copies plugins) */
102 static
103 SCIP_DECL_READERCOPY(readerCopyMst)
104 { /*lint --e{715}*/
105  assert(scip != NULL);
106  assert(reader != NULL);
107  assert(strcmp(SCIPreaderGetName(reader), READER_NAME) == 0);
108 
109  /* call inclusion method of reader */
111 
112  return SCIP_OKAY;
113 }
114 
115 
116 /** problem reading method of reader
117  *
118  * In order to determine the type of the file, we have to open it. Thus, it has to be opened
119  * twice. This might be removed, but is likely to not hurt the performance too much.
120  */
121 static
122 SCIP_DECL_READERREAD(readerReadMst)
123 { /*lint --e{715}*/
124  SCIP_FILE* file;
125  char buffer[SCIP_MAXSTRLEN];
126  char *s;
127 
128  assert(reader != NULL);
129  assert(strcmp(SCIPreaderGetName(reader), READER_NAME) == 0);
130  assert(result != NULL);
131 
132  *result = SCIP_DIDNOTRUN;
133 
135  {
136  SCIPerrorMessage("reading of partial solution file is only possible after a problem was created\n");
137  return SCIP_READERROR;
138  }
139 
141  {
142  SCIPerrorMessage("reading of partial solution file is only possible before the solving process is started\n");
143  return SCIP_READERROR;
144  }
145 
146  /* open input file in order to determine type */
147  file = SCIPfopen(filename, "r");
148  if( file == NULL )
149  {
150  SCIPerrorMessage("cannot open file <%s> for reading\n", filename);
151  SCIPprintSysError(filename);
152  return SCIP_NOFILE;
153  }
154 
155  /* get next line */
156  if( SCIPfgets(buffer, (int) sizeof(buffer), file) == NULL )
157  {
158  SCIPerrorMessage("cannot parse file.\n");
159  return SCIP_READERROR;
160  }
161  /* close file */
162  SCIPfclose(file);
163 
164  /* decide whether it is xml */
165  s = buffer;
166 
167  /* skip spaces */
168  while( isspace((unsigned char)*s) )
169  ++s;
170  if( s[0] == '<' && s[1] == '?' && s[2] == 'x' && s[3] == 'm' && s[4] == 'l' )
171  {
172  /* read XML solution and add it to the solution pool */
173  SCIP_CALL( readMst(scip, filename, TRUE) );
174  }
175  else
176  {
177  /* read the solution and add it to the solution pool */
178  SCIP_CALL( readMst(scip, filename, FALSE) );
179  }
180 
181  *result = SCIP_SUCCESS;
182 
183  return SCIP_OKAY;
184 }
185 
186 
187 /*
188  * sol file reader specific interface methods
189  */
190 
191 /** includes the mst file reader in SCIP */
193  SCIP* scip /**< SCIP data structure */
194  )
195 {
196  SCIP_READER* reader;
197 
198  /* include reader */
200 
201  assert(reader != NULL);
202 
203  /* set non fundamental callbacks via setter functions */
204  SCIP_CALL( SCIPsetReaderCopy(scip, reader, readerCopyMst) );
205  SCIP_CALL( SCIPsetReaderRead(scip, reader, readerReadMst) );
206 
207  return SCIP_OKAY;
208 }
SCIP_RETCODE SCIPcreatePartialSol(SCIP *scip, SCIP_SOL **sol, SCIP_HEUR *heur)
Definition: scip_sol.c:554
#define NULL
Definition: def.h:246
public methods for SCIP parameter handling
SCIP_STAGE SCIPgetStage(SCIP *scip)
Definition: scip_general.c:411
#define READER_NAME
Definition: reader_mst.c:37
#define SCIP_MAXSTRLEN
Definition: def.h:267
SCIP_RETCODE SCIPaddSolFree(SCIP *scip, SCIP_SOL **sol, SCIP_Bool *stored)
Definition: scip_sol.c:3072
const char * SCIPreaderGetName(SCIP_READER *reader)
Definition: reader.c:547
#define FALSE
Definition: def.h:72
#define TRUE
Definition: def.h:71
enum SCIP_Retcode SCIP_RETCODE
Definition: type_retcode.h:53
static SCIP_RETCODE readMst(SCIP *scip, const char *fname, SCIP_Bool xml)
Definition: reader_mst.c:48
SCIP_Bool SCIPisTransformed(SCIP *scip)
Definition: scip_general.c:610
SCIP_FILE * SCIPfopen(const char *path, const char *mode)
Definition: fileio.c:140
#define READER_EXTENSION
Definition: reader_mst.c:39
#define SCIPerrorMessage
Definition: pub_message.h:45
struct SCIP_File SCIP_FILE
Definition: pub_fileio.h:34
char * SCIPfgets(char *s, int size, SCIP_FILE *stream)
Definition: fileio.c:187
SCIP_RETCODE SCIPincludeReaderMst(SCIP *scip)
Definition: reader_mst.c:192
SCIP_RETCODE SCIPgetBoolParam(SCIP *scip, const char *name, SCIP_Bool *value)
Definition: scip_param.c:322
#define SCIP_CALL(x)
Definition: def.h:358
void SCIPverbMessage(SCIP *scip, SCIP_VERBLEVEL msgverblevel, FILE *file, const char *formatstr,...)
Definition: scip_message.c:296
wrapper functions to map file i/o to standard or zlib file i/o
public data structures and miscellaneous methods
#define SCIP_Bool
Definition: def.h:69
SCIP_RETCODE SCIPincludeReaderBasic(SCIP *scip, SCIP_READER **readerptr, const char *name, const char *desc, const char *extension, SCIP_READERDATA *readerdata)
Definition: scip_reader.c:180
void SCIPprintSysError(const char *message)
Definition: misc.c:10162
SCIP_RETCODE SCIPfreeSol(SCIP *scip, SCIP_SOL **sol)
Definition: scip_sol.c:1034
SCIP_RETCODE SCIPsetReaderCopy(SCIP *scip, SCIP_READER *reader, SCIP_DECL_READERCOPY((*readercopy)))
Definition: scip_reader.c:218
static SCIP_DECL_READERCOPY(readerCopyMst)
Definition: reader_mst.c:103
general public methods
SCIP_RETCODE SCIPreadSolFile(SCIP *scip, const char *filename, SCIP_SOL *sol, SCIP_Bool xml, SCIP_Bool *partial, SCIP_Bool *error)
Definition: scip_sol.c:2942
public methods for solutions
public methods for message output
public methods for input file readers
public methods for message handling
SCIP_RETCODE SCIPsetReaderRead(SCIP *scip, SCIP_READER *reader, SCIP_DECL_READERREAD((*readerread)))
Definition: scip_reader.c:266
file reader for partial primal solutions
int SCIPfclose(SCIP_FILE *fp)
Definition: fileio.c:219
#define READER_DESC
Definition: reader_mst.c:38
public methods for reader plugins
static SCIP_DECL_READERREAD(readerReadMst)
Definition: reader_mst.c:122