Scippy

SCIP

Solving Constraint Integer Programs

event_softtimelimit.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 event_softtimelimit.c
17  * @ingroup DEFPLUGINS_EVENT
18  * @brief eventhdlr for soft time limit
19  * @author Gerald Gamrath
20  */
21 
22 /*--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
23 
25 #include "scip/pub_event.h"
26 #include "scip/pub_message.h"
27 #include "scip/scip_event.h"
28 #include "scip/scip_mem.h"
29 #include "scip/scip_message.h"
30 #include "scip/scip_numerics.h"
31 #include "scip/scip_param.h"
32 #include <string.h>
33 
34 #define EVENTHDLR_NAME "softtimelimit"
35 #define EVENTHDLR_DESC "event handler for soft time limit"
36 
37 /*
38  * Data structures
39  */
40 
41 /** event handler data */
42 struct SCIP_EventhdlrData
43 {
44  SCIP_Real softtimelimit;
45  int filterpos;
46 };
47 
48 /*
49  * Callback methods of event handler
50  */
51 
52 /** copy method for event handler plugins (called when SCIP copies plugins) */
53 /**! [SnippetEventCopySofttimelimit] */
54 static
55 SCIP_DECL_EVENTCOPY(eventCopySofttimelimit)
56 { /*lint --e{715}*/
57  assert(scip != NULL);
58  assert(eventhdlr != NULL);
59  assert(strcmp(SCIPeventhdlrGetName(eventhdlr), EVENTHDLR_NAME) == 0);
60 
61  /* call inclusion method of event handler */
63 
64  return SCIP_OKAY;
65 }
66 /**! [SnippetEventCopySofttimelimit] */
67 
68 /** destructor of event handler to free user data (called when SCIP is exiting) */
69 /**! [SnippetEventFreeSofttimelimit] */
70 static
71 SCIP_DECL_EVENTFREE(eventFreeSofttimelimit)
72 { /*lint --e{715}*/
73  SCIP_EVENTHDLRDATA* eventhdlrdata;
74 
75  assert(scip != NULL);
76  assert(eventhdlr != NULL);
77  assert(strcmp(SCIPeventhdlrGetName(eventhdlr), EVENTHDLR_NAME) == 0);
78 
79  eventhdlrdata = SCIPeventhdlrGetData(eventhdlr);
80  assert(eventhdlrdata != NULL);
81 
82  SCIPfreeBlockMemory(scip, &eventhdlrdata);
83  SCIPeventhdlrSetData(eventhdlr, NULL);
84 
85  return SCIP_OKAY;
86 }
87 /**! [SnippetEventFreeSofttimelimit] */
88 
89 
90 
91 /** initialization method of event handler (called after problem was transformed) */
92 static
93 SCIP_DECL_EVENTINIT(eventInitSofttimelimit)
94 { /*lint --e{715}*/
95  SCIP_EVENTHDLRDATA* eventhdlrdata;
96 
97  assert(scip != NULL);
98  assert(eventhdlr != NULL);
99  assert(strcmp(SCIPeventhdlrGetName(eventhdlr), EVENTHDLR_NAME) == 0);
100 
101  eventhdlrdata = SCIPeventhdlrGetData(eventhdlr);
102  assert(eventhdlrdata != NULL);
103 
104  if( eventhdlrdata->filterpos < 0 && !SCIPisNegative(scip, eventhdlrdata->softtimelimit) )
105  {
106  /* notify SCIP that your event handler wants to react on the event type best solution found */
107  SCIP_CALL( SCIPcatchEvent(scip, SCIP_EVENTTYPE_BESTSOLFOUND, eventhdlr, NULL, &eventhdlrdata->filterpos) );
108  }
109 
110  return SCIP_OKAY;
111 }
112 
113 /** deinitialization method of event handler (called before transformed problem is freed) */
114 static
115 SCIP_DECL_EVENTEXIT(eventExitSofttimelimit)
116 { /*lint --e{715}*/
117  SCIP_EVENTHDLRDATA* eventhdlrdata;
118 
119  assert(scip != NULL);
120  assert(eventhdlr != NULL);
121  assert(strcmp(SCIPeventhdlrGetName(eventhdlr), EVENTHDLR_NAME) == 0);
122 
123  eventhdlrdata = SCIPeventhdlrGetData(eventhdlr);
124  assert(eventhdlrdata != NULL);
125 
126  /* notify SCIP that your event handler wants to drop the event type best solution found */
127  if( eventhdlrdata->filterpos >= 0 )
128  {
129  SCIP_CALL( SCIPdropEvent(scip, SCIP_EVENTTYPE_BESTSOLFOUND, eventhdlr, NULL, eventhdlrdata->filterpos) );
130  eventhdlrdata->filterpos = -1;
131  }
132 
133  return SCIP_OKAY;
134 }
135 
136 /** execution method of event handler */
137 static
138 SCIP_DECL_EVENTEXEC(eventExecSofttimelimit)
139 { /*lint --e{715}*/
140  SCIP_EVENTHDLRDATA* eventhdlrdata;
141  SCIP_Real timelimit;
142 
143  assert(eventhdlr != NULL);
144  assert(strcmp(SCIPeventhdlrGetName(eventhdlr), EVENTHDLR_NAME) == 0);
145  assert(event != NULL);
146  assert(scip != NULL);
148 
149  eventhdlrdata = SCIPeventhdlrGetData(eventhdlr);
150  assert(eventhdlrdata != NULL);
151 
152  SCIPdebugMsg(scip, "exec method of event handler for soft time limit\n");
153 
154  SCIP_CALL( SCIPgetRealParam(scip, "limits/time", &timelimit) );
155 
156  if( eventhdlrdata->softtimelimit < timelimit )
157  {
158  SCIP_CALL( SCIPsetRealParam(scip, "limits/time", eventhdlrdata->softtimelimit) );
159  }
160 
161  /* notify SCIP that your event handler wants to drop the event type best solution found */
162  SCIP_CALL( SCIPdropEvent(scip, SCIP_EVENTTYPE_BESTSOLFOUND, eventhdlr, NULL, eventhdlrdata->filterpos) );
163  eventhdlrdata->filterpos = -1;
164 
165  /* print best solution value */
166  SCIPverbMessage(scip, SCIP_VERBLEVEL_FULL, NULL, "changed time limit to %.1f after first solution was found\n",
167  eventhdlrdata->softtimelimit);
168 
169  return SCIP_OKAY;
170 }
171 
172 /** includes event handler for best solution found */
174  SCIP* scip /**< SCIP data structure */
175  )
176 {
177  SCIP_EVENTHDLRDATA* eventhdlrdata;
178  SCIP_EVENTHDLR* eventhdlr = NULL;
179 
180  SCIP_CALL( SCIPallocBlockMemory(scip, &eventhdlrdata) );
181  eventhdlrdata->filterpos = -1;
182 
183  /* create event handler for events on watched variables */
184  SCIP_CALL( SCIPincludeEventhdlrBasic(scip, &eventhdlr, EVENTHDLR_NAME, EVENTHDLR_DESC, eventExecSofttimelimit, eventhdlrdata) );
185  assert(eventhdlr != NULL);
186 
187  SCIP_CALL( SCIPsetEventhdlrCopy(scip, eventhdlr, eventCopySofttimelimit) );
188  SCIP_CALL( SCIPsetEventhdlrFree(scip, eventhdlr, eventFreeSofttimelimit) );
189  SCIP_CALL( SCIPsetEventhdlrInit(scip, eventhdlr, eventInitSofttimelimit) );
190  SCIP_CALL( SCIPsetEventhdlrExit(scip, eventhdlr, eventExitSofttimelimit) );
191 
192  SCIP_CALL( SCIPaddRealParam(scip, "limits/softtime",
193  "soft time limit which should be applied after first solution was found (-1.0: disabled)",
194  &eventhdlrdata->softtimelimit, FALSE, -1.0, -1.0, SCIP_REAL_MAX, NULL, NULL) );
195 
196  return SCIP_OKAY;
197 }
void SCIPeventhdlrSetData(SCIP_EVENTHDLR *eventhdlr, SCIP_EVENTHDLRDATA *eventhdlrdata)
Definition: event.c:335
static SCIP_DECL_EVENTEXIT(eventExitSofttimelimit)
public methods for SCIP parameter handling
public methods for memory management
struct SCIP_EventhdlrData SCIP_EVENTHDLRDATA
Definition: type_event.h:146
const char * SCIPeventhdlrGetName(SCIP_EVENTHDLR *eventhdlr)
Definition: event.c:315
void SCIPverbMessage(SCIP *scip, SCIP_VERBLEVEL msgverblevel, FILE *file, const char *formatstr,...)
Definition: scip_message.c:216
#define FALSE
Definition: def.h:73
enum SCIP_Retcode SCIP_RETCODE
Definition: type_retcode.h:54
#define SCIPfreeBlockMemory(scip, ptr)
Definition: scip_mem.h:95
#define SCIPallocBlockMemory(scip, ptr)
Definition: scip_mem.h:78
#define SCIPdebugMsg
Definition: scip_message.h:69
SCIP_RETCODE SCIPsetEventhdlrCopy(SCIP *scip, SCIP_EVENTHDLR *eventhdlr, SCIP_DECL_EVENTCOPY((*eventcopy)))
Definition: scip_event.c:127
public methods for numerical tolerances
SCIP_RETCODE SCIPsetRealParam(SCIP *scip, const char *name, SCIP_Real value)
Definition: scip_param.c:613
SCIP_EVENTHDLRDATA * SCIPeventhdlrGetData(SCIP_EVENTHDLR *eventhdlr)
Definition: event.c:325
static SCIP_DECL_EVENTEXEC(eventExecSofttimelimit)
SCIP_RETCODE SCIPincludeEventHdlrSofttimelimit(SCIP *scip)
SCIP_RETCODE SCIPsetEventhdlrFree(SCIP *scip, SCIP_EVENTHDLR *eventhdlr, SCIP_DECL_EVENTFREE((*eventfree)))
Definition: scip_event.c:141
#define EVENTHDLR_NAME
static SCIP_DECL_EVENTINIT(eventInitSofttimelimit)
static SCIP_DECL_EVENTFREE(eventFreeSofttimelimit)
public methods for event handler plugins and event handlers
SCIP_RETCODE SCIPcatchEvent(SCIP *scip, SCIP_EVENTTYPE eventtype, SCIP_EVENTHDLR *eventhdlr, SCIP_EVENTDATA *eventdata, int *filterpos)
Definition: scip_event.c:277
static SCIP_DECL_EVENTCOPY(eventCopySofttimelimit)
#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
SCIP_RETCODE SCIPgetRealParam(SCIP *scip, const char *name, SCIP_Real *value)
Definition: scip_param.c:298
SCIP_Bool SCIPisNegative(SCIP *scip, SCIP_Real val)
eventhdlr for soft time limit
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
SCIP_RETCODE SCIPaddRealParam(SCIP *scip, const char *name, const char *desc, SCIP_Real *valueptr, SCIP_Bool isadvanced, SCIP_Real defaultvalue, SCIP_Real minvalue, SCIP_Real maxvalue, SCIP_DECL_PARAMCHGD((*paramchgd)), SCIP_PARAMDATA *paramdata)
Definition: scip_param.c:130
#define SCIP_REAL_MAX
Definition: def.h:164
public methods for managing events
#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
public methods for message output
#define SCIP_Real
Definition: def.h:163
public methods for message handling
#define EVENTHDLR_DESC
SCIP_RETCODE SCIPsetEventhdlrExit(SCIP *scip, SCIP_EVENTHDLR *eventhdlr, SCIP_DECL_EVENTEXIT((*eventexit)))
Definition: scip_event.c:169