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-2014 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 
198  if( SCIPgetStage(scip) < SCIP_STAGE_PROBLEM )
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 */
205  SCIP_CALL( SCIPfreeTransform(scip) );
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 }
239