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-2016 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  SCIP_VAR* var;
90  SCIP_Real value;
91  SCIP_Bool infeasible;
92  SCIP_Bool fixed;
93  int nread;
94 
95  /* get next line */
96  if( SCIPfgets(buffer, (int) sizeof(buffer), file) == NULL )
97  break;
98  lineno++;
99 
100  /* the lines "solution status: ..." and "objective value: ..." may preceed the solution information */
101  if( strncasecmp(buffer, "solution status:", 16) == 0 || strncasecmp(buffer, "objective value:", 16) == 0 )
102  continue;
103 
104  /* parse the line */
105  nread = sscanf(buffer, "%s %s %s\n", varname, valuestring, objstring);
106  if( nread < 2 )
107  {
108  SCIPerrorMessage("invalid input line %d in solution file <%s>: <%s>\n", lineno, filename, buffer);
109  error = TRUE;
110  break;
111  }
112 
113  /* find the variable */
114  var = SCIPfindVar(scip, varname);
115  if( var == NULL )
116  {
117  if( !unknownvariablemessage )
118  {
119  SCIPwarningMessage(scip, "unknown variable <%s> in line %d of solution file <%s>\n", varname, lineno, filename);
120  SCIPwarningMessage(scip, " (further unknown variables are ignored)\n");
121  unknownvariablemessage = TRUE;
122  }
123  continue;
124  }
125 
126  /* cast the value */
127  if( strncasecmp(valuestring, "inv", 3) == 0 )
128  continue;
129  else if( strncasecmp(valuestring, "+inf", 4) == 0 || strncasecmp(valuestring, "inf", 3) == 0 )
130  value = SCIPinfinity(scip);
131  else if( strncasecmp(valuestring, "-inf", 4) == 0 )
132  value = -SCIPinfinity(scip);
133  else
134  {
135  nread = sscanf(valuestring, "%lf", &value);
136  if( nread != 1 )
137  {
138  SCIPerrorMessage("invalid solution value <%s> for variable <%s> in line %d of solution file <%s>\n",
139  valuestring, varname, lineno, filename);
140  error = TRUE;
141  break;
142  }
143  }
144 
145  /* fix the variable */
146  SCIP_CALL( SCIPfixVar(scip, var, value, &infeasible, &fixed) );
147  if( infeasible )
148  {
149  SCIPerrorMessage("infeasible solution value of <%s>[%.15g,%.15g] to %.15g in line %d of solution file <%s>\n",
150  varname, SCIPvarGetLbGlobal(var), SCIPvarGetUbGlobal(var), value, lineno, filename);
151  error = TRUE;
152  break;
153  }
154  if( fixed )
155  nfixed++;
156  }
157 
158  /* close input file */
159  SCIPfclose(file);
160 
161  /* display result */
162  SCIPverbMessage(scip, SCIP_VERBLEVEL_NORMAL, NULL, "fixed %d variables from solution file <%s>\n", nfixed, filename);
163 
164  if( error )
165  return SCIP_READERROR;
166  else
167  return SCIP_OKAY;
168 }
169 
170 /*
171  * Callback methods of reader
172  */
173 
174 /** copy method for reader plugins (called when SCIP copies plugins) */
175 static
176 SCIP_DECL_READERCOPY(readerCopyFix)
177 { /*lint --e{715}*/
178  assert(scip != NULL);
179  assert(reader != NULL);
180  assert(strcmp(SCIPreaderGetName(reader), READER_NAME) == 0);
181 
182  /* call inclusion method of reader */
184 
185  return SCIP_OKAY;
186 }
187 
188 /** problem reading method of reader */
189 static
190 SCIP_DECL_READERREAD(readerReadFix)
191 { /*lint --e{715}*/
192  assert(reader != NULL);
193  assert(strcmp(SCIPreaderGetName(reader), READER_NAME) == 0);
194  assert(result != NULL);
195 
196  *result = SCIP_DIDNOTRUN;
197 
199  {
200  SCIPerrorMessage("reading of fixing file is only possible after a problem was created\n");
201  return SCIP_READERROR;
202  }
203 
204  /* free transformed problem, s.t. fixings are applied to the original problem */
206 
207  /* read (partial) solution from fixing file */
208  SCIP_CALL( readSol(scip, filename) );
209 
210  *result = SCIP_SUCCESS;
211 
212  return SCIP_OKAY;
213 }
214 
215 /*
216  * fix file reader specific interface methods
217  */
218 
219 /** includes the fix file reader in SCIP */
221  SCIP* scip /**< SCIP data structure */
222  )
223 {
224  SCIP_READERDATA* readerdata;
225  SCIP_READER* reader;
226 
227  /* create reader data */
228  readerdata = NULL;
229 
230  /* include reader */
232 
233  /* set non fundamental callbacks via setter functions */
234  SCIP_CALL( SCIPsetReaderCopy(scip, reader, readerCopyFix) );
235  SCIP_CALL( SCIPsetReaderRead(scip, reader, readerReadFix) );
236 
237  return SCIP_OKAY;
238 }
SCIP_RETCODE SCIPfixVar(SCIP *scip, SCIP_VAR *var, SCIP_Real fixedval, SCIP_Bool *infeasible, SCIP_Bool *fixed)
Definition: scip.c:22777
#define READER_EXTENSION
Definition: reader_fix.c:46
const char * SCIPreaderGetName(SCIP_READER *reader)
Definition: reader.c:510
void SCIPwarningMessage(SCIP *scip, const char *formatstr,...)
Definition: scip.c:1248
#define SCIP_MAXSTRLEN
Definition: def.h:201
#define NULL
Definition: lpi_spx.cpp:130
SCIP_Real SCIPvarGetUbGlobal(SCIP_VAR *var)
Definition: var.c:17067
#define FALSE
Definition: def.h:56
#define TRUE
Definition: def.h:55
enum SCIP_Retcode SCIP_RETCODE
Definition: type_retcode.h:53
#define SCIP_CALL(x)
Definition: def.h:266
SCIP_RETCODE SCIPfreeTransform(SCIP *scip)
Definition: scip.c:15373
static SCIP_DECL_READERREAD(readerReadFix)
Definition: reader_fix.c:190
SCIP_RETCODE SCIPsetReaderCopy(SCIP *scip, SCIP_READER *reader, SCIP_DECL_READERCOPY((*readercopy)))
Definition: scip.c:4601
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: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
SCIP_Real SCIPinfinity(SCIP *scip)
Definition: scip.c:41637
SCIP_RETCODE SCIPsetReaderRead(SCIP *scip, SCIP_READER *reader, SCIP_DECL_READERREAD((*readerread)))
Definition: scip.c:4649
struct SCIP_ReaderData SCIP_READERDATA
Definition: type_reader.h:37
#define SCIP_Bool
Definition: def.h:53
void SCIPprintSysError(const char *message)
Definition: misc.c:8110
file reader for variable fixings
SCIP_RETCODE SCIPincludeReaderFix(SCIP *scip)
Definition: reader_fix.c:220
SCIP_STAGE SCIPgetStage(SCIP *scip)
Definition: scip.c:801
SCIP_RETCODE SCIPincludeReaderBasic(SCIP *scip, SCIP_READER **readerptr, const char *name, const char *desc, const char *extension, SCIP_READERDATA *readerdata)
Definition: scip.c:4563
void SCIPverbMessage(SCIP *scip, SCIP_VERBLEVEL msgverblevel, FILE *file, const char *formatstr,...)
Definition: scip.c:1298
SCIP_Real SCIPvarGetLbGlobal(SCIP_VAR *var)
Definition: var.c:17057
#define SCIP_Real
Definition: def.h:127
SCIP_VAR * SCIPfindVar(SCIP *scip, const char *name)
Definition: scip.c:11392
int SCIPfclose(SCIP_FILE *fp)
Definition: fileio.c:219
static SCIP_DECL_READERCOPY(readerCopyFix)
Definition: reader_fix.c:176
#define READER_DESC
Definition: reader_fix.c:45