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