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