Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

Loading...
Searching...
No Matches
standardpool.inc
Go to the documentation of this file.
1
29#pragma once
30
36#include <ogdf/lib/abacus/sub.h>
38
39namespace abacus {
40
41template<class BaseType, class CoType>
43 Master *master,
44 int size,
45 bool autoRealloc)
46 :
47Pool<BaseType, CoType>(master),
48 pool_(size),
49 autoRealloc_(autoRealloc)
50{
51 for (int i = 0; i < size; i++) {
52 pool_[i] = new PoolSlot<BaseType, CoType>(master, this);
53 freeSlots_.pushBack(pool_[i]);
54 }
55}
56
57
58template<class BaseType, class CoType>
59StandardPool<BaseType, CoType>::~StandardPool()
60{
61 const int s = size();
62
63 for (int i = 0; i < s; i++)
64 delete pool_[i];
65}
66
67
68template<class BaseType, class CoType>
69std::ostream &operator<<(std::ostream &out, const StandardPool<BaseType, CoType> &rhs)
70{
71 const int s = rhs.size();
72
73 for (int i = 0; i < s; i++) {
74 if (rhs.pool_[i]->conVar()) {
75 out << i << ": ";
76 rhs.pool_[i]->conVar()->print(out);
77 out << std::endl;
78 }
79 }
80
81 return out;
82}
83
84
85template<class BaseType, class CoType>
86PoolSlot<BaseType, CoType> * StandardPool<BaseType, CoType>::insert(
87 BaseType *cv)
88{
89 PoolSlot<BaseType, CoType>* slot = getSlot();
90 if (slot == nullptr) {
91 if(cleanup() == 0) {
92 if (autoRealloc_)
93 increase((int) (size()*1.1 + 1));
94 else {
95 if (removeNonActive(size()/10 + 1) == 0)
96 return nullptr;
97 }
98 }
99 slot = getSlot();
100 }
101
102 slot->insert(cv);
103 ++Pool<BaseType, CoType>::number_;
104 return slot;
105}
106
107
108template<class BaseType, class CoType>
109void StandardPool<BaseType, CoType>::increase(int size)
110{
111 int oldSize = pool_.size();
112
113 if (size < oldSize) {
114 Logger::ifout() << "StandardPool::increase(): the pool size cannot be decreased.\n";
116 }
117
118 pool_.resize(size);
119
120 for(int i = oldSize; i < size; i++) {
121 pool_[i] = new PoolSlot<BaseType, CoType>(Pool<BaseType, CoType>::master_, this);
122 freeSlots_.pushBack(pool_[i]);
123 }
124}
125
126
127template<class BaseType, class CoType>
128int StandardPool<BaseType, CoType>::cleanup()
129{
130 int nDeleted = 0;
131
132 for(int i = 0; i < Pool<BaseType, CoType>::number(); i++)
133 {
134 if(this->softDeleteConVar(pool_[i]) == 0)
135 {
136 nDeleted++;
137 // consider the case that a slot has been deleted although it was empty
138 // in softDeleteConVar(), number_ was decreased by 1
139 if (i != Pool<BaseType, CoType>::number())
140 {
141 //exchange the current slot with the last slot of the pool
143 pool_[i] = pool_[Pool<BaseType, CoType>::number()];
144 pool_[Pool<BaseType, CoType>::number()] = CMslot;
145 i--; // decrease i in order to consider the new current slot
146 }
147 }
148 }
149
150 Logger::ilout(Logger::Level::Minor) << "StandardPool::cleanup(): " << nDeleted << " items removed." << std::endl;
151 return nDeleted;
152
153}
154
155template<class BaseType, class CoType>
156int StandardPool<BaseType, CoType>::removeNonActive(int maxRemove)
157{
159 ArrayBuffer<int> elems(size(),false);
160 ArrayBuffer<int> keys(size(),false);
161
162 const int s = size();
163
164 for (int i = 0; i < s; i++) {
165 BaseType *cv = pool_[i]->conVar();
166 if (cv && !cv->active() && !cv->locked()) {
167 elems.push(i);
168 keys.push(cv->nReferences());
169 }
170 }
171
173
175
178 int nRemoved = 0;
179
180 while(nRemoved < maxRemove && !candidates.empty()) {
181 int c = candidates.extractMin();
182 this->hardDeleteConVar(pool_[c]);
183 nRemoved++;
184 }
185
186 Logger::ilout(Logger::Level::Minor) << nRemoved << " inactive items removed from pool." << std::endl;
187
188 return nRemoved;
189}
190
191
192template<class BaseType, class CoType>
193int StandardPool<BaseType, CoType>::separate(
194 double *z,
195 Active<CoType, BaseType> *active,
196 Sub *sub,
197 CutBuffer<BaseType, CoType> *cutBuffer,
198 double minAbsViolation,
199 int ranking)
200{
201 double violation;
202 int oldSep = cutBuffer->number();
203
204 Logger::ilout(Logger::Level::Minor) << "StandardPool::separate(): " << "size = " << size() << " n = " << Pool<BaseType, CoType>::number_;
205
207 const int s = size();
208
209 for (int i = 0; i < s; i++) {
210 slot = pool_[i];
211 BaseType *cv = slot->conVar();
212 if (cv && !cv->active() && (cv->global() || cv->valid(sub)))
213 if (cv->violated(active, z, &violation) && fabs(violation) > minAbsViolation) {
214 if (ranking == 0) {
215 if (cutBuffer->insert(slot, true))
216 break;
217 }
218 else if (ranking == 1) {
219 if (cutBuffer->insert(slot, true, violation))
220 break;
221 }
222 else if (ranking == 2) {
223 if (cutBuffer->insert(slot, true, fabs(violation)))
224 break;
225 }
226 else if (ranking == 3) {
227 if (cutBuffer->insert(slot, true, cv->rank()))
228 break;
229 }
230 }
231 }
232
233 Logger::ilout(Logger::Level::Minor) << " generated = " << cutBuffer->number() - oldSep << std::endl;
234 return cutBuffer->number() - oldSep;
235}
236
237}
StandardPool(Master *master, int size, bool autoRealloc=false)
Creates an empty pool.
constraint.
cutbuffer.
#define OGDF_THROW_PARAM(CLASS, PARAM)
Replacement for throw.
Definition exceptions.h:54
the master of the optimization.
static MultilevelBuilder * getDoubleFactoredZeroAdjustedMerger()
std::ostream & operator<<(std::ostream &os, const ogdf::Array< E, INDEX > &a)
Prints array a to output stream os.
Definition Array.h:978
poolslot.
variable.