SCIP Doxygen Documentation
Loading...
Searching...
No Matches
reader_sol.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_sol.c
26 * @ingroup DEFPLUGINS_READER
27 * @brief file reader for primal solutions
28 * @author Tobias Achterberg
29 * @author Timo Berthold
30 * @author Marc Pfetsch
31 *
32 */
33
34/*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
35
36#include <ctype.h>
37#include "scip/pub_fileio.h"
38#include "scip/pub_message.h"
39#include "scip/pub_misc.h"
40#include "scip/pub_reader.h"
41#include "scip/pub_sol.h"
42#include "scip/reader_sol.h"
43#include "scip/scip_general.h"
44#include "scip/scip_exact.h"
45#include "scip/scip_message.h"
46#include "scip/scip_param.h"
47#include "scip/scip_reader.h"
48#include "scip/scip_sol.h"
49
50
51#define READER_NAME "solreader"
52#define READER_DESC "file reader for primal solutions"
53#define READER_EXTENSION "sol"
54
55
56/*
57 * Local methods of reader
58 */
59
60/** reads a given SCIP solution file, problem has to be transformed in advance */
61static
63 SCIP* scip, /**< SCIP data structure */
64 const char* fname, /**< name of the input file */
65 SCIP_Bool xml /**< true, iff the given file is XML */
66 )
67{
69 SCIP_Bool error;
70 SCIP_Bool partial;
71 SCIP_Bool stored;
72 SCIP_Bool usevartable;
73
74 assert(scip != NULL);
75 assert(fname != NULL);
76
77 SCIP_CALL( SCIPgetBoolParam(scip, "misc/usevartable", &usevartable) );
78
79 if( !usevartable )
80 {
81 SCIPerrorMessage("Cannot read solution file if vartable is disabled. Make sure parameter 'misc/usevartable' is set to TRUE.\n");
82 return SCIP_READERROR;
83 }
84
85 /* create zero solution */
86 if( SCIPisExact(scip) )
87 {
89 }
90 else
91 {
93 }
94
95 SCIP_CALL( SCIPreadSolFile(scip, fname, sol, xml, &partial, &error) );
97
98 if( !error )
99 {
100 /* add and free the solution */
102 {
103 SCIP_Bool completely;
104
105 assert(!partial);
107
108 /* use display/allviols to decide whether to print all violations or just the first one */
109 SCIP_CALL( SCIPgetBoolParam(scip, "display/allviols", &completely) );
110
111 SCIP_CALL( SCIPtrySolFree(scip, &sol, TRUE, completely, TRUE, TRUE, TRUE, &stored) );
112
113 /* display result */
114 SCIPverbMessage(scip, SCIP_VERBLEVEL_NORMAL, NULL, "primal solution from solution file <%s> was %s\n",
115 fname, stored ? "accepted" : "rejected - solution is infeasible or objective too poor");
116 }
117 else
118 {
119 /* add primal solution to solution candidate storage, frees the solution afterwards */
120 SCIP_CALL( SCIPaddSolFree(scip, &sol, &stored) );
121
122 /* display result */
123 SCIPverbMessage(scip, SCIP_VERBLEVEL_NORMAL, NULL, "%sprimal solution from solution file <%s> was %s\n",
124 partial ? "partial " : "", fname, stored ? "accepted as candidate, will be checked when solving starts" : "rejected - solution objective too poor");
125 }
126
127 return SCIP_OKAY;
128 }
129 else
130 {
131 /* free solution */
133
134 return SCIP_READERROR;
135 }
136}
137
138/*
139 * Callback methods of reader
140 */
141
142/** copy method for reader plugins (called when SCIP copies plugins) */
143static
145{ /*lint --e{715}*/
146 assert(scip != NULL);
147 assert(reader != NULL);
148
150
151 /* call inclusion method of reader */
153
154 return SCIP_OKAY;
155}
156
157
158/** problem reading method of reader
159 *
160 * In order to determine the type of the file, we have to open it. Thus, it has to be opened
161 * twice. This might be removed, but is likely to not hurt the performance too much.
162 */
163static
165{ /*lint --e{715}*/
166 SCIP_FILE* file;
167 char buffer[SCIP_MAXSTRLEN];
168
169 assert(reader != NULL);
170 assert(result != NULL);
171
173
175
177 {
178 SCIPerrorMessage("reading of solution file is only possible after a problem was created\n");
179 return SCIP_READERROR;
180 }
181
183 {
185 "primal solution from solution file <%s> was ignored - problem is already solved to optimality\n",
186 filename);
188 return SCIP_OKAY;
189 }
190
191 /* open input file in order to determine type */
192 file = SCIPfopen(filename, "r");
193 if( file == NULL )
194 {
195 SCIPerrorMessage("cannot open file <%s> for reading\n", filename);
196 SCIPprintSysError(filename);
197 return SCIP_NOFILE;
198 }
199
200 /* get next line */
201 if( SCIPfgets(buffer, (int) sizeof(buffer), file) == NULL )
202 {
203 SCIPerrorMessage("cannot parse file.\n");
204 SCIPfclose(file);
205 return SCIP_READERROR;
206 }
207 /* close file */
208 SCIPfclose(file);
209
210 /* decide whether it is xml */
211 if( SCIPstrAtStart(buffer, "<?xml", (size_t) 5) )
212 {
213 /* read XML solution and add it to the solution pool */
214 SCIP_CALL( readSol(scip, filename, TRUE) );
215 }
216 else
217 {
218 /* read the solution and add it to the solution pool */
219 SCIP_CALL( readSol(scip, filename, FALSE) );
220 }
221
223
224 return SCIP_OKAY;
225}
226
227
228/*
229 * sol file reader specific interface methods
230 */
231
232/** includes the sol file reader in SCIP */
234 SCIP* scip /**< SCIP data structure */
235 )
236{
237 SCIP_READER* reader;
238
239 /* include reader */
241
242 /* reader is safe to use in exact solving mode */
243 SCIPreaderMarkExact(reader);
244
245 /* set non fundamental callbacks via setter functions */
246 SCIP_CALL( SCIPsetReaderCopy(scip, reader, readerCopySol) );
247 SCIP_CALL( SCIPsetReaderRead(scip, reader, readerReadSol) );
248
249 return SCIP_OKAY;
250}
251
#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 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 SCIPfclose(SCIP_FILE *fp)
Definition fileio.c:232
char * SCIPfgets(char *s, int size, SCIP_FILE *stream)
Definition fileio.c:200
SCIP_RETCODE SCIPincludeReaderSol(SCIP *scip)
Definition reader_sol.c:233
SCIP_Bool SCIPisTransformed(SCIP *scip)
SCIP_STAGE SCIPgetStage(SCIP *scip)
void SCIPverbMessage(SCIP *scip, SCIP_VERBLEVEL msgverblevel, FILE *file, const char *formatstr,...)
SCIP_RETCODE SCIPgetBoolParam(SCIP *scip, const char *name, SCIP_Bool *value)
Definition scip_param.c:250
SCIP_Bool SCIPisExact(SCIP *scip)
Definition scip_exact.c:193
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
void SCIPreaderMarkExact(SCIP_READER *reader)
Definition reader.c:690
SCIP_RETCODE SCIPreadSolFile(SCIP *scip, const char *filename, SCIP_SOL *sol, SCIP_Bool xml, SCIP_Bool *partial, SCIP_Bool *error)
Definition scip_sol.c:3807
SCIP_RETCODE SCIPaddSolFree(SCIP *scip, SCIP_SOL **sol, SCIP_Bool *stored)
Definition scip_sol.c:3914
SCIP_Bool SCIPsolIsPartial(SCIP_SOL *sol)
Definition sol.c:4175
SCIP_RETCODE SCIPtrySolFree(SCIP *scip, SCIP_SOL **sol, SCIP_Bool printreason, SCIP_Bool completely, SCIP_Bool checkbounds, SCIP_Bool checkintegrality, SCIP_Bool checklprows, SCIP_Bool *stored)
Definition scip_sol.c:4114
SCIP_RETCODE SCIPcreateSolExact(SCIP *scip, SCIP_SOL **sol, SCIP_HEUR *heur)
Definition scip_sol.c:564
SCIP_Bool SCIPsolIsExact(SCIP_SOL *sol)
Definition sol.c:4165
void SCIPprintSysError(const char *message)
Definition misc.c:10719
SCIP_Bool SCIPstrAtStart(const char *s, const char *t, size_t tlen)
Definition misc.c:11364
return SCIP_OKAY
SCIPfreeSol(scip, &heurdata->sol))
SCIPcreateSol(scip, &heurdata->sol, heur))
static SCIP_SOL * sol
assert(minobj< SCIPgetCutoffbound(scip))
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 primal CIP solutions
#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 *fname, SCIP_Bool xml)
Definition reader_sol.c:62
file reader for primal solutions
public methods for exact solving
general public methods
public methods for message handling
public methods for SCIP parameter handling
public methods for reader plugins
public methods for solutions
@ 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
@ SCIP_STAGE_SOLVED
Definition type_set.h:54
struct SCIP_Sol SCIP_SOL
Definition type_sol.h:57