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