2012-11-07 16:00:00 UTC

# The art of mergers

by Fel

Status: Passed
Results: 1368 (cyc: 6, node: 1460)
CPU Time: 27.014
Score: 1422.54
Submitted at: 2012-11-03 22:25:27 UTC
Scored at: 2012-11-03 22:39:54 UTC

Current Rank: 1067th (Highest: 15th )
Based on: The art of mergers (diff)
Basis for: The art of mergers (diff)
Basis for: The art of mergers (diff)
Basis for: There is no art (diff)

Code
```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);
xy{4}       = solver_pavan    (a, xyIn);
[~,best]    = min(N);
xyOut       = xy{best};

end

function xyOut = solver_alfonso(a, xyIn, wts)

randn('seed',2);

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.35*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 xyOut = solver_pavan(a, xyIn)
% Entry 67362: 10567 (cyc: 2, node: 225) CPU Time: 38.203 Score: 10575.5

s = 100;
zmax = 15;

orig_mean = mean(xyIn, 1);
len = length(xyIn);

xyIn = (xyIn - repmat(orig_mean, len, 1)).*s + repmat(orig_mean, len, 1);
for z = 1:zmax
xyIn = round(cell2mat(cellfun(@(a_row) mean(xyIn(a_row==1, :), 1), num2cell(a, 1), 'uni', false)'));
xyIn = tweak_overlap(xyIn);
xyIn = (xyIn - repmat(orig_mean, len, 1)).*1.05 + repmat(orig_mean, len, 1);
end

xyOut = xyIn;
%shift mean back
xyOut = round(xyOut + repmat(orig_mean - mean(xyOut, 1), length(xyIn), 1));

end

function xyIn = tweak_overlap(xyIn)
tweakdir = [1 0; 0 -1];
[u I J] = unique(xyIn, 'rows');
n = size(xyIn, 1) - size(u, 1);
while(n>0)
xyIn(setdiff(1:length(xyIn), I), :) = xyIn(setdiff(1:length(xyIn), I), :) + ones(n, 2) * tweakdir;
[u I J] = unique(xyIn, 'rows');
n = size(xyIn, 1) - size(u, 1);
end
end

A         = triu(a,1);
nLines    = nnz(A);
[p1i,p2i] = find(A);
pick      = tril(true(nLines),-1);
x1        = pickCoordinates(XYnew(p1i,1) ,1,nLines,pick);
x3        = pickCoordinates(XYnew(p1i,1)',nLines,1,pick);
y1        = pickCoordinates(XYnew(p1i,2) ,1,nLines,pick);
y3        = pickCoordinates(XYnew(p1i,2)',nLines,1,pick);
x2        = pickCoordinates(XYnew(p2i,1) ,1,nLines,pick);
x4        = pickCoordinates(XYnew(p2i,1)',nLines,1,pick);
y2        = pickCoordinates(XYnew(p2i,2) ,1,nLines,pick);
y4        = pickCoordinates(XYnew(p2i,2)',nLines,1,pick);

N         = sum(areIntersecting(x1,y1,x2,y2,x3,y3,x4,y4));
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 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)

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 = (isPointBetween(x1, x2, Px_n, Px_d) & ...
isPointBetween(y1, y2, Py_n, Py_d) & ...
isPointBetween(x3, x4, Px_n, Px_d) & ...
isPointBetween(y3, y4, Py_n, Py_d));

end

function bool = isPointBetween(pt1, pt2, num, den)

bool = ((pt1.*den <= num) & (num <= pt2.*den)) | ...
((pt1.*den >= num) & (num >= pt2.*den));
end
```