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-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_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 <assert.h>
35 #include <string.h>
36 #if defined(_WIN32) || defined(_WIN64)
37 #else
38 #include <strings.h> /*lint --e{766}*/ /* needed for strncasecmp() */
39 #endif
40 
41 #include "scip/reader_fix.h"
42 
43 
44 #define READER_NAME "fixreader"
45 #define READER_DESC "file reader for variable fixings"
46 #define READER_EXTENSION "fix"
47 
48 
49 /*
50  * local methods
51  */
52 
53 /** reads the given solution file */
54 static
56  SCIP* scip, /**< SCIP data structure */
57  const char* filename /**< name of the input file */
58  )
59 {
60  SCIP_FILE* file;
61  SCIP_Bool error;
62  SCIP_Bool unknownvariablemessage;
63  int lineno;
64  int nfixed;
65 
66  assert(scip != NULL);
67  assert(filename != NULL);
68 
69  /* open input file */
70  file = SCIPfopen(filename, "r");
71  if( file == NULL )
72  {
73  SCIPerrorMessage("cannot open file <%s> for reading\n", filename);
74  SCIPprintSysError(filename);
75  return SCIP_NOFILE;
76  }
77 
78  /* read the file */
79  error = FALSE;
80  unknownvariablemessage = FALSE;
81  lineno = 0;
82  nfixed = 0;
83  while( !SCIPfeof(file) && !error )
84  {
85  char buffer[SCIP_MAXSTRLEN];
86  char varname[SCIP_MAXSTRLEN];
87  char valuestring[SCIP_MAXSTRLEN];
88  char objstring[SCIP_MAXSTRLEN];
89  char format[SCIP_MAXSTRLEN];
90  SCIP_VAR* var;
91  SCIP_Real value;
92  SCIP_Bool infeasible;
93  SCIP_Bool fixed;
94  int nread;
95 
96  /* get next line */
97  if( SCIPfgets(buffer, (int) sizeof(buffer), file) == NULL )
98  break;
99  lineno++;
100 
101  /* the lines "solution status: ..." and "objective value: ..." may preceed the solution information */
102  if( strncasecmp(buffer, "solution status:", 16) == 0 || strncasecmp(buffer, "objective value:", 16) == 0 )
103  continue;
104 
105  /* parse the line */
106  (void) SCIPsnprintf(format, SCIP_MAXSTRLEN, "%%%ds %%%ds %%%ds\n", SCIP_MAXSTRLEN, SCIP_MAXSTRLEN, SCIP_MAXSTRLEN);
107  nread = sscanf(buffer, format, varname, valuestring, objstring);
108  if( nread < 2 )
109  {
110  SCIPerrorMessage("invalid input line %d in solution file <%s>: <%s>\n", lineno, filename, buffer);
111  error = TRUE;
112  break;
113  }
114 
115  /* find the variable */
116  var = SCIPfindVar(scip, varname);
117  if( var == NULL )
118  {
119  if( !unknownvariablemessage )
120  {
121  SCIPwarningMessage(scip, "unknown variable <%s> in line %d of solution file <%s>\n", varname, lineno, filename);
122  SCIPwarningMessage(scip, " (further unknown variables are ignored)\n");
123  unknownvariablemessage = TRUE;
124  }
125  continue;
126  }
127 
128  /* cast the value */
129  if( strncasecmp(valuestring, "inv", 3) == 0 )
130  continue;
131  else if( strncasecmp(valuestring, "+inf", 4) == 0 || strncasecmp(valuestring, "inf", 3) == 0 )
132  value = SCIPinfinity(scip);
133  else if( strncasecmp(valuestring, "-inf", 4) == 0 )
134  value = -SCIPinfinity(scip);
135  else
136  {
137  nread = sscanf(valuestring, "%lf", &value);
138  if( nread != 1 )
139  {
140  SCIPerrorMessage("invalid solution value <%s> for variable <%s> in line %d of solution file <%s>\n",
141  valuestring, varname, lineno, filename);
142  error = TRUE;
143  break;
144  }
145  }
146 
147  /* fix the variable */
148  SCIP_CALL( SCIPfixVar(scip, var, value, &infeasible, &fixed) );
149  if( infeasible )
150  {
151  SCIPerrorMessage("infeasible solution value of <%s>[%.15g,%.15g] to %.15g in line %d of solution file <%s>\n",
152  varname, SCIPvarGetLbGlobal(var), SCIPvarGetUbGlobal(var), value, lineno, filename);
153  error = TRUE;
154  break;
155  }
156  if( fixed )
157  nfixed++;
158  }
159 
160  /* close input file */
161  SCIPfclose(file);
162 
163  /* display result */
164  SCIPverbMessage(scip, SCIP_VERBLEVEL_NORMAL, NULL, "fixed %d variables from solution file <%s>\n", nfixed, filename);
165 
166  if( error )
167  return SCIP_READERROR;
168  else
169  return SCIP_OKAY;
170 }
171 
172 /*
173  * Callback methods of reader
174  */
175 
176 /** copy method for reader plugins (called when SCIP copies plugins) */
177 static
178 SCIP_DECL_READERCOPY(readerCopyFix)
179 { /*lint --e{715}*/
180  assert(scip != NULL);
181  assert(reader != NULL);
182  assert(strcmp(SCIPreaderGetName(reader), READER_NAME) == 0);
183 
184  /* call inclusion method of reader */
186 
187  return SCIP_OKAY;
188 }
189 
190 /** problem reading method of reader */
191 static
192 SCIP_DECL_READERREAD(readerReadFix)
193 { /*lint --e{715}*/
194  assert(reader != NULL);
195  assert(strcmp(SCIPreaderGetName(reader), READER_NAME) == 0);
196  assert(result != NULL);
197 
198  *result = SCIP_DIDNOTRUN;
199 
201  {
202  SCIPerrorMessage("reading of fixing file is only possible after a problem was created\n");
203  return SCIP_READERROR;
204  }
205 
206  /* free transformed problem, s.t. fixings are applied to the original problem */
208 
209  /* read (partial) solution from fixing file */
210  SCIP_CALL( readSol(scip, filename) );
211 
212  *result = SCIP_SUCCESS;
213 
214  return SCIP_OKAY;
215 }
216 
217 /*
218  * fix file reader specific interface methods
219  */
220 
221 /** includes the fix file reader in SCIP */
223  SCIP* scip /**< SCIP data structure */
224  )
225 {
226  SCIP_READER* reader;
227 
228  /* include reader */
230 
231  /* set non fundamental callbacks via setter functions */
232  SCIP_CALL( SCIPsetReaderCopy(scip, reader, readerCopyFix) );
233  SCIP_CALL( SCIPsetReaderRead(scip, reader, readerReadFix) );
234 
235  return SCIP_OKAY;
236 }
#define READER_EXTENSION
Definition: reader_fix.c:46
SCIP_STAGE SCIPgetStage(SCIP *scip)
Definition: scip.c:814
SCIP_Real SCIPvarGetLbGlobal(SCIP_VAR *var)
Definition: var.c:17166
#define SCIP_MAXSTRLEN
Definition: def.h:215
const char * SCIPreaderGetName(SCIP_READER *reader)
Definition: reader.c:515
#define FALSE
Definition: def.h:64
SCIP_Real SCIPinfinity(SCIP *scip)
Definition: scip.c:45816
int SCIPsnprintf(char *t, int len, const char *s,...)
Definition: misc.c:9340
#define TRUE
Definition: def.h:63
enum SCIP_Retcode SCIP_RETCODE
Definition: type_retcode.h:53
void SCIPwarningMessage(SCIP *scip, const char *formatstr,...)
Definition: scip.c:1260
static SCIP_DECL_READERREAD(readerReadFix)
Definition: reader_fix.c:192
SCIP_VAR * SCIPfindVar(SCIP *scip, const char *name)
Definition: scip.c:12325
SCIP_FILE * SCIPfopen(const char *path, const char *mode)
Definition: fileio.c:140
SCIP_Real SCIPvarGetUbGlobal(SCIP_VAR *var)
Definition: var.c:17176
SCIP_RETCODE SCIPincludeReaderFix(SCIP *scip)
Definition: reader_fix.c:222
static SCIP_RETCODE readSol(SCIP *scip, const char *filename)
Definition: reader_fix.c:55
#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:44
#define NULL
Definition: lpi_spx1.cpp:137
#define SCIP_CALL(x)
Definition: def.h:306
void SCIPverbMessage(SCIP *scip, SCIP_VERBLEVEL msgverblevel, FILE *file, const char *formatstr,...)
Definition: scip.c:1353
SCIP_RETCODE SCIPfreeTransform(SCIP *scip)
Definition: scip.c:16873
#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:5201
void SCIPprintSysError(const char *message)
Definition: misc.c:9276
file reader for variable fixings
SCIP_RETCODE SCIPfixVar(SCIP *scip, SCIP_VAR *var, SCIP_Real fixedval, SCIP_Bool *infeasible, SCIP_Bool *fixed)
Definition: scip.c:25141
SCIP_RETCODE SCIPsetReaderCopy(SCIP *scip, SCIP_READER *reader, SCIP_DECL_READERCOPY((*readercopy)))
Definition: scip.c:5239
#define SCIP_Real
Definition: def.h:135
SCIP_RETCODE SCIPsetReaderRead(SCIP *scip, SCIP_READER *reader, SCIP_DECL_READERREAD((*readerread)))
Definition: scip.c:5287
int SCIPfclose(SCIP_FILE *fp)
Definition: fileio.c:219
static SCIP_DECL_READERCOPY(readerCopyFix)
Definition: reader_fix.c:178
#define READER_DESC
Definition: reader_fix.c:45