-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathkantorovich_with_cost.m
69 lines (53 loc) · 1.69 KB
/
kantorovich_with_cost.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
function [fval, x, lambda] = kantorovich_with_cost(D, wX, wY, x0)
global A;
global default_options optim_options lpoptim_options;
n = length(wX);
m = length(wY);
wX = wX/sum(wX);
wY = wY/sum(wY);
if (length(wX) ~= n || length(wY) ~= m )
error('format not correct');
end
% might fail when input is NaN
if any(isnan(wX)) || any(isnan(wY))
fprintf('%f ',wX);fprintf('\n');
fprintf('%f ',wY);fprintf('\n');
end
% D = pdist2(X', Y', 'sqeuclidean');
f = reshape(D, n*m, 1);
% Aeq = A{n,m}(1:end-1,:);beq = [wX'; wY(1:end-1)'];
Aeq = A{n,m};beq = [wX'; wY'];
if nargin == 3
x0 = [];
else
x0 = reshape(x0,[n*m,1]);
if any(isnan(x0))
fprintf('%f ',x0);fprintf('\n');
x0=[];
end
end
if any(isnan(f)) || any(isnan(Aeq(:))) || any(isnan(beq))
disp X;
disp Y;
disp D;
%fprintf('%f ',f);fprintf('\n');
%fprintf('%f ',Aeq);fprintf('\n');
%fprintf('%f ',beq);fprintf('\n');
end
[x, fval, exitflag, ~, lambda] = linprog(f, [], [], Aeq, beq, zeros(n*m,1), [], x0, default_options );
if exitflag < 0
[x, fval, exitflag, ~, lambda] = linprog(f, [], [], Aeq, beq, zeros(n*m,1), [], x0, optim_options );
end
if exitflag < 0
[x, fval, exitflag, ~, lambda] = linprog(f, [], [], Aeq, beq, zeros(n*m,1), [], x0, lpoptim_options );
end
if exitflag < 0
[x, fval, exitflag, ~, lambda] = linprog(f, [], [], Aeq, beq, zeros(n*m,1), []);
end
if exitflag < 0
save(['err-lingprog-' datestr(clock, 0) '.mat'], 'f', 'Aeq', 'beq', 'n', 'm', 'x0');
error('linprog no search direction [%d, %f]', exitflag, fval);
end
x = reshape(x, n, m);
x(x<0) = 0;
end