function xyOut = solver(a, xyIn, wts)
% Place nodes in radial pattern
xy{1} = solver_alfonso (a, xyIn, wts);
xy{2} = solver_hannes (a, xyIn);
xy{3} = solver_jonathan (a);
N(1) = gradeIt(a,xyIn,xy{1},wts);
N(2) = gradeIt(a,xyIn,xy{2},wts);
N(3) = gradeIt(a,xyIn,xy{3},wts);
[~,best] = min(N);
xyOut = xy{best};
end
function xyOut = solver_alfonso(a, xyIn, wts)
N=length(wts);
deg=diag(sum(a));
[V,~]=eig(deg-a,deg);
xyOut=V(:,2:3);
for ncut=1:2
Mbeta=(0.9*eye(N)+0.1*bsxfun(@rdivide,a+eye(N),sum(a,2)+1))^10;
for n1=1:119
xyOut=Mbeta*xyOut;
[c1,D]=svd(cov(xyOut,1));
xyOut=(bsxfun(@minus,xyOut,mean(xyOut)))*c1*diag(1./sqrt(.1+diag(D)))*c1';
end
[i,j]=find(a>0.009);
dd=sum((xyOut(i,:)-xyOut(j,:)).^2,2);
k=find(dd>3.5*mean(dd));
if isempty(k), break; end
a(i(k)+N*(j(k)-1))=0.009;
end
xyOut0=sqrt(N)*detrend(xyOut,'constant')*diag(1./std(xyOut,1,1));
k=5;
[sxyOut0,idxequal]=sortrows(round(15*xyOut0)/15);
idxequal=idxequal(all(~diff(sxyOut0,1,1),2));
xyOut=round(xyOut0);
while size(unique(xyOut,'rows'),1)~=N
k=k*1.1;
xyOut=xyOut0*k;
xyOut(idxequal,:)=(xyOut0(idxequal,:)+randn(numel(idxequal),2)/10)*k;
xyOut=round(xyOut);
end
if N<35 % small map knot lines
xyOut=3*xyOut+randi([-1,1],N,2);
end
xyOut = bsxfun(@minus,xyOut,round(wts*(xyOut-xyIn)./sum(wts))); % AV's
end
function xyOut = solver_hannes(a, xyIn)
% Entry 67561: 2503 (cyc: 2, node: 166) CPU Time: 1.536 Score: 2505.98
sz = size(a,1);
deg = diag(sum(a));
L = deg-a;
[V,~] = eig(L,deg);
xyOut = 0;
outdiam = norm(max(V(:,2:3)) - min(V(:,2:3)));
indiam = norm(max(xyIn) - min(xyIn));
mult = indiam/outdiam;
while size(unique(xyOut,'rows'),1) < sz
xyOut = round(V(:,2:3)*mult);
[~,i] = unique(xyOut,'rows');
m = setdiff(1:sz,i);
xyOut(m,:) = xyOut(m,:) + round(rand(length(m),2))*2 - 1;
mult = mult*1.5;
end
end
function X = solver_jonathan(a)
% Entry 67763: 2523 (cyc: 4, node: 180) CPU Time: 1.371 Score: 2526.46
% JLS solver; Based on spectral graph theory.
L = diag(sum(a)) - a; % graph laplacian
% get eigenvectors of interest
[U, ~, ~] = svd(L);
U = U(:, end-2:end-1);
% scale to integers, with truncation
X = U;
n = size(X, 1);
k = max(fix(log10(abs(U(:))))) + ceil(log10(n));
X = round(U .* 10^k);
% resolve repeated rows arbitrarily
[~, m, ~] = unique(X, 'rows');
m = numel(m);
while m ~= n
for row1 = 1:size(X, 1)
idx = find(X(:, 1) == X(row1, 1) & X(:, 2) == X(row1, 2));
for row2 = 2:numel(idx)
X(idx(row2), 2) = X(idx(row2), 2) + row2;
end
end
[~, m, ~] = unique(X, 'rows');
m = numel(m);
end
end
function S = gradeIt(a,XYold,XYnew,wts)
N = numKnots(XYnew,a);
d = sqrt(sum((XYnew-XYold).^2,2));
A = bsxfun(@minus,XYold',mean(XYold',2));
S = full(dot(A,A,1));
D = bsxfun(@plus,S,S')-full(2*(A'*A));
D = sqrt(max(max(D)));
S = N + sum(d.*wts')/D/sum(wts);
end
function N = numKnots(XY,A)
A = triu(A,1);
nLines = nnz(A);
[p1i,p2i] = find(A);
pick = tril(true(nLines),-1);
x1 = pickCoordinates(XY(p1i,1) ,1,nLines,pick);
y1 = pickCoordinates(XY(p1i,2) ,1,nLines,pick);
x2 = pickCoordinates(XY(p2i,1) ,1,nLines,pick);
y2 = pickCoordinates(XY(p2i,2) ,1,nLines,pick);
x3 = pickCoordinates(XY(p1i,1)',nLines,1,pick);
y3 = pickCoordinates(XY(p1i,2)',nLines,1,pick);
x4 = pickCoordinates(XY(p2i,1)',nLines,1,pick);
y4 = pickCoordinates(XY(p2i,2)',nLines,1,pick);
N = sum(areIntersecting(x1,y1,x2,y2,x3,y3,x4,y4));
end
function x = pickCoordinates(xy,n,m,pick)
x = repmat(xy,n,m);
x = x(pick);
end
function bool = areIntersecting(x1,y1,x2,y2,x3,y3,x4,y4)
% Determine if two line segments intersect.
%
% line1 = [x1, y1, x2, y2]
% line2 = [x3, y3, x4, y4]
%
% This function is robust to floating point precision issues assuming all
% input coordinates are integer-valued. If all coordinates have absolute
% values that do not exceed x, then the arithmetic operations used in this
% function could, as an intermediate step, produce a value with magnitude
% as large as 8*x^4. For double-precision floating point, this implies
% that the following must be satisfied:
%
% 8*x^4 < 2^53
%
% or equivalently, x < 5792.
%
% http://en.wikipedia.org/wiki/Line-line_intersection
bool = false(size(x1));
gen = true(size(x1));
% Test to see if the lines share exactly one endpoint. If so, the lines
% intersect if either of the free points lies on the line segment formed by
% the other two points.
s13 = find((x1==x3)&(y1==y3));
bool(s13) = (isPointOnSegment(x2, y2, x1, y1, x4, y4, s13)|isPointOnSegment(x4, y4, x1, y1, x2, y2, s13));
gen(s13) = false;
s14 = find((x1==x4)&(y1==y4));
bool(s14) = (isPointOnSegment(x2, y2, x1, y1, x3, y3, s14)|isPointOnSegment(x3, y3, x1, y1, x2, y2, s14));
gen(s14) = false;
s23 = find((x2==x3)&(y2==y3));
bool(s23) = (isPointOnSegment(x1, y1, x2, y2, x4, y4, s23)|isPointOnSegment(x4, y4, x1, y1, x2, y2, s23));
gen(s23) = false;
s24 = find((x2==x4)&(y2==y4));
bool(s24) = (isPointOnSegment(x1, y1, x2, y2, x3, y3, s24)|isPointOnSegment(x3, y3, x1, y1, x2, y2, s24));
gen(s24) = false;
% Next we check for parallel and coincident lines. Parallel lines
% obviously don't intersect. Coincident lines intersect if an endpoint from
% one of the lines lies on the other segment.
ss = find(gen&haveSameSlope(x1, y1, x2, y2, x3, y3, x4, y4, 1:numel(x1)));
gen(ss) = false;
si = ss(haveSameIntercept(x1, y1, x2, y2, x3, y3, x4, y4,ss));
UNO = ones(size(x1));
bool(si) = ((isPointBetween(x3, x4, x1, UNO, si) & isPointBetween(y3, y4, y1, UNO, si)) | ...
(isPointBetween(x3, x4, x2, UNO, si) & isPointBetween(y3, y4, y2, UNO, si)) | ...
(isPointBetween(x1, x2, x3, UNO, si) & isPointBetween(y1, y2, y3, UNO, si)) | ...
(isPointBetween(x1, x2, x4, UNO, si) & isPointBetween(y1, y2, y4, UNO, si)));
% If we get this far, we're in the general case of two well-defined
% non-parallel lines. The lines formed by the two segments must intersect
% somewhere. Find this intersection point and determine if it lies on the
% segments.
gen = find(gen);
Px_n = (x1.*y2 - y1.*x2).*(x3 - x4) - (x1 - x2).*(x3.*y4 - y3.*x4);
Px_d = (x1 - x2).*(y3 - y4) - (y1 - y2).*(x3 - x4);
Py_n = (x1.*y2 - y1.*x2).*(y3 - y4) - (y1 - y2).*(x3.*y4 - y3.*x4);
Py_d = (x1 - x2).*(y3 - y4) - (y1 - y2).*(x3 - x4);
bool(gen) = (isPointBetween(x1, x2, Px_n, Px_d, gen) & ...
isPointBetween(y1, y2, Py_n, Py_d, gen) & ...
isPointBetween(x3, x4, Px_n, Px_d, gen) & ...
isPointBetween(y3, y4, Py_n, Py_d, gen));
end
function bool = haveSameIntercept(x1, y1, x2, y2, x3, y3, x4, y4, ii)
bool = (x4(ii) - x3(ii)).*(y1(ii).*(x2(ii) - x1(ii)) - x1(ii).*(y2(ii) - y1(ii))) == ...
(x2(ii) - x1(ii)).*(y3(ii).*(x4(ii) - x3(ii)) - x3(ii).*(y4(ii) - y3(ii)));
end
function bool = haveSameSlope(x1, y1, x2, y2, x3, y3, x4, y4, ii)
bool = (y2(ii) - y1(ii)).*(x4(ii) - x3(ii)) == (x2(ii) - x1(ii)).*(y4(ii) - y3(ii));
end
function bool = isPointOnSegment(x1, y1, x2, y2, x3, y3, ii)
% Determine if point (x1, y1) is on the segment formed by (x2, y2) and (x3, y3).
UNO = ones(size(x1));
bool = haveSameSlope(x2, y2, x1, y1, x1, y1, x3, y3, ii) & ...
isPointBetween(x2, x3, x1, UNO, ii) & ...
isPointBetween(y2, y3, y1, UNO, ii);
end
function bool = isPointBetween(pt1, pt2, num, den, ii)
% Determine if either of the following is satisfied:
% pt1 <= num/den <= pt2
% pt1 >= num/den >= pt2
bool = ((pt1(ii).*den(ii) <= num(ii)) & (num(ii) <= pt2(ii).*den(ii))) | ...
((pt1(ii).*den(ii) >= num(ii)) & (num(ii) >= pt2(ii).*den(ii)));
end
|