Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

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