2012-11-07 16:00:00 UTC

# The Fast Monkey

by Fel

Status: Passed
Results: 1309 (cyc: 7, node: 1160)
CPU Time: 33.532
Score: 1352.72
Submitted at: 2012-11-04 21:07:25 UTC
Scored at: 2012-11-04 21:21:54 UTC

Current Rank: 774th (Highest: 5th )
Based on: The Fast Monkey (diff)
Basis for: The Last Monkey (diff)
Basis for: The Fast Monkey (diff)
Basis for: Fast Monkey Jiggle (diff)
...and 2 others.

Code
```function xyOut = solver(a, xyIn, wts)
deg=diag(sum(a));
[V,S]=eig(deg-a,deg);
N = length(a);
A = triu(a,1);
for ss=1:3
[xy{ss} xy{ss+3}] = solver_alfonsoz (a, xyIn , wts, ss+1, N, V+rand(N)/25); % (Biswas)
end
[xy{7} xy{8}] = solver_alfonsoz (a, xyIn , wts, 1, N, V);
xy{9}  = solver_hannes(xyIn, N, V(:,2:3));
xy{10} = solver_fel(xyIn, V(:,2:3), S);

[~,best] = min(NN);
xyOut = xy{best};

end

function [xyOut xyOut1] = solver_alfonsoz(a, xyIn, wts, ss, N, V)
randn('seed',ss);
xyOut=V(:,2:3);

for ncut=1:2
Mbeta=((1-ss/15)*eye(N)+(ss/15)*bsxfun(@rdivide,a+eye(N),sum(a,2)+1))^10;
for n1=1:(117+ss)
xyOut=Mbeta*xyOut;
xc = bsxfun(@minus,xyOut,sum(xyOut)/N);
cov_xyOut = (xc'*xc)/N;
[c1,D]=svd(cov_xyOut);
xyOut=xc*c1*diag(1./sqrt(.1+diag(D)))*c1';
end
[i,j]=find(a>0.0075+ss*5.1e-4);
dd=sum((xyOut(i,:)-xyOut(j,:)).^2,2);
k=find(dd>(3.15 + ss*0.102)*mean(dd));
if isempty(k), break; end
a(i(k)+N*(j(k)-1))=0.0075+ss*5.1e-4;
end

xyOutH=xyOut;
single_idx = find(sum(a)==1);

for zsingle=1:2
xyOut0=sqrt(N)*detrend(xyOut,'constant')*diag(1./std(xyOut,1,1));
k=5;
[sxyOut0,idxequal]=sortrows(round(15*xyOut0)); % (Biswas)
idxequal=idxequal(all(~diff(sxyOut0,1,1),2));
xyOut=round(xyOut0);
while size(unique(xyOut,'rows'),1)~=N
k=k*(1.0+ss*0.055);
xyOut=xyOut0*k;
xyOut(idxequal,:)=(xyOut0(idxequal,:)+randn(numel(idxequal),2)/(9+ss))*k;
xyOut=round(xyOut);
end
xyOut = bsxfun(@minus,xyOut,round(wts*(xyOut-xyIn)./sum(wts)));
if zsingle==1
xyOut1=xyOut;
xyOut=xyOutH;
end
end
end

function xyOut = solver_hannes(xyIn, N, V)
mult    = norm(max(xyIn)-min(xyIn))/norm(max(V)-min(V));
xyOut   = [];
while size(unique(xyOut,'rows'),1)<N
xyOut       = round(V*mult);
[~,i]       = unique(xyOut,'rows');
m           = setdiff(1:N,i);
xyOut(m,:)  = xyOut(m,:) + round(rand(length(m),2))*2 - 1;
mult        = mult*1.5;
end
end

function xyOut = solver_fel(xyIn, xyOut0, S)

% Dimensiones
k       = 2*sum(sum(S));
xyOut   = round(k*xyOut0);
while size(unique(xyOut,'rows'),1)~=size(xyIn,1)
k       = k*1.1;
xyOut   = round(k*xyOut0);
end

end

nLines = nnz(A);
[p1i,p2i] = find(A);
pick = tril(true(nLines),-1);
for nt=1:2
x1{nt} = pickCoordinates(XYnew(p1i,nt) ,1,nLines,pick);
x3{nt} = pickCoordinates(XYnew(p1i,nt)',nLines,1,pick);
x2{nt} = pickCoordinates(XYnew(p2i,nt) ,1,nLines,pick);
x4{nt} = pickCoordinates(XYnew(p2i,nt)',nLines,1,pick);
end
N = sum(areIntersecting(x1{1},x1{2},x2{1},x2{2},x3{1},x3{2},x4{1},x4{2}));
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 bool = areIntersecting(x1,y1,x2,y2,x3,y3,x4,y4)
a = x1.*y2 - y1.*x2;
b = x3.*y4 - y3.*x4;
Px_n = a.*(x3 - x4) - b.*(x1 - x2);
Py_n = a.*(y3 - y4) - b.*(y1 - y2);
Pxy_d = (x1 - x2).*(y3 - y4) - (y1 - y2).*(x3 - x4);
bool = isPointBetween(x1, x2, Px_n, Pxy_d) & ...
isPointBetween(y1, y2, Py_n, Pxy_d) & ...
isPointBetween(x3, x4, Px_n, Pxy_d) & ...
isPointBetween(y3, y4, Py_n, Pxy_d);
end

function x = pickCoordinates(xy,n,m,pick)
x = repmat(xy,n,m);
x = x(pick);
end

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

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