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