Scippy

SCIP

Solving Constraint Integer Programs

message_pb.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 message_pb.c
26 * @brief messagehdlr for the Pseudo-Boolean output format
27 * @author Alexander Hoen
28 * @author Gioni Mexi
29 * @author Dominik Kamp
30 */
31
32/*--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
33
34#include <stdio.h>
35#include <string.h>
36
37#include "scip/scip.h"
38#include "message_pb.h"
39
40
41static
43 SCIP_MESSAGEHDLR* messagehdlr, /**< message handler */
44 FILE* file, /**< file stream to print message into */
45 const char* initial, /**< initial to prepend or NULL for no initial */
46 const char* msg /**< message to print or NULL to flush only */
47 )
48{
49 if( msg != NULL && msg[0] != '\0' )
50 {
51 SCIP_MESSAGEHDLRDATA* messagehdlrdata = SCIPmessagehdlrGetData(messagehdlr);
52
53 if( messagehdlrdata == NULL || messagehdlrdata->comment )
54 fputs("c ", file);
55
56 if( initial != NULL )
57 fputs(initial, file);
58
59 fputs(msg, file);
60 }
61
62 fflush(file);
63}
64
65/** default error printing method which is used to print all occurring errors */
66static
67SCIP_DECL_ERRORPRINTING(messageErrorPbSolver)
68{ /*lint --e{715}*/
69 printMessage(NULL, stderr, NULL, msg);
70}
71
72/** warning message print method of default message handler */
73static
74SCIP_DECL_MESSAGEWARNING(messageWarningPbSolver)
75{ /*lint --e{715}*/
76 printMessage(messagehdlr, file, "WARNING: ", msg);
77}
78
79/** dialog message print method of default message handler */
80static
81SCIP_DECL_MESSAGEDIALOG(messageDialogPbSolver)
82{ /*lint --e{715}*/
83 printMessage(messagehdlr, file, NULL, msg);
84}
85
86/** info message print method of default message handler */
87static
88SCIP_DECL_MESSAGEINFO(messageInfoPbSolver)
89{ /*lint --e{715}*/
90 printMessage(messagehdlr, file, NULL, msg);
91}
92
93static
94SCIP_DECL_MESSAGEHDLRFREE(messagehdlrFreePbSolver)
95{ /*lint --e{715}*/
96 assert(messagehdlr != NULL);
97 SCIP_MESSAGEHDLRDATA* messagehdlrdata = SCIPmessagehdlrGetData(messagehdlr);
98 assert(messagehdlrdata != NULL);
99
100 BMSfreeMemory(&messagehdlrdata);
101
102 return SCIP_OKAY;
103}
104
105/** creates default pbsolver message handler */
107 SCIP_MESSAGEHDLR** messagehdlr, /**< pointer to message handler */
108 SCIP_Bool buffered, /**< should the output be buffered */
109 const char* filename, /**< name of log file or NULL for stdout */
110 SCIP_Bool quiet /**< should screen messages be suppressed? */
111 )
112{
113 SCIP_MESSAGEHDLRDATA* messagehdlrdata;
114
115 SCIP_ALLOC( BMSallocMemory(&messagehdlrdata) );
116
117 messagehdlrdata->comment = TRUE;
118
119 SCIP_CALL( SCIPmessagehdlrCreate(messagehdlr, buffered, filename, quiet, messageWarningPbSolver,
120 messageDialogPbSolver, messageInfoPbSolver, messagehdlrFreePbSolver, messagehdlrdata) );
121
122 SCIPmessageSetErrorPrinting(messageErrorPbSolver, NULL);
123
124 return SCIP_OKAY;
125}
126
127/** prints that the problem instance is unsupported */
129 SCIP* scip /**< SCIP data structure */
130 )
131{
133 assert(messagehdlr != NULL);
134 SCIP_MESSAGEHDLRDATA* messagehdlrdata = SCIPmessagehdlrGetData(messagehdlr);
135 assert(messagehdlrdata != NULL);
136 SCIP_Bool quiet = SCIPmessagehdlrIsQuiet(messagehdlr);
137
138 /* competition console log */
139 if( messagehdlrdata->comment )
140 {
141 /* turn on message handler and remove comment declaration */
142 SCIPmessagehdlrSetQuiet(messagehdlr, FALSE);
143 messagehdlrdata->comment = FALSE;
144
145 SCIPinfoMessage(scip, NULL, "s UNSUPPORTED\n");
146
147 /* reset old values of message handler data */
148 SCIPmessagehdlrSetQuiet(messagehdlr, quiet);
149 messagehdlrdata->comment = TRUE;
150 }
151
152 return SCIP_OKAY;
153}
154
155/** prints the best primal solution */
157 SCIP* scip /**< SCIP data structure */
158 )
159{
161 assert(messagehdlr != NULL);
162 SCIP_MESSAGEHDLRDATA* messagehdlrdata = SCIPmessagehdlrGetData(messagehdlr);
163 assert(messagehdlrdata != NULL);
164 SCIP_SOL* bestsol;
165 SCIP_VAR** vars;
166 SCIP_STATUS status;
167 SCIP_Bool quiet = SCIPmessagehdlrIsQuiet(messagehdlr);
168 SCIP_Bool hasobj;
169 SCIP_Bool feasible;
170 char* solutiontext;
171 int strlength;
172 int printlength;
173 int nvars;
174 int n;
175 int v;
176
177 /* enable message handler */
178 SCIPmessagehdlrSetQuiet(messagehdlr, FALSE);
179
180 /* competition console log */
181 if( messagehdlrdata->comment )
182 {
183 /* disable comment prefix */
184 messagehdlrdata->comment = FALSE;
185
186 status = SCIPgetStatus(scip);
187 if( status == SCIP_STATUS_INFEASIBLE )
188 SCIPinfoMessage(scip, NULL, "s UNSATISFIABLE\n");
189 else
190 {
191 feasible = FALSE;
192 bestsol = SCIPgetBestSol(scip);
193
194 if( bestsol != NULL )
195 {
196 /* check if solution is feasible in original problem */
197 SCIP_CALL( SCIPcheckSolOrig(scip, bestsol, &feasible, FALSE, FALSE) );
198
199 /* if solution is not feasible in original problem; post UNKNOWN */
200 if( !feasible )
201 {
202 SCIPinfoMessage(scip, NULL, "c internal error\n");
203 SCIPinfoMessage(scip, NULL, "s UNKNOWN\n");
204 }
205 else
206 {
207 vars = SCIPgetOrigVars(scip);
208 nvars = SCIPgetNOrigVars(scip);
209 assert(vars != NULL || nvars == 0);
210 hasobj = FALSE;
211
212 if( status == SCIP_STATUS_OPTIMAL )
213 {
214 for( v = 0; v < nvars; ++v )
215 {
216 if( !SCIPisZero(scip, SCIPvarGetObj(vars[v])) )
217 {
218 hasobj = TRUE;
219 break;
220 }
221 }
222 }
223
224 if( !hasobj )
225 SCIPinfoMessage(scip, NULL, "s SATISFIABLE\n");
226 else
227 SCIPinfoMessage(scip, NULL, "s OPTIMUM FOUND\n");
228
229 strlength = 2 * SCIP_MAXSTRLEN;
230 printlength = SCIP_MAXSTRLEN / 8;
231 n = 0;
232 v = 0;
233 SCIP_CALL( SCIPallocBufferArray(scip, &solutiontext, strlength) );
234
235 while( v < nvars )
236 {
237 int printed = 0;
238
239 assert(SCIPvarGetStatus(vars[v]) == SCIP_VARSTATUS_ORIGINAL);
240
241 if( strstr(SCIPvarGetName(vars[v]), "andresultant_") == NULL
242 && strstr(SCIPvarGetName(vars[v]), "indslack_") == NULL
243 && strstr(SCIPvarGetName(vars[v]), "indicatorvar_") == NULL )
244 {
245 printed = SCIPsnprintf(solutiontext + n, strlength,
246 SCIPgetSolVal(scip, bestsol, vars[v]) > 0.5 ? " %s" : " -%s", SCIPvarGetName(vars[v]));
247 n += printed;
248 strlength -= printed;
249 }
250
251 if( n >= printlength || ( n >= 1 && v + 1 >= nvars ) )
252 {
253 if( strlength >= 1 )
254 {
255 strlength += n;
256 n = 0;
257 SCIPinfoMessage(scip, NULL, "v%s\n", solutiontext);
258 }
259 else
260 {
261 strlength += printed + SCIP_MAXSTRLEN;
262 n -= printed;
263 SCIP_CALL( SCIPreallocBufferArray(scip, &solutiontext, n + strlength) );
264
265 continue;
266 }
267 }
268 assert(strlength >= 1);
269
270 ++v;
271 }
272
273 SCIPfreeBufferArray(scip, &solutiontext);
274 }
275 }
276 else
277 SCIPinfoMessage(scip, NULL, "s UNKNOWN\n");
278 }
279
280 /* enable comment prefix */
281 messagehdlrdata->comment = TRUE;
282 }
283 /* default console log */
284 else
285 {
287 }
288
289 /* reset message handler */
290 SCIPmessagehdlrSetQuiet(messagehdlr, quiet);
291
292 return SCIP_OKAY;
293}
#define NULL
Definition: def.h:248
#define SCIP_MAXSTRLEN
Definition: def.h:269
#define SCIP_Bool
Definition: def.h:91
#define SCIP_ALLOC(x)
Definition: def.h:366
#define TRUE
Definition: def.h:93
#define FALSE
Definition: def.h:94
#define SCIP_CALL(x)
Definition: def.h:355
SCIP_STATUS SCIPgetStatus(SCIP *scip)
Definition: scip_general.c:562
SCIP_VAR ** SCIPgetOrigVars(SCIP *scip)
Definition: scip_prob.c:2811
int SCIPgetNOrigVars(SCIP *scip)
Definition: scip_prob.c:2838
void SCIPinfoMessage(SCIP *scip, FILE *file, const char *formatstr,...)
Definition: scip_message.c:208
SCIP_MESSAGEHDLR * SCIPgetMessagehdlr(SCIP *scip)
Definition: scip_message.c:88
#define SCIPallocBufferArray(scip, ptr, num)
Definition: scip_mem.h:124
#define SCIPreallocBufferArray(scip, ptr, num)
Definition: scip_mem.h:128
#define SCIPfreeBufferArray(scip, ptr)
Definition: scip_mem.h:136
SCIP_RETCODE SCIPcheckSolOrig(SCIP *scip, SCIP_SOL *sol, SCIP_Bool *feasible, SCIP_Bool printreason, SCIP_Bool completely)
Definition: scip_sol.c:4380
SCIP_SOL * SCIPgetBestSol(SCIP *scip)
Definition: scip_sol.c:2981
SCIP_RETCODE SCIPprintBestSol(SCIP *scip, FILE *file, SCIP_Bool printzeros)
Definition: scip_sol.c:3047
SCIP_Real SCIPgetSolVal(SCIP *scip, SCIP_SOL *sol, SCIP_VAR *var)
Definition: scip_sol.c:1765
SCIP_Bool SCIPisZero(SCIP *scip, SCIP_Real val)
SCIP_VARSTATUS SCIPvarGetStatus(SCIP_VAR *var)
Definition: var.c:23386
SCIP_Real SCIPvarGetObj(SCIP_VAR *var)
Definition: var.c:23900
const char * SCIPvarGetName(SCIP_VAR *var)
Definition: var.c:23267
int SCIPsnprintf(char *t, int len, const char *s,...)
Definition: misc.c:10827
#define BMSfreeMemory(ptr)
Definition: memory.h:145
#define BMSallocMemory(ptr)
Definition: memory.h:118
SCIP_RETCODE SCIPmessagehdlrCreate(SCIP_MESSAGEHDLR **messagehdlr, SCIP_Bool bufferedoutput, const char *filename, SCIP_Bool quiet, SCIP_DECL_MESSAGEWARNING((*messagewarning)), SCIP_DECL_MESSAGEDIALOG((*messagedialog)), SCIP_DECL_MESSAGEINFO((*messageinfo)), SCIP_DECL_MESSAGEHDLRFREE((*messagehdlrfree)), SCIP_MESSAGEHDLRDATA *messagehdlrdata)
Definition: message.c:295
SCIP_MESSAGEHDLRDATA * SCIPmessagehdlrGetData(SCIP_MESSAGEHDLR *messagehdlr)
Definition: message.c:887
void SCIPmessageSetErrorPrinting(SCIP_DECL_ERRORPRINTING((*errorPrinting)), void *data)
Definition: message.c:851
void SCIPmessagehdlrSetQuiet(SCIP_MESSAGEHDLR *messagehdlr, SCIP_Bool quiet)
Definition: message.c:411
SCIP_Bool SCIPmessagehdlrIsQuiet(SCIP_MESSAGEHDLR *messagehdlr)
Definition: message.c:910
SCIP_RETCODE SCIPprintSolutionPbSolver(SCIP *scip)
Definition: message_pb.c:156
static SCIP_DECL_MESSAGEHDLRFREE(messagehdlrFreePbSolver)
Definition: message_pb.c:94
static SCIP_DECL_MESSAGEWARNING(messageWarningPbSolver)
Definition: message_pb.c:74
static SCIP_DECL_MESSAGEINFO(messageInfoPbSolver)
Definition: message_pb.c:88
static void printMessage(SCIP_MESSAGEHDLR *messagehdlr, FILE *file, const char *initial, const char *msg)
Definition: message_pb.c:42
SCIP_RETCODE SCIPcreateMessagehdlrPbSolver(SCIP_MESSAGEHDLR **messagehdlr, SCIP_Bool buffered, const char *filename, SCIP_Bool quiet)
Definition: message_pb.c:106
static SCIP_DECL_MESSAGEDIALOG(messageDialogPbSolver)
Definition: message_pb.c:81
SCIP_RETCODE SCIPprintUnsupportedPbSolver(SCIP *scip)
Definition: message_pb.c:128
static SCIP_DECL_ERRORPRINTING(messageErrorPbSolver)
Definition: message_pb.c:67
messagehdlr for the Pseudo-Boolean output format
SCIP callable library.
struct SCIP_MessagehdlrData SCIP_MESSAGEHDLRDATA
Definition: type_message.h:67
@ SCIP_OKAY
Definition: type_retcode.h:42
enum SCIP_Retcode SCIP_RETCODE
Definition: type_retcode.h:63
@ SCIP_STATUS_OPTIMAL
Definition: type_stat.h:43
@ SCIP_STATUS_INFEASIBLE
Definition: type_stat.h:44
enum SCIP_Status SCIP_STATUS
Definition: type_stat.h:64
@ SCIP_VARSTATUS_ORIGINAL
Definition: type_var.h:51