... | ... |
@@ -84,7 +84,7 @@ void AtomicDomain::insert(uint64_t pos, float mass) |
84 | 84 |
// insert position into map |
85 | 85 |
GAPS_ASSERT(mAtomPositions.find(pos) == mAtomPositions.end()); |
86 | 86 |
std::map<uint64_t, uint64_t>::iterator iter, iterLeft, iterRight; |
87 |
- iter = mAtomPositions.insert(std::pair<uint64_t, uint64_t>(pos, mAtoms.size())).first; |
|
87 |
+ iter = mAtomPositions.insert(std::pair<uint64_t, uint64_t>(pos, size())).first; |
|
88 | 88 |
iterLeft = iter; |
89 | 89 |
iterRight = iter; |
90 | 90 |
|
... | ... |
@@ -94,11 +94,13 @@ void AtomicDomain::insert(uint64_t pos, float mass) |
94 | 94 |
{ |
95 | 95 |
--iterLeft; |
96 | 96 |
atom.leftNdx = iterLeft->second + 1; |
97 |
+ left(atom).rightNdx = size() + 1; |
|
97 | 98 |
} |
98 | 99 |
if (++iter != mAtomPositions.end()) |
99 | 100 |
{ |
100 | 101 |
++iterRight; |
101 | 102 |
atom.rightNdx = iterRight->second + 1; |
103 |
+ right(atom).leftNdx = size() + 1; |
|
102 | 104 |
} |
103 | 105 |
|
104 | 106 |
// add atom to vector |
... | ... |
@@ -108,76 +110,56 @@ void AtomicDomain::insert(uint64_t pos, float mass) |
108 | 110 |
mUsedPositions.insert(pos); |
109 | 111 |
GAPS_ASSERT(mAtoms.size() == mAtomPositions.size()); |
110 | 112 |
GAPS_ASSERT(mAtoms.size() == mUsedPositions.size()); |
111 |
-} |
|
112 |
- |
|
113 |
-void AtomicDomain::swap(uint64_t i1, uint64_t i2) |
|
114 |
-{ |
|
115 |
- if (i1 != i2) |
|
116 |
- { |
|
117 |
- // store the atom in position 1 |
|
118 |
- Atom temp1 = mAtoms[i1]; |
|
119 |
- Atom temp2 = mAtoms[i2]; |
|
120 |
- |
|
121 |
- // switch atoms |
|
122 |
- mAtoms[i1] = temp2; |
|
123 |
- mAtoms[i2] = temp1; |
|
124 |
- |
|
125 |
- // update neighbors |
|
126 |
- if (hasLeft(temp1)) |
|
127 |
- { |
|
128 |
- GAPS_ASSERT(i2 + 1 <= size()); |
|
129 |
- left(temp1).rightNdx = i2 + 1; |
|
130 |
- } |
|
131 |
- if (hasRight(temp1)) |
|
132 |
- { |
|
133 |
- GAPS_ASSERT(i2 + 1 <= size()); |
|
134 |
- right(temp1).leftNdx = i2 + 1; |
|
135 |
- } |
|
136 |
- if (hasLeft(temp2)) |
|
137 |
- { |
|
138 |
- GAPS_ASSERT(i1 + 1 <= size()); |
|
139 |
- left(temp2).rightNdx = i1 + 1; |
|
140 |
- } |
|
141 |
- if (hasRight(temp2)) |
|
142 |
- { |
|
143 |
- GAPS_ASSERT(i1 + 1 <= size()); |
|
144 |
- right(temp2).leftNdx = i1 + 1; |
|
145 |
- } |
|
146 |
- |
|
147 |
- // update atom positions |
|
148 |
- mAtomPositions.erase(mAtoms[i1].pos); |
|
149 |
- mAtomPositions.erase(mAtoms[i2].pos); |
|
150 |
- mAtomPositions.insert(std::pair<uint64_t, uint64_t>(mAtoms[i1].pos, |
|
151 |
- i1)); |
|
152 |
- mAtomPositions.insert(std::pair<uint64_t, uint64_t>(mAtoms[i2].pos, |
|
153 |
- i2)); |
|
154 |
- } |
|
113 |
+ GAPS_ASSERT(atom.rightNdx != size() + 1); |
|
114 |
+ GAPS_ASSERT(atom.leftNdx != size() + 1); |
|
155 | 115 |
} |
156 | 116 |
|
157 | 117 |
// O(logN) |
158 |
-// erase directly from position map |
|
118 |
+// erase directly from position map and used positions hash set |
|
159 | 119 |
// swap with last atom in vector, pop off the back |
160 |
-void AtomicDomain::erase(uint64_t pos, bool display) |
|
120 |
+void AtomicDomain::erase(uint64_t pos) |
|
161 | 121 |
{ |
162 | 122 |
// get vector index of this atom and erase it |
163 | 123 |
GAPS_ASSERT(mAtomPositions.find(pos) != mAtomPositions.end()); |
164 | 124 |
uint64_t index = mAtomPositions.at(pos); |
165 | 125 |
|
166 |
- // move atom to back |
|
167 |
- swap(index, mAtoms.size() - 1); |
|
168 |
- |
|
169 | 126 |
// connect neighbors of atom to be deleted |
170 |
- if (hasLeft(mAtoms.back())) |
|
127 |
+ if (hasLeft(mAtoms[index])) |
|
171 | 128 |
{ |
172 |
- left(mAtoms.back()).rightNdx = mAtoms.back().rightNdx; |
|
129 |
+ left(mAtoms[index]).rightNdx = mAtoms[index].rightNdx; |
|
173 | 130 |
} |
174 |
- if (hasRight(mAtoms.back())) |
|
131 |
+ if (hasRight(mAtoms[index])) |
|
175 | 132 |
{ |
176 |
- right(mAtoms.back()).leftNdx = mAtoms.back().leftNdx; |
|
133 |
+ right(mAtoms[index]).leftNdx = mAtoms[index].leftNdx; |
|
134 |
+ } |
|
135 |
+ |
|
136 |
+ // replace with atom from back |
|
137 |
+ if (index < size() - 1) |
|
138 |
+ { |
|
139 |
+ mAtoms[index] = mAtoms.back(); |
|
140 |
+ |
|
141 |
+ // update position in map |
|
142 |
+ mAtomPositions.erase(mAtoms[index].pos); |
|
143 |
+ mAtomPositions.insert(std::pair<uint64_t, uint64_t>(mAtoms[index].pos, |
|
144 |
+ index)); |
|
145 |
+ |
|
146 |
+ // update moved atom's neighbors |
|
147 |
+ if (hasLeft(mAtoms[index])) |
|
148 |
+ { |
|
149 |
+ left(mAtoms[index]).rightNdx = index + 1; |
|
150 |
+ } |
|
151 |
+ if (hasRight(mAtoms[index])) |
|
152 |
+ { |
|
153 |
+ right(mAtoms[index]).leftNdx = index + 1; |
|
154 |
+ } |
|
155 |
+ |
|
156 |
+ GAPS_ASSERT_MSG(mAtoms[index].rightNdx != index + 1, |
|
157 |
+ index << " , " << size()); |
|
158 |
+ GAPS_ASSERT_MSG(mAtoms[index].leftNdx != index + 1, |
|
159 |
+ index << " , " << size()); |
|
177 | 160 |
} |
178 | 161 |
|
179 | 162 |
// delete atom from vector in O(1) |
180 |
- GAPS_ASSERT(mAtomPositions.at(pos) == mAtoms.size() - 1); |
|
181 | 163 |
mAtomPositions.erase(pos); |
182 | 164 |
mAtoms.pop_back(); |
183 | 165 |
mUsedPositions.erase(pos); |
... | ... |
@@ -188,11 +170,12 @@ void AtomicDomain::erase(uint64_t pos, bool display) |
188 | 170 |
// O(logN) |
189 | 171 |
void AtomicDomain::updateMass(uint64_t pos, float newMass) |
190 | 172 |
{ |
191 |
- GAPS_ASSERT(mAtomPositions.find(pos) != mAtomPositions.end()); |
|
173 |
+ GAPS_ASSERT_MSG(mAtomPositions.find(pos) != mAtomPositions.end(), |
|
174 |
+ pos); |
|
192 | 175 |
mAtoms[mAtomPositions.at(pos)].mass = newMass; |
193 | 176 |
} |
194 | 177 |
|
195 |
-void AtomicDomain::test(uint64_t pos) const |
|
178 |
+bool AtomicDomain::test(uint64_t pos) const |
|
196 | 179 |
{ |
197 |
- GAPS_ASSERT(mAtomPositions.find(pos) != mAtomPositions.end()); |
|
180 |
+ return mAtomPositions.find(pos) != mAtomPositions.end(); |
|
198 | 181 |
} |
199 | 182 |
\ No newline at end of file |
... | ... |
@@ -36,6 +36,11 @@ public: |
36 | 36 |
{ |
37 | 37 |
return pos == other.pos; |
38 | 38 |
} |
39 |
+ |
|
40 |
+ bool operator!=(const Atom &other) const |
|
41 |
+ { |
|
42 |
+ return !(pos == other.pos); |
|
43 |
+ } |
|
39 | 44 |
}; |
40 | 45 |
|
41 | 46 |
// data structure that holds atoms |
... | ... |
@@ -67,10 +72,10 @@ public: |
67 | 72 |
|
68 | 73 |
// modify domain |
69 | 74 |
void insert(uint64_t pos, float mass); |
70 |
- void swap(uint64_t i1, uint64_t i2); |
|
71 |
- void erase(uint64_t pos, bool display=false); |
|
75 |
+ void erase(uint64_t pos); |
|
72 | 76 |
void updateMass(uint64_t pos, float newMass); |
73 |
- void test(uint64_t pos) const; |
|
77 |
+ |
|
78 |
+ bool test(uint64_t pos) const; |
|
74 | 79 |
|
75 | 80 |
// serialization |
76 | 81 |
friend Archive& operator<<(Archive &ar, AtomicDomain &domain); |
... | ... |
@@ -10,6 +10,7 @@ |
10 | 10 |
#include "AtomicDomain.h" |
11 | 11 |
|
12 | 12 |
#include <Rcpp.h> |
13 |
+#include <algorithm> |
|
13 | 14 |
|
14 | 15 |
// forward declarations needed for friend classes |
15 | 16 |
class AmplitudeGibbsSampler; |
... | ... |
@@ -51,20 +52,24 @@ protected: |
51 | 52 |
T* impl(); |
52 | 53 |
|
53 | 54 |
void processProposal(const AtomicProposal &prop); |
55 |
+ |
|
54 | 56 |
void birth(uint64_t pos, unsigned row, unsigned col); |
55 | 57 |
void death(uint64_t pos, float mass, unsigned row, unsigned col); |
56 | 58 |
void move(uint64_t src, float mass, uint64_t dest, unsigned r1, unsigned c1, |
57 | 59 |
unsigned r2, unsigned c2); |
58 | 60 |
void exchange(uint64_t p1, float mass1, uint64_t p2, float mass2, |
59 | 61 |
unsigned r1, unsigned c1, unsigned r2, unsigned c2); |
62 |
+ |
|
60 | 63 |
float gibbsMass(unsigned row, unsigned col, float mass); |
61 | 64 |
float gibbsMass(unsigned r1, unsigned c1, float m1, unsigned r2, |
62 | 65 |
unsigned c2, float m2); |
66 |
+ |
|
63 | 67 |
void addMass(uint64_t pos, float mass, unsigned row, unsigned col); |
64 | 68 |
void removeMass(uint64_t pos, float mass, unsigned row, unsigned col); |
69 |
+ float updateAtomMass(uint64_t pos, float mass, float delta); |
|
70 |
+ |
|
65 | 71 |
void acceptExchange(uint64_t p1, float m1, float d1, uint64_t p2, float m2, |
66 | 72 |
float d2, unsigned r1, unsigned c1, unsigned r2, unsigned c2); |
67 |
- bool updateAtomMass(uint64_t pos, float newMass); |
|
68 | 73 |
|
69 | 74 |
public: |
70 | 75 |
|
... | ... |
@@ -145,8 +150,10 @@ mMatrix(nrow, ncol), mOtherMatrix(NULL), mDMatrix(D), mSMatrix(S), |
145 | 150 |
mAPMatrix(D.nrow(), D.ncol()), mQueue(nrow * ncol, alpha), |
146 | 151 |
mAnnealingTemp(0.f), mNumRows(nrow), mNumCols(ncol) |
147 | 152 |
{ |
148 |
- mBinSize = std::numeric_limits<uint64_t>::max() / static_cast<uint64_t>(mNumRows * mNumCols); |
|
149 |
- uint64_t remain = std::numeric_limits<uint64_t>::max() % (mNumRows * mNumCols); |
|
153 |
+ mBinSize = std::numeric_limits<uint64_t>::max() |
|
154 |
+ / static_cast<uint64_t>(mNumRows * mNumCols); |
|
155 |
+ uint64_t remain = std::numeric_limits<uint64_t>::max() |
|
156 |
+ % static_cast<uint64_t>(mNumRows * mNumCols); |
|
150 | 157 |
mQueue.setDomainSize(std::numeric_limits<uint64_t>::max() - remain); |
151 | 158 |
} |
152 | 159 |
|
... | ... |
@@ -159,36 +166,15 @@ T* GibbsSampler<T, MatA, MatB>::impl() |
159 | 166 |
template <class T, class MatA, class MatB> |
160 | 167 |
void GibbsSampler<T, MatA, MatB>::update(unsigned nSteps) |
161 | 168 |
{ |
162 |
- unsigned n = 0; |
|
163 |
- while (n < nSteps) |
|
169 |
+ for (unsigned n = 0; n < nSteps; ++n) |
|
164 | 170 |
{ |
165 |
- /* |
|
166 |
- GAPS_ASSERT(nSteps - (queue.size() + n) >= 0); |
|
167 |
- mQueue.populate(mDomain, nSteps - (mQueue.size() + n)) |
|
168 |
- |
|
169 |
- // would making this a mulitple of nCores be better? |
|
170 |
- unsigned nJobs = mQueue.size(); |
|
171 |
- for (unsigned i = 0; i < nJobs; ++i) // can be run in parallel |
|
172 |
- { |
|
173 |
- processProposal(mQueue[i]); |
|
174 |
- } |
|
175 |
- mQueue.clear(); |
|
176 |
- n += nJobs; |
|
177 |
- GAPS_ASSERT(n <= nSteps); |
|
178 |
- */ |
|
179 |
- mQueue.populate(mDomain, 1); |
|
180 |
- GAPS_ASSERT(mQueue.size() == 1); |
|
181 |
- processProposal(mQueue[0]); |
|
182 |
- mQueue.clear(1); |
|
183 |
- GAPS_ASSERT(mQueue.size() == 0); |
|
184 |
- n++; |
|
171 |
+ processProposal(mQueue.makeProposal(mDomain)); |
|
185 | 172 |
} |
186 | 173 |
} |
187 | 174 |
|
188 | 175 |
template <class T, class MatA, class MatB> |
189 | 176 |
void GibbsSampler<T, MatA, MatB>::processProposal(const AtomicProposal &prop) |
190 | 177 |
{ |
191 |
- GAPS_ASSERT(prop.type == 'B' || prop.type == 'D' || prop.type == 'M' || prop.type == 'E'); |
|
192 | 178 |
unsigned r1 = impl()->getRow(prop.pos1); |
193 | 179 |
unsigned c1 = impl()->getCol(prop.pos1); |
194 | 180 |
unsigned r2 = 0, c2 = 0; |
... | ... |
@@ -220,6 +206,7 @@ void GibbsSampler<T, MatA, MatB>::addMass(uint64_t pos, float mass, unsigned row |
220 | 206 |
mMatrix(row, col) += mass; |
221 | 207 |
impl()->updateAPMatrix(row, col, mass); |
222 | 208 |
} |
209 |
+ |
|
223 | 210 |
template <class T, class MatA, class MatB> |
224 | 211 |
void GibbsSampler<T, MatA, MatB>::removeMass(uint64_t pos, float mass, unsigned row, unsigned col) |
225 | 212 |
{ |
... | ... |
@@ -234,7 +221,7 @@ template <class T, class MatA, class MatB> |
234 | 221 |
void GibbsSampler<T, MatA, MatB>::birth(uint64_t pos, unsigned row, |
235 | 222 |
unsigned col) |
236 | 223 |
{ |
237 |
- float mass = impl()->canUseGibbs(row, col) ? gibbsMass(row, col, 0.f) |
|
224 |
+ float mass = impl()->canUseGibbs(row, col) ? gibbsMass(row, col, mass) |
|
238 | 225 |
: gaps::random::exponential(mLambda); |
239 | 226 |
addMass(pos, mass, row, col); |
240 | 227 |
} |
... | ... |
@@ -246,16 +233,11 @@ void GibbsSampler<T, MatA, MatB>::death(uint64_t pos, float mass, unsigned row, |
246 | 233 |
unsigned col) |
247 | 234 |
{ |
248 | 235 |
removeMass(pos, mass, row, col); |
249 |
- mass = impl()->canUseGibbs(row, col) ? gibbsMass(row, col, mass) : mass; |
|
236 |
+ mass = impl()->canUseGibbs(row, col) ? gibbsMass(row, col, -mass) : mass; |
|
250 | 237 |
float deltaLL = impl()->computeDeltaLL(row, col, mass); |
251 | 238 |
if (deltaLL * mAnnealingTemp > std::log(gaps::random::uniform())) |
252 | 239 |
{ |
253 | 240 |
addMass(pos, mass, row, col); |
254 |
- mQueue.rejectDeath(); |
|
255 |
- } |
|
256 |
- else |
|
257 |
- { |
|
258 |
- mQueue.acceptDeath(); |
|
259 | 241 |
} |
260 | 242 |
} |
261 | 243 |
|
... | ... |
@@ -284,28 +266,21 @@ float m2, unsigned r1, unsigned c1, unsigned r2, unsigned c2) |
284 | 266 |
{ |
285 | 267 |
if (r1 != r2 || c1 != c2) // automatically reject if change in same bin |
286 | 268 |
{ |
287 |
- if (m1 < m2) |
|
269 |
+ if (m1 < m2) // mass 1 should always be larger |
|
288 | 270 |
{ |
289 |
- if (impl()->canUseGibbs(r2, c2, r1, c1)) |
|
290 |
- { |
|
291 |
- float gDelta = gibbsMass(r2, c2, m2, r1, c1, m1); |
|
292 |
- if (gDelta != 0.f) |
|
293 |
- { |
|
294 |
- acceptExchange(p2, m2, gDelta, p1, m1, -gDelta, r2, c2, r1, c1); |
|
295 |
- return; |
|
296 |
- } |
|
297 |
- } |
|
271 |
+ std::swap(r1, r2); |
|
272 |
+ std::swap(c1, c2); |
|
273 |
+ std::swap(p1, p2); |
|
274 |
+ std::swap(m1, m2); |
|
298 | 275 |
} |
299 |
- else |
|
276 |
+ |
|
277 |
+ if (impl()->canUseGibbs(r1, c1, r2, c2)) |
|
300 | 278 |
{ |
301 |
- if (impl()->canUseGibbs(r1, c1, r2, c2)) |
|
279 |
+ float gDelta = gibbsMass(r1, c1, m1, r2, c2, m2); |
|
280 |
+ if (gDelta < -m1) // janky, should be pair<bool, float> |
|
302 | 281 |
{ |
303 |
- float gDelta = gibbsMass(r1, c1, m1, r2, c2, m2); |
|
304 |
- if (gDelta != 0.f) |
|
305 |
- { |
|
306 |
- acceptExchange(p1, m1, gDelta, p2, m2, -gDelta, r1, c1, r2, c2); |
|
307 |
- return; |
|
308 |
- } |
|
282 |
+ acceptExchange(p1, m1, gDelta, p2, m2, -gDelta, r1, c1, r2, c2); |
|
283 |
+ return; |
|
309 | 284 |
} |
310 | 285 |
} |
311 | 286 |
|
... | ... |
@@ -334,24 +309,23 @@ float m2, unsigned r1, unsigned c1, unsigned r2, unsigned c2) |
334 | 309 |
} |
335 | 310 |
} |
336 | 311 |
} |
337 |
- mQueue.rejectDeath(); |
|
338 | 312 |
} |
339 | 313 |
|
340 | 314 |
// helper function for acceptExchange, used to conditionally update the mass |
341 | 315 |
// at a single position, deleting the atom if the new mass is too small |
342 | 316 |
template <class T, class MatA, class MatB> |
343 |
-bool GibbsSampler<T, MatA, MatB>::updateAtomMass(uint64_t pos, float newMass) |
|
317 |
+float GibbsSampler<T, MatA, MatB>::updateAtomMass(uint64_t pos, float mass, |
|
318 |
+float delta) |
|
344 | 319 |
{ |
345 |
- if (newMass < gaps::algo::epsilon) |
|
320 |
+ if (mass + delta < gaps::algo::epsilon) |
|
346 | 321 |
{ |
347 | 322 |
mDomain.erase(pos); |
348 |
- mQueue.acceptDeath(); |
|
349 |
- return false; |
|
323 |
+ return -mass; |
|
350 | 324 |
} |
351 | 325 |
else |
352 | 326 |
{ |
353 |
- mDomain.updateMass(pos, newMass); |
|
354 |
- return true; |
|
327 |
+ mDomain.updateMass(pos, mass + delta); |
|
328 |
+ return delta; |
|
355 | 329 |
} |
356 | 330 |
} |
357 | 331 |
|
... | ... |
@@ -362,14 +336,10 @@ void GibbsSampler<T, MatA, MatB>::acceptExchange(uint64_t p1, float m1, |
362 | 336 |
float d1, uint64_t p2, float m2, float d2, unsigned r1, unsigned c1, |
363 | 337 |
unsigned r2, unsigned c2) |
364 | 338 |
{ |
365 |
- bool b1 = updateAtomMass(p1, m1 + d1); |
|
366 |
- bool b2 = updateAtomMass(p2, m2 + d2); |
|
367 |
- GAPS_ASSERT(b1 || b2); |
|
339 |
+ GAPS_ASSERT(p1 != p2); |
|
340 |
+ d1 = updateAtomMass(p1, m1, d1); |
|
341 |
+ d2 = updateAtomMass(p2, m2, d2); |
|
368 | 342 |
|
369 |
- if (b1 && b2) |
|
370 |
- { |
|
371 |
- mQueue.rejectDeath(); |
|
372 |
- } |
|
373 | 343 |
mMatrix(r1, c1) += d1; |
374 | 344 |
mMatrix(r2, c2) += d2; |
375 | 345 |
impl()->updateAPMatrix(r1, c1, d1); |
... | ... |
@@ -382,19 +352,30 @@ float GibbsSampler<T, MatA, MatB>::gibbsMass(unsigned row, unsigned col, float m |
382 | 352 |
AlphaParameters alpha = impl()->alphaParameters(row, col); |
383 | 353 |
alpha.s *= mAnnealingTemp; |
384 | 354 |
alpha.su *= mAnnealingTemp; |
355 |
+ float mean = (alpha.su - mLambda) / alpha.s; |
|
356 |
+ float sd = 1.f / std::sqrt(alpha.s); |
|
357 |
+ float pLower = gaps::random::p_norm(0.f, mean, sd); |
|
385 | 358 |
|
386 |
- if (alpha.s > gaps::algo::epsilon) |
|
359 |
+ float newMass = 0.f; |
|
360 |
+ if (pLower == 1.f || alpha.s < 0.00001f) |
|
387 | 361 |
{ |
388 |
- float mean = (alpha.su - mLambda) / alpha.s; |
|
389 |
- float sd = 1.f / std::sqrt(alpha.s); |
|
390 |
- float pLower = gaps::random::p_norm(0.f, mean, sd); |
|
391 |
- |
|
392 |
- if (pLower < 1.f) |
|
362 |
+ newMass = mass < 0.f ? std::abs(mass) : 0.f; |
|
363 |
+ } |
|
364 |
+ else if (pLower >= 0.99f) // what's the point of this? TODO |
|
365 |
+ { |
|
366 |
+ float tmp1 = gaps::random::d_norm(0.f, mean, sd); |
|
367 |
+ float tmp2 = gaps::random::d_norm(10.f * mLambda, mean, sd); |
|
368 |
+ |
|
369 |
+ if (tmp1 > gaps::algo::epsilon && std::abs(tmp1 - tmp2) < gaps::algo::epsilon) |
|
393 | 370 |
{ |
394 |
- mass = gaps::random::inverseNormSample(pLower, 1.f, mean, sd); |
|
371 |
+ return mass < 0.f ? 0.0 : mass; |
|
395 | 372 |
} |
396 | 373 |
} |
397 |
- return std::min(std::max(gaps::algo::epsilon, mass), mMaxGibbsMass); |
|
374 |
+ else |
|
375 |
+ { |
|
376 |
+ newMass = gaps::random::inverseNormSample(pLower, 1.f, mean, sd); |
|
377 |
+ } |
|
378 |
+ return std::min(std::max(0.f, newMass), mMaxGibbsMass); |
|
398 | 379 |
} |
399 | 380 |
|
400 | 381 |
template <class T, class MatA, class MatB> |
... | ... |
@@ -412,14 +393,13 @@ unsigned r2, unsigned c2, float m2) |
412 | 393 |
float pLower = gaps::random::p_norm(-m1, mean, sd); |
413 | 394 |
float pUpper = gaps::random::p_norm(m2, mean, sd); |
414 | 395 |
|
415 |
- if (pLower < 0.95f && pUpper > 0.05f) |
|
396 |
+ if (!(pLower > 0.95f || pUpper < 0.05f)) |
|
416 | 397 |
{ |
417 |
- GAPS_ASSERT_MSG(pLower < pUpper, m1 << " , " << m2 << " , " << mean << " , " << sd); |
|
418 | 398 |
float delta = gaps::random::inverseNormSample(pLower, pUpper, mean, sd); |
419 | 399 |
return std::min(std::max(-m1, delta), m2); // conserve mass |
420 | 400 |
} |
421 | 401 |
} |
422 |
- return 0.f; |
|
402 |
+ return -m1 - 1.f; |
|
423 | 403 |
} |
424 | 404 |
|
425 | 405 |
template <class T, class MatA, class MatB> |
... | ... |
@@ -10,4 +10,10 @@ OBJECTS = Algorithms.o \ |
10 | 10 |
Random.o \ |
11 | 11 |
RcppExports.o \ |
12 | 12 |
test-runner.o \ |
13 |
+ cpp_tests/testAlgorithms.o \ |
|
14 |
+ cpp_tests/testAtomicDomain.o \ |
|
15 |
+ cpp_tests/testGibbsSampler.o \ |
|
16 |
+ cpp_tests/testMatrix.o \ |
|
17 |
+ cpp_tests/testProposalQueue.o \ |
|
13 | 18 |
cpp_tests/testRandom.o |
19 |
+ |
... | ... |
@@ -2,12 +2,6 @@ |
2 | 2 |
#include "ProposalQueue.h" |
3 | 3 |
#include "Random.h" |
4 | 4 |
|
5 |
-void ProposalQueue::populate(const AtomicDomain &domain, unsigned limit) |
|
6 |
-{ |
|
7 |
- unsigned nIter = 0; |
|
8 |
- while (nIter++ < limit && makeProposal(domain)); |
|
9 |
-} |
|
10 |
- |
|
11 | 5 |
void ProposalQueue::setNumBins(unsigned nBins) |
12 | 6 |
{ |
13 | 7 |
mNumBins = nBins; |
... | ... |
@@ -18,51 +12,12 @@ void ProposalQueue::setDomainSize(uint64_t size) |
18 | 12 |
mDomainSize = size; |
19 | 13 |
} |
20 | 14 |
|
21 |
-void ProposalQueue::setDimensionSize(unsigned size) |
|
22 |
-{ |
|
23 |
- mDimensionSize = size; |
|
24 |
-} |
|
25 |
- |
|
26 | 15 |
void ProposalQueue::setAlpha(float alpha) |
27 | 16 |
{ |
28 | 17 |
mAlpha = alpha; |
29 | 18 |
} |
30 | 19 |
|
31 |
-void ProposalQueue::clear(unsigned n) |
|
32 |
-{ |
|
33 |
- mQueue.erase(mQueue.end() - n, mQueue.end()); |
|
34 |
- mUsedIndices.erase(mUsedIndices.end() - n, mUsedIndices.end()); |
|
35 |
- mUsedPositions.erase(mUsedPositions.end() - n, mUsedPositions.end()); |
|
36 |
- GAPS_ASSERT(mMaxAtoms - mMinAtoms <= mQueue.size()); |
|
37 |
-} |
|
38 |
- |
|
39 |
-unsigned ProposalQueue::size() const |
|
40 |
-{ |
|
41 |
- return mQueue.size(); |
|
42 |
-} |
|
43 |
- |
|
44 |
-const AtomicProposal& ProposalQueue::operator[](int n) const |
|
45 |
-{ |
|
46 |
- return mQueue[mQueue.size() - 1 - n]; |
|
47 |
-} |
|
48 |
- |
|
49 |
-void ProposalQueue::acceptDeath() |
|
50 |
-{ |
|
51 |
- mMaxAtoms--; |
|
52 |
-} |
|
53 |
- |
|
54 |
-void ProposalQueue::rejectDeath() |
|
55 |
-{ |
|
56 |
- mMinAtoms++; |
|
57 |
-} |
|
58 |
- |
|
59 |
-void ProposalQueue::rejectBirth() |
|
60 |
-{ |
|
61 |
- mMinAtoms--; |
|
62 |
- mMaxAtoms--; |
|
63 |
-} |
|
64 |
- |
|
65 |
-float ProposalQueue::deleteProb(unsigned nAtoms) const |
|
20 |
+float ProposalQueue::deathProb(unsigned nAtoms) const |
|
66 | 21 |
{ |
67 | 22 |
double size = static_cast<double>(mDomainSize); |
68 | 23 |
double term1 = (size - static_cast<double>(nAtoms)) / size; |
... | ... |
@@ -70,147 +25,60 @@ float ProposalQueue::deleteProb(unsigned nAtoms) const |
70 | 25 |
return static_cast<double>(nAtoms) / (static_cast<double>(nAtoms) + term2); |
71 | 26 |
} |
72 | 27 |
|
73 |
-bool ProposalQueue::makeProposal(const AtomicDomain &domain) |
|
28 |
+AtomicProposal ProposalQueue::makeProposal(const AtomicDomain &domain) |
|
74 | 29 |
{ |
75 |
- // special indeterminate cases |
|
76 |
- if ((mMinAtoms == 0 && mMaxAtoms > 0) || (mMinAtoms < 2 && mMaxAtoms >= 2)) |
|
77 |
- { |
|
78 |
- return false; |
|
79 |
- } |
|
80 |
- |
|
81 | 30 |
// always birth when no atoms exist |
82 |
- if (mMaxAtoms == 0) |
|
31 |
+ if (domain.size() == 0) |
|
83 | 32 |
{ |
84 | 33 |
return birth(domain); |
85 | 34 |
} |
86 | 35 |
|
87 |
- float bdProb = mMaxAtoms < 2 ? 0.6667f : 0.5f; |
|
88 |
- float u1 = gaps::random::uniform(); // cache these values if a failure |
|
89 |
- float u2 = gaps::random::uniform(); // happens, needed to prevent bias |
|
90 |
- float lowerBound = deleteProb(mMinAtoms); |
|
91 |
- float upperBound = deleteProb(mMaxAtoms); |
|
92 |
- |
|
93 |
- if (u1 < bdProb && u2 >= upperBound) |
|
36 |
+ float bdProb = domain.size() < 2 ? 0.6667f : 0.5f; |
|
37 |
+ float u = gaps::random::uniform(); |
|
38 |
+ if (u < bdProb) |
|
94 | 39 |
{ |
95 |
- return birth(domain); |
|
40 |
+ return gaps::random::uniform() < deathProb(domain.size()) ? |
|
41 |
+ death(domain) : birth(domain); |
|
96 | 42 |
} |
97 |
- else if (u1 < bdProb && u2 < lowerBound) |
|
43 |
+ else if (u < 0.75f || domain.size() < 2) |
|
98 | 44 |
{ |
99 |
- return death(domain); |
|
45 |
+ return move(domain); |
|
100 | 46 |
} |
101 |
- else if (u1 >= bdProb) |
|
47 |
+ else |
|
102 | 48 |
{ |
103 |
- return (u1 < 0.75f || mMaxAtoms < 2) ? move(domain) : exchange(domain); |
|
49 |
+ return exchange(domain); |
|
104 | 50 |
} |
105 |
- return false; |
|
106 | 51 |
} |
107 | 52 |
|
108 |
-static bool isInVector(const std::vector<unsigned> &vec, unsigned n) |
|
109 |
-{ |
|
110 |
- return std::find(vec.begin(), vec.end(), n) != vec.end(); |
|
111 |
-} |
|
112 |
- |
|
113 |
-bool ProposalQueue::birth(const AtomicDomain &domain) |
|
53 |
+AtomicProposal ProposalQueue::birth(const AtomicDomain &domain) |
|
114 | 54 |
{ |
115 | 55 |
uint64_t pos = domain.randomFreePosition(); |
116 |
- //if (isInVector(mUsedIndices, pos / mDimensionSize)) |
|
117 |
- //{ |
|
118 |
- // return false; // matrix conflict - can't compute gibbs mass |
|
119 |
- //} |
|
120 |
- mQueue.push_back(AtomicProposal('B', pos)); |
|
121 |
- //mUsedIndices.push_back(pos / mDimensionSize); |
|
122 |
- //mUsedPositions.push_back(pos); |
|
123 |
- mMinAtoms++; |
|
124 |
- mMaxAtoms++; |
|
125 |
- return true; |
|
56 |
+ return AtomicProposal('B', pos); |
|
126 | 57 |
} |
127 | 58 |
|
128 |
-bool ProposalQueue::death(const AtomicDomain &domain) |
|
59 |
+AtomicProposal ProposalQueue::death(const AtomicDomain &domain) |
|
129 | 60 |
{ |
130 | 61 |
Atom a = domain.randomAtom(); |
131 |
- //if (isInVector(mUsedIndices, a.pos / mDimensionSize)) |
|
132 |
- //{ |
|
133 |
- // return false; // matrix conflict - can't compute gibbs mass or deltaLL |
|
134 |
- //} |
|
135 |
- mQueue.push_back(AtomicProposal('D', a.pos, a.mass)); |
|
136 |
- //mUsedIndices.push_back(a.pos / mDimensionSize); |
|
137 |
- //mUsedPositions.push_back(a.pos); |
|
138 |
- mMinAtoms--; |
|
139 |
- return true; |
|
62 |
+ GAPS_ASSERT(domain.test(a.pos)); |
|
63 |
+ return AtomicProposal('D', a.pos, a.mass); |
|
140 | 64 |
} |
141 | 65 |
|
142 |
-bool ProposalQueue::move(const AtomicDomain &domain) |
|
66 |
+AtomicProposal ProposalQueue::move(const AtomicDomain &domain) |
|
143 | 67 |
{ |
144 | 68 |
Atom a = domain.randomAtom(); |
145 | 69 |
uint64_t lbound = domain.hasLeft(a) ? domain.left(a).pos : 0; |
146 | 70 |
uint64_t rbound = domain.hasRight(a) ? domain.right(a).pos : mDomainSize; |
147 |
- |
|
148 |
- //for (unsigned i = 0; i < mUsedPositions.size(); ++i) |
|
149 |
- //{ |
|
150 |
- // if (mUsedPositions[i] >= lbound && mUsedPositions[i] <= rbound) |
|
151 |
- // { |
|
152 |
- // return false; // atomic conflict - don't know neighbors |
|
153 |
- // } |
|
154 |
- //} |
|
155 |
- |
|
156 | 71 |
uint64_t newLocation = gaps::random::uniform64(lbound + 1, rbound - 1); |
157 |
- //if (isInVector(mUsedIndices, a.pos / mDimensionSize) || isInVector(mUsedIndices, newLocation / mDimensionSize)) |
|
158 |
- //{ |
|
159 |
- // return false; // matrix conflict - can't compute deltaLL |
|
160 |
- //} |
|
161 |
- |
|
162 |
- mQueue.push_back(AtomicProposal('M', a.pos, a.mass, newLocation)); |
|
163 |
- //mUsedIndices.push_back(a.pos / mDimensionSize); |
|
164 |
- //mUsedIndices.push_back(newLocation / mDimensionSize); |
|
165 |
- //mUsedPositions.push_back(a.pos); |
|
166 |
- //mUsedPositions.push_back(newLocation); |
|
167 |
- return true; |
|
72 |
+ GAPS_ASSERT(domain.test(a.pos)); |
|
73 |
+ return AtomicProposal('M', a.pos, a.mass, newLocation); |
|
168 | 74 |
} |
169 | 75 |
|
170 |
-bool ProposalQueue::exchange(const AtomicDomain &domain) |
|
76 |
+AtomicProposal ProposalQueue::exchange(const AtomicDomain &domain) |
|
171 | 77 |
{ |
172 | 78 |
Atom a1 = domain.randomAtom(); |
173 |
- domain.test(a1.pos); |
|
174 |
- GAPS_ASSERT(a1.rightNdx <= domain.size()); |
|
175 |
- GAPS_ASSERT(a1.leftNdx <= domain.size()); |
|
176 |
- //if (a1.right) // has neighbor |
|
177 |
- //{ |
|
178 |
- // for (unsigned i = 0; i < mUsedPositions.size(); ++i) |
|
179 |
- // { |
|
180 |
- // if (mUsedPositions[i] >= a1.pos && mUsedPositions[i] <= a1.right->pos) |
|
181 |
- // { |
|
182 |
- // return false; // atomic conflict - don't know right neighbor |
|
183 |
- // } |
|
184 |
- // } |
|
185 |
- //} |
|
186 |
- //else // exchange with first atom |
|
187 |
- //{ |
|
188 |
- // for (unsigned i = 0; i < mUsedPositions.size(); ++i) |
|
189 |
- // { |
|
190 |
- // if (mUsedPositions[i] >= a1.pos || mUsedPositions[i] <= domain.front().pos) |
|
191 |
- // { |
|
192 |
- // return false; // atomic conflict - don't know right neighbor |
|
193 |
- // } |
|
194 |
- // } |
|
195 |
- //} |
|
196 |
- |
|
197 | 79 |
Atom a2 = domain.hasRight(a1) ? domain.right(a1) : domain.front(); |
198 |
- domain.test(a1.pos); |
|
199 |
- GAPS_ASSERT(a1.rightNdx <= domain.size()); |
|
200 |
- GAPS_ASSERT(a1.leftNdx <= domain.size()); |
|
201 |
- GAPS_ASSERT(a2.rightNdx <= domain.size()); |
|
202 |
- GAPS_ASSERT(a2.leftNdx <= domain.size()); |
|
203 |
- domain.test(a2.pos); |
|
204 |
- //if (isInVector(mUsedIndices, a1.pos / mDimensionSize) || isInVector(mUsedIndices, a2.pos / mDimensionSize)) |
|
205 |
- //{ |
|
206 |
- // return false; // matrix conflict - can't compute gibbs mass or deltaLL |
|
207 |
- //} |
|
208 |
- |
|
209 |
- mQueue.push_back(AtomicProposal('E', a1.pos, a1.mass, a2.pos, a2.mass)); |
|
210 |
- //mUsedIndices.push_back(a1.pos / mDimensionSize); |
|
211 |
- //mUsedIndices.push_back(a2.pos / mDimensionSize); |
|
212 |
- //mUsedPositions.push_back(a1.pos); |
|
213 |
- //mUsedPositions.push_back(a2.pos); |
|
214 |
- mMinAtoms--; |
|
215 |
- return true; |
|
80 |
+ GAPS_ASSERT(domain.test(a1.pos)); |
|
81 |
+ GAPS_ASSERT(domain.test(a2.pos)); |
|
82 |
+ GAPS_ASSERT(a1.pos != a2.pos); |
|
83 |
+ return AtomicProposal('E', a1.pos, a1.mass, a2.pos, a2.mass); |
|
216 | 84 |
} |
217 | 85 |
\ No newline at end of file |
... | ... |
@@ -20,57 +20,34 @@ struct AtomicProposal |
20 | 20 |
{} |
21 | 21 |
}; |
22 | 22 |
|
23 |
-// generate list of independent proposals |
|
24 |
-// does order really matter? i.e. could this be a stack? |
|
25 |
-// TODO need to cache failed proposal random numbers |
|
26 |
-// TODO use hash tables for atom conflicts |
|
23 |
+// generate single atomic proposal for now |
|
27 | 24 |
class ProposalQueue |
28 | 25 |
{ |
29 | 26 |
private: |
30 | 27 |
|
31 |
- std::vector<AtomicProposal> mQueue; // not really a queue for now |
|
32 |
- |
|
33 |
- std::vector<unsigned> mUsedIndices; // used rows/cols for A/P matrix |
|
34 |
- std::vector<unsigned> mUsedPositions; // used positions in atomic domain |
|
35 |
- |
|
36 |
- uint64_t mMinAtoms; |
|
37 |
- uint64_t mMaxAtoms; |
|
38 |
- |
|
39 | 28 |
uint64_t mNumBins; |
40 |
- uint64_t mDimensionSize; // rows of A, cols of P |
|
41 | 29 |
uint64_t mDomainSize; |
42 |
- |
|
43 | 30 |
float mAlpha; |
44 | 31 |
|
45 |
- float deleteProb(unsigned nAtoms) const; |
|
46 |
- bool makeProposal(const AtomicDomain &domain); |
|
47 |
- bool birth(const AtomicDomain &domain); |
|
48 |
- bool death(const AtomicDomain &domain); |
|
49 |
- bool move(const AtomicDomain &domain); |
|
50 |
- bool exchange(const AtomicDomain &domain); |
|
32 |
+ float deathProb(unsigned nAtoms) const; |
|
33 |
+ AtomicProposal birth(const AtomicDomain &domain); |
|
34 |
+ AtomicProposal death(const AtomicDomain &domain); |
|
35 |
+ AtomicProposal move(const AtomicDomain &domain); |
|
36 |
+ AtomicProposal exchange(const AtomicDomain &domain); |
|
51 | 37 |
|
52 | 38 |
public: |
53 | 39 |
|
54 | 40 |
ProposalQueue(unsigned nBins, float alpha) |
55 |
- : mMinAtoms(0), mMaxAtoms(0), mNumBins(nBins), mAlpha(alpha) |
|
41 |
+ : mNumBins(nBins), mAlpha(alpha) |
|
56 | 42 |
{} |
57 | 43 |
|
58 |
- // set variables |
|
44 |
+ // set parameters |
|
59 | 45 |
void setNumBins(unsigned nBins); |
60 |
- void setDimensionSize(unsigned nIndices); |
|
61 | 46 |
void setDomainSize(uint64_t size); |
62 | 47 |
void setAlpha(float alpha); |
63 | 48 |
|
64 | 49 |
// modify/access queue |
65 |
- void populate(const AtomicDomain &domain, unsigned limit); |
|
66 |
- void clear(unsigned n); |
|
67 |
- unsigned size() const; |
|
68 |
- const AtomicProposal& operator[](int n) const; |
|
69 |
- |
|
70 |
- // update min/max atoms |
|
71 |
- void acceptDeath(); |
|
72 |
- void rejectDeath(); |
|
73 |
- void rejectBirth(); |
|
50 |
+ AtomicProposal makeProposal(const AtomicDomain &domain); |
|
74 | 51 |
|
75 | 52 |
// serialization |
76 | 53 |
friend Archive& operator<<(Archive &ar, ProposalQueue &queue); |
... | ... |
@@ -142,8 +142,7 @@ float gaps::random::p_norm(float p, float mean, float sd) |
142 | 142 |
|
143 | 143 |
float gaps::random::inverseNormSample(float a, float b, float mean, float sd) |
144 | 144 |
{ |
145 |
- //GAPS_ASSERT(a != b); |
|
146 |
- GAPS_ASSERT(!((a == 0.f && b == 0.f) || (a == 1.f) && (b == 1.f))); |
|
145 |
+ GAPS_ASSERT(!((a == 0.f && b == 0.f) || (a == 1.f && b == 1.f))); |
|
147 | 146 |
float u = gaps::random::uniform(a, b); |
148 | 147 |
while (u == 0.f || u == 1.f) |
149 | 148 |
{ |
... | ... |
@@ -154,7 +153,7 @@ float gaps::random::inverseNormSample(float a, float b, float mean, float sd) |
154 | 153 |
|
155 | 154 |
float gaps::random::inverseGammaSample(float a, float b, float mean, float sd) |
156 | 155 |
{ |
157 |
- GAPS_ASSERT(a != b); |
|
156 |
+ GAPS_ASSERT(!((a == 0.f && b == 0.f) || (a == 1.f && b == 1.f))); |
|
158 | 157 |
float u = gaps::random::uniform(a, b); |
159 | 158 |
while (u == 0.f || u == 1.f) |
160 | 159 |
{ |
... | ... |
@@ -4,6 +4,8 @@ |
4 | 4 |
|
5 | 5 |
#define MAT_SUM(nR, nC) ((nR + nC - 2) * nR * nC / 2.f) |
6 | 6 |
|
7 |
+#if 0 |
|
8 |
+ |
|
7 | 9 |
TEST_CASE("Test Algorithms.h") |
8 | 10 |
{ |
9 | 11 |
unsigned nrow = 25; |
... | ... |
@@ -54,4 +56,6 @@ TEST_CASE("Test Algorithms.h") |
54 | 56 |
REQUIRE(gaps::algo::isColZero(A, 0)); |
55 | 57 |
REQUIRE(!gaps::algo::isColZero(A, 1)); |
56 | 58 |
} |
57 |
-} |
|
58 | 59 |
\ No newline at end of file |
60 |
+} |
|
61 |
+ |
|
62 |
+#endif |
|
59 | 63 |
\ No newline at end of file |
60 | 64 |
similarity index 96% |
61 | 65 |
rename from src/cpp_tests/testAtomicSupport.cpp |
62 | 66 |
rename to src/cpp_tests/testAtomicDomain.cpp |
... | ... |
@@ -1,8 +1,10 @@ |
1 | 1 |
#include "catch.h" |
2 |
-#include "../AtomicSupport.h" |
|
2 |
+#include "../AtomicDomain.h" |
|
3 | 3 |
|
4 | 4 |
#define TEST_APPROX(x) Approx(x).epsilon(0.001) |
5 | 5 |
|
6 |
+#if 0 |
|
7 |
+ |
|
6 | 8 |
TEST_CASE("Test AtomicSupport.h") |
7 | 9 |
{ |
8 | 10 |
unsigned nrow = 100, ncol = 500; |
... | ... |
@@ -232,4 +234,6 @@ TEST_CASE("Internal AtomicSupport Tests") |
232 | 234 |
} |
233 | 235 |
} |
234 | 236 |
|
237 |
+#endif |
|
238 |
+ |
|
235 | 239 |
#endif |
236 | 240 |
\ No newline at end of file |
... | ... |
@@ -7,6 +7,8 @@ |
7 | 7 |
|
8 | 8 |
#define TEST_APPROX(x) Approx(x).epsilon(0.001) |
9 | 9 |
|
10 |
+#if 0 |
|
11 |
+ |
|
10 | 12 |
TEST_CASE("Test GibbsSampler.h") |
11 | 13 |
{ |
12 | 14 |
gaps::random::setSeed(0); |
... | ... |
@@ -160,4 +162,6 @@ TEST_CASE("Internal GibbsSampler Tests") |
160 | 162 |
} |
161 | 163 |
} |
162 | 164 |
|
165 |
+#endif |
|
166 |
+ |
|
163 | 167 |
#endif |
164 | 168 |
\ No newline at end of file |
... | ... |
@@ -8,15 +8,12 @@ TEST_CASE("Test Matrix.h") |
8 | 8 |
Vector v(10); |
9 | 9 |
RowMatrix rm(10, 25); |
10 | 10 |
ColMatrix cm(10, 25); |
11 |
- TwoWayMatrix twm(10, 25); |
|
12 | 11 |
|
13 | 12 |
REQUIRE(v.size() == 10); |
14 | 13 |
REQUIRE(rm.nRow() == 10); |
15 | 14 |
REQUIRE(rm.nCol() == 25); |
16 | 15 |
REQUIRE(cm.nRow() == 10); |
17 | 16 |
REQUIRE(cm.nCol() == 25); |
18 |
- REQUIRE(twm.nRow() == 10); |
|
19 |
- REQUIRE(twm.nCol() == 25); |
|
20 | 17 |
} |
21 | 18 |
|
22 | 19 |
SECTION("Matrix Initialization from R Matrix") |
... | ... |
@@ -39,9 +36,6 @@ TEST_CASE("Test Matrix.h") |
39 | 36 |
ColMatrix cmD(rD); |
40 | 37 |
ColMatrix cmS(rS); |
41 | 38 |
|
42 |
- TwoWayMatrix twmD(rD); |
|
43 |
- TwoWayMatrix twmS(rS); |
|
44 |
- |
|
45 | 39 |
REQUIRE(rmD.nRow() == 1363); |
46 | 40 |
REQUIRE(rmD.nCol() == 9); |
47 | 41 |
|
... | ... |
@@ -53,12 +47,6 @@ TEST_CASE("Test Matrix.h") |
53 | 47 |
|
54 | 48 |
REQUIRE(cmS.nRow() == 1363); |
55 | 49 |
REQUIRE(cmS.nCol() == 9); |
56 |
- |
|
57 |
- REQUIRE(twmD.nRow() == 1363); |
|
58 |
- REQUIRE(twmD.nCol() == 9); |
|
59 |
- |
|
60 |
- REQUIRE(twmS.nRow() == 1363); |
|
61 |
- REQUIRE(twmS.nCol() == 9); |
|
62 | 50 |
} |
63 | 51 |
|
64 | 52 |
SECTION("Matrix Update") |
... | ... |
@@ -82,14 +70,5 @@ TEST_CASE("Test Matrix.h") |
82 | 70 |
REQUIRE(rm.getRow(3)[4] == 6.0); |
83 | 71 |
REQUIRE(cm.getCol(2)[1] == 18.0); |
84 | 72 |
} |
85 |
- |
|
86 |
- SECTION("TwoWayMatrix set") |
|
87 |
- { |
|
88 |
- TwoWayMatrix mat(100, 300); |
|
89 |
- mat.set(0,299,54.0); |
|
90 |
- |
|
91 |
- REQUIRE(mat.getRow(0)[299] == 54.0); |
|
92 |
- REQUIRE(mat.getCol(299)[0] == 54.0); |
|
93 |
- } |
|
94 | 73 |
} |
95 | 74 |
|
96 | 75 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,239 @@ |
1 |
+#include "catch.h" |
|
2 |
+#include "../AtomicDomain.h" |
|
3 |
+ |
|
4 |
+#define TEST_APPROX(x) Approx(x).epsilon(0.001) |
|
5 |
+ |
|
6 |
+#if 0 |
|
7 |
+ |
|
8 |
+TEST_CASE("Test AtomicSupport.h") |
|
9 |
+{ |
|
10 |
+ unsigned nrow = 100, ncol = 500; |
|
11 |
+ MatrixChange dummy('A', 0, 0, 0.f); |
|
12 |
+ |
|
13 |
+ SECTION("Make and Convert proposals") |
|
14 |
+ { |
|
15 |
+ AtomicSupport domain('A', nrow, ncol, 1.0, 0.5); |
|
16 |
+ REQUIRE(domain.alpha() == 1.0); |
|
17 |
+ REQUIRE(domain.lambda() == 0.5); |
|
18 |
+ |
|
19 |
+ gaps::random::setSeed(1); |
|
20 |
+ AtomicProposal prop = domain.makeProposal(); |
|
21 |
+ |
|
22 |
+ REQUIRE(prop.label == 'A'); |
|
23 |
+ REQUIRE(prop.type == 'B'); |
|
24 |
+ REQUIRE(prop.nChanges == 1); |
|
25 |
+ REQUIRE(prop.pos2 == 0); |
|
26 |
+ REQUIRE(prop.delta2 == 0.0); |
|
27 |
+ |
|
28 |
+ domain.acceptProposal(prop, dummy); |
|
29 |
+ |
|
30 |
+ REQUIRE(domain.numAtoms() == 1); |
|
31 |
+ |
|
32 |
+ for (unsigned i = 0; i < 10000; ++i) |
|
33 |
+ { |
|
34 |
+ prop = domain.makeProposal(); |
|
35 |
+ |
|
36 |
+ REQUIRE(prop.label == 'A'); |
|
37 |
+ bool cond1 = prop.type == 'B' && prop.nChanges == 1; |
|
38 |
+ bool cond2 = prop.type == 'D' && prop.nChanges == 1; |
|
39 |
+ bool cond3 = prop.type == 'M' && prop.nChanges == 2; |
|
40 |
+ bool cond4 = prop.type == 'E' && prop.nChanges == 2; |
|
41 |
+ bool cond = cond1 || cond2 || cond3 || cond4; |
|
42 |
+ |
|
43 |
+ REQUIRE(cond); |
|
44 |
+ |
|
45 |
+ uint64_t nOld = domain.numAtoms(); |
|
46 |
+ |
|
47 |
+ domain.acceptProposal(prop, dummy); |
|
48 |
+ |
|
49 |
+ if (prop.type == 'B') |
|
50 |
+ { |
|
51 |
+ REQUIRE(domain.numAtoms() == nOld + 1); |
|
52 |
+ } |
|
53 |
+ else if (prop.type == 'D') |
|
54 |
+ { |
|
55 |
+ REQUIRE(domain.numAtoms() == nOld - 1); |
|
56 |
+ } |
|
57 |
+ else if (prop.type == 'M') |
|
58 |
+ { |
|
59 |
+ REQUIRE(domain.numAtoms() == nOld); |
|
60 |
+ } |
|
61 |
+ |
|
62 |
+ } |
|
63 |
+ } |
|
64 |
+ |
|
65 |
+ SECTION("Proposal Distribution") |
|
66 |
+ { |
|
67 |
+ AtomicSupport domain('A', nrow, ncol, 0.01, 0.05); |
|
68 |
+ |
|
69 |
+ gaps::random::setSeed(1); |
|
70 |
+ |
|
71 |
+ unsigned nB = 0, nD = 0, nM = 0, nE = 0; |
|
72 |
+ for (unsigned i = 0; i < 5000; ++i) |
|
73 |
+ { |
|
74 |
+ AtomicProposal prop = domain.makeProposal(); |
|
75 |
+ domain.acceptProposal(prop, dummy); |
|
76 |
+ |
|
77 |
+ switch(prop.type) |
|
78 |
+ { |
|
79 |
+ case 'B': nB++; break; |
|
80 |
+ case 'D': nD++; break; |
|
81 |
+ case 'M': nM++; break; |
|
82 |
+ case 'E': nE++; break; |
|
83 |
+ } |
|
84 |
+ } |
|
85 |
+ REQUIRE(domain.numAtoms() > 100); |
|
86 |
+ REQUIRE(nB > 500); |
|
87 |
+ REQUIRE(nM > 500); |
|
88 |
+ REQUIRE(nE > 500); |
|
89 |
+ } |
|
90 |
+} |
|
91 |
+ |
|
92 |
+#ifdef GAPS_INTERNAL_TESTS |
|
93 |
+ |
|
94 |
+TEST_CASE("Internal AtomicSupport Tests") |
|
95 |
+{ |
|
96 |
+ unsigned nrow = 29, ncol = 53; |
|
97 |
+ AtomicSupport Adomain('A', nrow, ncol, 1.0, 0.5); |
|
98 |
+ AtomicSupport Pdomain('P', nrow, ncol, 1.0, 0.5); |
|
99 |
+ |
|
100 |
+ std::vector<uint64_t> aAtomPos; |
|
101 |
+ std::vector<uint64_t> pAtomPos; |
|
102 |
+ |
|
103 |
+ for (unsigned i = 0; i < 100; ++i) |
|
104 |
+ { |
|
105 |
+ AtomicProposal propA = Adomain.proposeBirth(); |
|
106 |
+ AtomicProposal propP = Pdomain.proposeBirth(); |
|
107 |
+ Adomain.acceptProposal(propA, dummy); |
|
108 |
+ Pdomain.acceptProposal(propP, dummy); |
|
109 |
+ aAtomPos.push_back(propA.pos1); |
|
110 |
+ pAtomPos.push_back(propP.pos1); |
|
111 |
+ } |
|
112 |
+ |
|
113 |
+ REQUIRE(Adomain.numAtoms() == 100); |
|
114 |
+ REQUIRE(Pdomain.numAtoms() == 100); |
|
115 |
+ |
|
116 |
+ SECTION("get row/col") |
|
117 |
+ { |
|
118 |
+ for (unsigned i = 0; i < 10000; ++i) |
|
119 |
+ { |
|
120 |
+ REQUIRE(Adomain.getRow(gaps::random::uniform64()) < nrow); |
|
121 |
+ REQUIRE(Adomain.getCol(gaps::random::uniform64()) < ncol); |
|
122 |
+ |
|
123 |
+ REQUIRE(Pdomain.getRow(gaps::random::uniform64()) < nrow); |
|
124 |
+ REQUIRE(Pdomain.getCol(gaps::random::uniform64()) < ncol); |
|
125 |
+ } |
|
126 |
+ } |
|
127 |
+ |
|
128 |
+ SECTION("randomFreePosition") |
|
129 |
+ { |
|
130 |
+ for (unsigned i = 0; i < 10000; ++i) |
|
131 |
+ { |
|
132 |
+ uint64_t posA = Adomain.randomFreePosition(); |
|
133 |
+ uint64_t posP = Pdomain.randomFreePosition(); |
|
134 |
+ |
|
135 |
+ REQUIRE(std::find(aAtomPos.begin(), aAtomPos.end(), posA) == aAtomPos.end()); |
|
136 |
+ REQUIRE(std::find(pAtomPos.begin(), pAtomPos.end(), posP) == pAtomPos.end()); |
|
137 |
+ } |
|
138 |
+ } |
|
139 |
+ |
|
140 |
+ SECTION("randomAtomPosition") |
|
141 |
+ { |
|
142 |
+ for (unsigned i = 0; i < 10000; ++i) |
|
143 |
+ { |
|
144 |
+ uint64_t posA = Adomain.randomAtomPosition(); |
|
145 |
+ uint64_t posP = Pdomain.randomAtomPosition(); |
|
146 |
+ |
|
147 |
+ REQUIRE(std::find(aAtomPos.begin(), aAtomPos.end(), posA) != aAtomPos.end()); |
|
148 |
+ REQUIRE(std::find(pAtomPos.begin(), pAtomPos.end(), posP) != pAtomPos.end()); |
|
149 |
+ } |
|
150 |
+ } |
|
151 |
+ |
|
152 |
+ SECTION("updateAtomMass") |
|
153 |
+ { |
|
154 |
+ float oldMass = 0.0; |
|
155 |
+ uint64_t posA = 0, posP = 0; |
|
156 |
+ for (unsigned i = 0; i < 1000; ++i) |
|
157 |
+ { |
|
158 |
+ posA = Adomain.randomAtomPosition(); |
|
159 |
+ oldMass = Adomain.at(posA); |
|
160 |
+ REQUIRE_NOTHROW(Adomain.updateAtomMass(posA, 0.05f)); |
|
161 |
+ REQUIRE(Adomain.at(posA) == TEST_APPROX(oldMass + 0.05f)); |
|
162 |
+ |
|
163 |
+ posP = Pdomain.randomAtomPosition(); |
|
164 |
+ oldMass = Pdomain.at(posP); |
|
165 |
+ REQUIRE_NOTHROW(Pdomain.updateAtomMass(posP, 0.05f)); |
|
166 |
+ REQUIRE(Pdomain.at(posP) == TEST_APPROX(oldMass + 0.05f)); |
|
167 |
+ } |
|
168 |
+ } |
|
169 |
+ |
|
170 |
+ SECTION("proposeBirth") |
|
171 |
+ { |
|
172 |
+ for (unsigned i = 0; i < 1000; ++i) |
|
173 |
+ { |
|
174 |
+ AtomicProposal propA = Adomain.proposeBirth(); |
|
175 |
+ REQUIRE(propA.nChanges == 1); |
|
176 |
+ REQUIRE(std::find(aAtomPos.begin(), aAtomPos.end(), propA.pos1) == aAtomPos.end()); |
|
177 |
+ REQUIRE(propA.delta1 > 0); |
|
178 |
+ |
|
179 |
+ AtomicProposal propP = Pdomain.proposeBirth(); |
|
180 |
+ REQUIRE(propP.nChanges == 1); |
|
181 |
+ REQUIRE(std::find(pAtomPos.begin(), pAtomPos.end(), propP.pos1) == pAtomPos.end()); |
|
182 |
+ REQUIRE(propP.delta1 > 0); |
|
183 |
+ } |
|
184 |
+ } |
|
185 |
+ |
|
186 |
+ SECTION("proposeDeath") |
|
187 |
+ { |
|
188 |
+ for (unsigned i = 0; i < 1000; ++i) |
|
189 |
+ { |
|
190 |
+ AtomicProposal propA = Adomain.proposeDeath(); |
|
191 |
+ REQUIRE(propA.nChanges == 1); |
|
192 |
+ REQUIRE(std::find(aAtomPos.begin(), aAtomPos.end(), propA.pos1) != aAtomPos.end()); |
|
193 |
+ REQUIRE(propA.delta1 < 0); |
|
194 |
+ |
|
195 |
+ AtomicProposal propP = Pdomain.proposeDeath(); |
|
196 |
+ REQUIRE(propP.nChanges == 1); |
|
197 |
+ REQUIRE(std::find(pAtomPos.begin(), pAtomPos.end(), propP.pos1) != pAtomPos.end()); |
|
198 |
+ REQUIRE(propP.delta1 < 0); |
|
199 |
+ } |
|
200 |
+ } |
|
201 |
+ |
|
202 |
+ SECTION("proposeMove") |
|
203 |
+ { |
|
204 |
+ for (unsigned i = 0; i < 1000; ++i) |
|
205 |
+ { |
|
206 |
+ AtomicProposal propA = Adomain.proposeMove(); |
|
207 |
+ REQUIRE(propA.nChanges == 2); |
|
208 |
+ REQUIRE(std::find(aAtomPos.begin(), aAtomPos.end(), propA.pos1) != aAtomPos.end()); |
|
209 |
+ REQUIRE(propA.delta1 < 0); |
|
210 |
+ REQUIRE(propA.delta1 == -propA.delta2); |
|
211 |
+ |
|
212 |
+ AtomicProposal propP = Pdomain.proposeMove(); |
|
213 |
+ REQUIRE(propP.nChanges == 2); |
|
214 |
+ REQUIRE(std::find(pAtomPos.begin(), pAtomPos.end(), propP.pos1) != pAtomPos.end()); |
|
215 |
+ REQUIRE(propP.delta1 < 0); |
|
216 |
+ REQUIRE(propP.delta1 == -propP.delta2); |
|
217 |
+ } |
|
218 |
+ } |
|
219 |
+ |
|
220 |
+ SECTION("proposeExchange") |
|
221 |
+ { |
|
222 |
+ for (unsigned i = 0; i < 1000; ++i) |
|
223 |
+ { |
|
224 |
+ AtomicProposal propA = Adomain.proposeExchange(); |
|
225 |
+ REQUIRE(propA.nChanges == 2); |
|
226 |
+ REQUIRE(std::find(aAtomPos.begin(), aAtomPos.end(), propA.pos1) != aAtomPos.end()); |
|
227 |
+ REQUIRE(propA.delta1 + propA.delta2 == 0.0); |
|
228 |
+ |
|
229 |
+ AtomicProposal propP = Pdomain.proposeExchange(); |
|
230 |
+ REQUIRE(propP.nChanges == 2); |
|
231 |
+ REQUIRE(std::find(pAtomPos.begin(), pAtomPos.end(), propP.pos1) != pAtomPos.end()); |
|
232 |
+ REQUIRE(propP.delta1 + propP.delta2 == 0.0); |
|
233 |
+ } |
|
234 |
+ } |
|
235 |
+} |
|
236 |
+ |
|
237 |
+#endif |
|
238 |
+ |
|
239 |
+#endif |
|
0 | 240 |
\ No newline at end of file |
... | ... |
@@ -6,6 +6,8 @@ |
6 | 6 |
#include "../InternalState.h" |
7 | 7 |
#include "../Random.h" |
8 | 8 |
|
9 |
+#if 0 |
|
10 |
+ |
|
9 | 11 |
TEST_CASE("Test Archive.h") |
10 | 12 |
{ |
11 | 13 |
SECTION("Reading/Writing to an Archive") |
... | ... |
@@ -196,4 +198,6 @@ TEST_CASE("Test Archive.h") |
196 | 198 |
REQUIRE(gaps::random::exponential(5.5) == randSequence[i]); |
197 | 199 |
} |
198 | 200 |
} |
199 |
-} |
|
200 | 201 |
\ No newline at end of file |
202 |
+} |
|
203 |
+ |
|
204 |
+#endif |
|
201 | 205 |
\ No newline at end of file |