SCIP

    Solving Constraint Integer Programs

    heur_trivial.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_trivial.c
    26 * @ingroup DEFPLUGINS_HEUR
    27 * @brief trivial primal heuristic
    28 * @author Timo Berthold
    29 */
    30
    31/*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
    32
    33#include "scip/heur_trivial.h"
    34#include "scip/pub_heur.h"
    35#include "scip/pub_message.h"
    36#include "scip/pub_var.h"
    37#include "scip/scip_heur.h"
    38#include "scip/scip_message.h"
    39#include "scip/scip_numerics.h"
    40#include "scip/scip_prob.h"
    41#include "scip/scip_sol.h"
    43
    44
    45#define HEUR_NAME "trivial"
    46#define HEUR_DESC "start heuristic which tries some trivial solutions"
    47#define HEUR_DISPCHAR SCIP_HEURDISPCHAR_TRIVIAL
    48#define HEUR_PRIORITY 10000
    49#define HEUR_FREQ 0
    50#define HEUR_FREQOFS 0
    51#define HEUR_MAXDEPTH -1
    52#define HEUR_TIMING SCIP_HEURTIMING_BEFOREPRESOL | SCIP_HEURTIMING_BEFORENODE
    53#define HEUR_USESSUBSCIP FALSE /**< does the heuristic use a secondary SCIP instance? */
    54
    55/*
    56 * Local methods
    57 */
    58
    59/** copy method for primal heuristic plugins (called when SCIP copies plugins) */
    60static
    61SCIP_DECL_HEURCOPY(heurCopyTrivial)
    62{ /*lint --e{715}*/
    63 assert(scip != NULL);
    64 assert(heur != NULL);
    65
    67
    68 /* call inclusion method of primal heuristic */
    70
    71 return SCIP_OKAY;
    72}
    73
    74
    75/** execution method of primal heuristic */
    76static
    77SCIP_DECL_HEUREXEC(heurExecTrivial)
    78{ /*lint --e{715}*/
    79 SCIP_VAR** vars;
    80 SCIP_SOL* zerosol; /* solution where all variables are set next to zero within bounds */
    81 SCIP_SOL* lbsol; /* solution where all variables are set to their lower bounds */
    82 SCIP_SOL* ubsol; /* solution where all variables are set to their upper bounds */
    83 SCIP_SOL* locksol; /* solution where all variables are set to the bound with the fewer locks */
    84 SCIP_Real large;
    85 SCIP_Bool difflb;
    86 SCIP_Bool diffub;
    87 SCIP_Bool difflock;
    88 SCIP_Bool success;
    89 int nvars;
    90 int i;
    91
    92 *result = SCIP_DIDNOTFIND;
    93
    94 /* initialize data structure */
    95 SCIP_CALL( SCIPcreateSol(scip, &zerosol, heur) );
    96 SCIP_CALL( SCIPcreateSol(scip, &lbsol, heur) );
    97 SCIP_CALL( SCIPcreateSol(scip, &ubsol, heur) );
    98 SCIP_CALL( SCIPcreateSol(scip, &locksol, heur) );
    99
    100 /* determine large value to set variables to */
    101 large = SCIPround(scip, MIN(1.0 / SCIPfeastol(scip), SCIPgetHugeValue(scip)) / 10.0); /*lint !e666 */
    102
    103 /* check zero solution once */
    104 difflb = FALSE;
    105 diffub = FALSE;
    106 difflock = FALSE;
    107
    108 SCIP_CALL( SCIPgetVarsData(scip, &vars, &nvars, NULL, NULL, NULL, NULL) );
    109 assert(vars != NULL || nvars == 0);
    110
    111 for( i = 0; i < nvars; ++i )
    112 {
    113 SCIP_Real lb;
    114 SCIP_Real ub;
    115 SCIP_Real zeroval;
    116 SCIP_Real solval;
    117
    118 assert(vars != NULL); /* this assert is needed for flexelint */
    119
    120 lb = SCIPvarGetLbLocal(vars[i]);
    121 ub = SCIPvarGetUbLocal(vars[i]);
    122
    123 /* if problem is obviously infeasible due to empty domain, stop */
    124 if( SCIPisFeasGT(scip, lb, ub) )
    125 goto TERMINATE;
    126
    127 /* set bounds to sufficient large value */
    128 if( SCIPisInfinity(scip, -lb) )
    129 lb = MIN(-large, ub);
    130 if( SCIPisInfinity(scip, ub) )
    131 ub = MAX(large, lb);
    132
    133 /* set value next to zero within bounds */
    134 zeroval = MAX(MIN(0.0, ub), lb);
    135
    136 /* set value to the bound with fewer locks, if tie choose an average value */
    138 solval = lb;
    140 solval = ub;
    141 else
    142 {
    143 solval = (lb+ub)/2.0;
    144
    145 /* if a tie occurs, roughly every third integer variable will be rounded up */
    146 if( SCIPvarIsIntegral(vars[i]) )
    147 solval = i % 3 == 0 ? SCIPceil(scip,solval) : SCIPfloor(scip,solval);
    148
    149 assert(SCIPisFeasLE(scip,SCIPvarGetLbLocal(vars[i]),solval) && SCIPisFeasLE(scip,solval,SCIPvarGetUbLocal(vars[i])));
    150 }
    151
    152 if( !SCIPisEQ(scip, lb, zeroval) )
    153 difflb = TRUE;
    154
    155 if( !SCIPisEQ(scip, ub, zeroval) )
    156 diffub = TRUE;
    157
    158 if( !SCIPisEQ(scip, solval, zeroval) )
    159 difflock = TRUE;
    160
    161 /* set variable to values */
    162 SCIP_CALL( SCIPsetSolVal(scip, zerosol, vars[i], zeroval) );
    163 SCIP_CALL( SCIPsetSolVal(scip, lbsol, vars[i], lb) );
    164 SCIP_CALL( SCIPsetSolVal(scip, ubsol, vars[i], ub) );
    165 SCIP_CALL( SCIPsetSolVal(scip, locksol, vars[i], solval) );
    166 }
    167
    168 /* try zero solution */
    169 SCIPdebugMsg(scip, "try zero solution\n");
    170 SCIP_CALL( SCIPtrySol(scip, zerosol, FALSE, FALSE, FALSE, FALSE, TRUE, &success) );
    171
    172 if( success )
    173 {
    174 SCIPdebugMsg(scip, "found feasible zero solution:\n");
    175 SCIPdebug( SCIP_CALL( SCIPprintSol(scip, zerosol, NULL, FALSE) ) );
    176
    177 *result = SCIP_FOUNDSOL;
    178 }
    179
    180 /* try lower bound solution */
    181 if( difflb )
    182 {
    183 SCIPdebugMsg(scip, "try lower bound solution\n");
    184 SCIP_CALL( SCIPtrySol(scip, lbsol, FALSE, FALSE, FALSE, FALSE, TRUE, &success) );
    185
    186 if( success )
    187 {
    188 SCIPdebugMsg(scip, "found feasible lower bound solution:\n");
    190
    191 *result = SCIP_FOUNDSOL;
    192 }
    193 }
    194
    195 /* try upper bound solution */
    196 if( diffub )
    197 {
    198 SCIPdebugMsg(scip, "try upper bound solution\n");
    199 SCIP_CALL( SCIPtrySol(scip, ubsol, FALSE, FALSE, FALSE, FALSE, TRUE, &success) );
    200
    201 if( success )
    202 {
    203 SCIPdebugMsg(scip, "found feasible upper bound solution:\n");
    205
    206 *result = SCIP_FOUNDSOL;
    207 }
    208 }
    209
    210 /* try lock solution */
    211 if( difflock )
    212 {
    213 SCIPdebugMsg(scip, "try lock solution\n");
    214 SCIP_CALL( SCIPtrySol(scip, locksol, FALSE, FALSE, FALSE, FALSE, TRUE, &success) );
    215
    216 if( success )
    217 {
    218 SCIPdebugMsg(scip, "found feasible lock solution:\n");
    219 SCIPdebug( SCIP_CALL( SCIPprintSol(scip, locksol, NULL, FALSE) ) );
    220
    221 *result = SCIP_FOUNDSOL;
    222 }
    223 }
    224
    225TERMINATE:
    226 /* free solutions */
    227 SCIP_CALL( SCIPfreeSol(scip, &locksol) );
    228 SCIP_CALL( SCIPfreeSol(scip, &ubsol) );
    229 SCIP_CALL( SCIPfreeSol(scip, &lbsol) );
    230 SCIP_CALL( SCIPfreeSol(scip, &zerosol) );
    231
    232 return SCIP_OKAY;
    233}
    234
    235
    236/*
    237 * primal heuristic specific interface methods
    238 */
    239
    240/** creates the trivial primal heuristic and includes it in SCIP */
    242 SCIP* scip /**< SCIP data structure */
    243 )
    244{
    245 SCIP_HEUR* heur;
    246
    247 /* include primal heuristic */
    250 HEUR_MAXDEPTH, HEUR_TIMING, HEUR_USESSUBSCIP, heurExecTrivial, NULL) );
    251
    252 assert(heur != NULL);
    253
    254 /* primal heuristic is safe to use in exact solving mode */
    255 SCIPheurMarkExact(heur);
    256
    257 /* set non-NULL pointers to callback methods */
    258 SCIP_CALL( SCIPsetHeurCopy(scip, heur, heurCopyTrivial) );
    259
    260 return SCIP_OKAY;
    261}
    #define NULL
    Definition: def.h:257
    #define SCIP_Bool
    Definition: def.h:100
    #define MIN(x, y)
    Definition: def.h:233
    #define SCIP_STRINGEQ(name, reference, retcode)
    Definition: def.h:454
    #define SCIP_Real
    Definition: def.h:165
    #define TRUE
    Definition: def.h:102
    #define FALSE
    Definition: def.h:103
    #define MAX(x, y)
    Definition: def.h:229
    #define SCIP_CALL(x)
    Definition: def.h:364
    SCIP_RETCODE SCIPgetVarsData(SCIP *scip, SCIP_VAR ***vars, int *nvars, int *nbinvars, int *nintvars, int *nimplvars, int *ncontvars)
    Definition: scip_prob.c:2115
    #define SCIPdebugMsg
    Definition: scip_message.h:78
    SCIP_RETCODE SCIPincludeHeurTrivial(SCIP *scip)
    Definition: heur_trivial.c:241
    SCIP_RETCODE SCIPsetHeurCopy(SCIP *scip, SCIP_HEUR *heur, SCIP_DECL_HEURCOPY((*heurcopy)))
    Definition: scip_heur.c:167
    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
    void SCIPheurMarkExact(SCIP_HEUR *heur)
    Definition: heur.c:1457
    const char * SCIPheurGetName(SCIP_HEUR *heur)
    Definition: heur.c:1467
    SCIP_RETCODE SCIPcreateSol(SCIP *scip, SCIP_SOL **sol, SCIP_HEUR *heur)
    Definition: scip_sol.c:514
    SCIP_RETCODE SCIPfreeSol(SCIP *scip, SCIP_SOL **sol)
    Definition: scip_sol.c:1250
    SCIP_RETCODE SCIPprintSol(SCIP *scip, SCIP_SOL *sol, FILE *file, SCIP_Bool printzeros)
    Definition: scip_sol.c:2351
    SCIP_RETCODE SCIPtrySol(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:4017
    SCIP_RETCODE SCIPsetSolVal(SCIP *scip, SCIP_SOL *sol, SCIP_VAR *var, SCIP_Real val)
    Definition: scip_sol.c:1569
    SCIP_Real SCIPfloor(SCIP *scip, SCIP_Real val)
    SCIP_Bool SCIPisInfinity(SCIP *scip, SCIP_Real val)
    SCIP_Real SCIPround(SCIP *scip, SCIP_Real val)
    SCIP_Bool SCIPisFeasLE(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
    SCIP_Real SCIPfeastol(SCIP *scip)
    SCIP_Real SCIPgetHugeValue(SCIP *scip)
    SCIP_Real SCIPceil(SCIP *scip, SCIP_Real val)
    SCIP_Bool SCIPisFeasGT(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
    SCIP_Bool SCIPisEQ(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
    int SCIPvarGetNLocksUpType(SCIP_VAR *var, SCIP_LOCKTYPE locktype)
    Definition: var.c:4380
    SCIP_Real SCIPvarGetUbLocal(SCIP_VAR *var)
    Definition: var.c:24300
    SCIP_Bool SCIPvarIsIntegral(SCIP_VAR *var)
    Definition: var.c:23522
    SCIP_Real SCIPvarGetLbLocal(SCIP_VAR *var)
    Definition: var.c:24266
    int SCIPvarGetNLocksDownType(SCIP_VAR *var, SCIP_LOCKTYPE locktype)
    Definition: var.c:4322
    static SCIP_DECL_HEURCOPY(heurCopyTrivial)
    Definition: heur_trivial.c:61
    #define HEUR_TIMING
    Definition: heur_trivial.c:52
    #define HEUR_FREQOFS
    Definition: heur_trivial.c:50
    #define HEUR_DESC
    Definition: heur_trivial.c:46
    #define HEUR_DISPCHAR
    Definition: heur_trivial.c:47
    #define HEUR_MAXDEPTH
    Definition: heur_trivial.c:51
    #define HEUR_PRIORITY
    Definition: heur_trivial.c:48
    #define HEUR_NAME
    Definition: heur_trivial.c:45
    #define HEUR_FREQ
    Definition: heur_trivial.c:49
    static SCIP_DECL_HEUREXEC(heurExecTrivial)
    Definition: heur_trivial.c:77
    #define HEUR_USESSUBSCIP
    Definition: heur_trivial.c:53
    trivial primal heuristic
    public methods for primal heuristics
    public methods for message output
    #define SCIPdebug(x)
    Definition: pub_message.h:93
    public methods for problem variables
    public methods for primal heuristic plugins and divesets
    public methods for message handling
    public methods for numerical tolerances
    public methods for global and local (sub)problems
    public methods for solutions
    public methods for querying solving statistics
    @ 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
    @ SCIP_LOCKTYPE_MODEL
    Definition: type_var.h:141