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