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