Scippy

    SCIP

    Solving Constraint Integer Programs

    objprobdata.cpp
    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-2025 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 objprobdata.cpp
    26 * @brief C++ wrapper for user problem data
    27 * @author Tobias Achterberg
    28 */
    29
    30/*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
    31
    32#include <cassert>
    33
    34#include "objprobdata.h"
    35
    36
    37
    38
    39/*
    40 * Data structures
    41 */
    42
    43/** user problem data */
    44struct SCIP_ProbData
    45{
    46 scip::ObjProbData* objprobdata; /**< user problem data object */
    47 SCIP_Bool deleteobject; /**< should the user problem data object be deleted when problem is freed? */
    48};
    49
    50
    51
    52
    53/*
    54 * Callback methods of user problem data
    55 */
    56
    57extern "C"
    58{
    59
    60/** frees user data of original problem (called when the original problem is freed) */
    61static
    62SCIP_DECL_PROBDELORIG(probDelorigObj)
    63{ /*lint --e{715}*/
    64 assert(probdata != NULL);
    65 assert(*probdata != NULL);
    66 assert((*probdata)->objprobdata != NULL);
    67
    68 /* call virtual method of probdata object */
    69 SCIP_CALL( (*probdata)->objprobdata->scip_delorig(scip) );
    70
    71 /* free probdata object */
    72 if( (*probdata)->deleteobject )
    73 delete (*probdata)->objprobdata;
    74
    75 /* free probdata data */
    76 delete *probdata;
    77 *probdata = 0; /*lint !e64*/
    78
    79 return SCIP_OKAY;
    80}
    81
    82
    83/** creates user data of transformed problem by transforming the original user problem data
    84 * (called after problem was transformed)
    85 */
    86static
    88{ /*lint --e{715}*/
    89 scip::ObjProbData* objprobdata; /*lint !e78 !e40 !e55 !e530 !e522*/
    90 SCIP_Bool deleteobject;
    91
    92 assert(sourcedata != NULL);
    93 assert(sourcedata->objprobdata != NULL);
    94 assert(targetdata != NULL);
    95 assert(*targetdata == NULL);
    96
    97 /* call virtual method of probdata object */
    98 SCIP_CALL( sourcedata->objprobdata->scip_trans(scip, &objprobdata, &deleteobject) ); /*lint !e40*/
    99
    100 /* create transformed user problem data */
    101 *targetdata = new SCIP_PROBDATA;
    102 (*targetdata)->objprobdata = objprobdata; /*lint !e40*/
    103 (*targetdata)->deleteobject = deleteobject;
    104
    105 return SCIP_OKAY;
    106}
    107
    108
    109/** frees user data of transformed problem (called when the transformed problem is freed) */
    110static
    112{ /*lint --e{715}*/
    113 assert(probdata != NULL);
    114 assert(*probdata != NULL);
    115 assert((*probdata)->objprobdata != NULL);
    116
    117 /* call virtual method of probdata object */
    118 SCIP_CALL( (*probdata)->objprobdata->scip_deltrans(scip) );
    119
    120 /* free probdata object */
    121 if( (*probdata)->deleteobject )
    122 delete (*probdata)->objprobdata;
    123
    124 /* free probdata data */
    125 delete *probdata;
    126 *probdata = 0; /*lint !e64*/
    127
    128 return SCIP_OKAY;
    129}
    130
    131
    132/** solving process initialization method of transformed data (called before the branch and bound process begins) */
    133static
    135{ /*lint --e{715}*/
    136 assert(probdata != NULL);
    137 assert(probdata->objprobdata != NULL);
    138
    139 /* call virtual method of probdata object */
    140 SCIP_CALL( probdata->objprobdata->scip_initsol(scip) );
    141
    142 return SCIP_OKAY;
    143}
    144
    145
    146/** solving process deinitialization method of transformed data (called before the branch and bound data is freed) */
    147static
    149{ /*lint --e{715}*/
    150 assert(probdata != NULL);
    151 assert(probdata->objprobdata != NULL);
    152
    153 /* call virtual method of probdata object */
    154 SCIP_CALL( probdata->objprobdata->scip_exitsol(scip, restart) );
    155
    156 return SCIP_OKAY;
    157}
    158
    159/** copies user data if you want to copy it to a subscip */
    160static
    162{ /*lint --e{715}*/
    163 scip::ObjProbData* objprobdata; /*lint !e78 !e40 !e55 !e530 !e522*/
    164
    165 assert(sourcedata != NULL);
    166 assert(sourcedata->objprobdata != NULL);
    167 assert(targetdata != NULL);
    168 assert(*targetdata == NULL);
    169
    170 /* call virtual method of probdata object */
    171 SCIP_CALL( sourcedata->objprobdata->scip_copy(scip, sourcescip, varmap, consmap, &objprobdata, global, result) ); /*lint !e40*/
    172
    173 if( objprobdata != 0 )
    174 {
    175 assert(*result == SCIP_SUCCESS);
    176
    177 /* create trarget user problem data */
    178 *targetdata = new SCIP_PROBDATA;
    179 (*targetdata)->objprobdata = objprobdata; /*lint !e40*/
    180 (*targetdata)->deleteobject = TRUE; /* always delete object, because we created it */
    181 }
    182 else
    183 {
    184 assert(*result == SCIP_DIDNOTRUN);
    185 *targetdata = 0;
    186 }
    187
    188 return SCIP_OKAY;
    189}
    190
    191}
    192
    193
    194
    195/*
    196 * user problem data specific interface methods
    197 */
    198
    199/** creates empty problem, initializes all solving data structures, and sets the user problem data to point to the
    200 * given user data object
    201 */
    203 SCIP* scip, /**< SCIP data structure */
    204 const char* name, /**< problem name */
    205 scip::ObjProbData* objprobdata, /**< user problem data object */
    206 SCIP_Bool deleteobject /**< should the user problem data object be deleted when problem is freed? */
    207 )
    208{
    209 SCIP_PROBDATA* probdata;
    210
    211 /* create user problem data */
    212 probdata = new SCIP_PROBDATA;
    213 probdata->objprobdata = objprobdata;
    214 probdata->deleteobject = deleteobject;
    215
    216 /* create problem */
    217 SCIP_CALL( SCIPcreateProb(scip, name, probDelorigObj, probTransObj, probDeltransObj,
    218 probInitsolObj, probExitsolObj, probCopyObj, probdata) ); /*lint !e429*/
    219
    220 return SCIP_OKAY; /*lint !e429*/
    221}
    222
    223/** gets user problem data object
    224 * Warning! This method should only be called after a problem was created with SCIPcreateObjProb().
    225 * Otherwise, a segmentation fault may arise, or an undefined pointer is returned.
    226 */
    228 SCIP* scip /**< SCIP data structure */
    229 )
    230{
    231 SCIP_PROBDATA* probdata;
    232
    233 probdata = SCIPgetProbData(scip);
    234 assert(probdata != NULL);
    235
    236 return probdata->objprobdata;
    237}
    238
    C++ wrapper for user problem data.
    Definition: objprobdata.h:53
    #define NULL
    Definition: def.h:248
    #define SCIP_Bool
    Definition: def.h:91
    #define TRUE
    Definition: def.h:93
    #define SCIP_CALL(x)
    Definition: def.h:355
    SCIP_PROBDATA * SCIPgetProbData(SCIP *scip)
    Definition: scip_prob.c:1139
    SCIP_RETCODE SCIPcreateProb(SCIP *scip, const char *name, SCIP_DECL_PROBDELORIG((*probdelorig)), SCIP_DECL_PROBTRANS((*probtrans)), SCIP_DECL_PROBDELTRANS((*probdeltrans)), SCIP_DECL_PROBINITSOL((*probinitsol)), SCIP_DECL_PROBEXITSOL((*probexitsol)), SCIP_DECL_PROBCOPY((*probcopy)), SCIP_PROBDATA *probdata)
    Definition: scip_prob.c:119
    static SCIP_DECL_PROBEXITSOL(probExitsolObj)
    static SCIP_DECL_PROBTRANS(probTransObj)
    Definition: objprobdata.cpp:87
    SCIP_RETCODE SCIPcreateObjProb(SCIP *scip, const char *name, scip::ObjProbData *objprobdata, SCIP_Bool deleteobject)
    static SCIP_DECL_PROBDELTRANS(probDeltransObj)
    scip::ObjProbData * SCIPgetObjProbData(SCIP *scip)
    static SCIP_DECL_PROBDELORIG(probDelorigObj)
    Definition: objprobdata.cpp:62
    static SCIP_DECL_PROBCOPY(probCopyObj)
    static SCIP_DECL_PROBINITSOL(probInitsolObj)
    C++ wrapper for user problem data.
    struct SCIP_ProbData SCIP_PROBDATA
    Definition: type_prob.h:53
    @ SCIP_DIDNOTRUN
    Definition: type_result.h:42
    @ SCIP_SUCCESS
    Definition: type_result.h:58
    @ SCIP_OKAY
    Definition: type_retcode.h:42
    enum SCIP_Retcode SCIP_RETCODE
    Definition: type_retcode.h:63