SCIP Doxygen Documentation
Loading...
Searching...
No Matches
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-2026 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
58
59#define READER_NAME "fixreader"
60#define READER_DESC "file reader for variable fixings"
61#define READER_EXTENSION "fix"
62
63
64/*
65 * local methods
66 */
67
68/** reads the given solution file */
69static
71 SCIP* scip, /**< SCIP data structure */
72 const char* filename /**< name of the input file */
73 )
74{
75 SCIP_RETCODE retcode;
76 SCIP_FILE* file;
77 SCIP_Bool error;
78 SCIP_Bool unknownvariablemessage;
79 int lineno;
80 int nfixed;
81
82 assert(scip != NULL);
83 assert(filename != NULL);
84
85 /* open input file */
86 file = SCIPfopen(filename, "r");
87 if( file == NULL )
88 {
89 SCIPerrorMessage("cannot open file <%s> for reading\n", filename);
90 SCIPprintSysError(filename);
91 return SCIP_NOFILE;
92 }
93
94 /* read the file */
95 error = FALSE;
96 unknownvariablemessage = FALSE;
97 lineno = 0;
98 nfixed = 0;
99 while( !SCIPfeof(file) && !error )
100 {
101 char buffer[SCIP_MAXSTRLEN];
102 char varname[SCIP_MAXSTRLEN];
103 char valuestring[SCIP_MAXSTRLEN];
104 char objstring[SCIP_MAXSTRLEN];
105 char format[SCIP_MAXSTRLEN];
106 SCIP_VAR* var;
107 SCIP_Real value;
108 SCIP_Bool infeasible;
109 SCIP_Bool fixed;
110 int nread;
111
112 /* get next line */
113 if( SCIPfgets(buffer, (int) sizeof(buffer), file) == NULL )
114 break;
115 lineno++;
116
117 /* the lines "solution status: ..." and "objective value: ..." may preceed the solution information */
118 if( SCIPstrncasecmp(buffer, "solution status:", 16) == 0 || SCIPstrncasecmp(buffer, "objective value:", 16) == 0 )
119 continue;
120
121 /* parse the line */
122 (void) SCIPsnprintf(format, SCIP_MAXSTRLEN, "%%%ds %%%ds %%%ds\n", SCIP_MAXSTRLEN, SCIP_MAXSTRLEN, SCIP_MAXSTRLEN);
123 nread = sscanf(buffer, format, varname, valuestring, objstring);
124 if( nread < 2 )
125 {
126 SCIPerrorMessage("invalid input line %d in solution file <%s>: <%s>\n", lineno, filename, buffer);
127 error = TRUE;
128 break;
129 }
130
131 /* find the variable */
132 var = SCIPfindVar(scip, varname);
133 if( var == NULL )
134 {
135 if( !unknownvariablemessage )
136 {
137 SCIPwarningMessage(scip, "unknown variable <%s> in line %d of solution file <%s>\n", varname, lineno, filename);
138 SCIPwarningMessage(scip, " (further unknown variables are ignored)\n");
139 unknownvariablemessage = TRUE;
140 }
141 continue;
142 }
143
144 /* cast the value */
145 if( SCIPstrncasecmp(valuestring, "inv", 3) == 0 )
146 continue;
147 else if( SCIPstrncasecmp(valuestring, "+inf", 4) == 0 || SCIPstrncasecmp(valuestring, "inf", 3) == 0 )
148 value = SCIPinfinity(scip);
149 else if( SCIPstrncasecmp(valuestring, "-inf", 4) == 0 )
150 value = -SCIPinfinity(scip);
151 else
152 {
153 /* coverity[secure_coding] */
154 nread = sscanf(valuestring, "%lf", &value);
155 if( nread != 1 )
156 {
157 SCIPerrorMessage("invalid solution value <%s> for variable <%s> in line %d of solution file <%s>\n",
158 valuestring, varname, lineno, filename);
159 error = TRUE;
160 break;
161 }
162 }
163
164 /* fix the variable */
165 retcode = SCIPfixVar(scip, var, value, &infeasible, &fixed);
166 if( retcode != SCIP_OKAY )
167 {
168 SCIPerrorMessage("Error fixing variable <%s> to value %.15g in line %d of bounds file <%s>\n",
169 varname, value, lineno, filename);
170 error = TRUE;
171 break;
172 }
173 if( infeasible )
174 {
175 SCIPerrorMessage("infeasible solution value of <%s>[%.15g,%.15g] to %.15g in line %d of solution file <%s>\n",
176 varname, SCIPvarGetLbGlobal(var), SCIPvarGetUbGlobal(var), value, lineno, filename);
177 error = TRUE;
178 break;
179 }
180 if( fixed )
181 nfixed++;
182 }
183
184 /* close input file */
185 SCIPfclose(file);
186
187 /* display result */
188 SCIPverbMessage(scip, SCIP_VERBLEVEL_NORMAL, NULL, "fixed %d variables from solution file <%s>\n", nfixed, filename);
189
190 if( error )
191 return SCIP_READERROR;
192 else
193 return SCIP_OKAY;
194}
195
196/*
197 * Callback methods of reader
198 */
199
200/** copy method for reader plugins (called when SCIP copies plugins) */
201static
203{ /*lint --e{715}*/
204 assert(scip != NULL);
205 assert(reader != NULL);
206
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(result != NULL);
221
223
225
227 {
228 SCIPerrorMessage("reading of fixing file is only possible after a problem was created\n");
229 return SCIP_READERROR;
230 }
231
232 /* free transformed problem, s.t. fixings are applied to the original problem */
234
235 /* read (partial) solution from fixing file */
236 SCIP_CALL( readSol(scip, filename) );
237
239
240 return SCIP_OKAY;
241}
242
243/*
244 * fix file reader specific interface methods
245 */
246
247/** includes the fix file reader in SCIP */
249 SCIP* scip /**< SCIP data structure */
250 )
251{
252 SCIP_READER* reader;
253
254 /* include reader */
256
257 /* set non fundamental callbacks via setter functions */
258 SCIP_CALL( SCIPsetReaderCopy(scip, reader, readerCopyFix) );
259 SCIP_CALL( SCIPsetReaderRead(scip, reader, readerReadFix) );
260
261 return SCIP_OKAY;
262}
#define NULL
Definition def.h:257
#define SCIP_MAXSTRLEN
Definition def.h:278
#define SCIP_Bool
Definition def.h:100
#define SCIP_STRINGEQ(name, reference, retcode)
Definition def.h:454
#define SCIP_Real
Definition def.h:165
#define TRUE
Definition def.h:102
#define FALSE
Definition def.h:103
#define SCIP_CALL(x)
Definition def.h:364
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:248
SCIP_STAGE SCIPgetStage(SCIP *scip)
SCIP_VAR * SCIPfindVar(SCIP *scip, const char *name)
Definition scip_prob.c:3189
void SCIPverbMessage(SCIP *scip, SCIP_VERBLEVEL msgverblevel, FILE *file, const char *formatstr,...)
void SCIPwarningMessage(SCIP *scip, const char *formatstr,...)
SCIP_RETCODE SCIPsetReaderCopy(SCIP *scip, SCIP_READER *reader,)
SCIP_RETCODE SCIPincludeReaderBasic(SCIP *scip, SCIP_READER **readerptr, const char *name, const char *desc, const char *extension, SCIP_READERDATA *readerdata)
SCIP_RETCODE SCIPsetReaderRead(SCIP *scip, SCIP_READER *reader,)
const char * SCIPreaderGetName(SCIP_READER *reader)
Definition reader.c:700
SCIP_RETCODE SCIPfreeTransform(SCIP *scip)
SCIP_Real SCIPinfinity(SCIP *scip)
SCIP_Real SCIPvarGetUbGlobal(SCIP_VAR *var)
Definition var.c:24174
SCIP_Real SCIPvarGetLbGlobal(SCIP_VAR *var)
Definition var.c:24152
SCIP_RETCODE SCIPfixVar(SCIP *scip, SCIP_VAR *var, SCIP_Real fixedval, SCIP_Bool *infeasible, SCIP_Bool *fixed)
Definition scip_var.c:10318
int SCIPsnprintf(char *t, int len, const char *s,...)
Definition misc.c:10827
void SCIPprintSysError(const char *message)
Definition misc.c:10719
int SCIPstrncasecmp(const char *s1, const char *s2, int length)
Definition misc.c:10876
return SCIP_OKAY
assert(minobj< SCIPgetCutoffbound(scip))
SCIP_VAR * var
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
#define READER_DESC
Definition reader_bnd.c:62
#define READER_EXTENSION
Definition reader_bnd.c:63
#define READER_NAME
Definition reader_bnd.c:61
static SCIP_RETCODE readSol(SCIP *scip, const char *filename)
Definition reader_fix.c:70
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
struct SCIP_Reader SCIP_READER
Definition type_reader.h:53
#define SCIP_DECL_READERREAD(x)
Definition type_reader.h:88
#define SCIP_DECL_READERCOPY(x)
Definition type_reader.h:63
@ SCIP_DIDNOTRUN
Definition type_result.h:42
@ SCIP_SUCCESS
Definition type_result.h:58
@ SCIP_NOFILE
@ SCIP_READERROR
@ SCIP_INVALIDCALL
enum SCIP_Retcode SCIP_RETCODE
struct Scip SCIP
Definition type_scip.h:39
@ SCIP_STAGE_PROBLEM
Definition type_set.h:45
struct SCIP_Var SCIP_VAR
Definition type_var.h:166