-
Notifications
You must be signed in to change notification settings - Fork 0
/
Graph.hpp
1721 lines (1448 loc) · 60.8 KB
/
Graph.hpp
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "Slave.hpp"
#include <cmath>
#define ABS(x) (x)<0?(-1*(x)):(x)
enum class OptimStrategy {
STEP,
STEP_SUBGRAD,
STEP_SQSUM,
STEP_K,
ADAPTIVE,
ADAPTIVE_SDECAY,
ADAPTIVE_LDECAY,
ADAPTIVE_KDECAY
};
class Graph
{
private:
/* Number of nodes and edges. */
int n_nodes;
int n_edges;
/* Number of labels for every node. */
int * n_labels;
/* Number of labels for every edge. */
int * e_labels;
/* Max number of labels in node energies and edge energies. */
int max_n_label;
int max_e_label;
/* Number of slaves. */
int n_slaves;
/* V x V -> E map. */
std::map< std::pair<int, int>, int > edge_id_from_node_ids;
/* E -> V x V map. */
std::map< int, std::pair<int, int> > node_ids_from_edge_id;
/* TODO: Design choice - full adjacency matrix or sparse neighbours-only matrix? */
bool ** adj_mat;
char decomposition[20];
std::vector< Slave * > slave_list;
/* Whether we already created slaves. */
bool slaves_created;
ValType ** node_energies;
ValType ** edge_energies;
/* Used to record the current edge count. This will be important for
updating our V x V -> E and E - > V x V maps. */
int current_edge_count;
/* Keep track of which nodes and edges are in which slaves. */
std::vector< std::vector<int> > nodes_in_slaves;
std::vector< std::vector<int> > edges_in_slaves;
/* Keep track of the number of slaves that contain each node or edge. */
std::vector< int > n_slaves_node;
std::vector< int > n_slaves_edge;
/* Variables to hold marked updates. */
ValType *** node_updates;
ValType *** edge_updates;
/* Whether to update a slave or not. */
bool ** _mark_slave_updates;
/* The multiplier for parameter udpates. */
ValType alpha;
/* The norm of the subgradient at an iteration of optimisation. */
ValType cur_subgradient;
/* Whether to check a particular node for updates. This is an array of size
n_nodes with an element set to true if that particular node has conflicting
labelling from the slave problems. In that case, that node should be
checked for updates.
Also, whether to check a particular edge for updates. Edge udpates are
marked if either of the end-points of the edges have updates marked
for them.
One array also to determine whether to check slaves. This tells us the
number of subproblems being solved at each iteration. */
bool * check_nodes;
bool * check_edges;
bool * check_slaves;
/* The number of nodes where we disagree on the labelling. */
int n_miss;
/* The number of slaves to be solved. */
int n_slaves_to_solve;
/* The iteration during an optimisation. */
int optim_it;
/* Print optimisation report every ... */
int _print_every;
/* K in OptimStrategy::STEP_K and OptimStrategy::ADAPTIVE_KDECAY */
float _decayk;
public:
/* The primal solution is stored in the memer labels. */
/* Making labels public so that one can easily read the labelling
after optimisation is complete. */
int * labels;
/* Debug mode. */
bool debug;
/* Verbose mode. */
bool _verbose;
/* Primal and dual costs. */
ValType primal_cost;
ValType dual_cost;
/* Best primal and dual costs so far. */
ValType best_primal_cost;
ValType best_dual_cost;
/* Primal and dual costs history. */
std::vector< ValType > primal_hist;
std::vector< ValType > dual_hist;
/* n_miss history: number of disagreeing nodes. */
std::vector< int > n_miss_hist;
/* History of subgradient. */
std::vector< ValType > subgrad_hist;
/* History of alpha. */
std::vector< ValType > alpha_hist;
/* Best primal solution */
std::vector< int > best_primal_solution;
/* --------- Member functions --------- */
/* Specify node energies for a node. */
int set_node_energies(int, ValType *);
/* Specify edge energies for a node. */
int set_edge_energies(int, int, ValType *);
/* Optimise the energy over the Graph using the current decomposition. */
void optimise(ValType, int, OptimStrategy);
/* Return the edge ID of an edge specified by its end points. */
int get_edge_id(int, int);
/* Return the node IDs of the ends of a specified edge. */
std::pair<int, int> get_node_id(int);
/* Add a cycle slave to the graph specified by nodes. The
nodes are assumed to be in order when supplied. */
void add_cycle_slave(std::vector<int>);
/* Add tree slave. The node list and edge list need to be specified.
Strictly speaking, the node list is not necessary to represent a
sub-tree of the Graph. But just to be safe, the node list
is asked as well. Can be easily modified to deduce the node
list from the edge list. */
void add_tree_slave(std::vector<int>, std::vector<int>);
/* Find a spanning tree decomposition. Simply looks for spanning
trees until all edges are accounted for. */
void decompose_spanning_trees(void);
/* Finalise Graph decomposition. This tells the Graph
that no more slaves are to be added.
Energies are thus divided between slaves and
other necessary variables are set. */
void finalise_decomposition(void);
/* Check whether a decomposition is valid. The subproblems must
sum to the PRIMAL. */
bool check_decomposition(void);
/* Ask the Graph to optimise slaves. This function iterates
over all slaves and solves the ones that are marked to be solved. */
void optimise_slaves(void);
/* Estimate the PRIMAL from the current state. */
void estimate_primal(void);
/* Compute the PRIMAL cost for the current Graph labelling. */
void compute_primal_cost(void);
/* Compute the current DUAL cost. */
void compute_dual(void);
/* Find conflicting nodes and edges, i.e., nodes and edges that do not
receive the same labelling for contributing slaves. */
void find_conflicts(void);
/* Reset the updates variable to zero. */
void _reset_updates(void);
/* Compute parameter updates. */
void compute_param_updates();
/* Apply parameter updates. */
void apply_param_updates(ValType, OptimStrategy);
/* Print Graph state during a phase of optimisation. Useful
for debugging. */
void _print_state(int);
/* Set verbosity */
void verbose(bool);
/* Specify how frequently to print optimisation status. */
void print_every(int);
/* Specify K for OptimStrategy::STEP_K and OptimStrategy::ADAPTIVE_KDECAY */
void set_k_decayk(float);
/* ------------------------------------ */
/* Constructor. */
Graph(int nn, int ne, int *nl)
{
n_nodes = nn;
n_edges = ne;
/* Set max_n_label and max_e_label to 0. */
max_n_label = 0;
max_e_label = 0;
/* Allocate space for n_labels. */
n_labels = new int [n_nodes];
for(int i = 0; i < n_nodes; i ++)
{
n_labels[i] = nl[i];
if(nl[i] > max_n_label)
max_n_label = nl[i];
}
/* Set slaves_created to false as they have not been created yet. */
slaves_created = false;
/* Allocate memory for e_labels. */
e_labels = new int [n_edges];
/* Used to record the current edge count. This will be important for
updating our V x V -> E and E - > V x V maps. */
current_edge_count = 0;
/* Allocate memory for node energies. */
node_energies = new ValType * [n_nodes];
for(int i = 0; i < n_nodes; i ++)
/* For every node, allocate space only for as many labels as required. */
node_energies[i] = new ValType[n_labels[i]];
/* Allocate energy for edge energies. */
edge_energies = new ValType *[n_edges];
/* Space for specific edges will be allocated later when the edge are specified. */
/* Allocate space for adjacency matrix. */
adj_mat = new bool *[n_nodes];
for(int i = 0; i < n_nodes; i ++)
{
adj_mat[i] = (bool *)calloc(n_nodes, sizeof(bool));
}
/* Allocate space for the labelling. */
labels = new int [n_nodes];
/* Set the number of slaves to zero - we have not specified any decompositions yet. */
n_slaves = 0;
/* Allocate space for nodes_in_slaves and edges_in_slaves. */
nodes_in_slaves.assign(n_nodes, std::vector<int>());
edges_in_slaves.assign(n_edges, std::vector<int>());
/* Allocate space for n_slaves_node and n_slaves_edge. */
n_slaves_node.assign(n_nodes, 0);
n_slaves_edge.assign(n_edges, 0);
/* Allocate space for check_nodes and check_edges. */
check_nodes = new bool [n_nodes];
check_edges = new bool [n_edges];
/* Initialise the best primal and dual costs to zero. */
best_primal_cost = 0;
best_dual_cost = 0;
/* Create empty best primal solution. */
best_primal_solution.assign(n_nodes, 0);
/* Set debug mode to false by default. */
debug = false;
/* Set verbose to true by default. */
_verbose = true;
/* By default, print every iteration. */
_print_every = 1;
} /* End of Graph(). */
/* Destructor. */
~Graph()
{
for(int s = 0; s < n_slaves; s ++)
{
for(int i = 0; i < slave_list[s]->n_nodes; i ++)
{
delete [] node_updates[s][i];
}
delete [] node_updates[s];
for(int e = 0; e < slave_list[s]->n_edges; e ++)
{
delete [] edge_updates[s][e];
}
delete [] edge_updates[s];
}
delete [] labels;
delete [] _mark_slave_updates[0];
delete [] _mark_slave_updates[1];
delete [] _mark_slave_updates;
delete [] node_updates;
delete [] edge_updates;
delete [] n_labels;
delete [] e_labels;
for(int i = 0; i < n_nodes; i ++)
free(adj_mat[i]);
delete [] adj_mat;
for(int i = 0; i < n_nodes; i ++)
delete [] node_energies[i];
delete [] node_energies;
for(int i = 0; i < n_edges; i ++)
delete [] edge_energies[i];
delete [] edge_energies;
for(int i = 0; i < n_slaves; i ++)
delete slave_list[i];
delete [] check_nodes;
delete [] check_edges;
delete [] check_slaves;
} /* End of ~Graph(). */
}; /* End of definition of class Graph. */
/* Public member functions' definitions for Graph to follow. */
/*
* Graph::add_cycle_slave -- Add a cycle slave. The order of the cycle
* is given by the order in which the nodes are specified. All edges
* should be existant.
*/
void Graph::add_cycle_slave(std::vector<int> sl_nodes)
{
/* Variables required to create slaves. */
ValType ** slave_node_e;
ValType ** slave_edge_e;
int * slave_node_l;
int * slave_edge_l;
int * slave_n_labels;
/* The number of nodes and edges in this slave. */
int slave_n_nodes;
int slave_n_edges;
/* The slave ID for this new slave. */
int s_id = n_slaves;
/* Increment the number of slaves. */
++ n_slaves;
/* Get the node list and number of nodes and edges. */
slave_n_nodes = sl_nodes.size();
slave_n_edges = sl_nodes.size();
slave_node_l = new int [slave_n_nodes];
slave_edge_l = new int [slave_n_nodes];
slave_n_labels = new int [slave_n_nodes];
/* Allocate memory for node and edge energies. */
slave_node_e = new ValType * [slave_n_nodes];
slave_edge_e = new ValType * [slave_n_edges];
for(int i = 0; i < slave_n_nodes; i ++) /* Begin populating node energies, etc. for all nodes. */
{
/* Get the from/to edge. */
int from = sl_nodes[i];
int to = sl_nodes[(i+1)%slave_n_nodes];
/* Push the node ID into the node list for this slave. */
slave_node_l[i] = from;
/* Push the edge ID into the edge list for this slave. */
int edge_id = edge_id_from_node_ids[std::make_pair(from, to)];
slave_edge_l[i] = edge_id;
/* Push the number of labels for this node into the list of
number of labels. */
slave_n_labels[i] = n_labels[from];
/* Allocate space for node energies and edge energies for this node and edge. */
slave_node_e[i] = new ValType [n_labels[from]];
slave_edge_e[i] = new ValType [n_labels[from]*n_labels[to]];
/* Push the node energies for this node into slave_node_e. */
for(int j = 0; j < n_labels[from]; j ++)
{
slave_node_e[i][j] = node_energies[from][j];
}
/* Push the edge energies for this edge into slave_edge_e. */
/* But first, determine which is the smaller node ID, because edges are
always assumed to go from the smaller node ID to the larger node ID.
However, in cycle slaves, node IDs for an edge always obey the order
in which the node IDs appear in the node list. The node list, in turn,
always respects the way in which the cycle is built. */
int _from, _to;
_from = MIN(from, to);
_to = from + to - _from;
int graph_lid; /* Index to iterate over these edge energies in the Graph. */
int slave_lid; /* Index to iterate over these edge energies in the Slave. */
int _from_nl = n_labels[_from];
int _to_nl = n_labels[_to];
for(int j = 0; j < _from_nl; j ++)
{
for(int k = 0; k < _to_nl; k ++)
{
graph_lid = j*_to_nl + k;
if(!(_from < _to))
{
/* We store the transposed edge energies in the slave, so for this
case, slave_lid and graph_lid are the same. */
slave_lid = graph_lid;
}
else
{
slave_lid = k*_from_nl + j;
}
/* Finally, copy the values. */
slave_edge_e[i][slave_lid] = edge_energies[edge_id][graph_lid];
} /* Finished iterating over n_labels[_to]. */
} /* Finished iterating over n_labels[_from]. */
/* For this node and edge, update nodes_in_slaves and edges_in_slaves. */
nodes_in_slaves[from].push_back(s_id);
edges_in_slaves[edge_id].push_back(s_id);
/* Update slave counts for this node and edge. */
++ n_slaves_node[from];
++ n_slaves_edge[edge_id];
} /* Finished iterating over the nodes in this slave. */
/* Create a pointer to a slave. */
Slave * this_slave = new Slave(s_id, slave_n_nodes, slave_n_edges, slave_node_l, slave_edge_l,
slave_n_labels, slave_node_e, slave_edge_e, 'c', 0, 0, 0);
/* Add this slave to slave_list. */
slave_list.push_back(this_slave);
return;
} /* End of Graph::add_cycle_slave */
void Graph::add_tree_slave(std::vector<int> nlist, std::vector<int> elist)
{
/* Add a tree-structured slave. */
/* The slave ID for this new slave. */
int s_id = n_slaves;
/* Increment the number of slaves. */
++ n_slaves;
/* Get data on the subproblem's structure. */
int slave_n_nodes = nlist.size();
int slave_n_edges = elist.size();
/* Allocate some memory to store node and edge energies. */
ValType ** slave_node_e = new ValType *[slave_n_nodes];
ValType ** slave_edge_e = new ValType *[slave_n_edges];
/* Arrays to store node and edge lists. */
int * slave_node_l = new int [slave_n_nodes];
int * slave_edge_l = new int [slave_n_edges];
int * slave_n_labels = new int [slave_n_nodes];
/* Create two maps to store E -> V x V and V -> E x E maps
for this slave. */
std::map< int, std::pair<int, int> > * n_from_e = new std::map< int, std::pair<int, int> >;
std::map< std::pair<int, int>, int > * e_from_n = new std::map< std::pair<int, int>, int >;
/* Node map to be used for this creation. */
std::map<int, int> node_map;
/* Adjacency matrix for this slave. */
bool ** slave_adj = new bool * [slave_n_nodes];
for(int i = 0; i < slave_n_nodes; i ++)
{
slave_adj[i] = (bool *)calloc(slave_n_nodes, sizeof(bool));
}
/* Start populating node energies. */
for(int i = 0; i < slave_n_nodes; i ++)
{
/* The node ID. */
int n_id = nlist[i];
slave_node_l[i] = n_id;
/* The number of labels for this node. */
int nl_node = n_labels[n_id];
slave_n_labels[i] = nl_node;
/* This node's node energy. */
slave_node_e[i] = new ValType [nl_node];
/* Copy node energies. */
for(int j = 0; j < nl_node; j ++)
{
slave_node_e[i][j] = node_energies[n_id][j];
}
/* Push this node into the node map. */
node_map[n_id] = i;
/* Update nodes_in_slaves for this node. */
nodes_in_slaves[n_id].push_back(s_id);
/* Update n_slaves count for this node. */
++ n_slaves_node[n_id];
}
/* Start populating edge energies and maps. */
for(int e = 0; e < slave_n_edges; e ++)
{
/* The edge ID. */
int e_id = elist[e];
slave_edge_l[e] = e_id;
/* The end points for this edge. */
int _from, _to;
_from = node_ids_from_edge_id[e_id].first;
_to = node_ids_from_edge_id[e_id].second;
/* End points in this slave. */
int _efrom, _eto;
_efrom = node_map[_from];
_eto = node_map[_to];
/* Whether to transpose the edge energies. They should be transposed
if _efrom > _eto. */
bool _transposed = (_efrom > _eto);
/* Correct the indices :: the smaller one should be _efrom. */
int _total = _efrom + _eto;
_efrom = MIN(_efrom, _eto);
_eto = _total - _efrom;
/* Add this edge to the adjacency matrix. */
slave_adj[_efrom][_eto] = true;
slave_adj[_eto][_efrom] = true;
/* Update maps to be used by the slave. */
(*n_from_e)[e] = std::make_pair(_efrom, _eto);
(*e_from_n)[std::make_pair(_efrom, _eto)] = e;
(*e_from_n)[std::make_pair(_eto, _efrom)] = e;
/* Allocate space for edge energies of this edge. */
slave_edge_e[e] = new ValType [e_labels[e_id]];
int l_id;
for(int j = 0; j < n_labels[_from]; j ++)
{
for(int k = 0; k < n_labels[_to]; k ++)
{
l_id = (_transposed) ? (k*n_labels[_from]+j) : (j*n_labels[_to] + k);
slave_edge_e[e][l_id] = edge_energies[e_id][j*n_labels[_to] + k];
}
}
/* Update edges_in_slaves for this edge. */
edges_in_slaves[e_id].push_back(s_id);
/* Update n_slaves count for this edge. */
++ n_slaves_edge[e_id];
}
/* Create this slave. */
Slave *this_slave = new Slave(s_id, slave_n_nodes, slave_n_edges, slave_node_l, slave_edge_l, slave_n_labels,
slave_node_e, slave_edge_e, 't', slave_adj, n_from_e, e_from_n);
/* Add this slave to slave_list. */
slave_list.push_back(this_slave);
return;
} /* End of Graph::add_tree_slave */
/*
* Graph::decompose_spanning_trees -- Decompose the graph into spanning
* trees.
*/
void Graph::decompose_spanning_trees(void)
{
/* Random seed. */
srand(time(NULL));
/* Array to say if all edges have been marked. */
bool *marked_edges = new bool [n_edges];
/* Set the array to zero. */
memset(marked_edges, 0, sizeof(bool)*n_edges);
bool _cont = true;
/* Node degrees. */
int *node_degrees = new int [n_nodes];
int _this_deg;
/* Node list. It is just the list of all nodes, because
all the trees are spanning trees. We can reuse this. */
std::vector< int > node_list;
/* Create copy of adj_mat. */
bool ** _adj = new bool *[n_nodes];
for(int i = 0; i < n_nodes; i ++)
{
_this_deg = 0;
_adj[i] = new bool [n_nodes];
for(int j = 0; j < n_nodes; j ++)
{
_adj[i][j] = adj_mat[i][j];
if(adj_mat[i][j])
++ _this_deg;
}
node_degrees[i] = _this_deg;
/* Push i into node_list. */
node_list.push_back(i);
}
while(_cont)
{
/* Find nodes with the maximum degree. */
int _max_degree = node_degrees[0];
int _n_max_degree = 1;
for(int i = 1; i < n_nodes; i ++)
{
if(node_degrees[i] == _max_degree)
{
++ _n_max_degree;
}
else if(node_degrees[i] > _max_degree)
{
_max_degree = node_degrees[i];
_n_max_degree = 1;
}
}
/* Choose a random node among these. */
int rand_idx = rand() % _n_max_degree;
int chosen_node = 0, _rand_it = 0;
for(int i = 0; i < n_nodes; i ++)
{
if(node_degrees[i] == _max_degree)
{
if(_rand_it == rand_idx)
{
chosen_node = i;
break;
}
++ _rand_it;
}
}
std::vector< std::pair<int,int> > edge_ends = _generate_tree_with_root(adj_mat, n_nodes, chosen_node, -1);
std::vector< int > edge_list;
//printf("Choosing node %d with degree %d, and %lu edges.\n", chosen_node, node_degrees[chosen_node], edge_ends.size());
for(unsigned int e = 0; e < edge_ends.size(); e ++)
{
/* Remove this edge from the adjacency matrix. */
int _from = edge_ends[e].first;
int _to = edge_ends[e].second;
int this_edge = edge_id_from_node_ids[edge_ends[e]];
/* Mark this edge as recorded. */
marked_edges[this_edge] = true;
/* Push this edge into edge_list. */
edge_list.push_back(this_edge);
/* Remove these edges. */
_adj[_from][_to] = false;
_adj[_to][_from] = false;
/* Reduce node degrees for _from and _to. */
node_degrees[_from] -= 1;
node_degrees[_to] -= 1;
}
/* Add this tree slave. */
add_tree_slave(node_list, edge_list);
/* Compute stopping condition. */
_cont = false;
for(int e = 0; e < n_edges; e ++)
{
if(!marked_edges[e])
{
_cont = true;
break;
}
}
/* Recompute node degrees. */
for(int i = 0; i < n_nodes; i ++)
{
_this_deg = 0;
for(int j = 0; j < n_nodes; j ++)
{
if(_adj[i][j])
++ _this_deg;
}
node_degrees[i] = _this_deg;
}
}
delete [] marked_edges;
delete [] node_degrees;
for(int i = 0; i < n_nodes; i ++)
delete [] _adj[i];
delete [] _adj;
} /* End of Graph::decompose_spanning_trees */
void Graph::finalise_decomposition(void)
{
/* Set slaves_created to true. */
slaves_created = true;
/* Now, we must divide the node and edge energies for every slave,
so that the total gives us the corresponding node and edge energies in
the Graph. */
std::vector<int> slaves_this_factor;
int n_slaves_this_factor;
int f_id_this_slave;
/* Do it for nodes first. */
for(int n_id = 0; n_id < n_nodes; n_id ++)
{
/* Slave list for this node ::: IDs of all slaves that contain this node. */
slaves_this_factor = nodes_in_slaves[n_id];
/* Number of slaves that contain this node. */
n_slaves_this_factor = n_slaves_node[n_id];
/* Iterate over all slaves to divide the energies appropriately. */
for(int j = 0; j < n_slaves_this_factor; j ++)
{
/* Get the id for this node ID in this slave. */
f_id_this_slave = slave_list[slaves_this_factor[j]]->node_map(n_id);
/* Divide the energy for every label equally. */
for(int k = 0; k < n_labels[n_id]; k ++)
{
slave_list[slaves_this_factor[j]]->node_energies[f_id_this_slave][k] /= n_slaves_this_factor;
}
} /* Finished iterating over every slave containing n_id. */
} /* Finished dividing energies for nodes. */
/* Do it for edges next. */
for(int e_id = 0; e_id < n_edges; e_id ++)
{
/* Slave list for this edge ::: IDs of all slaves that contain this edge. */
slaves_this_factor = edges_in_slaves[e_id];
/* Number of slaves that contain this edge. */
n_slaves_this_factor = n_slaves_edge[e_id];
/* Iterate over all slaves to divide the energies accordingly. */
for(int j = 0; j < n_slaves_this_factor; j ++)
{
/* Get the ID for this edge ID in the Slave. */
f_id_this_slave = slave_list[slaves_this_factor[j]]->edge_map(e_id);
/* Divide the energy for every label equally. */
for(int k = 0; k < e_labels[e_id]; k ++)
{
slave_list[slaves_this_factor[j]]->edge_energies[f_id_this_slave][k] /= n_slaves_this_factor;
}
} /* Finished iterating over every slave containing e_id. */
} /* Finished dividing energies for edges. */
/* Create memory for check flags on slaves. */
check_slaves = new bool [n_slaves];
/* Set the number of slaves to be solved to n_slaves initially (all of them are to be solved). */
n_slaves_to_solve = n_slaves;
/* Also initialise all elements in check_slaves to true. */
for(int s = 0; s < n_slaves; s ++)
{
check_slaves[s] = true;
}
/* Allocate memory for updates. */
node_updates = new ValType ** [n_slaves];
edge_updates = new ValType ** [n_slaves];
for(int s = 0; s < n_slaves; s ++)
{
int n_nodes_in_slave = slave_list[s]->n_nodes;
int n_edges_in_slave = slave_list[s]->n_edges;
node_updates[s] = new ValType * [n_nodes_in_slave];
edge_updates[s] = new ValType * [n_edges_in_slave];
for(int i = 0; i < n_nodes_in_slave; i ++)
{
node_updates[s][i] = new ValType [slave_list[s]->n_labels[i]];
}
for(int e = 0; e < n_edges_in_slave; e ++)
{
edge_updates[s][e] = new ValType [slave_list[s]->e_labels[e]];
}
}
/* Allocate memory for flags: whether to update a slave's energies or not. */
_mark_slave_updates = new bool *[2];
_mark_slave_updates[0] = new bool [n_slaves];
_mark_slave_updates[1] = new bool [n_slaves];
} /* End of Graph::finalise_decomposition() */
bool Graph::check_decomposition(void)
{
/* Check whether a decomposition is correct.
All slave energies should add up to the
Graph energies. */
std::vector<int> slaves_this_factor;
int n_slaves_this_factor;
/* Check nodes first. Node energies should be correctly divided among all slaves. */
for(int n_id = 0; n_id < n_nodes; n_id ++)
{
slaves_this_factor = nodes_in_slaves[n_id];
n_slaves_this_factor = slaves_this_factor.size();
/* Store total node energy here. */
ValType * total_e = (ValType *)calloc(n_labels[n_id], sizeof(ValType));
for(int s = 0; s < n_slaves_this_factor; s ++)
{
/* node_id for this node in this slave. */
int _n_id_in_slave = slave_list[slaves_this_factor[s]]->node_map(n_id);
for(int k = 0; k < n_labels[n_id]; k ++)
{
/* Accumulate in total_e[k]. */
total_e[k] += slave_list[slaves_this_factor[s]]->node_energies[_n_id_in_slave][k];
}
}
/* Check if the total is within EPS of the node energies specified by the Graph. */
for(int k = 0; k < n_labels[n_id]; k ++)
{
if(total_e[k] - node_energies[n_id][k] > EPS*1e-2 ||
node_energies[n_id][k] - total_e[k] > EPS*1e-2)
{
/* total_e[k] and node_energies[n_id][k] are more than EPS apart. */
printf("Energies do not match for node %d, label %d: Graph: %.10lf, Slaves: %.10lf\n", n_id, k, node_energies[n_id][k], total_e[k]);
return false;
}
}
/* Delete total_e, we do not need it anymore. */
free(total_e);
} /* Finished checking nodes. */
/* Check edges now. */
for(int e_id = 0; e_id < n_edges; e_id ++)
{
int _from, _to;
/* Retrieve ends of this edge. */
_from = node_ids_from_edge_id[e_id].first;
_to = node_ids_from_edge_id[e_id].second;
/* Slaves containing this edge. */
slaves_this_factor = edges_in_slaves[e_id];
n_slaves_this_factor = slaves_this_factor.size();
/* Store the total energy. */
ValType * total_e = (ValType *)calloc(e_labels[e_id], sizeof(ValType));
for(int s = 0; s < n_slaves_this_factor; s ++)
{
/* edge id for this edge in this slave. */
int _e_id_in_slave = slave_list[slaves_this_factor[s]]->edge_map(e_id);
/* Node IDs for _from and _to in this slave. */
int _sfrom, _sto;
_sfrom = slave_list[slaves_this_factor[s]]->node_map(_from);
_sto = slave_list[slaves_this_factor[s]]->node_map(_to);
/* Whether to transpose the energies. */
bool _transposed = (slave_list[slaves_this_factor[s]]->type() == 'c' && _from < _to) ||
(slave_list[slaves_this_factor[s]]->type() == 't' && _sfrom > _sto);
/* Iterate over all labels to compute the total. */
for(int j = 0; j < n_labels[_from]; j ++)
{
for(int k = 0; k < n_labels[_to]; k ++)
{
if(_transposed)
total_e[j*n_labels[_to] + k] += slave_list[slaves_this_factor[s]]->edge_energies[_e_id_in_slave][k*n_labels[_from] + j];
else
total_e[j*n_labels[_to] + k] += slave_list[slaves_this_factor[s]]->edge_energies[_e_id_in_slave][j*n_labels[_to] + k];
}
}
} /* Finished summing over all slaves. */
/* Check if the total is within EPS of the node energies specified by the Graph. */
for(int k = 0; k < e_labels[e_id]; k ++)
{
if(total_e[k] - edge_energies[e_id][k] > EPS*1e-2 ||
edge_energies[e_id][k] - total_e[k] > EPS*1e-2)
{
/* total_e[k] and node_energies[n_id][k] are more than EPS apart. */
printf("Energies do not match for edge %d, label %d: Graph: %.10lf, Slaves: %.10lf\n", e_id, k, edge_energies[e_id][k], total_e[k]);
return false;
}
} /* Finished checking for consistency. */
/* Delete total_e, we do not need it anymore. */
free(total_e);
}
/* No problems found. The decomposition is correct. */
return true;
} /* End of Graph::check_decomposition */
void Graph::optimise_slaves(void)
{
/* Optimise all slaves in this Graph. */
/* #pragma omp parallel for */
for(int i = 0; i < n_slaves; i ++)
{
/* Check whether this slave is to be solved. */
if(check_slaves[i])
{
slave_list[i]->optimise();
slave_list[i]->compute_energy();
}
}
} /* End of Graph::optimise_slaves */
void Graph::estimate_primal(void)
{
/* Very crude strategy for now: just assign the label
assigned by the first slave in nodes_in_slaves.
TODO: Use a better strategy. Min-marginals
obtained from slaves? */
for(int n_id = 0; n_id < n_nodes; n_id ++)
{
std::vector<int> slist = nodes_in_slaves[n_id];
/* Calculate how many votes each label gets. */
int *votes = (int *)calloc(n_labels[n_id], sizeof(int));
for(int i = 0; i < n_slaves_node[n_id]; i ++)
++ votes[slave_list[slist[i]]->get_node_label(n_id)];
/* Choose the most-voted label. */
int _argmax = 0;
for(int j = 1; j < n_labels[n_id]; j ++)
{
if(votes[j] > votes[_argmax])
{
_argmax = j;
}
}
/* Choose _argmax as the label for this node. */
labels[n_id] = _argmax;
/* Free the memory allocated to votes. */
free(votes);
}
/* Compute the primal cost now. */
compute_primal_cost();
return ;
} /* End of Graph::estiamte_primal */
void Graph::compute_primal_cost(void)
{
/* Compute primal cost now. */
primal_cost = 0;
int _from, _to;
int _lf, _lt;
std::pair< int, int > _node_ids;
/* Add energies due to nodes. */
for(int n_id = 0; n_id < n_nodes; n_id ++)
{
/* Get labelling and contribution from this node. */
_lf = labels[n_id];
primal_cost += node_energies[n_id][_lf];
}
/* Add energies due to edges. */
for(int e_id = 0; e_id < n_edges; e_id ++)
{
/* Get node IDs for this edge. */
_node_ids = node_ids_from_edge_id[e_id];
_from = _node_ids.first;
_to = _node_ids.second;
/* Get labelling for this edge. */
_lf = labels[_from];
_lt = labels[_to];
primal_cost += edge_energies[e_id][_lf*n_labels[_to] + _lt];
}
/* Update the best primal cost. */
if(optim_it == 0 || best_primal_cost > primal_cost)
{
best_primal_cost = primal_cost;
/* Record the best primal solution. */
for(int i = 0; i < n_nodes; i ++)
best_primal_solution[i] = labels[i];
}