Mercurial > hg > camir-aes2014
comparison toolboxes/FullBNT-1.0.7/KPMtools/bipartiteMatchingIntProg.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 function [a,ass] = bipartiteMatchingIntProg(dst, nmatches) | |
2 % BIPARTITEMATCHINGINTPROG Use binary integer programming (linear objective) to solve for optimal linear assignment | |
3 % function a = bipartiteMatchingIntProg(dst) | |
4 % a(i) = best matching column for row i | |
5 % | |
6 % This gives the same result as bipartiteMatchingHungarian. | |
7 % | |
8 % function a = bibpartiteMatchingIntProg(dst, nmatches) | |
9 % only matches the specified number (must be <= min(size(dst))). | |
10 % This can be used to allow outliers in both source and target. | |
11 % | |
12 % For details, see Marciel & Costeira, "A global solution to sparse correspondence | |
13 % problems", PAMI 25(2), 2003 | |
14 | |
15 if nargin < 2, nmatches = []; end | |
16 | |
17 [p1 p2] = size(dst); | |
18 p1orig = p1; p2orig = p2; | |
19 dstorig = dst; | |
20 | |
21 if isempty(nmatches) % no outliers allowed (modulo size difference) | |
22 % ensure matrix is square | |
23 m = max(dst(:)); | |
24 if p1<p2 | |
25 dst = [dst; m*ones(p2-p1, p2)]; | |
26 elseif p1>p2 | |
27 dst = [dst m*ones(p1, p1-p2)]; | |
28 end | |
29 end | |
30 [p1 p2] = size(dst); | |
31 | |
32 | |
33 c = dst(:); % vectorize cost matrix | |
34 | |
35 % row-sum: ensure each column sums to 1 | |
36 A2 = kron(eye(p2), ones(1,p1)); | |
37 b2 = ones(p2,1); | |
38 | |
39 % col-sum: ensure each row sums to 1 | |
40 A3 = kron(ones(1,p2), eye(p1)); | |
41 b3 = ones(p1,1); | |
42 | |
43 if isempty(nmatches) | |
44 % enforce doubly stochastic | |
45 A = [A2; A3]; | |
46 b = [b2; b3]; | |
47 Aineq = zeros(1, p1*p2); | |
48 bineq = 0; | |
49 else | |
50 nmatches = min([nmatches, p1, p2]); | |
51 Aineq = [A2; A3]; | |
52 bineq = [b2; b3]; % row and col sums <= 1 | |
53 A = ones(1,p1*p2); | |
54 b = nmatches; % total num matches = b (otherwise get degenerate soln) | |
55 end | |
56 | |
57 | |
58 ass = bintprog(c, Aineq, bineq, A, b); | |
59 ass = reshape(ass, p1, p2); | |
60 | |
61 a = zeros(1, p1orig); | |
62 for i=1:p1orig | |
63 ndx = find(ass(i,:)==1); | |
64 if ~isempty(ndx) & (ndx <= p2orig) | |
65 a(i) = ndx; | |
66 end | |
67 end | |
68 | |
69 |