Scippy

SCIP

Solving Constraint Integer Programs

reader_sol.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-2018 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_sol.c
17  * @brief file reader for primal solutions
18  * @author Tobias Achterberg
19  * @author Timo Berthold
20  * @author Marc Pfetsch
21  *
22  */
23 
24 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
25 
26 #include <ctype.h>
27 #include "scip/pub_fileio.h"
28 #include "scip/pub_message.h"
29 #include "scip/pub_misc.h"
30 #include "scip/pub_reader.h"
31 #include "scip/pub_sol.h"
32 #include "scip/reader_sol.h"
33 #include "scip/scip_general.h"
34 #include "scip/scip_message.h"
35 #include "scip/scip_param.h"
36 #include "scip/scip_reader.h"
37 #include "scip/scip_sol.h"
38 #include <string.h>
39 
40 #define READER_NAME "solreader"
41 #define READER_DESC "file reader for primal solutions"
42 #define READER_EXTENSION "sol"
43 
44 
45 /*
46  * Local methods of reader
47  */
48 
49 /** reads a given SCIP solution file, problem has to be transformed in advance */
50 static
52  SCIP* scip, /**< SCIP data structure */
53  const char* fname, /**< name of the input file */
54  SCIP_Bool xml /**< true, iff the given file is XML */
55  )
56 {
57  SCIP_SOL* sol;
58  SCIP_Bool error;
59  SCIP_Bool partial;
60  SCIP_Bool stored;
61  SCIP_Bool usevartable;
62 
63  assert(scip != NULL);
64  assert(fname != NULL);
65 
66  SCIP_CALL( SCIPgetBoolParam(scip, "misc/usevartable", &usevartable) );
67 
68  if( !usevartable )
69  {
70  SCIPerrorMessage("Cannot read solution file if vartable is disabled. Make sure parameter 'misc/usevartable' is set to TRUE.\n");
71  return SCIP_READERROR;
72  }
73 
74  /* create zero solution */
75  SCIP_CALL( SCIPcreateSol(scip, &sol, NULL) );
76 
77  SCIP_CALL( SCIPreadSolFile(scip, fname, sol, xml, &partial, &error) );
78 
79  if( !error )
80  {
81  /* add and free the solution */
82  if( SCIPisTransformed(scip) )
83  {
84  SCIP_Bool completely;
85 
86  assert(!partial);
87  assert(!SCIPsolIsPartial(sol));
88 
89  /* use display/allviols to decide whether to print all violations or just the first one */
90  SCIP_CALL( SCIPgetBoolParam(scip, "display/allviols", &completely) );
91 
92  SCIP_CALL( SCIPtrySolFree(scip, &sol, TRUE, completely, TRUE, TRUE, TRUE, &stored) );
93 
94  /* display result */
95  SCIPverbMessage(scip, SCIP_VERBLEVEL_NORMAL, NULL, "primal solution from solution file <%s> was %s\n",
96  fname, stored ? "accepted" : "rejected - solution is infeasible or objective too poor");
97  }
98  else
99  {
100  /* add primal solution to solution candidate storage, frees the solution afterwards */
101  SCIP_CALL( SCIPaddSolFree(scip, &sol, &stored) );
102 
103  /* display result */
104  SCIPverbMessage(scip, SCIP_VERBLEVEL_NORMAL, NULL, "%sprimal solution from solution file <%s> was %s\n",
105  partial ? "partial " : "", fname, stored ? "accepted as candidate, will be checked when solving starts" : "rejected - solution objective too poor");
106  }
107 
108  return SCIP_OKAY;
109  }
110  else
111  {
112  /* free solution */
113  SCIP_CALL( SCIPfreeSol(scip, &sol) );
114 
115  return SCIP_READERROR;
116  }
117 }
118 
119 /*
120  * Callback methods of reader
121  */
122 
123 /** copy method for reader plugins (called when SCIP copies plugins) */
124 static
125 SCIP_DECL_READERCOPY(readerCopySol)
126 { /*lint --e{715}*/
127  assert(scip != NULL);
128  assert(reader != NULL);
129  assert(strcmp(SCIPreaderGetName(reader), READER_NAME) == 0);
130 
131  /* call inclusion method of reader */
133 
134  return SCIP_OKAY;
135 }
136 
137 
138 /** problem reading method of reader
139  *
140  * In order to determine the type of the file, we have to open it. Thus, it has to be opened
141  * twice. This might be removed, but is likely to not hurt the performance too much.
142  */
143 static
144 SCIP_DECL_READERREAD(readerReadSol)
145 { /*lint --e{715}*/
146  SCIP_FILE* file;
147  char buffer[SCIP_MAXSTRLEN];
148  char *s;
149 
150  assert(reader != NULL);
151  assert(strcmp(SCIPreaderGetName(reader), READER_NAME) == 0);
152  assert(result != NULL);
153 
154  *result = SCIP_DIDNOTRUN;
155 
157  {
158  SCIPerrorMessage("reading of solution file is only possible after a problem was created\n");
159  return SCIP_READERROR;
160  }
161 
163  {
165  "primal solution from solution file <%s> was ignored - problem is already solved to optimality\n",
166  filename);
167  *result = SCIP_SUCCESS;
168  return SCIP_OKAY;
169  }
170 
171  /* open input file in order to determine type */
172  file = SCIPfopen(filename, "r");
173  if( file == NULL )
174  {
175  SCIPerrorMessage("cannot open file <%s> for reading\n", filename);
176  SCIPprintSysError(filename);
177  return SCIP_NOFILE;
178  }
179 
180  /* get next line */
181  if( SCIPfgets(buffer, (int) sizeof(buffer), file) == NULL )
182  {
183  SCIPerrorMessage("cannot parse file.\n");
184  return SCIP_READERROR;
185  }
186  /* close file */
187  SCIPfclose(file);
188 
189  /* decide whether it is xml */
190  s = buffer;
191 
192  /* skip spaces */
193  while( isspace((unsigned char)*s) )
194  ++s;
195  if( s[0] == '<' && s[1] == '?' && s[2] == 'x' && s[3] == 'm' && s[4] == 'l' )
196  {
197  /* read XML solution and add it to the solution pool */
198  SCIP_CALL( readSol(scip, filename, TRUE) );
199  }
200  else
201  {
202  /* read the solution and add it to the solution pool */
203  SCIP_CALL( readSol(scip, filename, FALSE) );
204  }
205 
206  *result = SCIP_SUCCESS;
207 
208  return SCIP_OKAY;
209 }
210 
211 
212 /*
213  * sol file reader specific interface methods
214  */
215 
216 /** includes the sol file reader in SCIP */
218  SCIP* scip /**< SCIP data structure */
219  )
220 {
221  SCIP_READER* reader;
222 
223  /* include reader */
225 
226  assert(reader != NULL);
227 
228  /* set non fundamental callbacks via setter functions */
229  SCIP_CALL( SCIPsetReaderCopy(scip, reader, readerCopySol) );
230  SCIP_CALL( SCIPsetReaderRead(scip, reader, readerReadSol) );
231 
232  return SCIP_OKAY;
233 }
234 
#define NULL
Definition: def.h:239
public methods for SCIP parameter handling
SCIP_STAGE SCIPgetStage(SCIP *scip)
Definition: scip_general.c:412
#define READER_NAME
Definition: reader_sol.c:40
#define SCIP_MAXSTRLEN
Definition: def.h:260
SCIP_RETCODE SCIPaddSolFree(SCIP *scip, SCIP_SOL **sol, SCIP_Bool *stored)
Definition: scip_sol.c:3087
const char * SCIPreaderGetName(SCIP_READER *reader)
Definition: reader.c:547
#define FALSE
Definition: def.h:65
static SCIP_DECL_READERREAD(readerReadSol)
Definition: reader_sol.c:144
#define TRUE
Definition: def.h:64
enum SCIP_Retcode SCIP_RETCODE
Definition: type_retcode.h:53
static SCIP_DECL_READERCOPY(readerCopySol)
Definition: reader_sol.c:125
SCIP_Bool SCIPisTransformed(SCIP *scip)
Definition: scip_general.c:611
SCIP_FILE * SCIPfopen(const char *path, const char *mode)
Definition: fileio.c:140
#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 SCIPgetBoolParam(SCIP *scip, const char *name, SCIP_Bool *value)
Definition: scip_param.c:322
public methods for primal CIP solutions
#define SCIP_CALL(x)
Definition: def.h:351
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:62
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:9926
SCIP_RETCODE SCIPtrySolFree(SCIP *scip, SCIP_SOL **sol, SCIP_Bool printreason, SCIP_Bool completely, SCIP_Bool checkbounds, SCIP_Bool checkintegrality, SCIP_Bool checklprows, SCIP_Bool *stored)
Definition: scip_sol.c:3291
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
#define READER_EXTENSION
Definition: reader_sol.c:42
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:2957
public methods for solutions
static SCIP_RETCODE readSol(SCIP *scip, const char *fname, SCIP_Bool xml)
Definition: reader_sol.c:51
public methods for message output
file reader for primal solutions
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
#define READER_DESC
Definition: reader_sol.c:41
SCIP_Bool SCIPsolIsPartial(SCIP_SOL *sol)
Definition: sol.c:2480
int SCIPfclose(SCIP_FILE *fp)
Definition: fileio.c:219
public methods for reader plugins
SCIP_RETCODE SCIPincludeReaderSol(SCIP *scip)
Definition: reader_sol.c:217
SCIP_RETCODE SCIPcreateSol(SCIP *scip, SCIP_SOL **sol, SCIP_HEUR *heur)
Definition: scip_sol.c:377