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-2024 Zuse Institute Berlin (ZIB) */
7/* */
8/* Licensed under the Apache License, Version 2.0 (the "License"); */
9/* you may not use this file except in compliance with the License. */
10/* You may obtain a copy of the License at */
11/* */
12/* http://www.apache.org/licenses/LICENSE-2.0 */
13/* */
14/* Unless required by applicable law or agreed to in writing, software */
15/* distributed under the License is distributed on an "AS IS" BASIS, */
16/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
17/* See the License for the specific language governing permissions and */
18/* limitations under the License. */
19/* */
20/* You should have received a copy of the Apache-2.0 license */
21/* along with SCIP; see the file LICENSE. If not visit scipopt.org. */
22/* */
23/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
24
25/**@file reader_fix.c
26 * @ingroup DEFPLUGINS_READER
27 * @brief file reader for variable fixings
28 * @author Tobias Achterberg
29 *
30 * This reader allows to read a file containing fixation values for variables of the current problem. Each line of the
31 * file should have format
32 *
33 * <variable name> <value to fix>
34 *
35 * Note that only a subset of the variables may need to appear in the file. Lines with unknown variable names are
36 * ignored. The writing functionality is currently not supported.
37 *
38 * @note The format is equal to the (not xml) solution format of SCIP.
39 *
40 */
41
42/*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
43
44#include "scip/pub_fileio.h"
45#include "scip/pub_message.h"
46#include "scip/pub_misc.h"
47#include "scip/pub_reader.h"
48#include "scip/pub_var.h"
49#include "scip/reader_fix.h"
50#include "scip/scip_general.h"
51#include "scip/scip_message.h"
52#include "scip/scip_numerics.h"
53#include "scip/scip_prob.h"
54#include "scip/scip_reader.h"
55#include "scip/scip_solve.h"
56#include "scip/scip_var.h"
57#include <string.h>
58
59
60#define READER_NAME "fixreader"
61#define READER_DESC "file reader for variable fixings"
62#define READER_EXTENSION "fix"
63
64
65/*
66 * local methods
67 */
68
69/** reads the given solution file */
70static
72 SCIP* scip, /**< SCIP data structure */
73 const char* filename /**< name of the input file */
74 )
75{
76 SCIP_RETCODE retcode;
77 SCIP_FILE* file;
78 SCIP_Bool error;
79 SCIP_Bool unknownvariablemessage;
80 int lineno;
81 int nfixed;
82
83 assert(scip != NULL);
84 assert(filename != NULL);
85
86 /* open input file */
87 file = SCIPfopen(filename, "r");
88 if( file == NULL )
89 {
90 SCIPerrorMessage("cannot open file <%s> for reading\n", filename);
91 SCIPprintSysError(filename);
92 return SCIP_NOFILE;
93 }
94
95 /* read the file */
96 error = FALSE;
97 unknownvariablemessage = FALSE;
98 lineno = 0;
99 nfixed = 0;
100 while( !SCIPfeof(file) && !error )
101 {
102 char buffer[SCIP_MAXSTRLEN];
103 char varname[SCIP_MAXSTRLEN];
104 char valuestring[SCIP_MAXSTRLEN];
105 char objstring[SCIP_MAXSTRLEN];
106 char format[SCIP_MAXSTRLEN];
107 SCIP_VAR* var;
108 SCIP_Real value;
109 SCIP_Bool infeasible;
110 SCIP_Bool fixed;
111 int nread;
112
113 /* get next line */
114 if( SCIPfgets(buffer, (int) sizeof(buffer), file) == NULL )
115 break;
116 lineno++;
117
118 /* the lines "solution status: ..." and "objective value: ..." may preceed the solution information */
119 if( SCIPstrncasecmp(buffer, "solution status:", 16) == 0 || SCIPstrncasecmp(buffer, "objective value:", 16) == 0 )
120 continue;
121
122 /* parse the line */
123 (void) SCIPsnprintf(format, SCIP_MAXSTRLEN, "%%%ds %%%ds %%%ds\n", SCIP_MAXSTRLEN, SCIP_MAXSTRLEN, SCIP_MAXSTRLEN);
124 nread = sscanf(buffer, format, varname, valuestring, objstring);
125 if( nread < 2 )
126 {
127 SCIPerrorMessage("invalid input line %d in solution file <%s>: <%s>\n", lineno, filename, buffer);
128 error = TRUE;
129 break;
130 }
131
132 /* find the variable */
133 var = SCIPfindVar(scip, varname);
134 if( var == NULL )
135 {
136 if( !unknownvariablemessage )
137 {
138 SCIPwarningMessage(scip, "unknown variable <%s> in line %d of solution file <%s>\n", varname, lineno, filename);
139 SCIPwarningMessage(scip, " (further unknown variables are ignored)\n");
140 unknownvariablemessage = TRUE;
141 }
142 continue;
143 }
144
145 /* cast the value */
146 if( SCIPstrncasecmp(valuestring, "inv", 3) == 0 )
147 continue;
148 else if( SCIPstrncasecmp(valuestring, "+inf", 4) == 0 || SCIPstrncasecmp(valuestring, "inf", 3) == 0 )
149 value = SCIPinfinity(scip);
150 else if( SCIPstrncasecmp(valuestring, "-inf", 4) == 0 )
151 value = -SCIPinfinity(scip);
152 else
153 {
154 /* coverity[secure_coding] */
155 nread = sscanf(valuestring, "%lf", &value);
156 if( nread != 1 )
157 {
158 SCIPerrorMessage("invalid solution value <%s> for variable <%s> in line %d of solution file <%s>\n",
159 valuestring, varname, lineno, filename);
160 error = TRUE;
161 break;
162 }
163 }
164
165 /* fix the variable */
166 retcode = SCIPfixVar(scip, var, value, &infeasible, &fixed);
167 if( retcode != SCIP_OKAY )
168 {
169 SCIPerrorMessage("Error fixing variable <%s> to value %.15g in line %d of bounds file <%s>\n",
170 varname, value, lineno, filename);
171 error = TRUE;
172 break;
173 }
174 if( infeasible )
175 {
176 SCIPerrorMessage("infeasible solution value of <%s>[%.15g,%.15g] to %.15g in line %d of solution file <%s>\n",
177 varname, SCIPvarGetLbGlobal(var), SCIPvarGetUbGlobal(var), value, lineno, filename);
178 error = TRUE;
179 break;
180 }
181 if( fixed )
182 nfixed++;
183 }
184
185 /* close input file */
186 SCIPfclose(file);
187
188 /* display result */
189 SCIPverbMessage(scip, SCIP_VERBLEVEL_NORMAL, NULL, "fixed %d variables from solution file <%s>\n", nfixed, filename);
190
191 if( error )
192 return SCIP_READERROR;
193 else
194 return SCIP_OKAY;
195}
196
197/*
198 * Callback methods of reader
199 */
200
201/** copy method for reader plugins (called when SCIP copies plugins) */
202static
204{ /*lint --e{715}*/
205 assert(scip != NULL);
206 assert(reader != NULL);
207 assert(strcmp(SCIPreaderGetName(reader), READER_NAME) == 0);
208
209 /* call inclusion method of reader */
211
212 return SCIP_OKAY;
213}
214
215/** problem reading method of reader */
216static
218{ /*lint --e{715}*/
219 assert(reader != NULL);
220 assert(strcmp(SCIPreaderGetName(reader), READER_NAME) == 0);
221 assert(result != NULL);
222
223 *result = SCIP_DIDNOTRUN;
224
226 {
227 SCIPerrorMessage("reading of fixing file is only possible after a problem was created\n");
228 return SCIP_READERROR;
229 }
230
231 /* free transformed problem, s.t. fixings are applied to the original problem */
233
234 /* read (partial) solution from fixing file */
235 SCIP_CALL( readSol(scip, filename) );
236
237 *result = SCIP_SUCCESS;
238
239 return SCIP_OKAY;
240}
241
242/*
243 * fix file reader specific interface methods
244 */
245
246/** includes the fix file reader in SCIP */
248 SCIP* scip /**< SCIP data structure */
249 )
250{
251 SCIP_READER* reader;
252
253 /* include reader */
255
256 /* set non fundamental callbacks via setter functions */
257 SCIP_CALL( SCIPsetReaderCopy(scip, reader, readerCopyFix) );
258 SCIP_CALL( SCIPsetReaderRead(scip, reader, readerReadFix) );
259
260 return SCIP_OKAY;
261}
#define NULL
Definition: def.h:266
#define SCIP_MAXSTRLEN
Definition: def.h:287
#define SCIP_Bool
Definition: def.h:91
#define SCIP_Real
Definition: def.h:172
#define TRUE
Definition: def.h:93
#define FALSE
Definition: def.h:94
#define SCIP_CALL(x)
Definition: def.h:373
SCIP_FILE * SCIPfopen(const char *path, const char *mode)
Definition: fileio.c:153
int SCIPfeof(SCIP_FILE *stream)
Definition: fileio.c:227
int SCIPfclose(SCIP_FILE *fp)
Definition: fileio.c:232
char * SCIPfgets(char *s, int size, SCIP_FILE *stream)
Definition: fileio.c:200
SCIP_RETCODE SCIPincludeReaderFix(SCIP *scip)
Definition: reader_fix.c:247
SCIP_STAGE SCIPgetStage(SCIP *scip)
Definition: scip_general.c:390
SCIP_VAR * SCIPfindVar(SCIP *scip, const char *name)
Definition: scip_prob.c:2685
void SCIPverbMessage(SCIP *scip, SCIP_VERBLEVEL msgverblevel, FILE *file, const char *formatstr,...)
Definition: scip_message.c:225
void SCIPwarningMessage(SCIP *scip, const char *formatstr,...)
Definition: scip_message.c:120
SCIP_RETCODE SCIPincludeReaderBasic(SCIP *scip, SCIP_READER **readerptr, const char *name, const char *desc, const char *extension, SCIP_READERDATA *readerdata)
Definition: scip_reader.c:109
SCIP_RETCODE SCIPsetReaderCopy(SCIP *scip, SCIP_READER *reader, SCIP_DECL_READERCOPY((*readercopy)))
Definition: scip_reader.c:147
const char * SCIPreaderGetName(SCIP_READER *reader)
Definition: reader.c:557
SCIP_RETCODE SCIPsetReaderRead(SCIP *scip, SCIP_READER *reader, SCIP_DECL_READERREAD((*readerread)))
Definition: scip_reader.c:195
SCIP_RETCODE SCIPfreeTransform(SCIP *scip)
Definition: scip_solve.c:3350
SCIP_Real SCIPinfinity(SCIP *scip)
SCIP_Real SCIPvarGetUbGlobal(SCIP_VAR *var)
Definition: var.c:18087
SCIP_Real SCIPvarGetLbGlobal(SCIP_VAR *var)
Definition: var.c:18077
SCIP_RETCODE SCIPfixVar(SCIP *scip, SCIP_VAR *var, SCIP_Real fixedval, SCIP_Bool *infeasible, SCIP_Bool *fixed)
Definition: scip_var.c:8399
int SCIPsnprintf(char *t, int len, const char *s,...)
Definition: misc.c:10880
void SCIPprintSysError(const char *message)
Definition: misc.c:10772
int SCIPstrncasecmp(const char *s1, const char *s2, int length)
Definition: misc.c:10929
wrapper functions to map file i/o to standard or zlib file i/o
struct SCIP_File SCIP_FILE
Definition: pub_fileio.h:43
public methods for message output
#define SCIPerrorMessage
Definition: pub_message.h:64
public data structures and miscellaneous methods
public methods for input file readers
public methods for problem variables
static SCIP_RETCODE readSol(SCIP *scip, const char *filename)
Definition: reader_fix.c:71
#define READER_DESC
Definition: reader_fix.c:61
#define READER_EXTENSION
Definition: reader_fix.c:62
static SCIP_DECL_READERREAD(readerReadFix)
Definition: reader_fix.c:217
#define READER_NAME
Definition: reader_fix.c:60
static SCIP_DECL_READERCOPY(readerCopyFix)
Definition: reader_fix.c:203
file reader for variable fixings
general public methods
public methods for message handling
public methods for numerical tolerances
public methods for global and local (sub)problems
public methods for reader plugins
public solving methods
public methods for SCIP variables
@ SCIP_VERBLEVEL_NORMAL
Definition: type_message.h:55
@ SCIP_DIDNOTRUN
Definition: type_result.h:42
@ SCIP_SUCCESS
Definition: type_result.h:58
@ SCIP_NOFILE
Definition: type_retcode.h:47
@ SCIP_READERROR
Definition: type_retcode.h:45
@ SCIP_OKAY
Definition: type_retcode.h:42
enum SCIP_Retcode SCIP_RETCODE
Definition: type_retcode.h:63
@ SCIP_STAGE_PROBLEM
Definition: type_set.h:45