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