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-2021 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 
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  SCIPfclose(file);
160  return SCIP_READERROR;
161  }
162  /* close file */
163  SCIPfclose(file);
164 
165  /* decide whether it is xml */
166  if( SCIPstrAtStart(buffer, "<?xml", (size_t) 5) )
167  {
168  /* read XML solution and add it to the solution pool */
169  SCIP_CALL( readMst(scip, filename, TRUE) );
170  }
171  else
172  {
173  /* read the solution and add it to the solution pool */
174  SCIP_CALL( readMst(scip, filename, FALSE) );
175  }
176 
177  *result = SCIP_SUCCESS;
178 
179  return SCIP_OKAY;
180 }
181 
182 
183 /*
184  * sol file reader specific interface methods
185  */
186 
187 /** includes the mst file reader in SCIP */
189  SCIP* scip /**< SCIP data structure */
190  )
191 {
192  SCIP_READER* reader;
193 
194  /* include reader */
196 
197  assert(reader != NULL);
198 
199  /* set non fundamental callbacks via setter functions */
200  SCIP_CALL( SCIPsetReaderCopy(scip, reader, readerCopyMst) );
201  SCIP_CALL( SCIPsetReaderRead(scip, reader, readerReadMst) );
202 
203  return SCIP_OKAY;
204 }
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:279
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:2886
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:370
SCIP_Bool SCIPstrAtStart(const char *s, const char *t, size_t tlen)
Definition: misc.c:11114
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:3016
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:188
public methods for message output
public methods for input file readers
public methods for message handling
void SCIPprintSysError(const char *message)
Definition: misc.c:10513
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