Scippy

GCG

Branch-and-Price & Column Generation for Everyone

colpool.c
Go to the documentation of this file.
1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2/* */
3/* This file is part of the program and library */
4/* GCG --- Generic Column Generation */
5/* a Dantzig-Wolfe decomposition based extension */
6/* of the branch-cut-and-price framework */
7/* SCIP --- Solving Constraint Integer Programs */
8/* */
9/* Copyright (C) 2010-2026 Operations Research, RWTH Aachen University */
10/* Zuse Institute Berlin (ZIB) */
11/* */
12/* Licensed under the Apache License, Version 2.0 (the "License"); */
13/* you may not use this file except in compliance with the License. */
14/* You may obtain a copy of the License at */
15/* */
16/* http://www.apache.org/licenses/LICENSE-2.0 */
17/* */
18/* Unless required by applicable law or agreed to in writing, software */
19/* distributed under the License is distributed on an "AS IS" BASIS, */
20/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
21/* See the License for the specific language governing permissions and */
22/* limitations under the License. */
23/* */
24/* You should have received a copy of the Apache-2.0 license */
25/* along with GCG; see the file LICENSE. If not visit gcg.or.rwth-aachen.de.*/
26/* */
27/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
28
29/**@file colpool.c
30 * @brief methods for storing cols in a col pool (based on SCIP's cut pool)
31 * @author Jonas Witt
32 * @author Erik Muehmer
33 */
34
35/*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
36
37#include <assert.h>
38
39#include "scip/clock.h"
40
41#include "gcg/gcg.h"
42#include "gcg/pub_gcgcol.h"
43#include "gcg/colpool.h"
44#include "gcg/struct_colpool.h"
45#include "gcg/pricestore_gcg.h"
47#include "gcg/pricer_gcg.h"
48#include "gcg/struct_gcgcol.h"
49
50#define GCG_USESMALLTABLES FALSE
51#define GCG_HASHSIZE_COLPOOLS_SMALL 100 /**< size of hash table in col pools for small problems */
52#define GCG_HASHSIZE_COLPOOLS 500 /**< size of hash table in col pools */
53
54/*
55 * dynamic memory arrays
56 */
57
58/** resizes cols array to be able to store at least num entries */
59static
61 GCG_COLPOOL* colpool, /**< col pool */
62 int num /**< minimal number of slots in array */
63 )
64{
65 assert(colpool != NULL);
66
67 if( num > colpool->colssize )
68 {
69 int newsize;
70
71 newsize = SCIPcalcMemGrowSize(colpool->scip, num);
72 SCIP_CALL( SCIPreallocBlockMemoryArray(colpool->scip, &colpool->cols, colpool->colssize, newsize) );
73 colpool->colssize = newsize;
74 }
75 assert(num <= colpool->colssize);
76
77 return SCIP_OKAY;
78}
79
80/*
81 * Col methods
82 */
83
84/*
85 * Colpool methods
86 */
87
88/** creates col pool */
90 GCG* gcg, /**< GCG data structure */
91 GCG_COLPOOL** colpool, /**< pointer to store col pool */
92 int agelimit /**< maximum age a col can reach before it is deleted from the pool (-1 fpr no limit) */
93 )
94{
96 assert(colpool != NULL);
97 assert(agelimit >= -1);
98
99 SCIP_CALL( SCIPallocMemory(scip, colpool) );
100
101 SCIP_CALL( SCIPcreateClock(scip, &(*colpool)->poolclock) );
102
103 SCIP_CALL( SCIPhashtableCreate(&(*colpool)->hashtable, SCIPblkmem(scip),
105 GCGhashGetKeyCol, GCGhashKeyEqCol, GCGhashKeyValCol, (void*) scip) );
106
107 (*colpool)->gcg = gcg;
108 (*colpool)->scip = scip;
109 (*colpool)->nodenr = -1;
110 (*colpool)->infarkas = FALSE;
111 (*colpool)->cols = NULL;
112 (*colpool)->colssize = 0;
113 (*colpool)->ncols = 0;
114 (*colpool)->agelimit = agelimit;
115 (*colpool)->processedlp = -1;
116 (*colpool)->processedlpsol = -1;
117 (*colpool)->firstunprocessed = 0;
118 (*colpool)->firstunprocessedsol = 0;
119 (*colpool)->maxncols = 0;
120 (*colpool)->ncalls = 0;
121 (*colpool)->ncolsfound = 0;
122
123 return SCIP_OKAY;
124}
125
126/** frees col pool */
128 GCG_COLPOOL** colpool /**< pointer to store col pool */
129 )
130{
131 SCIP* scip;
132 assert(colpool != NULL);
133 assert(*colpool != NULL);
134
135 scip = (*colpool)->scip;
136
137 /* remove all cols from the pool */
138 SCIP_CALL( GCGcolpoolClear(*colpool) );
139
140 SCIPverbMessage(scip, SCIP_VERBLEVEL_NORMAL, NULL, "Pricing time in colpool = %f sec\n", GCGcolpoolGetTime(*colpool));
141
142 /* free clock */
143 SCIP_CALL( SCIPfreeClock(scip, &(*colpool)->poolclock) );
144
145 /* free hash table */
146 SCIPhashtableFree(&(*colpool)->hashtable);
147
148 SCIPfreeBlockMemoryArrayNull(scip, &(*colpool)->cols, (*colpool)->colssize);
149 SCIPfreeMemory(scip, colpool);
150
151 return SCIP_OKAY;
152}
153
154/** removes the col from the col pool */
155static
157 GCG_COLPOOL* colpool, /**< col pool */
158 GCG_COL* col, /**< col to remove */
159 SCIP_Bool freecol /**< should the col be freed? */
160 )
161{
162 int pos;
163
164 assert(colpool != NULL);
165 assert(colpool->firstunprocessed <= colpool->ncols);
166 assert(colpool->firstunprocessedsol <= colpool->ncols);
167 assert(col != NULL);
168
169 pos = col->pos;
170 col->pos = -1;
171 assert(0 <= pos && pos < colpool->ncols);
172 assert(colpool->cols[pos] == col);
173
174 /* remove the col from the hash table */
175 assert(SCIPhashtableExists(colpool->hashtable, (void*)col));
176 SCIP_CALL( SCIPhashtableRemove(colpool->hashtable, (void*)col) );
177
178 /* free the col */
179 if( freecol )
180 SCIP_CALL( GCGfreeGcgCol(&colpool->cols[pos]) );
181
182 /* move the last col of the pool to the free position */
183 if( pos < colpool->ncols-1 )
184 {
185 colpool->cols[pos] = colpool->cols[colpool->ncols-1];
186 colpool->cols[pos]->pos = pos;
187 }
188
189 colpool->ncols--;
190
191 return SCIP_OKAY;
192}
193
194
195/** removes all rows from the col pool */
197 GCG_COLPOOL* colpool /**< col pool */
198 )
199{
200 int i;
201
202 assert(colpool != NULL);
203
204 /* free cols (in reverse order!) */
205 for( i = colpool->ncols - 1; i >= 0; --i )
206 {
207 SCIP_CALL( colpoolDelCol(colpool, colpool->cols[i], TRUE) );
208 }
209 colpool->ncols = 0;
210
211 return SCIP_OKAY;
212}
213
214/** if not already existing, adds col to col pool and captures it */
216 GCG_COLPOOL* colpool, /**< col pool */
217 GCG_COL* col, /**< column to add */
218 SCIP_Bool freeduplicate /**< shouldl the col be freed if it is a duplicate? */
219 )
220{
221 assert(colpool != NULL);
222 assert(col != NULL);
223
224 /* check in hash table, if col already exists in the pool */
225 if( SCIPhashtableRetrieve(colpool->hashtable, (void*)col) == NULL )
226 {
227 SCIP_CALL( GCGcolpoolAddNewCol(colpool, col) );
228 }
229 else if( freeduplicate )
230 {
231 assert(col->pos == -1);
232 SCIP_CALL( GCGfreeGcgCol(&col) );
233 }
234
235 return SCIP_OKAY;
236}
237
238/** adds row to col pool and captures it; doesn't check for multiple cols */
240 GCG_COLPOOL* colpool, /**< col pool */
241 GCG_COL* col /**< column to add */
242 )
243{
244
245 assert(colpool != NULL);
246 assert(col != NULL);
247 assert(col->pos == -1);
248
249 col->pos = colpool->ncols;
250
251 /* add col to the pool */
252 SCIP_CALL( colpoolEnsureColsMem(colpool, colpool->ncols+1) );
253 colpool->cols[colpool->ncols] = col;
254 colpool->ncols++;
255 colpool->maxncols = MAX(colpool->maxncols, colpool->ncols);
256
257 /* insert col in the hash table */
258 SCIP_CALL( SCIPhashtableInsert(colpool->hashtable, (void*)col) );
259
260 return SCIP_OKAY;
261}
262
263/** removes the LP row from the col pool */
265 GCG_COLPOOL* colpool, /**< col pool */
266 GCG_COL* col, /**< col to remove */
267 SCIP_Bool freecol /**< should the col be freed? */
268 )
269{
270 assert(colpool != NULL);
271 assert(col != NULL);
272
273 /* find the col in hash table */
274 col = (GCG_COL*)SCIPhashtableRetrieve(colpool->hashtable, (void*)col);
275 if( col == NULL )
276 {
277 SCIPerrorMessage("col %p is not existing in colpool %p\n", (void*)col, (void*)colpool);
278 return SCIP_INVALIDDATA;
279 }
280
281 SCIP_CALL( colpoolDelCol(colpool, col, freecol) );
282
283 return SCIP_OKAY;
284}
285
286
287/** prices cols of the col pool */
289 GCG_COLPOOL* colpool, /**< col pool */
290 SCIP_SOL* sol, /**< solution to be separated (or NULL for LP-solution) */
291 int* nfoundvars /**< pointer to store the result of the separation call */
292 )
293{
294 GCG_COL* col;
295 int c;
296
297 assert(colpool != NULL);
298 assert(colpool->firstunprocessed <= colpool->ncols);
299 assert(colpool->firstunprocessedsol <= colpool->ncols);
300 assert(nfoundvars != NULL);
302
303 colpool->ncalls++;
304
305 SCIPdebugMessage("separating%s col pool %p with %d cols\n", ( sol == NULL ) ? "" : " solution from", (void*)colpool, colpool->ncols);
306
307 /* start timing */
308 SCIP_CALL( SCIPstartClock(colpool->scip, colpool->poolclock) );
309
310 /* process all unprocessed cols in the pool */
311 *nfoundvars = 0;
312
313 for( c = colpool->ncols - 1; c >= 0; --c )
314 {
315 SCIP_Real redcost;
316
317 col = colpool->cols[c];
318 assert(col != NULL);
319 assert(col->pos == c);
320
321 redcost = GCGcolGetRedcost(col);
322
323 if( SCIPisDualfeasNegative(colpool->scip, redcost) )
324 {
325 SCIP_Bool added;
326 /* insert col in separation storage */
327 SCIPdebugMessage(" -> col %p from the col pool (redcost: %g)\n",
328 (void*)col, redcost );
329
330 SCIP_CALL( colpoolDelCol(colpool, col, FALSE) );
331 SCIP_CALL( GCGpricerAddColResult(colpool->gcg, col, &added) );
332
333 if( added )
334 *nfoundvars = *nfoundvars + 1;
335
336 col->age = 0;
337 }
338 else
339 {
340 col->age++;
341 if( GCGcolIsAged(col, colpool->agelimit) )
342 {
343 SCIP_CALL( colpoolDelCol(colpool, col, TRUE) );
344 }
345 }
346 }
347
348 /* update the number of found cols */
349 colpool->ncolsfound += *nfoundvars; /*lint !e776*/
350
351 /* stop timing */
352 SCIP_CALL( SCIPstopClock(colpool->scip, colpool->poolclock) );
353
354 return SCIP_OKAY;
355}
356
357/** update node at which columns of column pool are feasible */
359 GCG_COLPOOL* colpool /**< col pool */
360 )
361{
362 assert(colpool != NULL);
364
365 if( colpool->nodenr < 0 )
366 {
367 colpool->nodenr = SCIPnodeGetNumber(SCIPgetCurrentNode(colpool->scip));
368 }
369 else if( colpool->nodenr != SCIPnodeGetNumber(SCIPgetCurrentNode(colpool->scip)) )
370 {
371 SCIP_CALL( GCGcolpoolClear(colpool) );
372
373 colpool->nodenr = SCIPnodeGetNumber(SCIPgetCurrentNode(colpool->scip));
374 }
375
376 return SCIP_OKAY;
377}
378
379/** update reduced cost and compute master coefs of columns in column pool */
381 GCG_COLPOOL* colpool /**< col pool */
382 )
383{
384 GCG_COL** cols;
385 int ncols;
386
387 int i;
388
389 ncols = GCGcolpoolGetNCols(colpool);
390 cols = GCGcolpoolGetCols(colpool);
391
392 for( i = 0; i < ncols; ++i )
393 {
394 GCG_COL* col;
395 SCIP_Real redcost;
396
397 col = cols[i];
398
399 SCIP_CALL( GCGcomputeColMastercoefs(colpool->gcg, col) );
400
401 redcost = GCGcomputeRedCostGcgCol(colpool->gcg, colpool->infarkas, col, NULL);
402
403 GCGcolUpdateRedcost(col, redcost, FALSE);
404 }
405
406 return SCIP_OKAY;
407}
408
409/** gets number of cols in the col pool */
411 GCG_COLPOOL* colpool /**< col pool */
412 )
413{
414 colpool->infarkas = TRUE;
415}
416
417/** gets number of cols in the col pool */
419 GCG_COLPOOL* colpool /**< col pool */
420 )
421{
422 colpool->infarkas = FALSE;
423}
424
425
426/** gets array of cols in the col pool */
428 GCG_COLPOOL* colpool /**< col pool */
429 )
430{
431 assert(colpool != NULL);
432
433 return colpool->cols;
434}
435
436/** gets number of cols in the col pool */
438 GCG_COLPOOL* colpool /**< col pool */
439 )
440{
441 assert(colpool != NULL);
442
443 return colpool->ncols;
444}
445
446/** gets maximum number of cols that were stored in the col pool at the same time */
448 GCG_COLPOOL* colpool /**< col pool */
449 )
450{
451 assert(colpool != NULL);
452
453 return colpool->maxncols;
454}
455
456/** gets time in seconds used for separating cols from the pool */
458 GCG_COLPOOL* colpool /**< col pool */
459 )
460{
461 assert(colpool != NULL);
462
463 return SCIPgetClockTime(colpool->scip, colpool->poolclock);
464}
465
466/** get number of times, the col pool was separated */
468 GCG_COLPOOL* colpool /**< col pool */
469 )
470{
471 assert(colpool != NULL);
472
473 return colpool->ncalls;
474}
475
476/** get total number of cols that were separated from the col pool */
478 GCG_COLPOOL* colpool /**< col pool */
479 )
480{
481 assert(colpool != NULL);
482
483 return colpool->ncolsfound;
484}
485
487 GCG_COLPOOL* colpool
488 )
489{
490 GCG_COL* col;
491 int c;
492 int i;
493
494 for( c = colpool->ncols - 1; c >= 0; --c )
495 {
496 col = colpool->cols[c];
497 assert(col != NULL);
498 assert(col->pricingprob != NULL);
499
500 for( i = 0; i < col->nvars; ++i )
501 {
502 assert(GCGvarIsPricing(col->vars[i]) && GCGpricingVarGetNOrigvars(col->vars[i]) > 0 && GCGpricingVarGetOrigvars(col->vars[i])[0] != NULL);
505 {
506 colpoolDelCol(colpool, col, TRUE);
507 break;
508 }
509 }
510 }
511
512 return SCIP_OKAY;
513}
#define GCG_HASHSIZE_COLPOOLS
Definition: colpool.c:52
#define GCG_HASHSIZE_COLPOOLS_SMALL
Definition: colpool.c:51
static SCIP_RETCODE colpoolEnsureColsMem(GCG_COLPOOL *colpool, int num)
Definition: colpool.c:60
static SCIP_RETCODE colpoolDelCol(GCG_COLPOOL *colpool, GCG_COL *col, SCIP_Bool freecol)
Definition: colpool.c:156
#define GCG_USESMALLTABLES
Definition: colpool.c:50
internal methods for storing cols in a col pool
#define SCIP_Longint
#define SCIP_Bool
#define SCIP_Real
#define MAX(x, y)
#define SCIP_CALL(x)
#define TRUE
#define FALSE
GCG interface methods.
SCIP_VAR ** GCGpricingVarGetOrigvars(SCIP_VAR *var)
Definition: gcgvar.c:1100
int GCGpricingVarGetNOrigvars(SCIP_VAR *var)
Definition: gcgvar.c:1082
SCIP_RETCODE GCGpricerAddColResult(GCG *gcg, GCG_COL *col, SCIP_Bool *added)
SCIP_RETCODE GCGcomputeColMastercoefs(GCG *gcg, GCG_COL *gcgcol)
SCIP_Real GCGcomputeRedCostGcgCol(GCG *gcg, SCIP_Bool infarkas, GCG_COL *gcgcol, SCIP_Real *objvalptr)
void GCGcolpoolEndFarkas(GCG_COLPOOL *colpool)
Definition: colpool.c:418
SCIP_Real GCGcolpoolGetTime(GCG_COLPOOL *colpool)
Definition: colpool.c:457
void GCGcolpoolStartFarkas(GCG_COLPOOL *colpool)
Definition: colpool.c:410
SCIP_RETCODE GCGcolpoolUpdateNode(GCG_COLPOOL *colpool)
Definition: colpool.c:358
SCIP_RETCODE GCGcolpoolAddCol(GCG_COLPOOL *colpool, GCG_COL *col, SCIP_Bool freeduplicate)
Definition: colpool.c:215
SCIP_RETCODE GCGcolpoolCreate(GCG *gcg, GCG_COLPOOL **colpool, int agelimit)
Definition: colpool.c:89
SCIP_RETCODE GCGcolpoolFree(GCG_COLPOOL **colpool)
Definition: colpool.c:127
SCIP_RETCODE GCGcolpoolDelCol(GCG_COLPOOL *colpool, GCG_COL *col, SCIP_Bool freecol)
Definition: colpool.c:264
GCG_COL ** GCGcolpoolGetCols(GCG_COLPOOL *colpool)
Definition: colpool.c:427
SCIP_RETCODE GCGcolpoolPrice(GCG_COLPOOL *colpool, SCIP_SOL *sol, int *nfoundvars)
Definition: colpool.c:288
int GCGcolpoolGetMaxNCols(GCG_COLPOOL *colpool)
Definition: colpool.c:447
SCIP_RETCODE GCGcolpoolPropagateGlobalBounds(GCG_COLPOOL *colpool)
Definition: colpool.c:486
SCIP_Longint GCGcolpoolGetNColsFound(GCG_COLPOOL *colpool)
Definition: colpool.c:477
SCIP_Longint GCGcolpoolGetNCalls(GCG_COLPOOL *colpool)
Definition: colpool.c:467
SCIP_RETCODE GCGcolpoolUpdateRedcost(GCG_COLPOOL *colpool)
Definition: colpool.c:380
int GCGcolpoolGetNCols(GCG_COLPOOL *colpool)
Definition: colpool.c:437
SCIP_RETCODE GCGcolpoolClear(GCG_COLPOOL *colpool)
Definition: colpool.c:196
SCIP_RETCODE GCGcolpoolAddNewCol(GCG_COLPOOL *colpool, GCG_COL *col)
Definition: colpool.c:239
void GCGcolUpdateRedcost(GCG_COL *gcgcol, SCIP_Real redcost, SCIP_Bool growold)
Definition: gcgcol.c:491
SCIP_RETCODE GCGfreeGcgCol(GCG_COL **gcgcol)
Definition: gcgcol.c:231
SCIP_Bool GCGcolIsAged(GCG_COL *col, int agelimit)
Definition: gcgcol.c:814
SCIP_Real GCGcolGetRedcost(GCG_COL *gcgcol)
Definition: gcgcol.c:475
SCIP * GCGgetMasterprob(GCG *gcg)
Definition: gcg.cpp:99
void SCIPhashtableFree(SCIP_HASHTABLE **hashtable)
SCIP_Bool SCIPhashtableExists(SCIP_HASHTABLE *hashtable, void *element)
SCIP_RETCODE SCIPhashtableCreate(SCIP_HASHTABLE **hashtable, BMS_BLKMEM *blkmem, int tablesize, SCIP_DECL_HASHGETKEY((*hashgetkey)), SCIP_DECL_HASHKEYEQ((*hashkeyeq)), SCIP_DECL_HASHKEYVAL((*hashkeyval)), void *userptr)
void * SCIPhashtableRetrieve(SCIP_HASHTABLE *hashtable, void *key)
SCIP_RETCODE SCIPhashtableRemove(SCIP_HASHTABLE *hashtable, void *element)
SCIP_RETCODE SCIPhashtableInsert(SCIP_HASHTABLE *hashtable, void *element)
void SCIPverbMessage(SCIP *scip, SCIP_VERBLEVEL msgverblevel, FILE *file, const char *formatstr,...)
BMS_BLKMEM * SCIPblkmem(SCIP *scip)
int SCIPcalcMemGrowSize(SCIP *scip, int num)
#define SCIPallocMemory(scip, ptr)
#define SCIPfreeMemory(scip, ptr)
#define SCIPreallocBlockMemoryArray(scip, ptr, oldnum, newnum)
#define SCIPfreeBlockMemoryArrayNull(scip, ptr, num)
SCIP_NODETYPE SCIPnodeGetType(SCIP_NODE *node)
SCIP_Longint SCIPnodeGetNumber(SCIP_NODE *node)
SCIP_RETCODE SCIPcreateClock(SCIP *scip, SCIP_CLOCK **clck)
SCIP_RETCODE SCIPstopClock(SCIP *scip, SCIP_CLOCK *clck)
SCIP_RETCODE SCIPfreeClock(SCIP *scip, SCIP_CLOCK **clck)
SCIP_Real SCIPgetClockTime(SCIP *scip, SCIP_CLOCK *clck)
SCIP_RETCODE SCIPstartClock(SCIP *scip, SCIP_CLOCK *clck)
SCIP_Bool SCIPisDualfeasNegative(SCIP *scip, SCIP_Real val)
SCIP_Bool SCIPisFeasLT(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
SCIP_Bool SCIPisFeasGT(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
SCIP_NODE * SCIPgetCurrentNode(SCIP *scip)
SCIP_Real SCIPvarGetUbGlobal(SCIP_VAR *var)
SCIP_Real SCIPvarGetLbGlobal(SCIP_VAR *var)
SCIP_Bool GCGvarIsPricing(SCIP_VAR *var)
Definition: gcgvar.c:159
#define NULL
GCG variable pricer.
methods for storing priced cols (based on SCIP's separation storage)
public methods for working with gcg columns
#define SCIPerrorMessage
#define SCIPdebugMessage
SCIP * pricingprob
Definition: struct_gcgcol.h:53
SCIP_Real * vals
Definition: struct_gcgcol.h:56
SCIP_VAR ** vars
Definition: struct_gcgcol.h:55
int firstunprocessed
SCIP_HASHTABLE * hashtable
SCIP_Bool infarkas
SCIP_Longint nodenr
GCG_COL ** cols
SCIP_CLOCK * poolclock
SCIP_Longint ncolsfound
SCIP_Longint ncalls
int firstunprocessedsol
Definition: struct_gcg.h:55
data structures for storing cols in a col pool
data structure to store columns (solutions from a pricing problem)
datastructures for storing priced cols
SCIP_VERBLEVEL_NORMAL
SCIP_INVALIDDATA
SCIP_OKAY
enum SCIP_Retcode SCIP_RETCODE
SCIP_NODETYPE_PROBINGNODE