SCIP

    Solving Constraint Integer Programs

    heur_sync.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 heur_sync.c
    26 * @ingroup DEFPLUGINS_HEUR
    27 * @brief primal heuristic that adds solutions from synchronization
    28 * @author Leona Gottwald
    29 *
    30 * This heuristic takes solutions during synchronization and then adds them.
    31 */
    32
    33/*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
    34
    35#include "scip/heur_sync.h"
    36#include "scip/scip.h"
    37
    38
    39#define HEUR_NAME "sync"
    40#define HEUR_DESC "heuristic for synchronizing solution"
    41#define HEUR_DISPCHAR 'S'
    42#define HEUR_PRIORITY -3000000 /* should process after all other heuristics */
    43#define HEUR_FREQ -1
    44#define HEUR_FREQOFS 0
    45#define HEUR_MAXDEPTH -1
    46#define HEUR_TIMING SCIP_HEURTIMING_DURINGLPLOOP | SCIP_HEURTIMING_BEFOREPRESOL | SCIP_HEURTIMING_BEFORENODE
    47#define HEUR_USESSUBSCIP FALSE /**< does the heuristic use a secondary SCIP instance? */
    48
    49
    50/*
    51 * Data structures
    52 */
    53
    54
    55/** primal heuristic data */
    56struct SCIP_HeurData
    57{
    58 SCIP_SOL** sols; /**< storing solutions passed to heuristic sorted by objective value */
    59 int nsols; /**< number of soluions stored */
    60 int maxnsols; /**< maximum number of solutions that can be stored */
    61};
    62
    63
    64/*
    65 * Callback methods of primal heuristic
    66 */
    67
    68/** destructor of primal heuristic to free user data (called when SCIP is exiting) */
    69static
    70SCIP_DECL_HEURFREE(heurFreeSync)
    71{ /*lint --e{715}*/
    72 SCIP_HEURDATA* heurdata;
    73
    74 assert( heur != NULL );
    75 assert( scip != NULL );
    76
    78
    79 SCIPdebugMessage("free method of sync primal heuristic.\n");
    80
    81 /* get heuristic data */
    82 heurdata = SCIPheurGetData(heur);
    83 assert(heurdata != NULL);
    84 assert(heurdata->nsols == 0);
    85 SCIPfreeBlockMemoryArray(scip, &heurdata->sols, heurdata->maxnsols);
    86 SCIPfreeBlockMemory(scip, &heurdata);
    87
    88 return SCIP_OKAY;
    89}
    90
    91
    92/** deinitialization method of primal heuristic (called before transformed problem is freed) */
    93static
    95{ /*lint --e{715}*/
    96 SCIP_HEURDATA* heurdata;
    97 int i;
    98
    99 assert( heur != NULL );
    100 assert( scip != NULL );
    101
    103
    104 SCIPdebugMessage("exit method of sync primal heuristic.\n");
    105
    106 /* get heuristic data */
    107 heurdata = SCIPheurGetData(heur);
    108 assert(heurdata != NULL);
    109
    110 /* free solution if one is still present */
    111 for( i = 0; i < heurdata->nsols; ++i )
    112 {
    113 SCIP_CALL( SCIPfreeSol(scip, &heurdata->sols[i]) );
    114 }
    115 heurdata->nsols = 0;
    116
    117 return SCIP_OKAY;
    118}
    119
    120
    121/** execution method of primal heuristic */
    122static
    124{ /*lint --e{715}*/
    125 SCIP_HEURDATA* heurdata;
    126 SCIP_Bool stored;
    127 int i;
    128
    129 assert(heur != NULL);
    130 assert(scip != NULL);
    131 assert(result != NULL);
    132 assert(SCIPheurGetFreq(heur) == 1);
    133
    135
    136 SCIPheurSetFreq(heur, -1);
    137
    138 /* get heuristic data */
    139 heurdata = SCIPheurGetData(heur);
    140 assert(heurdata != NULL);
    141 assert(heurdata->nsols > 0);
    142
    143 SCIPdebugMessage("exec method of sync primal heuristic.\n");
    144 *result = SCIP_DIDNOTFIND;
    145 for( i = 0; i < heurdata->nsols; ++i )
    146 {
    147 SCIP_CALL( SCIPtrySolFree(scip, &heurdata->sols[i], FALSE, FALSE, FALSE, FALSE, FALSE, &stored) );
    148 if( stored )
    149 *result = SCIP_FOUNDSOL;
    150 }
    151
    152 heurdata->nsols = 0;
    153
    154 return SCIP_OKAY;
    155}
    156
    157/*
    158 * primal heuristic specific interface methods
    159 */
    160
    161/** creates the sync primal heuristic and includes it in SCIP */
    163 SCIP* scip /**< SCIP data structure */
    164 )
    165{
    166 SCIP_HEURDATA* heurdata;
    167 SCIP_HEUR* heur;
    168
    169 /* create heuristic data */
    170 SCIP_CALL( SCIPallocBlockMemory(scip, &heurdata) );
    171 SCIP_CALL( SCIPgetIntParam(scip, "concurrent/sync/maxnsols", &heurdata->maxnsols) );
    172 SCIP_CALL( SCIPallocBlockMemoryArray(scip, &heurdata->sols, heurdata->maxnsols) );
    173 heurdata->nsols = 0;
    174
    175 /* include primal heuristic */
    178 HEUR_MAXDEPTH, HEUR_TIMING, HEUR_USESSUBSCIP, heurExecSync, heurdata) );
    179
    180 assert(heur != NULL);
    181
    182 /* primal heuristic is safe to use in exact solving mode */
    183 SCIPheurMarkExact(heur);
    184
    185 /* set non-NULL pointers to callback methods */
    186 SCIP_CALL( SCIPsetHeurFree(scip, heur, heurFreeSync) );
    187 SCIP_CALL( SCIPsetHeurExit(scip, heur, heurExitSync) );
    188
    189 return SCIP_OKAY;
    190}
    191
    192
    193/** pass solution to sync heuristic */
    195 SCIP* scip, /**< SCIP data structure */
    196 SCIP_HEUR* heur, /**< sync heuristic */
    197 SCIP_SOL* sol /**< solution to be passed */
    198 )
    199{
    200 SCIP_HEURDATA* heurdata;
    201 SCIP_Real solobj;
    202 int i;
    203
    204 assert(scip != NULL);
    205 assert(heur != NULL);
    206 assert(sol != NULL);
    207
    209
    210 /* get heuristic data */
    211 heurdata = SCIPheurGetData(heur);
    212 assert(heurdata != NULL);
    213
    214 SCIPsolSetHeur(sol, heur);
    215 solobj = SCIPgetSolTransObj(scip, sol);
    216
    217 /* check if we have an empty slot in the solution array or if we need to discard the worst solution */
    218 if( heurdata->nsols < heurdata->maxnsols )
    219 {
    220 /* if the maximum number of solutions is not yet reached just insert the solution sorted by its objective value */
    221 i = heurdata->nsols++;
    222 while( i > 0 && solobj > SCIPgetSolTransObj(scip, heurdata->sols[i - 1]) )
    223 {
    224 heurdata->sols[i] = heurdata->sols[i - 1];
    225 --i;
    226 }
    227 heurdata->sols[i] = sol;
    228 }
    229 else
    230 {
    231 /* already have reached the maximum number of solutions, so we need to check if the solution is better than a
    232 * previous one and free the worst solution to make room for it if this is the case */
    233 for( i = 0; i < heurdata->nsols && solobj < SCIPgetSolTransObj(scip, heurdata->sols[i]); ++i )
    234 {
    235 if( i > 0 )
    236 heurdata->sols[i - 1] = heurdata->sols[i];
    237 else
    238 {
    239 SCIP_CALL( SCIPfreeSol(scip, &heurdata->sols[i]) );
    240 }
    241 }
    242
    243 /* check if solution must be inserted or discarded */
    244 if( i > 0)
    245 {
    246 /* found position to insert the solution sorted by objective value */
    247 heurdata->sols[i-1] = sol;
    248 }
    249 else
    250 {
    251 /* solution is not better just discard it */
    252 SCIP_CALL( SCIPfreeSol(scip, &sol) );
    253 }
    254 }
    255 assert(heurdata->nsols > 0);
    256 assert(heurdata->nsols <= heurdata->maxnsols);
    257
    258 SCIPheurSetFreq(heur, 1);
    259
    260 return SCIP_OKAY;
    261}
    #define NULL
    Definition: def.h:257
    #define SCIP_Bool
    Definition: def.h:100
    #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
    SCIP_RETCODE SCIPheurSyncPassSol(SCIP *scip, SCIP_HEUR *heur, SCIP_SOL *sol)
    Definition: heur_sync.c:194
    SCIP_RETCODE SCIPgetIntParam(SCIP *scip, const char *name, int *value)
    Definition: scip_param.c:269
    SCIP_RETCODE SCIPincludeHeurSync(SCIP *scip)
    Definition: heur_sync.c:162
    SCIP_HEURDATA * SCIPheurGetData(SCIP_HEUR *heur)
    Definition: heur.c:1368
    SCIP_RETCODE SCIPincludeHeurBasic(SCIP *scip, SCIP_HEUR **heur, const char *name, const char *desc, char dispchar, int priority, int freq, int freqofs, int maxdepth, SCIP_HEURTIMING timingmask, SCIP_Bool usessubscip, SCIP_DECL_HEUREXEC((*heurexec)), SCIP_HEURDATA *heurdata)
    Definition: scip_heur.c:122
    SCIP_RETCODE SCIPsetHeurFree(SCIP *scip, SCIP_HEUR *heur, SCIP_DECL_HEURFREE((*heurfree)))
    Definition: scip_heur.c:183
    SCIP_RETCODE SCIPsetHeurExit(SCIP *scip, SCIP_HEUR *heur, SCIP_DECL_HEUREXIT((*heurexit)))
    Definition: scip_heur.c:215
    void SCIPheurSetFreq(SCIP_HEUR *heur, int freq)
    Definition: heur.c:1562
    void SCIPheurMarkExact(SCIP_HEUR *heur)
    Definition: heur.c:1457
    int SCIPheurGetFreq(SCIP_HEUR *heur)
    Definition: heur.c:1552
    const char * SCIPheurGetName(SCIP_HEUR *heur)
    Definition: heur.c:1467
    #define SCIPfreeBlockMemoryArray(scip, ptr, num)
    Definition: scip_mem.h:110
    #define SCIPallocBlockMemoryArray(scip, ptr, num)
    Definition: scip_mem.h:93
    #define SCIPfreeBlockMemory(scip, ptr)
    Definition: scip_mem.h:108
    #define SCIPallocBlockMemory(scip, ptr)
    Definition: scip_mem.h:89
    SCIP_RETCODE SCIPfreeSol(SCIP *scip, SCIP_SOL **sol)
    Definition: scip_sol.c:1250
    SCIP_RETCODE SCIPtrySolFree(SCIP *scip, SCIP_SOL **sol, SCIP_Bool printreason, SCIP_Bool completely, SCIP_Bool checkbounds, SCIP_Bool checkintegrality, SCIP_Bool checklprows, SCIP_Bool *stored)
    Definition: scip_sol.c:4114
    SCIP_Real SCIPgetSolTransObj(SCIP *scip, SCIP_SOL *sol)
    Definition: scip_sol.c:2003
    void SCIPsolSetHeur(SCIP_SOL *sol, SCIP_HEUR *heur)
    Definition: sol.c:4319
    #define HEUR_TIMING
    Definition: heur_sync.c:46
    #define HEUR_FREQOFS
    Definition: heur_sync.c:44
    #define HEUR_DESC
    Definition: heur_sync.c:40
    static SCIP_DECL_HEUREXITSOL(heurExitSync)
    Definition: heur_sync.c:94
    #define HEUR_DISPCHAR
    Definition: heur_sync.c:41
    #define HEUR_MAXDEPTH
    Definition: heur_sync.c:45
    #define HEUR_PRIORITY
    Definition: heur_sync.c:42
    static SCIP_DECL_HEURFREE(heurFreeSync)
    Definition: heur_sync.c:70
    #define HEUR_NAME
    Definition: heur_sync.c:39
    static SCIP_DECL_HEUREXEC(heurExecSync)
    Definition: heur_sync.c:123
    #define HEUR_FREQ
    Definition: heur_sync.c:43
    #define HEUR_USESSUBSCIP
    Definition: heur_sync.c:47
    primal heuristic that adds solutions from synchronization
    #define SCIPdebugMessage
    Definition: pub_message.h:96
    SCIP callable library.
    struct SCIP_HeurData SCIP_HEURDATA
    Definition: type_heur.h:77
    @ SCIP_DIDNOTFIND
    Definition: type_result.h:44
    @ SCIP_FOUNDSOL
    Definition: type_result.h:56
    @ SCIP_OKAY
    Definition: type_retcode.h:42
    @ SCIP_INVALIDCALL
    Definition: type_retcode.h:51
    enum SCIP_Retcode SCIP_RETCODE
    Definition: type_retcode.h:63