GCG 4.1.1 (dev)

Generic Column Generation

Loading...
Searching...
No Matches
gcgpqueue.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 gcgpqueue.c
30 * @brief methods for working with priority queue
31 * @author Jonas Witt
32 *
33 * Various methods to work with the priority queue
34 *
35 */
36
37/*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
38
39#include "gcg/gcgpqueue.h"
40#include "gcg/pub_gcgpqueue.h"
41
42#include "gcg/gcg.h"
43#include "scip/def.h"
44#include "scip/scip.h"
46
47#include <assert.h>
48
49
50
51/*
52 * Priority Queue
53 */
54
55#define PQ_PARENT(q) (((q)+1)/2-1)
56#define PQ_LEFTCHILD(p) (2*(p)+1)
57#define PQ_RIGHTCHILD(p) (2*(p)+2)
58
59
60/** resizes element memory to hold at least the given number of elements */
61static
63 GCG_PQUEUE* pqueue, /**< pointer to a priority queue */
64 int minsize /**< minimal number of storable elements */
65 )
66{
67 int newsize;
69 assert(pqueue != NULL);
70
71 if( minsize <= pqueue->size )
72 return retcode;
73
74 newsize = SCIPcalcMemGrowSize(pqueue->scip, minsize);
75
76 GCG_SET_LOCK(pqueue->memorylock);
77 retcode = SCIPreallocBlockMemoryArray(pqueue->scip, &pqueue->slots, pqueue->size, newsize);
78 GCG_UNSET_LOCK(pqueue->memorylock);
79
80 pqueue->size = newsize;
81
82 return retcode;
83}
84
85/** heapifies element at position pos into corresponding subtrees */
86static
88 GCG_PQUEUE* pqueue, /**< pointer to a priority queue */
89 int pos /**< heapify element at position pos into corresponding subtrees */
90 )
91{
92 int childpos;
93 int brotherpos;
94 void* elem;
95
96 assert(pqueue != NULL);
97 assert(pqueue->len > 0);
98
99 /* move the better child of elem to its parents position until element
100 * is better than its children
101 */
102 elem = pqueue->slots[pos];
103
104 while( pos <= PQ_PARENT(pqueue->len-1) )
105 {
106 childpos = PQ_LEFTCHILD(pos);
107 brotherpos = PQ_RIGHTCHILD(pos);
108 if( brotherpos < pqueue->len && (*pqueue->ptrcomp)(pqueue->slots[brotherpos], pqueue->slots[childpos]) < 0 )
109 childpos = brotherpos;
110 if( (*pqueue->ptrcomp)(elem, pqueue->slots[childpos]) < 0 )
111 break;
112 pqueue->slots[pos] = pqueue->slots[childpos];
113 pqueue->slots[childpos] = elem;
114
115 pos = childpos;
116 }
117 assert(0 <= pos && pos < pqueue->len);
118
119 return SCIP_OKAY;
120}
121
122
123/** creates priority queue */
125 SCIP* scip, /**< SCIP data structure */
126 GCG_PQUEUE** pqueue, /**< pointer to a priority queue */
127 int initsize, /**< initial number of available element slots */
128 SCIP_DECL_SORTPTRCOMP((*ptrcomp)), /**< data element comparator */
129 GCG_LOCK* memorylock /**< memory lock */
130 )
131{
132 assert(pqueue != NULL);
133 assert(ptrcomp != NULL);
134
135 initsize = MAX(1, initsize);
136
138 (*pqueue)->len = 0;
139 (*pqueue)->size = 0;
140 (*pqueue)->scip = scip;
141 (*pqueue)->slots = NULL;
142 (*pqueue)->ptrcomp = ptrcomp;
143#ifdef _OPENMP
144 (*pqueue)->memorylock = memorylock;
145#endif
146 SCIP_CALL( pqueueResize(*pqueue, initsize) );
147
148 return SCIP_OKAY;
149}
150
151/** frees priority queue, but not the data elements themselves */
153 GCG_PQUEUE** pqueue /**< pointer to a priority queue */
154 )
155{
156 assert(pqueue != NULL);
157
158 SCIPfreeBlockMemoryArray((*pqueue)->scip, &(*pqueue)->slots, (*pqueue)->size);
159 SCIPfreeBlockMemory((*pqueue)->scip, pqueue);
160}
161
162/** clears the priority queue, but doesn't free the data elements themselves */
164 GCG_PQUEUE* pqueue /**< priority queue */
165 )
166{
167 assert(pqueue != NULL);
168
169 pqueue->len = 0;
170}
171
172/** inserts element into priority queue */
174 GCG_PQUEUE* pqueue, /**< priority queue */
175 void* elem /**< element to be inserted */
176 )
177{
178 int pos;
179 int parentpos;
180
181 assert(pqueue != NULL);
182 assert(pqueue->len >= 0);
183 assert(elem != NULL);
184
185 SCIP_CALL( pqueueResize(pqueue, pqueue->len+1) );
186
187 /* insert element as leaf in the tree, move it towards the root as long it is better than its parent */
188 pos = pqueue->len;
189 parentpos = PQ_PARENT(pos);
190 pqueue->len++;
191 while( pos > 0 && (*pqueue->ptrcomp)(elem, pqueue->slots[parentpos]) < 0 )
192 {
193 pqueue->slots[pos] = pqueue->slots[parentpos];
194 pos = parentpos;
195 parentpos = PQ_PARENT(pos);
196 }
197 pqueue->slots[pos] = elem;
198
199 return SCIP_OKAY;
200}
201
202/** removes and returns best element from the priority queue */
204 GCG_PQUEUE* pqueue /**< priority queue */
205 )
206{
207 void* root;
208 void* last;
209 int pos;
210 int childpos;
211 int brotherpos;
212
213 assert(pqueue != NULL);
214 assert(pqueue->len >= 0);
215
216 if( pqueue->len == 0 )
217 return NULL;
218
219 /* remove root element of the tree, move the better child to its parents position until the last element
220 * of the queue could be placed in the empty slot
221 */
222 root = pqueue->slots[0];
223 last = pqueue->slots[pqueue->len-1];
224 pqueue->len--;
225 pos = 0;
226 while( pos <= PQ_PARENT(pqueue->len-1) )
227 {
228 childpos = PQ_LEFTCHILD(pos);
229 brotherpos = PQ_RIGHTCHILD(pos);
230 if( brotherpos <= pqueue->len && (*pqueue->ptrcomp)(pqueue->slots[brotherpos], pqueue->slots[childpos]) < 0 )
231 childpos = brotherpos;
232 if( (*pqueue->ptrcomp)(last, pqueue->slots[childpos]) <= 0 )
233 break;
234 pqueue->slots[pos] = pqueue->slots[childpos];
235 pos = childpos;
236 }
237 assert(pos <= pqueue->len);
238 pqueue->slots[pos] = last;
239
240 return root;
241}
242
243/** resorts priority queue after changing the key values */
245 GCG_PQUEUE* pqueue /**< priority queue */
246 )
247{
248 int lastinner;
249 int pos;
250
251 assert(pqueue != NULL);
252 assert(pqueue->len >= 0);
253
254 if( pqueue->len == 0 )
255 {
256 return SCIP_OKAY;
257 }
258
259 lastinner = PQ_PARENT(pqueue->len - 1);
260
261 for( pos = lastinner; pos >= 0; --pos )
262 {
263 SCIP_CALL( pqueueHeapify(pqueue, pos) );
264 }
265
266 return SCIP_OKAY;
267}
268
269
270/** set the comperator of the priority queue */
272 GCG_PQUEUE* pqueue, /**< priority queue */
273 SCIP_DECL_SORTPTRCOMP((*ptrcomp)) /**< data element comparator */
274 )
275{
276 pqueue->ptrcomp = ptrcomp;
277
278 return SCIP_OKAY;
279}
280
281/* todo: write method to get comperator */
282
283/**< delete item at position pos and insert last item at this position and resort pqueue */
285 GCG_PQUEUE* pqueue, /**< priority queue */
286 int pos, /**< position of item that should be deleted */
287 void** elem /**< pointer to store element that was deleted from pqueue */
288 )
289{
290 void* last;
291 int childpos;
292 int brotherpos;
293
294 assert(pqueue != NULL);
295 assert(pqueue->len >= 0);
296
297 assert(pos < pqueue->len);
298
299 /* remove element at position pos of the tree, move the better child to its parents position until the last element
300 * of the queue could be placed in the empty slot
301 */
302 *elem = pqueue->slots[pos];
303 last = pqueue->slots[pqueue->len-1];
304 pqueue->len--;
305 while( pos <= PQ_PARENT(pqueue->len-1) )
306 {
307 childpos = PQ_LEFTCHILD(pos);
308 brotherpos = PQ_RIGHTCHILD(pos);
309 if( brotherpos <= pqueue->len && (*pqueue->ptrcomp)(pqueue->slots[brotherpos], pqueue->slots[childpos]) < 0 )
310 childpos = brotherpos;
311 if( (*pqueue->ptrcomp)(last, pqueue->slots[childpos]) <= 0 )
312 break;
313 pqueue->slots[pos] = pqueue->slots[childpos];
314 pos = childpos;
315 }
316 assert(pos <= pqueue->len);
317 pqueue->slots[pos] = last;
318
319 return SCIP_OKAY;
320}
321
322/** returns the best element of the queue without removing it */
324 GCG_PQUEUE* pqueue /**< priority queue */
325 )
326{
327 assert(pqueue != NULL);
328 assert(pqueue->len >= 0);
329
330 if( pqueue->len == 0 )
331 return NULL;
332
333 return pqueue->slots[0];
334}
335
336/** returns the number of elements in the queue */
338 GCG_PQUEUE* pqueue /**< priority queue */
339 )
340{
341 assert(pqueue != NULL);
342 assert(pqueue->len >= 0);
343
344 return pqueue->len;
345}
346
347/** returns the elements of the queue; changing the returned array may destroy the queue's ordering! */
349 GCG_PQUEUE* pqueue /**< priority queue */
350 )
351{
352 assert(pqueue != NULL);
353 assert(pqueue->len >= 0);
354
355 return pqueue->slots;
356}
#define MAX(x, y)
GCG interface methods.
#define PQ_RIGHTCHILD(p)
Definition gcgpqueue.c:57
#define PQ_PARENT(q)
Definition gcgpqueue.c:55
static SCIP_RETCODE pqueueHeapify(GCG_PQUEUE *pqueue, int pos)
Definition gcgpqueue.c:87
#define PQ_LEFTCHILD(p)
Definition gcgpqueue.c:56
static SCIP_RETCODE pqueueResize(GCG_PQUEUE *pqueue, int minsize)
Definition gcgpqueue.c:62
private methods for working with priority queue
int GCGpqueueNElems(GCG_PQUEUE *pqueue)
Definition gcgpqueue.c:337
SCIP_RETCODE GCGpqueueDelete(GCG_PQUEUE *pqueue, int pos, void **elem)
Definition gcgpqueue.c:284
void * GCGpqueueRemove(GCG_PQUEUE *pqueue)
Definition gcgpqueue.c:203
void ** GCGpqueueElems(GCG_PQUEUE *pqueue)
Definition gcgpqueue.c:348
SCIP_RETCODE GCGpqueueSetComperator(GCG_PQUEUE *pqueue,)
Definition gcgpqueue.c:271
SCIP_RETCODE GCGpqueueInsert(GCG_PQUEUE *pqueue, void *elem)
Definition gcgpqueue.c:173
SCIP_RETCODE GCGpqueueCreate(SCIP *scip, GCG_PQUEUE **pqueue, int initsize, SCIP_DECL_SORTPTRCOMP((*ptrcomp)), GCG_LOCK *memorylock)
Definition gcgpqueue.c:124
void * GCGpqueueFirst(GCG_PQUEUE *pqueue)
Definition gcgpqueue.c:323
void GCGpqueueClear(GCG_PQUEUE *pqueue)
Definition gcgpqueue.c:163
SCIP_RETCODE GCGpqueueResort(GCG_PQUEUE *pqueue)
Definition gcgpqueue.c:244
void GCGpqueueFree(GCG_PQUEUE **pqueue)
Definition gcgpqueue.c:152
SCIP_DECL_SORTPTRCOMP(SCIPbendersComp)
#define SCIPfreeBlockMemoryArray(scip, ptr, num)
int SCIPcalcMemGrowSize(SCIP *scip, int num)
#define SCIPreallocBlockMemoryArray(scip, ptr, oldnum, newnum)
#define SCIPfreeBlockMemory(scip, ptr)
#define SCIPallocBlockMemory(scip, ptr)
static assert(heur !=NULL)
SCIP_CALL(SCIPgetExternBranchCands(origprob, &lpcands, &lpcandssol, NULL, &nlpcands, NULL, NULL, NULL, NULL))
SCIP_RETCODE retcode
#define NULL
struct GCG_PQueue GCG_PQUEUE
#define GCG_SET_LOCK(lockname)
Definition type_locks.h:68
#define GCG_UNSET_LOCK(lockname)
Definition type_locks.h:70
#define GCG_LOCK
Definition type_locks.h:64
enum SCIP_Retcode SCIP_RETCODE
struct Scip SCIP