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-2016 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 static
47 SCIP_DECL_EVENTCOPY(eventCopySofttimelimit)
48 { /*lint --e{715}*/
49  assert(scip != NULL);
50  assert(eventhdlr != NULL);
51  assert(strcmp(SCIPeventhdlrGetName(eventhdlr), EVENTHDLR_NAME) == 0);
52 
53 #if 0 /* should the event handler be copied??? */
54  /* call inclusion method of event handler */
56 #endif
57 
58  return SCIP_OKAY;
59 }
60 
61 /** destructor of event handler to free user data (called when SCIP is exiting) */
62 static
63 SCIP_DECL_EVENTFREE(eventFreeSofttimelimit)
64 { /*lint --e{715}*/
65  SCIP_EVENTHDLRDATA* eventhdlrdata;
66 
67  assert(scip != NULL);
68  assert(eventhdlr != NULL);
69  assert(strcmp(SCIPeventhdlrGetName(eventhdlr), EVENTHDLR_NAME) == 0);
70 
71  eventhdlrdata = SCIPeventhdlrGetData(eventhdlr);
72  assert(eventhdlrdata != NULL);
73 
74  SCIPfreeMemory(scip, &eventhdlrdata);
75  SCIPeventhdlrSetData(eventhdlr, NULL);
76 
77  return SCIP_OKAY;
78 }
79 
80 
81 
82 /** initialization method of event handler (called after problem was transformed) */
83 static
84 SCIP_DECL_EVENTINIT(eventInitSofttimelimit)
85 { /*lint --e{715}*/
86  SCIP_EVENTHDLRDATA* eventhdlrdata;
87 
88  assert(scip != NULL);
89  assert(eventhdlr != NULL);
90  assert(strcmp(SCIPeventhdlrGetName(eventhdlr), EVENTHDLR_NAME) == 0);
91 
92  eventhdlrdata = SCIPeventhdlrGetData(eventhdlr);
93  assert(eventhdlrdata != NULL);
94 
95  if( eventhdlrdata->filterpos < 0 && !SCIPisNegative(scip, eventhdlrdata->softtimelimit) )
96  {
97  /* notify SCIP that your event handler wants to react on the event type best solution found */
98  SCIP_CALL( SCIPcatchEvent(scip, SCIP_EVENTTYPE_BESTSOLFOUND, eventhdlr, NULL, &eventhdlrdata->filterpos) );
99  }
100 
101  return SCIP_OKAY;
102 }
103 
104 /** deinitialization method of event handler (called before transformed problem is freed) */
105 static
106 SCIP_DECL_EVENTEXIT(eventExitSofttimelimit)
107 { /*lint --e{715}*/
108  SCIP_EVENTHDLRDATA* eventhdlrdata;
109 
110  assert(scip != NULL);
111  assert(eventhdlr != NULL);
112  assert(strcmp(SCIPeventhdlrGetName(eventhdlr), EVENTHDLR_NAME) == 0);
113 
114  eventhdlrdata = SCIPeventhdlrGetData(eventhdlr);
115  assert(eventhdlrdata != NULL);
116 
117  /* notify SCIP that your event handler wants to drop the event type best solution found */
118  if( eventhdlrdata->filterpos >= 0 )
119  {
120  SCIP_CALL( SCIPdropEvent(scip, SCIP_EVENTTYPE_BESTSOLFOUND, eventhdlr, NULL, eventhdlrdata->filterpos) );
121  eventhdlrdata->filterpos = -1;
122  }
123 
124  return SCIP_OKAY;
125 }
126 
127 /** execution method of event handler */
128 static
129 SCIP_DECL_EVENTEXEC(eventExecSofttimelimit)
130 { /*lint --e{715}*/
131  SCIP_EVENTHDLRDATA* eventhdlrdata;
132 
133  assert(eventhdlr != NULL);
134  assert(strcmp(SCIPeventhdlrGetName(eventhdlr), EVENTHDLR_NAME) == 0);
135  assert(event != NULL);
136  assert(scip != NULL);
138 
139  eventhdlrdata = SCIPeventhdlrGetData(eventhdlr);
140  assert(eventhdlrdata != NULL);
141 
142  SCIPdebugMessage("exec method of event handler for soft time limit\n");
143 
144  SCIP_CALL( SCIPsetRealParam(scip, "limits/time", eventhdlrdata->softtimelimit) );
145 
146  /* notify SCIP that your event handler wants to drop the event type best solution found */
147  SCIP_CALL( SCIPdropEvent(scip, SCIP_EVENTTYPE_BESTSOLFOUND, eventhdlr, NULL, eventhdlrdata->filterpos) );
148  eventhdlrdata->filterpos = -1;
149 
150  /* print best solution value */
151  SCIPinfoMessage(scip, NULL, "changed time limit to %.1f after first solution was found\n",
152  eventhdlrdata->softtimelimit);
153 
154  return SCIP_OKAY;
155 }
156 
157 /** includes event handler for best solution found */
159  SCIP* scip /**< SCIP data structure */
160  )
161 {
162  SCIP_EVENTHDLRDATA* eventhdlrdata;
163  SCIP_EVENTHDLR* eventhdlr;
164 
165  SCIP_CALL( SCIPallocMemory(scip, &eventhdlrdata) );
166  eventhdlrdata->filterpos = -1;
167 
168  eventhdlr = NULL;
169  /* create event handler for events on watched variables */
170  SCIP_CALL( SCIPincludeEventhdlrBasic(scip, &eventhdlr, EVENTHDLR_NAME, EVENTHDLR_DESC, eventExecSofttimelimit, eventhdlrdata) );
171  assert(eventhdlr != NULL);
172 
173  SCIP_CALL( SCIPsetEventhdlrCopy(scip, eventhdlr, eventCopySofttimelimit) );
174  SCIP_CALL( SCIPsetEventhdlrFree(scip, eventhdlr, eventFreeSofttimelimit) );
175  SCIP_CALL( SCIPsetEventhdlrInit(scip, eventhdlr, eventInitSofttimelimit) );
176  SCIP_CALL( SCIPsetEventhdlrExit(scip, eventhdlr, eventExitSofttimelimit) );
177 
178  SCIP_CALL( SCIPaddRealParam(scip, "limits/softtime",
179  "soft time limit which should be applied after first solution was found",
180  &eventhdlrdata->softtimelimit, FALSE, -1.0, -1.0, SCIP_REAL_MAX, NULL, NULL) );
181 
182 
183  return SCIP_OKAY;
184 }
#define SCIPallocMemory(scip, ptr)
Definition: scip.h:20526
static SCIP_DECL_EVENTEXIT(eventExitSofttimelimit)
#define NULL
Definition: lpi_spx.cpp:130
struct SCIP_EventhdlrData SCIP_EVENTHDLRDATA
Definition: type_event.h:129
#define FALSE
Definition: def.h:56
SCIP_RETCODE SCIPincludeEventhdlrBasic(SCIP *scip, SCIP_EVENTHDLR **eventhdlrptr, const char *name, const char *desc, SCIP_DECL_EVENTEXEC((*eventexec)), SCIP_EVENTHDLRDATA *eventhdlrdata)
Definition: scip.c:7778
enum SCIP_Retcode SCIP_RETCODE
Definition: type_retcode.h:53
SCIP_RETCODE SCIPsetEventhdlrInit(SCIP *scip, SCIP_EVENTHDLR *eventhdlr, SCIP_DECL_EVENTINIT((*eventinit)))
Definition: scip.c:7838
#define SCIP_CALL(x)
Definition: def.h:266
SCIP_RETCODE SCIPsetRealParam(SCIP *scip, const char *name, SCIP_Real value)
Definition: scip.c:4109
#define SCIPdebugMessage
Definition: pub_message.h:77
void SCIPeventhdlrSetData(SCIP_EVENTHDLR *eventhdlr, SCIP_EVENTHDLRDATA *eventhdlrdata)
Definition: event.c:298
static SCIP_DECL_EVENTEXEC(eventExecSofttimelimit)
SCIP_RETCODE SCIPincludeEventHdlrSofttimelimit(SCIP *scip)
#define SCIPfreeMemory(scip, ptr)
Definition: scip.h:20542
#define EVENTHDLR_NAME
static SCIP_DECL_EVENTINIT(eventInitSofttimelimit)
SCIP_Bool SCIPisNegative(SCIP *scip, SCIP_Real val)
Definition: scip.c:41709
static SCIP_DECL_EVENTFREE(eventFreeSofttimelimit)
static SCIP_DECL_EVENTCOPY(eventCopySofttimelimit)
SCIP_EVENTHDLRDATA * SCIPeventhdlrGetData(SCIP_EVENTHDLR *eventhdlr)
Definition: event.c:288
SCIP_RETCODE SCIPsetEventhdlrExit(SCIP *scip, SCIP_EVENTHDLR *eventhdlr, SCIP_DECL_EVENTEXIT((*eventexit)))
Definition: scip.c:7852
eventhdlr for soft time limit
#define SCIP_REAL_MAX
Definition: def.h:128
#define SCIP_EVENTTYPE_BESTSOLFOUND
Definition: type_event.h:82
SCIP_RETCODE SCIPdropEvent(SCIP *scip, SCIP_EVENTTYPE eventtype, SCIP_EVENTHDLR *eventhdlr, SCIP_EVENTDATA *eventdata, int filterpos)
Definition: scip.c:36588
void SCIPinfoMessage(SCIP *scip, FILE *file, const char *formatstr,...)
Definition: scip.c:1281
const char * SCIPeventhdlrGetName(SCIP_EVENTHDLR *eventhdlr)
Definition: event.c:278
#define SCIP_Real
Definition: def.h:127
SCIP_RETCODE SCIPsetEventhdlrFree(SCIP *scip, SCIP_EVENTHDLR *eventhdlr, SCIP_DECL_EVENTFREE((*eventfree)))
Definition: scip.c:7824
SCIP_EVENTTYPE SCIPeventGetType(SCIP_EVENT *event)
Definition: event.c:917
#define EVENTHDLR_DESC
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:3629
SCIP_RETCODE SCIPsetEventhdlrCopy(SCIP *scip, SCIP_EVENTHDLR *eventhdlr, SCIP_DECL_EVENTCOPY((*eventcopy)))
Definition: scip.c:7810
SCIP_RETCODE SCIPcatchEvent(SCIP *scip, SCIP_EVENTTYPE eventtype, SCIP_EVENTHDLR *eventhdlr, SCIP_EVENTDATA *eventdata, int *filterpos)
Definition: scip.c:36554