-
Notifications
You must be signed in to change notification settings - Fork 11
/
da.c
300 lines (278 loc) · 9.2 KB
/
da.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/**
Michael Motro github.com/motrom/fastmurty 4/2/19
*/
#include <string.h> // memcpy
#include <stddef.h> // size_t
#include "da.h" // many types and functions
/*
queue of problems
as well as storage for internal algorithms
the current structure of the data on heap is:
current problem struct (pointers to problem data)
current problem data
queue (array of QueueEntries)
array of [problem struct, problem data]
--- separate ---
workvarsforSSP
--- separate ---
workvarsforSplit
Changing this internal structure shouldn't change the function of the code
(changing the internal structure of Subproblem will as memcpy is used to copy parts)
*/
WorkvarsforDA allocateWorkvarsforDA(int m, int n, int nsols){
WorkvarsforDA workvars;
workvars.m = m;
workvars.n = n;
workvars.nsols = nsols;
size_t solutionsize = sizeof(int)*(m + n) + sizeof(double)*n;
size_t subproblemsize = solutionsize +
sizeof(int)*(m + n) + sizeof(bool)*n;
size_t fullsize = (subproblemsize + sizeof(Subproblem))*(nsols+1);
fullsize += sizeof(QueueEntry)*nsols;
char* buffer = (char*) malloc(fullsize);
assert(buffer != NULL);
workvars.buffer = buffer;
workvars.currentproblem = (Subproblem*) buffer;
buffer += sizeof(Subproblem);
assert(buffer < workvars.buffer + fullsize);
*workvars.currentproblem = allocateSubproblem(m, n, buffer);
buffer += subproblemsize;
workvars.Q = (QueueEntry *) buffer;
// set up each subproblem's pointers
QueueEntry* qele = workvars.Q;
assert(buffer < workvars.buffer + fullsize);
buffer += sizeof(QueueEntry)*nsols;
int sol;
Subproblem* prbele;
for(sol=0; sol<nsols; sol++){
assert(buffer < workvars.buffer + fullsize);
prbele = (Subproblem*) buffer;
qele->val = prbele;
qele++;
buffer += sizeof(Subproblem);
assert(buffer < workvars.buffer + fullsize);
*prbele = allocateSubproblem(m, n, buffer);
buffer += subproblemsize;
}
workvars.sspvars = allocateWorkvarsforSSP(m, n);
workvars.splitvars = allocateWorkvarsforSplit(m, n);
return workvars;
};
void deallocateWorkvarsforDA(WorkvarsforDA workvars){
deallocateWorkvarsforSSP(workvars.sspvars);
deallocateWorkvarsforSplit(workvars.splitvars);
free(workvars.buffer);
};
int da(inputmatrixtype c, int nrow_priors, bool* row_priors, double* row_prior_weights,
int ncol_priors, bool* col_priors, double* col_prior_weights,
int K, int* out_assocs, double* out_costs, WorkvarsforDA* workvars){
const double inf = 1000000000;
int m, n, m2, n2, m3, n3;
int i, j, k, solidx, rowidx;
WorkvarsforSSP* sspvars;
WorkvarsforSplit* splitvars;
QueueEntry* Q;
int Qsize;
QueueEntry bestsol, worstsol;
int rowprior_idx, colprior_idx;
double costbound;
Subproblem* prb;
Subproblem* bestprb;
double C;
size_t subproblemsize, solutionsize, eliminationsize;
#ifndef NDEBUG
const double eps_debug = 0.00000001;
bool passed = true;
int ri, ri2, cj, cj2;// id, jd, j2d, ;
#endif
m = workvars->m;
n = workvars->n;
if ((K == 0) || (nrow_priors == 0) || (ncol_priors == 0)) {
for (k = 0; k < K; k++) {
out_costs[k] = inf;
}
return 0;
}
for (j=0; j < K*(m+n)*2; j++){
out_assocs[j] = -2;
}
// prep variables
subproblemsize = sizeofSubproblemData(m, n);
solutionsize = sizeofSolutionData(m, n);
eliminationsize = sizeof(bool)*n;
// unpack workvars
sspvars = &(workvars->sspvars);
splitvars = &(workvars->splitvars);
prb = workvars->currentproblem;
for(j=0; j<n; j++){
prb->eliminateels[j] = false;
}
prb->eliminatemiss = false;
Q = workvars->Q;
Qsize = K;
// initial queue full of infinite costs
for(solidx=0; solidx<Qsize; solidx++){
Q[solidx].key = inf;
}
// find best solutions for each input hypothesis
if (K==1) worstsol = Q[0]; else worstsol = Q[1];
for(rowprior_idx=0; rowprior_idx < nrow_priors; rowprior_idx++){
// partition so included rows are first
m2 = 0;
m3 = m;
for(i=0; i<m; i++){
if(row_priors[rowprior_idx*m + i]){
prb->rows2use[m2] = i;
m2++;
prb->solution.x[i] = -1; //unnecessary?
} else {
m3--;
prb->rows2use[m3] = i;
prb->solution.x[i] = -2;
}
}
prb->m = m2;
for(colprior_idx=0; colprior_idx < ncol_priors; colprior_idx++){
n2 = 0;
n3 = n;
for(j=0; j<n; j++){
if(col_priors[colprior_idx*n + j]){
prb->cols2use[n2] = j;
n2++;
prb->solution.y[j] = -1;
} else {
n3--;
prb->cols2use[n3] = j;
prb->solution.y[j] = -2;
}
prb->solution.v[j] = 0;
}
prb->n = n2;
C = SSP(c, prb, sspvars);
C += row_prior_weights[rowprior_idx];
C += col_prior_weights[colprior_idx];
if(C < worstsol.key){
copySubproblem(worstsol.val, prb, subproblemsize);
worstsol.key = C;
worstsol = qReplaceMax(Q, worstsol, Qsize);
#ifndef NDEBUG
for (ri = 0; ri < m; ri++) {
for (ri2 = 0; ri2 < ri; ri2++) {
assert(prb->rows2use[ri] != prb->rows2use[ri2]);
}
}
for (cj = 0; cj < n; cj++) {
for (cj2 = 0; cj2 < cj; cj2++) {
assert(prb->cols2use[cj] != prb->cols2use[cj2]);
}
}
#endif
}
}
}
for(k=0; k<K; k++){
Qsize = K-k-1;
bestsol = qPopMin(Q, Qsize);
if(bestsol.key >= inf){
// less than K valid associations
for(solidx=k; solidx<K; solidx++){
out_costs[solidx] = inf;
}
return 1;
}
// copy solution to output
out_costs[k] = bestsol.key;
bestprb = bestsol.val;
rowidx = k*(m+n)*2;
for(j=0; j<n; j++){
i = bestprb->solution.y[j];
if (i==-1){ // unmatched measurements first
out_assocs[rowidx] = -1;
rowidx++;
out_assocs[rowidx] = j;
rowidx++;
}
}
for(i=0; i<m; i++){
j = bestprb->solution.x[i];
if (j!=-2){
out_assocs[rowidx] = i;
rowidx++;
out_assocs[rowidx] = j;
rowidx++;
}
}
if(Qsize == 0) break;
// prep for creating subproblems
copySubproblem(prb, bestprb, subproblemsize);
murtySplit(c, prb, splitvars);
prb->n = splitvars->n_start;
// set missing columns as uneliminated
for(j = 0; j < n; j++){
prb->eliminateels[j] = false;
}
j = 0;
costbound = worstsol.key - bestsol.key;
for(prb->m = splitvars->m_start+1; prb->m <= bestprb->m; prb->m++){
copySolution(prb, bestprb, solutionsize);
i = prb->rows2use[prb->m-1];
prb->eliminateels[j] = false; // last subproblem's elimination
prb->eliminatemiss = false;
if(prb->m == bestprb->m){
// this subproblem is originating problem w/ one more elimination
// inherits originating problem's eliminations
memcpy(prb->eliminateels, bestprb->eliminateels, eliminationsize);
prb->eliminatemiss = bestprb->eliminatemiss;
}
j = prb->solution.x[i];
if(j == -1){
prb->eliminatemiss = true;
} else {
#ifndef NDEBUG
if (j != prb->cols2use[prb->n]) {
printf("ordering fail n=%d j=%d, cols[n]=%d\n",
prb->n, j, prb->cols2use[prb->n]);
return 1;
}
#endif
prb->eliminateels[j] = true;
prb->n++;
}
// solve new subproblem
C = spStep(c, prb, sspvars, costbound);
if(C < costbound){
copySubproblem(worstsol.val, prb, subproblemsize);
worstsol.key = C + bestsol.key;
worstsol = qReplaceMax(Q, worstsol, Qsize);
costbound = worstsol.key - bestsol.key;
#ifndef NDEBUG
assert(prb->m <= bestprb->m);
assert(prb->n <= bestprb->n);
assert(C > -eps_debug); // new solution should not be lower than previous best solution
for (ri = 0; ri < m; ri++) {
for (ri2 = 0; ri2 < ri; ri2++) {
assert(prb->rows2use[ri] != prb->rows2use[ri2]);
}
}
for (cj = 0; cj < n; cj++) {
for (cj2 = 0; cj2 < cj; cj2++) {
assert(prb->cols2use[cj] != prb->cols2use[cj2]);
}
}
if (C < inf){
// shouldn't get solution when an entire row has been eliminated
passed = false;
if (!prb->eliminatemiss){ passed = true; }
for(cj = 0; cj < prb->n; cj++){
if (!prb->eliminateels[prb->cols2use[cj]]){
passed = true;
}
}
assert(passed);
}
#endif
}
}
}
return 0;
};