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-2020 Konrad-Zuse-Zentrum */
7 /* fuer Informationstechnik Berlin */
8 /* */
9 /* SCIP is distributed under the terms of the ZIB Academic License. */
10 /* */
11 /* You should have received a copy of the ZIB Academic License */
12 /* along with SCIP; see the file COPYING. If not visit scipopt.org. */
13 /* */
14 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
15 
16 /**@file STP/src/event_bestsol.c
17  * @brief eventhdlr for best solution found
18  * @author Gerald Gamrath
19  * @author Daniel Rehfeldt
20  *
21  * Event handler for printing DIMACS solution file.
22  */
23 
24 /*--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
25 
26 #include "event_bestsol.h"
27 #include "probdata_stp.h"
28 #include "grph.h"
29 
30 #include <string.h>
31 
32 #define EVENTHDLR_NAME "bestsol"
33 #define EVENTHDLR_DESC "event handler for best solutions found"
34 
35 /** copy method for event handler plugins (called when SCIP copies plugins) */
36 static
37 SCIP_DECL_EVENTCOPY(eventCopyBestsol)
38 { /*lint --e{715}*/
39  assert(scip != NULL);
40  assert(eventhdlr != NULL);
41  assert(strcmp(SCIPeventhdlrGetName(eventhdlr), EVENTHDLR_NAME) == 0);
42 
43  /* call inclusion method of event handler */
45 
46  return SCIP_OKAY;
47 }
48 
49 /** initialization method of event handler (called after problem was transformed) */
50 static
51 SCIP_DECL_EVENTINIT(eventInitBestsol)
52 { /*lint --e{715}*/
53  assert(scip != NULL);
54  assert(eventhdlr != NULL);
55  assert(strcmp(SCIPeventhdlrGetName(eventhdlr), EVENTHDLR_NAME) == 0);
56 
57  /* notify SCIP that your event handler wants to react on the event type best solution found */
59 
60  return SCIP_OKAY;
61 }
62 
63 /** deinitialization method of event handler (called before transformed problem is freed) */
64 static
65 SCIP_DECL_EVENTEXIT(eventExitBestsol)
66 { /*lint --e{715}*/
67  assert(scip != NULL);
68  assert(eventhdlr != NULL);
69  assert(strcmp(SCIPeventhdlrGetName(eventhdlr), EVENTHDLR_NAME) == 0);
70 
71  /* notify SCIP that your event handler wants to drop the event type best solution found */
73 
74  return SCIP_OKAY;
75 }
76 
77 /** execution method of event handler */
78 static
79 SCIP_DECL_EVENTEXEC(eventExecBestsol)
80 { /*lint --e{715}*/
81  SCIP_SOL* bestsol;
82  SCIP_Real solvalue;
83  SCIP_Real factor = 1.0;
84 
86  factor = -1.0;
87 
88  assert(eventhdlr != NULL);
89  assert(strcmp(SCIPeventhdlrGetName(eventhdlr), EVENTHDLR_NAME) == 0);
90  assert(event != NULL);
91  assert(scip != NULL);
93 
94  SCIPdebugMessage("exec method of event handler for best solution found\n");
95 
96  bestsol = SCIPgetBestSol(scip);
97  assert(bestsol != NULL);
98  solvalue = factor * SCIPgetSolOrigObj(scip, bestsol);
99 
100  SCIPprobdataWriteLogLine(scip, "Solution %.1f %16.9g\n", SCIPgetTotalTime(scip), solvalue);
101 
103 
104  return SCIP_OKAY;
105 }
106 
107 /** includes event handler for best solution found */
109  SCIP* scip /**< SCIP data structure */
110  )
111 {
112  SCIP_EVENTHDLRDATA* eventhdlrdata;
113  SCIP_EVENTHDLR* eventhdlr;
114  eventhdlrdata = NULL;
115 
116  eventhdlr = NULL;
117  /* create event handler for events on watched variables */
118  SCIP_CALL( SCIPincludeEventhdlrBasic(scip, &eventhdlr, EVENTHDLR_NAME, EVENTHDLR_DESC, eventExecBestsol, eventhdlrdata) );
119  assert(eventhdlr != NULL);
120 
121  SCIP_CALL( SCIPsetEventhdlrCopy(scip, eventhdlr, eventCopyBestsol) );
122  SCIP_CALL( SCIPsetEventhdlrInit(scip, eventhdlr, eventInitBestsol) );
123  SCIP_CALL( SCIPsetEventhdlrExit(scip, eventhdlr, eventExitBestsol) );
124 
125  return SCIP_OKAY;
126 }
static SCIP_DECL_EVENTEXIT(eventExitBestsol)
Definition: event_bestsol.c:65
eventhdlr for best solution found
struct SCIP_EventhdlrData SCIP_EVENTHDLRDATA
Definition: type_event.h:146
const char * SCIPeventhdlrGetName(SCIP_EVENTHDLR *eventhdlr)
Definition: event.c:315
SCIP_RETCODE SCIPprobdataWriteIntermediateSolution(SCIP *scip)
Problem data for stp problem.
enum SCIP_Retcode SCIP_RETCODE
Definition: type_retcode.h:54
#define SCIPdebugMessage
Definition: pub_message.h:87
SCIP_RETCODE SCIPsetEventhdlrCopy(SCIP *scip, SCIP_EVENTHDLR *eventhdlr, SCIP_DECL_EVENTCOPY((*eventcopy)))
Definition: scip_event.c:127
void SCIPprobdataWriteLogLine(SCIP *scip, const char *formatstr,...)
#define EVENTHDLR_NAME
Definition: event_bestsol.c:32
static SCIP_DECL_EVENTEXEC(eventExecBestsol)
Definition: event_bestsol.c:79
SCIP_RETCODE SCIPcatchEvent(SCIP *scip, SCIP_EVENTTYPE eventtype, SCIP_EVENTHDLR *eventhdlr, SCIP_EVENTDATA *eventdata, int *filterpos)
Definition: scip_event.c:277
#define NULL
Definition: lpi_spx1.cpp:155
#define SCIP_CALL(x)
Definition: def.h:364
SCIP_EVENTTYPE SCIPeventGetType(SCIP_EVENT *event)
Definition: event.c:1021
int SCIPprobdataGetType(SCIP *scip)
SCIP_Real SCIPgetTotalTime(SCIP *scip)
Definition: scip_timing.c:333
#define STP_MWCSP
Definition: grph.h:47
static SCIP_DECL_EVENTINIT(eventInitBestsol)
Definition: event_bestsol.c:51
static SCIP_DECL_EVENTCOPY(eventCopyBestsol)
Definition: event_bestsol.c:37
SCIP_RETCODE SCIPincludeEventhdlrBasic(SCIP *scip, SCIP_EVENTHDLR **eventhdlrptr, const char *name, const char *desc, SCIP_DECL_EVENTEXEC((*eventexec)), SCIP_EVENTHDLRDATA *eventhdlrdata)
Definition: scip_event.c:95
includes various files containing graph methods used for Steiner tree problems
#define SCIP_EVENTTYPE_BESTSOLFOUND
Definition: type_event.h:96
SCIP_RETCODE SCIPsetEventhdlrInit(SCIP *scip, SCIP_EVENTHDLR *eventhdlr, SCIP_DECL_EVENTINIT((*eventinit)))
Definition: scip_event.c:155
SCIP_RETCODE SCIPdropEvent(SCIP *scip, SCIP_EVENTTYPE eventtype, SCIP_EVENTHDLR *eventhdlr, SCIP_EVENTDATA *eventdata, int filterpos)
Definition: scip_event.c:311
#define SCIP_Real
Definition: def.h:163
#define EVENTHDLR_DESC
Definition: event_bestsol.c:33
SCIP_RETCODE SCIPincludeEventHdlrBestsol(SCIP *scip)
SCIP_SOL * SCIPgetBestSol(SCIP *scip)
Definition: scip_sol.c:2305
SCIP_RETCODE SCIPsetEventhdlrExit(SCIP *scip, SCIP_EVENTHDLR *eventhdlr, SCIP_DECL_EVENTEXIT((*eventexit)))
Definition: scip_event.c:169
SCIP_Real SCIPgetSolOrigObj(SCIP *scip, SCIP_SOL *sol)
Definition: scip_sol.c:1436