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-2022 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_RETCODE retcode;
73  SCIP_FILE* file;
74  SCIP_Bool error;
75  SCIP_Bool unknownvariablemessage;
76  int lineno;
77  int nfixed;
78 
79  assert(scip != NULL);
80  assert(filename != NULL);
81 
82  /* open input file */
83  file = SCIPfopen(filename, "r");
84  if( file == NULL )
85  {
86  SCIPerrorMessage("cannot open file <%s> for reading\n", filename);
87  SCIPprintSysError(filename);
88  return SCIP_NOFILE;
89  }
90 
91  /* read the file */
92  error = FALSE;
93  unknownvariablemessage = FALSE;
94  lineno = 0;
95  nfixed = 0;
96  while( !SCIPfeof(file) && !error )
97  {
98  char buffer[SCIP_MAXSTRLEN];
99  char varname[SCIP_MAXSTRLEN];
100  char valuestring[SCIP_MAXSTRLEN];
101  char objstring[SCIP_MAXSTRLEN];
102  char format[SCIP_MAXSTRLEN];
103  SCIP_VAR* var;
104  SCIP_Real value;
105  SCIP_Bool infeasible;
106  SCIP_Bool fixed;
107  int nread;
108 
109  /* get next line */
110  if( SCIPfgets(buffer, (int) sizeof(buffer), file) == NULL )
111  break;
112  lineno++;
113 
114  /* the lines "solution status: ..." and "objective value: ..." may preceed the solution information */
115  if( strncasecmp(buffer, "solution status:", 16) == 0 || strncasecmp(buffer, "objective value:", 16) == 0 )
116  continue;
117 
118  /* parse the line */
119  (void) SCIPsnprintf(format, SCIP_MAXSTRLEN, "%%%ds %%%ds %%%ds\n", SCIP_MAXSTRLEN, SCIP_MAXSTRLEN, SCIP_MAXSTRLEN);
120  nread = sscanf(buffer, format, varname, valuestring, objstring);
121  if( nread < 2 )
122  {
123  SCIPerrorMessage("invalid input line %d in solution file <%s>: <%s>\n", lineno, filename, buffer);
124  error = TRUE;
125  break;
126  }
127 
128  /* find the variable */
129  var = SCIPfindVar(scip, varname);
130  if( var == NULL )
131  {
132  if( !unknownvariablemessage )
133  {
134  SCIPwarningMessage(scip, "unknown variable <%s> in line %d of solution file <%s>\n", varname, lineno, filename);
135  SCIPwarningMessage(scip, " (further unknown variables are ignored)\n");
136  unknownvariablemessage = TRUE;
137  }
138  continue;
139  }
140 
141  /* cast the value */
142  if( strncasecmp(valuestring, "inv", 3) == 0 )
143  continue;
144  else if( strncasecmp(valuestring, "+inf", 4) == 0 || strncasecmp(valuestring, "inf", 3) == 0 )
145  value = SCIPinfinity(scip);
146  else if( strncasecmp(valuestring, "-inf", 4) == 0 )
147  value = -SCIPinfinity(scip);
148  else
149  {
150  /* coverity[secure_coding] */
151  nread = sscanf(valuestring, "%lf", &value);
152  if( nread != 1 )
153  {
154  SCIPerrorMessage("invalid solution value <%s> for variable <%s> in line %d of solution file <%s>\n",
155  valuestring, varname, lineno, filename);
156  error = TRUE;
157  break;
158  }
159  }
160 
161  /* fix the variable */
162  retcode = SCIPfixVar(scip, var, value, &infeasible, &fixed);
163  if( retcode != SCIP_OKAY )
164  {
165  SCIPerrorMessage("Error fixing variable <%s> to value %.15g in line %d of bounds file <%s>\n",
166  varname, value, lineno, filename);
167  error = TRUE;
168  break;
169  }
170  if( infeasible )
171  {
172  SCIPerrorMessage("infeasible solution value of <%s>[%.15g,%.15g] to %.15g in line %d of solution file <%s>\n",
173  varname, SCIPvarGetLbGlobal(var), SCIPvarGetUbGlobal(var), value, lineno, filename);
174  error = TRUE;
175  break;
176  }
177  if( fixed )
178  nfixed++;
179  }
180 
181  /* close input file */
182  SCIPfclose(file);
183 
184  /* display result */
185  SCIPverbMessage(scip, SCIP_VERBLEVEL_NORMAL, NULL, "fixed %d variables from solution file <%s>\n", nfixed, filename);
186 
187  if( error )
188  return SCIP_READERROR;
189  else
190  return SCIP_OKAY;
191 }
192 
193 /*
194  * Callback methods of reader
195  */
196 
197 /** copy method for reader plugins (called when SCIP copies plugins) */
198 static
199 SCIP_DECL_READERCOPY(readerCopyFix)
200 { /*lint --e{715}*/
201  assert(scip != NULL);
202  assert(reader != NULL);
203  assert(strcmp(SCIPreaderGetName(reader), READER_NAME) == 0);
204 
205  /* call inclusion method of reader */
207 
208  return SCIP_OKAY;
209 }
210 
211 /** problem reading method of reader */
212 static
213 SCIP_DECL_READERREAD(readerReadFix)
214 { /*lint --e{715}*/
215  assert(reader != NULL);
216  assert(strcmp(SCIPreaderGetName(reader), READER_NAME) == 0);
217  assert(result != NULL);
218 
219  *result = SCIP_DIDNOTRUN;
220 
222  {
223  SCIPerrorMessage("reading of fixing file is only possible after a problem was created\n");
224  return SCIP_READERROR;
225  }
226 
227  /* free transformed problem, s.t. fixings are applied to the original problem */
229 
230  /* read (partial) solution from fixing file */
231  SCIP_CALL( readSol(scip, filename) );
232 
233  *result = SCIP_SUCCESS;
234 
235  return SCIP_OKAY;
236 }
237 
238 /*
239  * fix file reader specific interface methods
240  */
241 
242 /** includes the fix file reader in SCIP */
244  SCIP* scip /**< SCIP data structure */
245  )
246 {
247  SCIP_READER* reader;
248 
249  /* include reader */
251 
252  /* set non fundamental callbacks via setter functions */
253  SCIP_CALL( SCIPsetReaderCopy(scip, reader, readerCopyFix) );
254  SCIP_CALL( SCIPsetReaderRead(scip, reader, readerReadFix) );
255 
256  return SCIP_OKAY;
257 }
#define READER_EXTENSION
Definition: reader_fix.c:58
SCIP_STAGE SCIPgetStage(SCIP *scip)
Definition: scip_general.c:356
SCIP_Real SCIPvarGetLbGlobal(SCIP_VAR *var)
Definition: var.c:17910
#define SCIP_MAXSTRLEN
Definition: def.h:293
public solving methods
const char * SCIPreaderGetName(SCIP_READER *reader)
Definition: reader.c:548
#define FALSE
Definition: def.h:87
SCIP_Real SCIPinfinity(SCIP *scip)
int SCIPsnprintf(char *t, int len, const char *s,...)
Definition: misc.c:10755
#define TRUE
Definition: def.h:86
enum SCIP_Retcode SCIP_RETCODE
Definition: type_retcode.h:54
public methods for problem variables
public methods for SCIP variables
void SCIPwarningMessage(SCIP *scip, const char *formatstr,...)
Definition: scip_message.c:111
static SCIP_DECL_READERREAD(readerReadFix)
Definition: reader_fix.c:213
public methods for numerical tolerances
SCIP_VAR * SCIPfindVar(SCIP *scip, const char *name)
Definition: scip_prob.c:2684
SCIP_FILE * SCIPfopen(const char *path, const char *mode)
Definition: fileio.c:144
SCIP_Real SCIPvarGetUbGlobal(SCIP_VAR *var)
Definition: var.c:17920
SCIP_RETCODE SCIPincludeReaderFix(SCIP *scip)
Definition: reader_fix.c:243
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:384
void SCIPverbMessage(SCIP *scip, SCIP_VERBLEVEL msgverblevel, FILE *file, const char *formatstr,...)
Definition: scip_message.c:216
wrapper functions to map file i/o to standard or zlib file i/o
public data structures and miscellaneous methods
SCIP_RETCODE SCIPfreeTransform(SCIP *scip)
Definition: scip_solve.c:3432
#define SCIP_Bool
Definition: def.h:84
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
void SCIPprintSysError(const char *message)
Definition: misc.c:10664
file reader for variable fixings
SCIP_RETCODE SCIPfixVar(SCIP *scip, SCIP_VAR *var, SCIP_Real fixedval, SCIP_Bool *infeasible, SCIP_Bool *fixed)
Definition: scip_var.c:8273
SCIP_RETCODE SCIPsetReaderCopy(SCIP *scip, SCIP_READER *reader, SCIP_DECL_READERCOPY((*readercopy)))
Definition: scip_reader.c:138
general public methods
public methods for message output
#define SCIP_Real
Definition: def.h:177
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:186
int SCIPfclose(SCIP_FILE *fp)
Definition: fileio.c:223
static SCIP_DECL_READERCOPY(readerCopyFix)
Definition: reader_fix.c:199
public methods for reader plugins
public methods for global and local (sub)problems
#define READER_DESC
Definition: reader_fix.c:57