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-2026 Zuse Institute Berlin (ZIB) */
    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
    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 */
    51struct 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] */
    63static
    64SCIP_DECL_EVENTCOPY(eventCopySofttimelimit)
    65{ /*lint --e{715}*/
    66 assert(scip != NULL);
    67 assert(eventhdlr != NULL);
    68
    70
    71 /* call inclusion method of event handler */
    73
    74 return SCIP_OKAY;
    75}
    76/**! [SnippetEventCopySofttimelimit] */
    77
    78/** destructor of event handler to free user data (called when SCIP is exiting) */
    79/**! [SnippetEventFreeSofttimelimit] */
    80static
    81SCIP_DECL_EVENTFREE(eventFreeSofttimelimit)
    82{ /*lint --e{715}*/
    83 SCIP_EVENTHDLRDATA* eventhdlrdata;
    84
    85 assert(scip != NULL);
    86 assert(eventhdlr != NULL);
    87
    89
    90 eventhdlrdata = SCIPeventhdlrGetData(eventhdlr);
    91 assert(eventhdlrdata != NULL);
    92
    93 SCIPfreeBlockMemory(scip, &eventhdlrdata);
    94 SCIPeventhdlrSetData(eventhdlr, NULL);
    95
    96 return SCIP_OKAY;
    97}
    98/**! [SnippetEventFreeSofttimelimit] */
    99
    100
    101
    102/** initialization method of event handler (called after problem was transformed) */
    103static
    104SCIP_DECL_EVENTINIT(eventInitSofttimelimit)
    105{ /*lint --e{715}*/
    106 SCIP_EVENTHDLRDATA* eventhdlrdata;
    107
    108 assert(scip != NULL);
    109 assert(eventhdlr != NULL);
    110
    112
    113 eventhdlrdata = SCIPeventhdlrGetData(eventhdlr);
    114 assert(eventhdlrdata != NULL);
    115
    116 if( eventhdlrdata->filterpos < 0 && !SCIPisNegative(scip, eventhdlrdata->softtimelimit) )
    117 {
    118 /* notify SCIP that your event handler wants to react on the event type best solution found */
    119 SCIP_CALL( SCIPcatchEvent(scip, SCIP_EVENTTYPE_BESTSOLFOUND, eventhdlr, NULL, &eventhdlrdata->filterpos) );
    120 }
    121
    122 return SCIP_OKAY;
    123}
    124
    125/** deinitialization method of event handler (called before transformed problem is freed) */
    126static
    127SCIP_DECL_EVENTEXIT(eventExitSofttimelimit)
    128{ /*lint --e{715}*/
    129 SCIP_EVENTHDLRDATA* eventhdlrdata;
    130
    131 assert(scip != NULL);
    132 assert(eventhdlr != NULL);
    133
    135
    136 eventhdlrdata = SCIPeventhdlrGetData(eventhdlr);
    137 assert(eventhdlrdata != NULL);
    138
    139 /* notify SCIP that your event handler wants to drop the event type best solution found */
    140 if( eventhdlrdata->filterpos >= 0 )
    141 {
    142 SCIP_CALL( SCIPdropEvent(scip, SCIP_EVENTTYPE_BESTSOLFOUND, eventhdlr, NULL, eventhdlrdata->filterpos) );
    143 eventhdlrdata->filterpos = -1;
    144 }
    145
    146 return SCIP_OKAY;
    147}
    148
    149/** execution method of event handler */
    150static
    151SCIP_DECL_EVENTEXEC(eventExecSofttimelimit)
    152{ /*lint --e{715}*/
    153 SCIP_EVENTHDLRDATA* eventhdlrdata;
    154 SCIP_Real timelimit;
    155
    156 assert(eventhdlr != NULL);
    157 assert(event != NULL);
    158 assert(scip != NULL);
    160
    162
    163 eventhdlrdata = SCIPeventhdlrGetData(eventhdlr);
    164 assert(eventhdlrdata != NULL);
    165
    166 SCIPdebugMsg(scip, "exec method of event handler for soft time limit\n");
    167
    168 SCIP_CALL( SCIPgetRealParam(scip, "limits/time", &timelimit) );
    169
    170 if( eventhdlrdata->softtimelimit < timelimit )
    171 {
    172 SCIP_CALL( SCIPsetRealParam(scip, "limits/time", eventhdlrdata->softtimelimit) );
    173 }
    174
    175 /* notify SCIP that your event handler wants to drop the event type best solution found */
    176 SCIP_CALL( SCIPdropEvent(scip, SCIP_EVENTTYPE_BESTSOLFOUND, eventhdlr, NULL, eventhdlrdata->filterpos) );
    177 eventhdlrdata->filterpos = -1;
    178
    179 /* print best solution value */
    180 SCIPverbMessage(scip, SCIP_VERBLEVEL_FULL, NULL, "changed time limit to %.1f after first solution was found\n",
    181 eventhdlrdata->softtimelimit);
    182
    183 return SCIP_OKAY;
    184}
    185
    186/** includes event handler for best solution found */
    188 SCIP* scip /**< SCIP data structure */
    189 )
    190{
    191 SCIP_EVENTHDLRDATA* eventhdlrdata;
    192 SCIP_EVENTHDLR* eventhdlr = NULL;
    193
    194 SCIP_CALL( SCIPallocBlockMemory(scip, &eventhdlrdata) );
    195 eventhdlrdata->filterpos = -1;
    196
    197 /* create event handler for events on watched variables */
    198 SCIP_CALL( SCIPincludeEventhdlrBasic(scip, &eventhdlr, EVENTHDLR_NAME, EVENTHDLR_DESC, eventExecSofttimelimit, eventhdlrdata) );
    199 assert(eventhdlr != NULL);
    200
    201 SCIP_CALL( SCIPsetEventhdlrCopy(scip, eventhdlr, eventCopySofttimelimit) );
    202 SCIP_CALL( SCIPsetEventhdlrFree(scip, eventhdlr, eventFreeSofttimelimit) );
    203 SCIP_CALL( SCIPsetEventhdlrInit(scip, eventhdlr, eventInitSofttimelimit) );
    204 SCIP_CALL( SCIPsetEventhdlrExit(scip, eventhdlr, eventExitSofttimelimit) );
    205
    206 SCIP_CALL( SCIPaddRealParam(scip, "limits/softtime",
    207 "soft time limit which should be applied after first solution was found (-1.0: disabled)",
    208 &eventhdlrdata->softtimelimit, FALSE, -1.0, -1.0, SCIP_REAL_MAX, NULL, NULL) );
    209
    210 return SCIP_OKAY;
    211}
    #define NULL
    Definition: def.h:257
    #define SCIP_REAL_MAX
    Definition: def.h:167
    #define SCIP_STRINGEQ(name, reference, retcode)
    Definition: def.h:454
    #define SCIP_Real
    Definition: def.h:165
    #define FALSE
    Definition: def.h:103
    #define SCIP_CALL(x)
    Definition: def.h:364
    static SCIP_DECL_EVENTEXIT(eventExitSofttimelimit)
    static SCIP_DECL_EVENTINIT(eventInitSofttimelimit)
    static SCIP_DECL_EVENTFREE(eventFreeSofttimelimit)
    SCIP_RETCODE SCIPincludeEventHdlrSofttimelimit(SCIP *scip)
    static SCIP_DECL_EVENTEXEC(eventExecSofttimelimit)
    #define EVENTHDLR_DESC
    #define EVENTHDLR_NAME
    static SCIP_DECL_EVENTCOPY(eventCopySofttimelimit)
    eventhdlr for soft time limit
    void SCIPverbMessage(SCIP *scip, SCIP_VERBLEVEL msgverblevel, FILE *file, const char *formatstr,...)
    Definition: scip_message.c:225
    #define SCIPdebugMsg
    Definition: scip_message.h:78
    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
    SCIP_RETCODE SCIPgetRealParam(SCIP *scip, const char *name, SCIP_Real *value)
    Definition: scip_param.c:307
    SCIP_RETCODE SCIPsetRealParam(SCIP *scip, const char *name, SCIP_Real value)
    Definition: scip_param.c:603
    SCIP_RETCODE SCIPsetEventhdlrCopy(SCIP *scip, SCIP_EVENTHDLR *eventhdlr, SCIP_DECL_EVENTCOPY((*eventcopy)))
    Definition: scip_event.c:143
    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:111
    const char * SCIPeventhdlrGetName(SCIP_EVENTHDLR *eventhdlr)
    Definition: event.c:396
    SCIP_EVENTHDLRDATA * SCIPeventhdlrGetData(SCIP_EVENTHDLR *eventhdlr)
    Definition: event.c:406
    void SCIPeventhdlrSetData(SCIP_EVENTHDLR *eventhdlr, SCIP_EVENTHDLRDATA *eventhdlrdata)
    Definition: event.c:416
    SCIP_RETCODE SCIPsetEventhdlrFree(SCIP *scip, SCIP_EVENTHDLR *eventhdlr, SCIP_DECL_EVENTFREE((*eventfree)))
    Definition: scip_event.c:157
    SCIP_RETCODE SCIPsetEventhdlrExit(SCIP *scip, SCIP_EVENTHDLR *eventhdlr, SCIP_DECL_EVENTEXIT((*eventexit)))
    Definition: scip_event.c:185
    SCIP_RETCODE SCIPsetEventhdlrInit(SCIP *scip, SCIP_EVENTHDLR *eventhdlr, SCIP_DECL_EVENTINIT((*eventinit)))
    Definition: scip_event.c:171
    SCIP_EVENTTYPE SCIPeventGetType(SCIP_EVENT *event)
    Definition: event.c:1194
    SCIP_RETCODE SCIPcatchEvent(SCIP *scip, SCIP_EVENTTYPE eventtype, SCIP_EVENTHDLR *eventhdlr, SCIP_EVENTDATA *eventdata, int *filterpos)
    Definition: scip_event.c:293
    SCIP_RETCODE SCIPdropEvent(SCIP *scip, SCIP_EVENTTYPE eventtype, SCIP_EVENTHDLR *eventhdlr, SCIP_EVENTDATA *eventdata, int filterpos)
    Definition: scip_event.c:333
    #define SCIPfreeBlockMemory(scip, ptr)
    Definition: scip_mem.h:108
    #define SCIPallocBlockMemory(scip, ptr)
    Definition: scip_mem.h:89
    SCIP_Bool SCIPisNegative(SCIP *scip, SCIP_Real val)
    public methods for managing events
    public methods for message output
    public methods for event handler plugins and event handlers
    public methods for memory management
    public methods for message handling
    public methods for numerical tolerances
    public methods for SCIP parameter handling
    struct SCIP_EventhdlrData SCIP_EVENTHDLRDATA
    Definition: type_event.h:160
    #define SCIP_EVENTTYPE_BESTSOLFOUND
    Definition: type_event.h:106
    @ SCIP_VERBLEVEL_FULL
    Definition: type_message.h:62
    @ SCIP_OKAY
    Definition: type_retcode.h:42
    @ SCIP_INVALIDCALL
    Definition: type_retcode.h:51
    enum SCIP_Retcode SCIP_RETCODE
    Definition: type_retcode.h:63