comparison toolboxes/FullBNT-1.0.7/KPMtools/bipartiteMatchingDemo.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e9a9cd732c1e
1 % Consider matching sources to detections
2
3 % s1 d2
4 % s2 d3
5 % d1
6
7 %a = bipartiteMatchingHungarian([52;0.01])
8
9 % sources(:,i) = [x y] coords
10 sources = [0.1 0.7; 0.6 0.4]';
11 detections = [0.2 0.2; 0.2 0.8; 0.7 0.1]';
12 dst = sqdist(sources, detections);
13
14 % a = [2 3] which means s1-d2, s2-d3
15 a = bipartiteMatchingHungarian(dst);
16 a2 = bipartiteMatchingIntProg(dst);
17 assert(isequal(a(:),a2(:)))
18
19
20 figure(1); clf
21 bipartiteMatchingDemoPlot(sources, detections, a)
22
23
24
25
26 %%%% Flip roles of sources and detections
27
28 %dst = dst';
29 dst = sqdist(detections, sources);
30 % a = [0 1 2] which means d1-0, d2-s1, d3-s2
31 a = bipartiteMatchingHungarian(dst);
32
33 a2 = bipartiteMatchingIntProg(dst);
34 assert(isequal(a(:),a2(:)))
35
36 figure(2); clf
37 bipartiteMatchingDemoPlot(detections, sources, a) % swapped args
38
39
40
41
42 %%%%%%%%%% Move s1 nearer to d1
43 % d2
44 % s2 d3
45 % s1 d1
46
47 sources = [0.1 0.3; 0.6 0.4]';
48 detections = [0.2 0.2; 0.2 0.8; 0.7 0.1]';
49 dst = sqdist(sources, detections);
50
51 % a = [2 3] which means s1-d2, s2-d3
52 a = bipartiteMatchingHungarian(dst);
53 [a2, ass] = bipartiteMatchingIntProg(dst);
54 assert(isequal(a(:),a2(:)))
55
56
57 figure(3); clf
58 bipartiteMatchingDemoPlot(sources, detections, a)
59
60
61
62 %%%%%%%%%%
63
64 % Use random points
65
66 % Generate 2D data from a mixture of 2 Gaussians (from netlab demgmm1)
67 randn('state', 0); rand('state', 0);
68 gmix = gmm(2, 2, 'spherical');
69 ndat1 = 10; ndat2 = 10; ndata = ndat1+ndat2;
70 %gmix.centres = [0.3 0.3; 0.7 0.7];
71 %gmix.covars = [0.01 0.01];
72 gmix.centres = [0.5 0.5; 0.5 0.5];
73 gmix.covars = [0.1 0.01];
74 [x, label] = gmmsamp(gmix, ndata);
75
76 ndx = find(label==1);
77 sources = x(ndx,:)';
78 ndx = find(label==2);
79 detections = x(ndx,:)';
80 dst = sqdist(sources, detections);
81
82 [a, ass] = bipartiteMatchingIntProg(dst);
83 [a2] = bipartiteMatchingHungarian(dst);
84 assert(isequal(a(:), a2(:)))
85
86 figure(4); clf
87 bipartiteMatchingDemoPlot(sources, detections, a)
88
89 % only match 80% of points
90 p1 = size(sources, 2);
91 p2 = size(detections, 2);
92 nmatch = ceil(0.8*min(p1,p2));
93 a2 = bipartiteMatchingIntProg(dst, nmatch);
94 figure(5); clf
95 bipartiteMatchingDemoPlot(sources, detections, a2)
96
97
98 %%% swap roles
99
100 ndx = find(label==2);
101 sources = x(ndx,:)';
102 ndx = find(label==1);
103 detections = x(ndx,:)';
104 dst = sqdist(sources, detections);
105
106 % only match 80% of points
107 p1 = size(sources, 2);
108 p2 = size(detections, 2);
109 nmatch = ceil(0.8*min(p1,p2));
110 a2 = bipartiteMatchingIntProg(dst, nmatch);
111 figure(6); clf
112 bipartiteMatchingDemoPlot(sources, detections, a2)