Scippy

SCIP

Solving Constraint Integer Programs

reader_fix.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-2020 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_fix.c
17  * @ingroup DEFPLUGINS_READER
18  * @brief file reader for variable fixings
19  * @author Tobias Achterberg
20  *
21  * This reader allows to read a file containing fixation values for variables of the current problem. Each line of the
22  * file should have format
23  *
24  * <variable name> <value to fix>
25  *
26  * Note that only a subset of the variables may need to appear in the file. Lines with unknown variable names are
27  * ignored. The writing functionality is currently not supported.
28  *
29  * @note The format is equal to the (not xml) solution format of SCIP.
30  *
31  */
32 
33 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
34 
35 #include "scip/pub_fileio.h"
36 #include "scip/pub_message.h"
37 #include "scip/pub_misc.h"
38 #include "scip/pub_reader.h"
39 #include "scip/pub_var.h"
40 #include "scip/reader_fix.h"
41 #include "scip/scip_general.h"
42 #include "scip/scip_message.h"
43 #include "scip/scip_numerics.h"
44 #include "scip/scip_prob.h"
45 #include "scip/scip_reader.h"
46 #include "scip/scip_solve.h"
47 #include "scip/scip_var.h"
48 #include <string.h>
49 
50 #if !defined(_WIN32) && !defined(_WIN64)
51 #include <strings.h> /*lint --e{766}*/ /* needed for strncasecmp() */
52 #endif
53 
54 
55 
56 #define READER_NAME "fixreader"
57 #define READER_DESC "file reader for variable fixings"
58 #define READER_EXTENSION "fix"
59 
60 
61 /*
62  * local methods
63  */
64 
65 /** reads the given solution file */
66 static
68  SCIP* scip, /**< SCIP data structure */
69  const char* filename /**< name of the input file */
70  )
71 {
72  SCIP_FILE* file;
73  SCIP_Bool error;
74  SCIP_Bool unknownvariablemessage;
75  int lineno;
76  int nfixed;
77 
78  assert(scip != NULL);
79  assert(filename != NULL);
80 
81  /* open input file */
82  file = SCIPfopen(filename, "r");
83  if( file == NULL )
84  {
85  SCIPerrorMessage("cannot open file <%s> for reading\n", filename);
86  SCIPprintSysError(filename);
87  return SCIP_NOFILE;
88  }
89 
90  /* read the file */
91  error = FALSE;
92  unknownvariablemessage = FALSE;
93  lineno = 0;
94  nfixed = 0;
95  while( !SCIPfeof(file) && !error )
96  {
97  char buffer[SCIP_MAXSTRLEN];
98  char varname[SCIP_MAXSTRLEN];
99  char valuestring[SCIP_MAXSTRLEN];
100  char objstring[SCIP_MAXSTRLEN];
101  char format[SCIP_MAXSTRLEN];
102  SCIP_VAR* var;
103  SCIP_Real value;
104  SCIP_Bool infeasible;
105  SCIP_Bool fixed;
106  int nread;
107 
108  /* get next line */
109  if( SCIPfgets(buffer, (int) sizeof(buffer), file) == NULL )
110  break;
111  lineno++;
112 
113  /* the lines "solution status: ..." and "objective value: ..." may preceed the solution information */
114  if( strncasecmp(buffer, "solution status:", 16) == 0 || strncasecmp(buffer, "objective value:", 16) == 0 )
115  continue;
116 
117  /* parse the line */
118  (void) SCIPsnprintf(format, SCIP_MAXSTRLEN, "%%%ds %%%ds %%%ds\n", SCIP_MAXSTRLEN, SCIP_MAXSTRLEN, SCIP_MAXSTRLEN);
119  nread = sscanf(buffer, format, varname, valuestring, objstring);
120  if( nread < 2 )
121  {
122  SCIPerrorMessage("invalid input line %d in solution file <%s>: <%s>\n", lineno, filename, buffer);
123  error = TRUE;
124  break;
125  }
126 
127  /* find the variable */
128  var = SCIPfindVar(scip, varname);
129  if( var == NULL )
130  {
131  if( !unknownvariablemessage )
132  {
133  SCIPwarningMessage(scip, "unknown variable <%s> in line %d of solution file <%s>\n", varname, lineno, filename);
134  SCIPwarningMessage(scip, " (further unknown variables are ignored)\n");
135  unknownvariablemessage = TRUE;
136  }
137  continue;
138  }
139 
140  /* cast the value */
141  if( strncasecmp(valuestring, "inv", 3) == 0 )
142  continue;
143  else if( strncasecmp(valuestring, "+inf", 4) == 0 || strncasecmp(valuestring, "inf", 3) == 0 )
144  value = SCIPinfinity(scip);
145  else if( strncasecmp(valuestring, "-inf", 4) == 0 )
146  value = -SCIPinfinity(scip);
147  else
148  {
149  /* coverity[secure_coding] */
150  nread = sscanf(valuestring, "%lf", &value);
151  if( nread != 1 )
152  {
153  SCIPerrorMessage("invalid solution value <%s> for variable <%s> in line %d of solution file <%s>\n",
154  valuestring, varname, lineno, filename);
155  error = TRUE;
156  break;
157  }
158  }
159 
160  /* fix the variable */
161  SCIP_CALL( SCIPfixVar(scip, var, value, &infeasible, &fixed) );
162  if( infeasible )
163  {
164  SCIPerrorMessage("infeasible solution value of <%s>[%.15g,%.15g] to %.15g in line %d of solution file <%s>\n",
165  varname, SCIPvarGetLbGlobal(var), SCIPvarGetUbGlobal(var), value, lineno, filename);
166  error = TRUE;
167  break;
168  }
169  if( fixed )
170  nfixed++;
171  }
172 
173  /* close input file */
174  SCIPfclose(file);
175 
176  /* display result */
177  SCIPverbMessage(scip, SCIP_VERBLEVEL_NORMAL, NULL, "fixed %d variables from solution file <%s>\n", nfixed, filename);
178 
179  if( error )
180  return SCIP_READERROR;
181  else
182  return SCIP_OKAY;
183 }
184 
185 /*
186  * Callback methods of reader
187  */
188 
189 /** copy method for reader plugins (called when SCIP copies plugins) */
190 static
191 SCIP_DECL_READERCOPY(readerCopyFix)
192 { /*lint --e{715}*/
193  assert(scip != NULL);
194  assert(reader != NULL);
195  assert(strcmp(SCIPreaderGetName(reader), READER_NAME) == 0);
196 
197  /* call inclusion method of reader */
199 
200  return SCIP_OKAY;
201 }
202 
203 /** problem reading method of reader */
204 static
205 SCIP_DECL_READERREAD(readerReadFix)
206 { /*lint --e{715}*/
207  assert(reader != NULL);
208  assert(strcmp(SCIPreaderGetName(reader), READER_NAME) == 0);
209  assert(result != NULL);
210 
211  *result = SCIP_DIDNOTRUN;
212 
214  {
215  SCIPerrorMessage("reading of fixing file is only possible after a problem was created\n");
216  return SCIP_READERROR;
217  }
218 
219  /* free transformed problem, s.t. fixings are applied to the original problem */
221 
222  /* read (partial) solution from fixing file */
223  SCIP_CALL( readSol(scip, filename) );
224 
225  *result = SCIP_SUCCESS;
226 
227  return SCIP_OKAY;
228 }
229 
230 /*
231  * fix file reader specific interface methods
232  */
233 
234 /** includes the fix file reader in SCIP */
236  SCIP* scip /**< SCIP data structure */
237  )
238 {
239  SCIP_READER* reader;
240 
241  /* include reader */
243 
244  /* set non fundamental callbacks via setter functions */
245  SCIP_CALL( SCIPsetReaderCopy(scip, reader, readerCopyFix) );
246  SCIP_CALL( SCIPsetReaderRead(scip, reader, readerReadFix) );
247 
248  return SCIP_OKAY;
249 }
SCIP_EXPORT const char * SCIPreaderGetName(SCIP_READER *reader)
Definition: reader.c:548
#define READER_EXTENSION
Definition: reader_fix.c:58
#define SCIP_MAXSTRLEN
Definition: def.h:273
void SCIPwarningMessage(SCIP *scip, const char *formatstr,...)
Definition: scip_message.c:123
public solving methods
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 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
public methods for problem variables
public methods for SCIP variables
static SCIP_DECL_READERREAD(readerReadFix)
Definition: reader_fix.c:205
public methods for numerical tolerances
SCIP_FILE * SCIPfopen(const char *path, const char *mode)
Definition: fileio.c:144
static SCIP_RETCODE readSol(SCIP *scip, const char *filename)
Definition: reader_fix.c:67
#define SCIPerrorMessage
Definition: pub_message.h:55
int SCIPfeof(SCIP_FILE *stream)
Definition: fileio.c:218
struct SCIP_File SCIP_FILE
Definition: pub_fileio.h:34
char * SCIPfgets(char *s, int size, SCIP_FILE *stream)
Definition: fileio.c:191
#define READER_NAME
Definition: reader_fix.c:56
#define NULL
Definition: lpi_spx1.cpp:155
#define SCIP_CALL(x)
Definition: def.h:364
wrapper functions to map file i/o to standard or zlib file i/o
SCIP_Real SCIPinfinity(SCIP *scip)
public data structures and miscellaneous methods
#define SCIP_Bool
Definition: def.h:70
file reader for variable fixings
SCIP_EXPORT SCIP_Real SCIPvarGetUbGlobal(SCIP_VAR *var)
Definition: var.c:17672
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 SCIPfixVar(SCIP *scip, SCIP_VAR *var, SCIP_Real fixedval, SCIP_Bool *infeasible, SCIP_Bool *fixed)
Definition: scip_var.c:8255
SCIP_VAR * SCIPfindVar(SCIP *scip, const char *name)
Definition: scip_prob.c:2679
general public methods
SCIP_EXPORT SCIP_Real SCIPvarGetLbGlobal(SCIP_VAR *var)
Definition: var.c:17662
public methods for message output
int SCIPsnprintf(char *t, int len, const char *s,...)
Definition: misc.c:10590
#define SCIP_Real
Definition: def.h:163
public methods for input file readers
public methods for message handling
void SCIPprintSysError(const char *message)
Definition: misc.c:10499
int SCIPfclose(SCIP_FILE *fp)
Definition: fileio.c:223
static SCIP_DECL_READERCOPY(readerCopyFix)
Definition: reader_fix.c:191
SCIP_STAGE SCIPgetStage(SCIP *scip)
Definition: scip_general.c:356
SCIP_RETCODE SCIPincludeReaderFix(SCIP *scip)
Definition: reader_fix.c:235
public methods for reader plugins
public methods for global and local (sub)problems
#define READER_DESC
Definition: reader_fix.c:57
SCIP_RETCODE SCIPfreeTransform(SCIP *scip)
Definition: scip_solve.c:3329