-
Notifications
You must be signed in to change notification settings - Fork 7
/
BaBMatchingSolver.m
116 lines (106 loc) · 3.18 KB
/
BaBMatchingSolver.m
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
function [x] = BaBMatchingSolver(NofNodes, Factors, Potentials, U1, V1, BaBOptions)
NofStates = NofNodes * ones(1, NofNodes);
GUB = 1e20;
GLB = -1e20;
U = zeros(1, NofNodes);
V = zeros(1, NofNodes);
RootNode.Constraints = [];
RootNode.UB = inf;
RootNode.LB = -inf;
RootNode.Factors = Factors;
RootNode.Potentials = Potentials;
if(~isempty(U1) && ~isempty(V1))
RootNode.U = U1;
RootNode.V = V1;
else
RootNode.U = U;
RootNode.V = V;
end
%RootNode.Price = 0.1;
Q = [];
Q = [Q,RootNode];
BaBIter = 1;
while(~isempty(Q))
AllUB = [Q.UB];
[CUB, idx] = max(AllUB);
if(CUB <= GLB)
break;
end
CNode = Q(idx);
Q(idx) = [];
AllUB(idx) = [];
nPotentials1 = CNode.Potentials;
[nc, ~] = size(CNode.Constraints);
NofAssigns = zeros(1, NofNodes);
IsFeasible = true;
for i=1:nc
NodeIdx = CNode.Constraints(i, 1);
NodeValue = CNode.Constraints(i, 2);
if(NodeValue > 0)
if(NofAssigns(NodeIdx) ~= 0 && NofAssigns(NodeIdx)~= NodeValue)
IsFeasible = false;
break;
else
NofAssigns(NodeIdx) = NodeValue;
for xi = 1:NofStates
if(xi ~= NodeValue)
nPotentials1{NodeIdx}(xi) = -100;
end
end
end
else
NodeValue = - NodeValue;
nPotentials1{NodeIdx}(NodeValue) = -100;
end
end
if(IsFeasible == false)
continue;
end
[xhat, nFactors, nPotentials1, dual, U, V] = HungarianBPMex(NofNodes, NofStates, CNode.Factors, nPotentials1, CNode.U, CNode.V, 1, BaBOptions.bpoptions.innerIter,GLB);
UB = dual;
AllUB = [AllUB(:); dual];
GUB = min(GUB, max(AllUB));
LB = CluComputeObjMex(xhat, NofStates, Factors, Potentials);
if(LB >= GLB)
GLB = LB;
x = xhat;
end
if(UB > GLB)
NodePotentials = cell2mat(nPotentials1(1:NofNodes));
% figure(1);imshow(imresize(reshape(exp(NodePotentials),[NofNodes, NofNodes]),4, 'nearest'), [] );
% drawnow;
Prices = zeros(1, NofNodes);
Values = zeros(1, NofNodes);
for i=1:NofNodes
[maxv, Values(i)] = max(NodePotentials(i,:));
offset = abs(NodePotentials(i,:) - maxv);
Prices(i) = sum(offset < 1e-4);
end
[~, NodeToSplit] = max(Prices);
NodeAssign = Values(NodeToSplit);
LNode.UB = dual;
LNode.LB = -1000;
LNode.Factors = nFactors;
LNode.Potentials = nPotentials1;
LNode.U = U;
LNode.V = V;
LNode.Constraints = [CNode.Constraints; NodeToSplit, NodeAssign];
RNode.UB = dual;
RNode.LB = -1000;
RNode.Constraints = [CNode.Constraints; NodeToSplit, -NodeAssign];
RNode.Factors = nFactors;
RNode.Potentials = nPotentials1;
RNode.U = U;
RNode.V = V;
Q = [Q; LNode; RNode];
end
% fprintf('Iter = %d, GUB = %12.7f, GLB= %12.7f, GAP = %12.2f%%\n', BaBIter, GUB, GLB, (GUB - GLB) / GLB * 100);
if(abs(GUB - GLB)/GLB*100 < 0.5)
break;
end
if(BaBIter >= BaBOptions.MaxIter)
break;
end
BaBIter = BaBIter + 1;
end
end