Scippy

    SCIP

    Solving Constraint Integer Programs

    vardata_binpacking.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-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 vardata_binpacking.c
    26 * @brief Variable data containing the ids of constraints in which the variable appears
    27 * @author Timo Berthold
    28 * @author Stefan Heinz
    29 *
    30 * This file implements the handling of the variable data which is attached to each file. See SCIP_VarData and \ref BINPACKING_PRICER.
    31 */
    32
    33/*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
    34
    35#include "probdata_binpacking.h"
    36#include "vardata_binpacking.h"
    37
    38/** Variable data which is attached to all variables.
    39 *
    40 * This variable data is used to store in which constraints this variable appears. Therefore, the variable data
    41 * contains the ids of constraints in which the variable is part of. Hence, that data give us a column view.
    42 */
    43struct SCIP_VarData
    44{
    45 int* consids;
    46 int nconsids;
    47};
    48
    49/**@name Local methods
    50 *
    51 * @{
    52 */
    53
    54/** create a vardata */
    55static
    57 SCIP* scip, /**< SCIP data structure */
    58 SCIP_VARDATA** vardata, /**< pointer to vardata */
    59 int* consids, /**< array of constraints ids */
    60 int nconsids /**< number of constraints */
    61 )
    62{
    64
    65 SCIP_CALL( SCIPduplicateBlockMemoryArray(scip, &(*vardata)->consids, consids, nconsids) );
    66 SCIPsortInt((*vardata)->consids, nconsids);
    67
    68 (*vardata)->nconsids = nconsids;
    69
    70 return SCIP_OKAY;
    71}
    72
    73/** frees user data of variable */
    74static
    76 SCIP* scip, /**< SCIP data structure */
    77 SCIP_VARDATA** vardata /**< vardata to delete */
    78 )
    79{
    80 SCIPfreeBlockMemoryArray(scip, &(*vardata)->consids, (*vardata)->nconsids);
    81 SCIPfreeBlockMemory(scip, vardata);
    82
    83 return SCIP_OKAY;
    84}
    85
    86/**@} */
    87
    88
    89/**@name Callback methods
    90 *
    91 * @{
    92 */
    93
    94/** frees user data of transformed variable (called when the transformed variable is freed) */
    95static
    96SCIP_DECL_VARDELTRANS(vardataDelTrans)
    97{
    98 SCIP_CALL( vardataDelete(scip, vardata) );
    99
    100 return SCIP_OKAY;
    101}/*lint !e715*/
    102
    103/**@} */
    104
    105
    106/**@name Interface methods
    107 *
    108 * @{
    109 */
    110
    111/** create variable data */
    113 SCIP* scip, /**< SCIP data structure */
    114 SCIP_VARDATA** vardata, /**< pointer to vardata */
    115 int* consids, /**< array of constraints ids */
    116 int nconsids /**< number of constraints */
    117 )
    118{
    119 SCIP_CALL( vardataCreate(scip, vardata, consids, nconsids) );
    120
    121 return SCIP_OKAY;
    122}
    123
    124/** get number of constraints */
    126 SCIP_VARDATA* vardata /**< variable data */
    127 )
    128{
    129 return vardata->nconsids;
    130}
    131
    132/** returns sorted constraint id array */
    134 SCIP_VARDATA* vardata /**< variable data */
    135 )
    136{
    137 /* check if the consids are sorted */
    138#ifndef NDEBUG
    139 {
    140 int i;
    141
    142 for( i = 1; i < vardata->nconsids; ++i )
    143 assert( vardata->consids[i-1] < vardata->consids[i]);
    144 }
    145#endif
    146
    147 return vardata->consids;
    148}
    149
    150/** creates variable */
    152 SCIP* scip, /**< SCIP data structure */
    153 SCIP_VAR** var, /**< pointer to variable object */
    154 const char* name, /**< name of variable, or NULL for automatic name creation */
    155 SCIP_Real obj, /**< objective function value */
    156 SCIP_Bool initial, /**< should var's column be present in the initial root LP? */
    157 SCIP_Bool removable, /**< is var's column removable from the LP (due to aging or cleanup)? */
    158 SCIP_VARDATA* vardata /**< user data for this specific variable */
    159 )
    160{
    161 assert(scip != NULL);
    162 assert(var != NULL);
    163
    164 /* create a basic variable object */
    165 SCIP_CALL( SCIPcreateVarBasic(scip, var, name, 0.0, 1.0, obj, SCIP_VARTYPE_BINARY) );
    166 assert(*var != NULL);
    167
    168 /* set callback functions */
    169 SCIPvarSetData(*var, vardata);
    170 SCIPvarSetDeltransData(*var, vardataDelTrans);
    171
    172 /* set initial and removable flag */
    173 SCIP_CALL( SCIPvarSetInitial(*var, initial) );
    174 SCIP_CALL( SCIPvarSetRemovable(*var, removable) );
    175
    177
    178 SCIPdebug( SCIPprintVar(scip, *var, NULL) );
    179
    180 return SCIP_OKAY;
    181}
    182
    183/** prints vardata to file stream */
    185 SCIP* scip, /**< SCIP data structure */
    186 SCIP_VARDATA* vardata, /**< variable data */
    187 FILE* file /**< the text file to store the information into */
    188 )
    189{
    190 SCIP_PROBDATA* probdata;
    191 int* ids;
    192 int i;
    193
    194 probdata = SCIPgetProbData(scip);
    195 assert(probdata != NULL);
    196
    197 ids = SCIPprobdataGetIds(probdata);
    198 assert(ids != NULL);
    199
    200 SCIPinfoMessage(scip, file, "consids = {");
    201
    202 for( i = 0; i < vardata->nconsids; ++i )
    203 {
    204 SCIPinfoMessage(scip, file, "%d->%d", ids[vardata->consids[i]], vardata->consids[i]);
    205
    206 if( i < vardata->nconsids - 1 )
    207 SCIPinfoMessage(scip, file, ",");
    208 }
    209
    210 SCIPinfoMessage(scip, file, "}\n");
    211}
    212
    213/**@} */
    #define NULL
    Definition: def.h:248
    #define SCIP_Bool
    Definition: def.h:91
    #define SCIP_Real
    Definition: def.h:156
    #define SCIP_CALL(x)
    Definition: def.h:355
    SCIP_PROBDATA * SCIPgetProbData(SCIP *scip)
    Definition: scip_prob.c:1139
    void SCIPinfoMessage(SCIP *scip, FILE *file, const char *formatstr,...)
    Definition: scip_message.c:208
    #define SCIPfreeBlockMemoryArray(scip, ptr, num)
    Definition: scip_mem.h:110
    #define SCIPfreeBlockMemory(scip, ptr)
    Definition: scip_mem.h:108
    #define SCIPallocBlockMemory(scip, ptr)
    Definition: scip_mem.h:89
    #define SCIPduplicateBlockMemoryArray(scip, ptr, source, num)
    Definition: scip_mem.h:105
    void SCIPvarSetDeltransData(SCIP_VAR *var, SCIP_DECL_VARDELTRANS((*vardeltrans)))
    Definition: var.c:23332
    void SCIPvarMarkDeletable(SCIP_VAR *var)
    Definition: var.c:23546
    SCIP_RETCODE SCIPvarSetInitial(SCIP_VAR *var, SCIP_Bool initial)
    Definition: var.c:23354
    void SCIPvarSetData(SCIP_VAR *var, SCIP_VARDATA *vardata)
    Definition: var.c:23297
    SCIP_RETCODE SCIPvarSetRemovable(SCIP_VAR *var, SCIP_Bool removable)
    Definition: var.c:23370
    SCIP_RETCODE SCIPprintVar(SCIP *scip, SCIP_VAR *var, FILE *file)
    Definition: scip_var.c:12465
    SCIP_RETCODE SCIPcreateVarBasic(SCIP *scip, SCIP_VAR **var, const char *name, SCIP_Real lb, SCIP_Real ub, SCIP_Real obj, SCIP_VARTYPE vartype)
    Definition: scip_var.c:184
    void SCIPsortInt(int *intarray, int len)
    int * SCIPprobdataGetIds(SCIP_PROBDATA *probdata)
    Problem data for binpacking problem.
    #define SCIPdebug(x)
    Definition: pub_message.h:93
    struct SCIP_ProbData SCIP_PROBDATA
    Definition: type_prob.h:53
    @ SCIP_OKAY
    Definition: type_retcode.h:42
    enum SCIP_Retcode SCIP_RETCODE
    Definition: type_retcode.h:63
    struct SCIP_VarData SCIP_VARDATA
    Definition: type_var.h:167
    @ SCIP_VARTYPE_BINARY
    Definition: type_var.h:64
    int * SCIPvardataGetConsids(SCIP_VARDATA *vardata)
    static SCIP_RETCODE vardataDelete(SCIP *scip, SCIP_VARDATA **vardata)
    void SCIPvardataPrint(SCIP *scip, SCIP_VARDATA *vardata, FILE *file)
    SCIP_RETCODE SCIPcreateVarBinpacking(SCIP *scip, SCIP_VAR **var, const char *name, SCIP_Real obj, SCIP_Bool initial, SCIP_Bool removable, SCIP_VARDATA *vardata)
    SCIP_RETCODE SCIPvardataCreateBinpacking(SCIP *scip, SCIP_VARDATA **vardata, int *consids, int nconsids)
    static SCIP_RETCODE vardataCreate(SCIP *scip, SCIP_VARDATA **vardata, int *consids, int nconsids)
    static SCIP_DECL_VARDELTRANS(vardataDelTrans)
    int SCIPvardataGetNConsids(SCIP_VARDATA *vardata)
    Variable data containing the ids of constraints in which the variable appears.