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