Scippy

SCIP

Solving Constraint Integer Programs

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-2025 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#include <string.h>
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{
68 SCIP_SOL* sol;
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) );
96 assert(!SCIPsolIsExact(sol) || !SCIPsolIsPartial(sol));
97
98 if( !error )
99 {
100 /* add and free the solution */
102 {
103 SCIP_Bool completely;
104
105 assert(!partial);
106 assert(!SCIPsolIsPartial(sol));
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 */
132 SCIP_CALL( SCIPfreeSol(scip, &sol) );
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 assert(strcmp(SCIPreaderGetName(reader), READER_NAME) == 0);
149
150 /* call inclusion method of reader */
152
153 return SCIP_OKAY;
154}
155
156
157/** problem reading method of reader
158 *
159 * In order to determine the type of the file, we have to open it. Thus, it has to be opened
160 * twice. This might be removed, but is likely to not hurt the performance too much.
161 */
162static
164{ /*lint --e{715}*/
165 SCIP_FILE* file;
166 char buffer[SCIP_MAXSTRLEN];
167
168 assert(reader != NULL);
169 assert(strcmp(SCIPreaderGetName(reader), READER_NAME) == 0);
170 assert(result != NULL);
171
172 *result = SCIP_DIDNOTRUN;
173
175 {
176 SCIPerrorMessage("reading of solution file is only possible after a problem was created\n");
177 return SCIP_READERROR;
178 }
179
181 {
183 "primal solution from solution file <%s> was ignored - problem is already solved to optimality\n",
184 filename);
185 *result = SCIP_SUCCESS;
186 return SCIP_OKAY;
187 }
188
189 /* open input file in order to determine type */
190 file = SCIPfopen(filename, "r");
191 if( file == NULL )
192 {
193 SCIPerrorMessage("cannot open file <%s> for reading\n", filename);
194 SCIPprintSysError(filename);
195 return SCIP_NOFILE;
196 }
197
198 /* get next line */
199 if( SCIPfgets(buffer, (int) sizeof(buffer), file) == NULL )
200 {
201 SCIPerrorMessage("cannot parse file.\n");
202 SCIPfclose(file);
203 return SCIP_READERROR;
204 }
205 /* close file */
206 SCIPfclose(file);
207
208 /* decide whether it is xml */
209 if( SCIPstrAtStart(buffer, "<?xml", (size_t) 5) )
210 {
211 /* read XML solution and add it to the solution pool */
212 SCIP_CALL( readSol(scip, filename, TRUE) );
213 }
214 else
215 {
216 /* read the solution and add it to the solution pool */
217 SCIP_CALL( readSol(scip, filename, FALSE) );
218 }
219
220 *result = SCIP_SUCCESS;
221
222 return SCIP_OKAY;
223}
224
225
226/*
227 * sol file reader specific interface methods
228 */
229
230/** includes the sol file reader in SCIP */
232 SCIP* scip /**< SCIP data structure */
233 )
234{
235 SCIP_READER* reader;
236
237 /* include reader */
239
240 /* reader is safe to use in exact solving mode */
241 SCIPreaderMarkExact(reader);
242
243 /* set non fundamental callbacks via setter functions */
244 SCIP_CALL( SCIPsetReaderCopy(scip, reader, readerCopySol) );
245 SCIP_CALL( SCIPsetReaderRead(scip, reader, readerReadSol) );
246
247 return SCIP_OKAY;
248}
249
#define NULL
Definition: def.h:248
#define SCIP_MAXSTRLEN
Definition: def.h:269
#define SCIP_Bool
Definition: def.h:91
#define TRUE
Definition: def.h:93
#define FALSE
Definition: def.h:94
#define SCIP_CALL(x)
Definition: def.h:355
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:231
SCIP_Bool SCIPisTransformed(SCIP *scip)
Definition: scip_general.c:647
SCIP_STAGE SCIPgetStage(SCIP *scip)
Definition: scip_general.c:444
void SCIPverbMessage(SCIP *scip, SCIP_VERBLEVEL msgverblevel, FILE *file, const char *formatstr,...)
Definition: scip_message.c:225
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 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:680
SCIP_RETCODE SCIPsetReaderRead(SCIP *scip, SCIP_READER *reader, SCIP_DECL_READERREAD((*readerread)))
Definition: scip_reader.c:195
void SCIPreaderMarkExact(SCIP_READER *reader)
Definition: reader.c:670
SCIP_RETCODE SCIPcreateSol(SCIP *scip, SCIP_SOL **sol, SCIP_HEUR *heur)
Definition: scip_sol.c:516
SCIP_RETCODE SCIPreadSolFile(SCIP *scip, const char *filename, SCIP_SOL *sol, SCIP_Bool xml, SCIP_Bool *partial, SCIP_Bool *error)
Definition: scip_sol.c:3802
SCIP_RETCODE SCIPfreeSol(SCIP *scip, SCIP_SOL **sol)
Definition: scip_sol.c:1252
SCIP_RETCODE SCIPaddSolFree(SCIP *scip, SCIP_SOL **sol, SCIP_Bool *stored)
Definition: scip_sol.c:3909
SCIP_Bool SCIPsolIsPartial(SCIP_SOL *sol)
Definition: sol.c:4160
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:4109
SCIP_RETCODE SCIPcreateSolExact(SCIP *scip, SCIP_SOL **sol, SCIP_HEUR *heur)
Definition: scip_sol.c:566
SCIP_Bool SCIPsolIsExact(SCIP_SOL *sol)
Definition: sol.c:4150
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
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
static SCIP_DECL_READERREAD(readerReadSol)
Definition: reader_sol.c:163
#define READER_DESC
Definition: reader_sol.c:52
static SCIP_DECL_READERCOPY(readerCopySol)
Definition: reader_sol.c:144
#define READER_EXTENSION
Definition: reader_sol.c:53
static SCIP_RETCODE readSol(SCIP *scip, const char *fname, SCIP_Bool xml)
Definition: reader_sol.c:62
#define READER_NAME
Definition: reader_sol.c:51
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
Definition: type_message.h:60
@ 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
@ SCIP_STAGE_SOLVED
Definition: type_set.h:54