Scippy

SCIP

Solving Constraint Integer Programs

event_bestsol.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 event_bestsol.c
26 * @brief eventhdlr to print the best solution value
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 "message_pb.h"
35#include "event_bestsol.h"
36
37#include <string.h>
38
39#define EVENTHDLR_NAME "bestsol"
40#define EVENTHDLR_DESC "event handler for best solutions found"
41
42
43/** initialization method of event handler (called after problem was transformed) */
44static
45SCIP_DECL_EVENTINIT(eventInitBestsol)
46{ /*lint --e{715}*/
47 assert(scip != NULL);
48 assert(eventhdlr != NULL);
49 assert(strcmp(SCIPeventhdlrGetName(eventhdlr), EVENTHDLR_NAME) == 0);
50
52
53 return SCIP_OKAY;
54}
55
56/** deinitialization method of event handler (called before transformed problem is freed) */
57static
58SCIP_DECL_EVENTEXIT(eventExitBestsol)
59{ /*lint --e{715}*/
60 assert(scip != NULL);
61 assert(eventhdlr != NULL);
62 assert(strcmp(SCIPeventhdlrGetName(eventhdlr), EVENTHDLR_NAME) == 0);
63
65
66 return SCIP_OKAY;
67}
68
69/** frees specific event data */
70static
71SCIP_DECL_EVENTDELETE(eventDeleteBestsol)
72{ /*lint --e{715}*/
73 assert(scip != NULL);
74 assert(eventhdlr != NULL);
75 assert(strcmp(SCIPeventhdlrGetName(eventhdlr), EVENTHDLR_NAME) == 0);
76 assert(eventdata != NULL);
77 assert(*eventdata != NULL);
78
79 SCIPfreeMemory(scip, eventdata);
80
81 return SCIP_OKAY;
82}
83
84/** execution method of event handler */
85static
86SCIP_DECL_EVENTEXEC(eventExecBestsol)
87{ /*lint --e{715}*/
88 SCIP_MESSAGEHDLR* messagehdlr;
89 SCIP_MESSAGEHDLRDATA* messagehdlrdata;
90 SCIP_Real solval;
91 SCIP_Bool quiet;
92 SCIP_Bool comment;
93
94 assert(scip != NULL);
95 assert(eventhdlr != NULL);
96 assert(strcmp(SCIPeventhdlrGetName(eventhdlr), EVENTHDLR_NAME) == 0);
97 assert(event != NULL);
99
100 SCIPdebugMessage("exec method of best solution event of pbsolver\n");
101
102 /* get best solution value */
104 assert(!SCIPisHugeValue(scip, ABS(solval)));
105
106 messagehdlr = SCIPgetMessagehdlr(scip);
107 assert(messagehdlr != NULL);
108 messagehdlrdata = SCIPmessagehdlrGetData(messagehdlr);
109 assert(messagehdlrdata != NULL);
110
111 /* store old quiet and comment value of message handler */
112 quiet = SCIPmessagehdlrIsQuiet(messagehdlr);
113 comment = messagehdlrdata->comment;
114
115 /* turn on message handler */
116 SCIPmessagehdlrSetQuiet(messagehdlr, FALSE);
117 messagehdlrdata->comment = FALSE;
118
119 /* print best solution value */
120 SCIPinfoMessage(scip, NULL, "o %.0lf\n", -solval != 0.0 ? solval : 0.0); /*lint !e777*/
121
122 /* reset old values of message handler data */
123 SCIPmessagehdlrSetQuiet(messagehdlr, quiet);
124 messagehdlrdata->comment = comment;
125
126 return SCIP_OKAY;
127}
128
129/** creates event handler for best solution found */
131 SCIP* scip /**< SCIP data structure */
132 )
133{
134 /* create event handler for events on best solutions */
136 NULL, NULL, eventInitBestsol, eventExitBestsol, NULL, NULL, eventDeleteBestsol, eventExecBestsol, NULL) );
137
138 return SCIP_OKAY;
139}
static SCIP_DECL_EVENTDELETE(eventDeleteBestsol)
Definition: event_bestsol.c:71
SCIP_RETCODE SCIPcreateEventHdlrBestsol(SCIP *scip)
static SCIP_DECL_EVENTINIT(eventInitBestsol)
Definition: event_bestsol.c:45
static SCIP_DECL_EVENTEXIT(eventExitBestsol)
Definition: event_bestsol.c:58
static SCIP_DECL_EVENTEXEC(eventExecBestsol)
Definition: event_bestsol.c:86
#define EVENTHDLR_DESC
Definition: event_bestsol.c:40
#define EVENTHDLR_NAME
Definition: event_bestsol.c:39
#define NULL
Definition: def.h:248
#define SCIP_Bool
Definition: def.h:91
#define SCIP_Real
Definition: def.h:156
#define ABS(x)
Definition: def.h:216
#define FALSE
Definition: def.h:94
#define SCIP_CALL(x)
Definition: def.h:355
eventhdlr for best solution found
void SCIPinfoMessage(SCIP *scip, FILE *file, const char *formatstr,...)
Definition: scip_message.c:208
SCIP_MESSAGEHDLR * SCIPgetMessagehdlr(SCIP *scip)
Definition: scip_message.c:88
SCIP_RETCODE SCIPincludeEventhdlr(SCIP *scip, const char *name, const char *desc, SCIP_DECL_EVENTCOPY((*eventcopy)), SCIP_DECL_EVENTFREE((*eventfree)), SCIP_DECL_EVENTINIT((*eventinit)), SCIP_DECL_EVENTEXIT((*eventexit)), SCIP_DECL_EVENTINITSOL((*eventinitsol)), SCIP_DECL_EVENTEXITSOL((*eventexitsol)), SCIP_DECL_EVENTDELETE((*eventdelete)), SCIP_DECL_EVENTEXEC((*eventexec)), SCIP_EVENTHDLRDATA *eventhdlrdata)
Definition: scip_event.c:66
const char * SCIPeventhdlrGetName(SCIP_EVENTHDLR *eventhdlr)
Definition: event.c:396
SCIP_EVENTTYPE SCIPeventGetType(SCIP_EVENT *event)
Definition: event.c:1194
SCIP_RETCODE SCIPcatchEvent(SCIP *scip, SCIP_EVENTTYPE eventtype, SCIP_EVENTHDLR *eventhdlr, SCIP_EVENTDATA *eventdata, int *filterpos)
Definition: scip_event.c:293
SCIP_RETCODE SCIPdropEvent(SCIP *scip, SCIP_EVENTTYPE eventtype, SCIP_EVENTHDLR *eventhdlr, SCIP_EVENTDATA *eventdata, int filterpos)
Definition: scip_event.c:333
#define SCIPfreeMemory(scip, ptr)
Definition: scip_mem.h:78
SCIP_SOL * SCIPgetBestSol(SCIP *scip)
Definition: scip_sol.c:2981
SCIP_Real SCIPgetSolOrigObj(SCIP *scip, SCIP_SOL *sol)
Definition: scip_sol.c:1892
SCIP_Bool SCIPisHugeValue(SCIP *scip, SCIP_Real val)
SCIP_Real SCIPround(SCIP *scip, SCIP_Real val)
SCIP_MESSAGEHDLRDATA * SCIPmessagehdlrGetData(SCIP_MESSAGEHDLR *messagehdlr)
Definition: message.c:887
void SCIPmessagehdlrSetQuiet(SCIP_MESSAGEHDLR *messagehdlr, SCIP_Bool quiet)
Definition: message.c:411
SCIP_Bool SCIPmessagehdlrIsQuiet(SCIP_MESSAGEHDLR *messagehdlr)
Definition: message.c:910
messagehdlr for the Pseudo-Boolean output format
#define SCIPdebugMessage
Definition: pub_message.h:96
#define SCIP_EVENTTYPE_BESTSOLFOUND
Definition: type_event.h:106
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