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 2002-2022 Zuse Institute Berlin */
7 /* */
8 /* Licensed under the Apache License, Version 2.0 (the "License"); */
9 /* you may not use this file except in compliance with the License. */
10 /* You may obtain a copy of the License at */
11 /* */
12 /* http://www.apache.org/licenses/LICENSE-2.0 */
13 /* */
14 /* Unless required by applicable law or agreed to in writing, software */
15 /* distributed under the License is distributed on an "AS IS" BASIS, */
16 /* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
17 /* See the License for the specific language governing permissions and */
18 /* limitations under the License. */
19 /* */
20 /* You should have received a copy of the Apache-2.0 license */
21 /* along with SCIP; see the file LICENSE. If not visit scipopt.org. */
22 /* */
23 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
24 
25 /**@file event_softtimelimit.c
26  * @ingroup DEFPLUGINS_EVENT
27  * @brief eventhdlr for soft time limit
28  * @author Gerald Gamrath
29  */
30 
31 /*--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
32 
34 #include "scip/pub_event.h"
35 #include "scip/pub_message.h"
36 #include "scip/scip_event.h"
37 #include "scip/scip_mem.h"
38 #include "scip/scip_message.h"
39 #include "scip/scip_numerics.h"
40 #include "scip/scip_param.h"
41 #include <string.h>
42 
43 #define EVENTHDLR_NAME "softtimelimit"
44 #define EVENTHDLR_DESC "event handler for soft time limit"
45 
46 /*
47  * Data structures
48  */
49 
50 /** event handler data */
51 struct SCIP_EventhdlrData
52 {
53  SCIP_Real softtimelimit;
54  int filterpos;
55 };
56 
57 /*
58  * Callback methods of event handler
59  */
60 
61 /** copy method for event handler plugins (called when SCIP copies plugins) */
62 /**! [SnippetEventCopySofttimelimit] */
63 static
64 SCIP_DECL_EVENTCOPY(eventCopySofttimelimit)
65 { /*lint --e{715}*/
66  assert(scip != NULL);
67  assert(eventhdlr != NULL);
68  assert(strcmp(SCIPeventhdlrGetName(eventhdlr), EVENTHDLR_NAME) == 0);
69 
70  /* call inclusion method of event handler */
72 
73  return SCIP_OKAY;
74 }
75 /**! [SnippetEventCopySofttimelimit] */
76 
77 /** destructor of event handler to free user data (called when SCIP is exiting) */
78 /**! [SnippetEventFreeSofttimelimit] */
79 static
80 SCIP_DECL_EVENTFREE(eventFreeSofttimelimit)
81 { /*lint --e{715}*/
82  SCIP_EVENTHDLRDATA* eventhdlrdata;
83 
84  assert(scip != NULL);
85  assert(eventhdlr != NULL);
86  assert(strcmp(SCIPeventhdlrGetName(eventhdlr), EVENTHDLR_NAME) == 0);
87 
88  eventhdlrdata = SCIPeventhdlrGetData(eventhdlr);
89  assert(eventhdlrdata != NULL);
90 
91  SCIPfreeBlockMemory(scip, &eventhdlrdata);
92  SCIPeventhdlrSetData(eventhdlr, NULL);
93 
94  return SCIP_OKAY;
95 }
96 /**! [SnippetEventFreeSofttimelimit] */
97 
98 
99 
100 /** initialization method of event handler (called after problem was transformed) */
101 static
102 SCIP_DECL_EVENTINIT(eventInitSofttimelimit)
103 { /*lint --e{715}*/
104  SCIP_EVENTHDLRDATA* eventhdlrdata;
105 
106  assert(scip != NULL);
107  assert(eventhdlr != NULL);
108  assert(strcmp(SCIPeventhdlrGetName(eventhdlr), EVENTHDLR_NAME) == 0);
109 
110  eventhdlrdata = SCIPeventhdlrGetData(eventhdlr);
111  assert(eventhdlrdata != NULL);
112 
113  if( eventhdlrdata->filterpos < 0 && !SCIPisNegative(scip, eventhdlrdata->softtimelimit) )
114  {
115  /* notify SCIP that your event handler wants to react on the event type best solution found */
116  SCIP_CALL( SCIPcatchEvent(scip, SCIP_EVENTTYPE_BESTSOLFOUND, eventhdlr, NULL, &eventhdlrdata->filterpos) );
117  }
118 
119  return SCIP_OKAY;
120 }
121 
122 /** deinitialization method of event handler (called before transformed problem is freed) */
123 static
124 SCIP_DECL_EVENTEXIT(eventExitSofttimelimit)
125 { /*lint --e{715}*/
126  SCIP_EVENTHDLRDATA* eventhdlrdata;
127 
128  assert(scip != NULL);
129  assert(eventhdlr != NULL);
130  assert(strcmp(SCIPeventhdlrGetName(eventhdlr), EVENTHDLR_NAME) == 0);
131 
132  eventhdlrdata = SCIPeventhdlrGetData(eventhdlr);
133  assert(eventhdlrdata != NULL);
134 
135  /* notify SCIP that your event handler wants to drop the event type best solution found */
136  if( eventhdlrdata->filterpos >= 0 )
137  {
138  SCIP_CALL( SCIPdropEvent(scip, SCIP_EVENTTYPE_BESTSOLFOUND, eventhdlr, NULL, eventhdlrdata->filterpos) );
139  eventhdlrdata->filterpos = -1;
140  }
141 
142  return SCIP_OKAY;
143 }
144 
145 /** execution method of event handler */
146 static
147 SCIP_DECL_EVENTEXEC(eventExecSofttimelimit)
148 { /*lint --e{715}*/
149  SCIP_EVENTHDLRDATA* eventhdlrdata;
150  SCIP_Real timelimit;
151 
152  assert(eventhdlr != NULL);
153  assert(strcmp(SCIPeventhdlrGetName(eventhdlr), EVENTHDLR_NAME) == 0);
154  assert(event != NULL);
155  assert(scip != NULL);
157 
158  eventhdlrdata = SCIPeventhdlrGetData(eventhdlr);
159  assert(eventhdlrdata != NULL);
160 
161  SCIPdebugMsg(scip, "exec method of event handler for soft time limit\n");
162 
163  SCIP_CALL( SCIPgetRealParam(scip, "limits/time", &timelimit) );
164 
165  if( eventhdlrdata->softtimelimit < timelimit )
166  {
167  SCIP_CALL( SCIPsetRealParam(scip, "limits/time", eventhdlrdata->softtimelimit) );
168  }
169 
170  /* notify SCIP that your event handler wants to drop the event type best solution found */
171  SCIP_CALL( SCIPdropEvent(scip, SCIP_EVENTTYPE_BESTSOLFOUND, eventhdlr, NULL, eventhdlrdata->filterpos) );
172  eventhdlrdata->filterpos = -1;
173 
174  /* print best solution value */
175  SCIPverbMessage(scip, SCIP_VERBLEVEL_FULL, NULL, "changed time limit to %.1f after first solution was found\n",
176  eventhdlrdata->softtimelimit);
177 
178  return SCIP_OKAY;
179 }
180 
181 /** includes event handler for best solution found */
183  SCIP* scip /**< SCIP data structure */
184  )
185 {
186  SCIP_EVENTHDLRDATA* eventhdlrdata;
187  SCIP_EVENTHDLR* eventhdlr = NULL;
188 
189  SCIP_CALL( SCIPallocBlockMemory(scip, &eventhdlrdata) );
190  eventhdlrdata->filterpos = -1;
191 
192  /* create event handler for events on watched variables */
193  SCIP_CALL( SCIPincludeEventhdlrBasic(scip, &eventhdlr, EVENTHDLR_NAME, EVENTHDLR_DESC, eventExecSofttimelimit, eventhdlrdata) );
194  assert(eventhdlr != NULL);
195 
196  SCIP_CALL( SCIPsetEventhdlrCopy(scip, eventhdlr, eventCopySofttimelimit) );
197  SCIP_CALL( SCIPsetEventhdlrFree(scip, eventhdlr, eventFreeSofttimelimit) );
198  SCIP_CALL( SCIPsetEventhdlrInit(scip, eventhdlr, eventInitSofttimelimit) );
199  SCIP_CALL( SCIPsetEventhdlrExit(scip, eventhdlr, eventExitSofttimelimit) );
200 
201  SCIP_CALL( SCIPaddRealParam(scip, "limits/softtime",
202  "soft time limit which should be applied after first solution was found (-1.0: disabled)",
203  &eventhdlrdata->softtimelimit, FALSE, -1.0, -1.0, SCIP_REAL_MAX, NULL, NULL) );
204 
205  return SCIP_OKAY;
206 }
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:307
SCIP_RETCODE SCIPsetEventhdlrExit(SCIP *scip, SCIP_EVENTHDLR *eventhdlr, SCIP_DECL_EVENTEXIT((*eventexit)))
Definition: scip_event.c:178
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:104
struct SCIP_EventhdlrData SCIP_EVENTHDLRDATA
Definition: type_event.h:155
#define FALSE
Definition: def.h:96
const char * SCIPeventhdlrGetName(SCIP_EVENTHDLR *eventhdlr)
Definition: event.c:324
SCIP_Bool SCIPisNegative(SCIP *scip, SCIP_Real val)
enum SCIP_Retcode SCIP_RETCODE
Definition: type_retcode.h:63
#define SCIPfreeBlockMemory(scip, ptr)
Definition: scip_mem.h:108
#define SCIPallocBlockMemory(scip, ptr)
Definition: scip_mem.h:89
SCIP_RETCODE SCIPsetRealParam(SCIP *scip, const char *name, SCIP_Real value)
Definition: scip_param.c:603
#define SCIPdebugMsg
Definition: scip_message.h:78
public methods for numerical tolerances
void SCIPeventhdlrSetData(SCIP_EVENTHDLR *eventhdlr, SCIP_EVENTHDLRDATA *eventhdlrdata)
Definition: event.c:344
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:150
SCIP_RETCODE SCIPsetEventhdlrCopy(SCIP *scip, SCIP_EVENTHDLR *eventhdlr, SCIP_DECL_EVENTCOPY((*eventcopy)))
Definition: scip_event.c:136
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:164
static SCIP_DECL_EVENTCOPY(eventCopySofttimelimit)
#define NULL
Definition: lpi_spx1.cpp:164
#define SCIP_CALL(x)
Definition: def.h:393
void SCIPverbMessage(SCIP *scip, SCIP_VERBLEVEL msgverblevel, FILE *file, const char *formatstr,...)
Definition: scip_message.c:225
SCIP_RETCODE SCIPcatchEvent(SCIP *scip, SCIP_EVENTTYPE eventtype, SCIP_EVENTHDLR *eventhdlr, SCIP_EVENTDATA *eventdata, int *filterpos)
Definition: scip_event.c:286
SCIP_EVENTTYPE SCIPeventGetType(SCIP_EVENT *event)
Definition: event.c:1030
SCIP_RETCODE SCIPdropEvent(SCIP *scip, SCIP_EVENTTYPE eventtype, SCIP_EVENTHDLR *eventhdlr, SCIP_EVENTDATA *eventdata, int filterpos)
Definition: scip_event.c:320
eventhdlr for soft time limit
#define SCIP_REAL_MAX
Definition: def.h:187
public methods for managing events
#define SCIP_EVENTTYPE_BESTSOLFOUND
Definition: type_event.h:105
public methods for message output
#define SCIP_Real
Definition: def.h:186
public methods for message handling
#define EVENTHDLR_DESC
SCIP_EVENTHDLRDATA * SCIPeventhdlrGetData(SCIP_EVENTHDLR *eventhdlr)
Definition: event.c:334
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:139