Scippy

    SCIP

    Solving Constraint Integer Programs

    cons_knapsack.h
    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 cons_knapsack.h
    26 * @ingroup CONSHDLRS
    27 * @brief Constraint handler for knapsack constraints of the form \f$a^T x \le b\f$, x binary and \f$a \ge 0\f$.
    28 * @author Tobias Achterberg
    29 * @author Kati Wolter
    30 * @author Michael Winkler
    31 *
    32 */
    33
    34/*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
    35
    36#ifndef __SCIP_CONS_KNAPSACK_H__
    37#define __SCIP_CONS_KNAPSACK_H__
    38
    39#include "scip/def.h"
    40#include "scip/type_cons.h"
    41#include "scip/type_lp.h"
    42#include "scip/type_retcode.h"
    43#include "scip/type_scip.h"
    44#include "scip/type_sepa.h"
    45#include "scip/type_sol.h"
    46#include "scip/type_var.h"
    47
    48#ifdef __cplusplus
    49extern "C" {
    50#endif
    51
    52/** creates the handler for knapsack constraints and includes it in SCIP
    53 *
    54 * @ingroup ConshdlrIncludes
    55 * */
    56SCIP_EXPORT
    58 SCIP* scip /**< SCIP data structure */
    59 );
    60
    61/**@addtogroup CONSHDLRS
    62 *
    63 * @{
    64 *
    65 * @name Knapsack Constraints
    66 *
    67 * @{
    68 *
    69 * This constraint handler handles a special type of linear constraints, namely knapsack constraints.
    70 * A knapsack constraint has the form
    71 * \f[
    72 * \sum_{i=1}^n a_i x_i \leq b
    73 * \f]
    74 * with non-negative integer coefficients \f$a_i\f$, integer right-hand side \f$b\f$, and binary variables \f$x_i\f$.
    75 */
    76
    77/** creates and captures a knapsack constraint
    78 *
    79 * @note the constraint gets captured, hence at one point you have to release it using the method SCIPreleaseCons()
    80 */
    81SCIP_EXPORT
    83 SCIP* scip, /**< SCIP data structure */
    84 SCIP_CONS** cons, /**< pointer to hold the created constraint */
    85 const char* name, /**< name of constraint */
    86 int nvars, /**< number of items in the knapsack */
    87 SCIP_VAR** vars, /**< array with item variables */
    88 SCIP_Longint* weights, /**< array with item weights */
    89 SCIP_Longint capacity, /**< capacity of knapsack (right hand side of inequality) */
    90 SCIP_Bool initial, /**< should the LP relaxation of constraint be in the initial LP?
    91 * Usually set to TRUE. Set to FALSE for 'lazy constraints'. */
    92 SCIP_Bool separate, /**< should the constraint be separated during LP processing?
    93 * Usually set to TRUE. */
    94 SCIP_Bool enforce, /**< should the constraint be enforced during node processing?
    95 * TRUE for model constraints, FALSE for additional, redundant constraints. */
    96 SCIP_Bool check, /**< should the constraint be checked for feasibility?
    97 * TRUE for model constraints, FALSE for additional, redundant constraints. */
    98 SCIP_Bool propagate, /**< should the constraint be propagated during node processing?
    99 * Usually set to TRUE. */
    100 SCIP_Bool local, /**< is constraint only valid locally?
    101 * Usually set to FALSE. Has to be set to TRUE, e.g., for branching constraints. */
    102 SCIP_Bool modifiable, /**< is constraint modifiable (subject to column generation)?
    103 * Usually set to FALSE. In column generation applications, set to TRUE if pricing
    104 * adds coefficients to this constraint. */
    105 SCIP_Bool dynamic, /**< is constraint subject to aging?
    106 * Usually set to FALSE. Set to TRUE for own cuts which
    107 * are separated as constraints. */
    108 SCIP_Bool removable, /**< should the relaxation be removed from the LP due to aging or cleanup?
    109 * Usually set to FALSE. Set to TRUE for 'lazy constraints' and 'user cuts'. */
    110 SCIP_Bool stickingatnode /**< should the constraint always be kept at the node where it was added, even
    111 * if it may be moved to a more global node?
    112 * Usually set to FALSE. Set to TRUE to for constraints that represent node data. */
    113 );
    114
    115/** creates and captures a knapsack constraint
    116 * in its most basic version, i. e., all constraint flags are set to their basic value as explained for the
    117 * method SCIPcreateConsKnapsack(); all flags can be set via SCIPsetConsFLAGNAME-methods in scip.h
    118 *
    119 * @see SCIPcreateConsKnapsack() for information about the basic constraint flag configuration
    120 *
    121 * @note the constraint gets captured, hence at one point you have to release it using the method SCIPreleaseCons()
    122 */
    123SCIP_EXPORT
    125 SCIP* scip, /**< SCIP data structure */
    126 SCIP_CONS** cons, /**< pointer to hold the created constraint */
    127 const char* name, /**< name of constraint */
    128 int nvars, /**< number of items in the knapsack */
    129 SCIP_VAR** vars, /**< array with item variables */
    130 SCIP_Longint* weights, /**< array with item weights */
    131 SCIP_Longint capacity /**< capacity of knapsack */
    132 );
    133
    134/** adds new item to knapsack constraint */
    135SCIP_EXPORT
    137 SCIP* scip, /**< SCIP data structure */
    138 SCIP_CONS* cons, /**< constraint data */
    139 SCIP_VAR* var, /**< item variable */
    140 SCIP_Longint weight /**< item weight */
    141 );
    142
    143/** gets the capacity of the knapsack constraint */
    144SCIP_EXPORT
    146 SCIP* scip, /**< SCIP data structure */
    147 SCIP_CONS* cons /**< constraint data */
    148 );
    149
    150/** changes capacity of the knapsack constraint
    151 *
    152 * @note This method can only be called during problem creation stage (SCIP_STAGE_PROBLEM)
    153 */
    154SCIP_EXPORT
    156 SCIP* scip, /**< SCIP data structure */
    157 SCIP_CONS* cons, /**< constraint data */
    158 SCIP_Longint capacity /**< new capacity of knapsack */
    159 );
    160
    161/** gets the number of items in the knapsack constraint */
    162SCIP_EXPORT
    164 SCIP* scip, /**< SCIP data structure */
    165 SCIP_CONS* cons /**< constraint data */
    166 );
    167
    168/** gets the array of variables in the knapsack constraint; the user must not modify this array! */
    169SCIP_EXPORT
    171 SCIP* scip, /**< SCIP data structure */
    172 SCIP_CONS* cons /**< constraint data */
    173 );
    174
    175/** gets the array of weights in the knapsack constraint; the user must not modify this array! */
    176SCIP_EXPORT
    178 SCIP* scip, /**< SCIP data structure */
    179 SCIP_CONS* cons /**< constraint data */
    180 );
    181
    182/** gets the dual solution of the knapsack constraint in the current LP */
    183SCIP_EXPORT
    185 SCIP* scip, /**< SCIP data structure */
    186 SCIP_CONS* cons /**< constraint data */
    187 );
    188
    189/** gets the dual Farkas value of the knapsack constraint in the current infeasible LP */
    190SCIP_EXPORT
    192 SCIP* scip, /**< SCIP data structure */
    193 SCIP_CONS* cons /**< constraint data */
    194 );
    195
    196/** returns the linear relaxation of the given knapsack constraint; may return NULL if no LP row was yet created;
    197 * the user must not modify the row!
    198 */
    199SCIP_EXPORT
    201 SCIP* scip, /**< SCIP data structure */
    202 SCIP_CONS* cons /**< constraint data */
    203 );
    204
    205/** creates and returns the row of the given knapsack constraint */
    207 SCIP* scip, /**< SCIP data structure */
    208 SCIP_CONS* cons /**< constraint data */
    209 );
    210
    211/** solves knapsack problem in maximization form exactly using dynamic programming;
    212 * if needed, one can provide arrays to store all selected items and all not selected items
    213 *
    214 * @note in case you provide the solitems or nonsolitems array you also have to provide the counter part, as well
    215 *
    216 * @note the algorithm will first compute a greedy solution and terminate
    217 * if the greedy solution is proven to be optimal.
    218 * The dynamic programming algorithm runs with a time and space complexity
    219 * of O(nitems * capacity).
    220 */
    221SCIP_EXPORT
    223 SCIP* scip, /**< SCIP data structure */
    224 int nitems, /**< number of available items */
    225 SCIP_Longint* weights, /**< item weights */
    226 SCIP_Real* profits, /**< item profits */
    227 SCIP_Longint capacity, /**< capacity of knapsack */
    228 int* items, /**< item numbers */
    229 int* solitems, /**< array to store items in solution, or NULL */
    230 int* nonsolitems, /**< array to store items not in solution, or NULL */
    231 int* nsolitems, /**< pointer to store number of items in solution, or NULL */
    232 int* nnonsolitems, /**< pointer to store number of items not in solution, or NULL */
    233 SCIP_Real* solval, /**< pointer to store optimal solution value, or NULL */
    234 SCIP_Bool* success /**< pointer to store if an error occured during solving
    235 * (normally a memory problem) */
    236 );
    237
    238/** solves knapsack problem in maximization form approximately by solving the LP-relaxation of the problem using Dantzig's
    239 * method and rounding down the solution; if needed, one can provide arrays to store all selected items and all not
    240 * selected items
    241 */
    242SCIP_EXPORT
    244 SCIP* scip, /**< SCIP data structure */
    245 int nitems, /**< number of available items */
    246 SCIP_Longint* weights, /**< item weights */
    247 SCIP_Real* profits, /**< item profits */
    248 SCIP_Longint capacity, /**< capacity of knapsack */
    249 int* items, /**< item numbers */
    250 int* solitems, /**< array to store items in solution, or NULL */
    251 int* nonsolitems, /**< array to store items not in solution, or NULL */
    252 int* nsolitems, /**< pointer to store number of items in solution, or NULL */
    253 int* nnonsolitems, /**< pointer to store number of items not in solution, or NULL */
    254 SCIP_Real* solval /**< pointer to store optimal solution value, or NULL */
    255 );
    256
    257/** separates different classes of valid inequalities for the 0-1 knapsack problem */
    258SCIP_EXPORT
    260 SCIP* scip, /**< SCIP data structure */
    261 SCIP_CONS* cons, /**< originating constraint of the knapsack problem, or NULL */
    262 SCIP_SEPA* sepa, /**< originating separator of the knapsack problem, or NULL */
    263 SCIP_VAR** vars, /**< variables in knapsack constraint */
    264 int nvars, /**< number of variables in knapsack constraint */
    265 SCIP_Longint* weights, /**< weights of variables in knapsack constraint */
    266 SCIP_Longint capacity, /**< capacity of knapsack */
    267 SCIP_SOL* sol, /**< primal SCIP solution to separate, NULL for current LP solution */
    268 SCIP_Bool usegubs, /**< should GUB information be used for separation? */
    269 SCIP_Bool* cutoff, /**< pointer to store whether a cutoff has been detected */
    270 int* ncuts /**< pointer to add up the number of found cuts */
    271 );
    272
    273/* relaxes given general linear constraint into a knapsack constraint and separates lifted knapsack cover inequalities */
    274SCIP_EXPORT
    276 SCIP* scip, /**< SCIP data structure */
    277 SCIP_CONS* cons, /**< originating constraint of the knapsack problem, or NULL */
    278 SCIP_SEPA* sepa, /**< originating separator of the knapsack problem, or NULL */
    279 int nknapvars, /**< number of variables in the continuous knapsack constraint */
    280 SCIP_VAR** knapvars, /**< variables in the continuous knapsack constraint */
    281 SCIP_Real* knapvals, /**< coefficients of the variables in the continuous knapsack constraint */
    282 SCIP_Real valscale, /**< -1.0 if lhs of row is used as rhs of c. k. constraint, +1.0 otherwise */
    283 SCIP_Real rhs, /**< right hand side of the continuous knapsack constraint */
    284 SCIP_SOL* sol, /**< primal CIP solution, NULL for current LP solution */
    285 SCIP_Bool* cutoff, /**< pointer to store whether a cutoff was found */
    286 int* ncuts /**< pointer to add up the number of found cuts */
    287 );
    288
    289/** cleans up (multi-)aggregations and fixings from knapsack constraints */
    290SCIP_EXPORT
    292 SCIP* scip, /**< SCIP data structure */
    293 SCIP_Bool onlychecked, /**< should only checked constraints be cleaned up? */
    294 SCIP_Bool* infeasible, /**< pointer to return whether the problem was detected to be infeasible */
    295 int* ndelconss /**< pointer to count number of deleted constraints */
    296 );
    297
    298/** @} */
    299
    300/** @} */
    301
    302#ifdef __cplusplus
    303}
    304#endif
    305
    306#endif
    common defines and data types used in all packages of SCIP
    #define SCIP_Longint
    Definition: def.h:141
    #define SCIP_Bool
    Definition: def.h:91
    #define SCIP_Real
    Definition: def.h:156
    int SCIPgetNVarsKnapsack(SCIP *scip, SCIP_CONS *cons)
    SCIP_RETCODE SCIPcreateRowKnapsack(SCIP *scip, SCIP_CONS *cons)
    SCIP_RETCODE SCIPaddCoefKnapsack(SCIP *scip, SCIP_CONS *cons, SCIP_VAR *var, SCIP_Longint weight)
    SCIP_RETCODE SCIPsolveKnapsackApproximately(SCIP *scip, int nitems, SCIP_Longint *weights, SCIP_Real *profits, SCIP_Longint capacity, int *items, int *solitems, int *nonsolitems, int *nsolitems, int *nnonsolitems, SCIP_Real *solval)
    SCIP_RETCODE SCIPcleanupConssKnapsack(SCIP *scip, SCIP_Bool onlychecked, SCIP_Bool *infeasible, int *ndelconss)
    SCIP_RETCODE SCIPseparateKnapsackCuts(SCIP *scip, SCIP_CONS *cons, SCIP_SEPA *sepa, SCIP_VAR **vars, int nvars, SCIP_Longint *weights, SCIP_Longint capacity, SCIP_SOL *sol, SCIP_Bool usegubs, SCIP_Bool *cutoff, int *ncuts)
    SCIP_RETCODE SCIPcreateConsBasicKnapsack(SCIP *scip, SCIP_CONS **cons, const char *name, int nvars, SCIP_VAR **vars, SCIP_Longint *weights, SCIP_Longint capacity)
    SCIP_RETCODE SCIPchgCapacityKnapsack(SCIP *scip, SCIP_CONS *cons, SCIP_Longint capacity)
    SCIP_RETCODE SCIPseparateRelaxedKnapsack(SCIP *scip, SCIP_CONS *cons, SCIP_SEPA *sepa, int nknapvars, SCIP_VAR **knapvars, SCIP_Real *knapvals, SCIP_Real valscale, SCIP_Real rhs, SCIP_SOL *sol, SCIP_Bool *cutoff, int *ncuts)
    SCIP_RETCODE SCIPsolveKnapsackExactly(SCIP *scip, int nitems, SCIP_Longint *weights, SCIP_Real *profits, SCIP_Longint capacity, int *items, int *solitems, int *nonsolitems, int *nsolitems, int *nnonsolitems, SCIP_Real *solval, SCIP_Bool *success)
    SCIP_RETCODE SCIPcreateConsKnapsack(SCIP *scip, SCIP_CONS **cons, const char *name, int nvars, SCIP_VAR **vars, SCIP_Longint *weights, SCIP_Longint capacity, SCIP_Bool initial, SCIP_Bool separate, SCIP_Bool enforce, SCIP_Bool check, SCIP_Bool propagate, SCIP_Bool local, SCIP_Bool modifiable, SCIP_Bool dynamic, SCIP_Bool removable, SCIP_Bool stickingatnode)
    SCIP_Longint * SCIPgetWeightsKnapsack(SCIP *scip, SCIP_CONS *cons)
    SCIP_Longint SCIPgetCapacityKnapsack(SCIP *scip, SCIP_CONS *cons)
    SCIP_VAR ** SCIPgetVarsKnapsack(SCIP *scip, SCIP_CONS *cons)
    SCIP_Real SCIPgetDualfarkasKnapsack(SCIP *scip, SCIP_CONS *cons)
    SCIP_ROW * SCIPgetRowKnapsack(SCIP *scip, SCIP_CONS *cons)
    SCIP_Real SCIPgetDualsolKnapsack(SCIP *scip, SCIP_CONS *cons)
    SCIP_RETCODE SCIPincludeConshdlrKnapsack(SCIP *scip)
    static SCIP_RETCODE separate(SCIP *scip, SCIP_SEPA *sepa, SCIP_SOL *sol, SCIP_RESULT *result)
    Main separation function.
    Definition: sepa_flower.c:1221
    type definitions for constraints and constraint handlers
    type definitions for LP management
    type definitions for return codes for SCIP methods
    enum SCIP_Retcode SCIP_RETCODE
    Definition: type_retcode.h:63
    type definitions for SCIP's main datastructure
    type definitions for separators
    type definitions for storing primal CIP solutions
    type definitions for problem variables