2012-11-07 16:00:00 UTC

# Art twk

Status: Passed
Results: 1346 (cyc: 5, node: 975)
CPU Time: 31.505
Score: 1381.8
Submitted at: 2012-11-04 02:23:53 UTC
Scored at: 2012-11-04 02:27:01 UTC

Current Rank: 975th (Highest: 1st )
Based on: The art of mergers (diff)
Basis for: The Art of Push (diff)

Code
```function xyOut = solver(a, xyIn, wts)

% Place nodes in radial pattern
xy{4}       = solver_hannes(a, xyIn);
for n=1:3
xy{n}     = solver_alfonso  (a, xyIn , wts, n+1);
end

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

end

function xyOut = solver_alfonso(a, xyIn, wts, ss)

randn('seed',ss);
N=length(wts);

deg=diag(sum(a));
[V,~]=eig(deg-a,deg);
xyOut=V(:,2:3);

for ncut=1:2
Mbeta=((1-ss/10)*eye(N)+(ss/10)*bsxfun(@rdivide,a+eye(N),sum(a,2)+1))^10;
for n1=1:(117+ss)
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.0075+ss*1.02/2000);
dd=sum((xyOut(i,:)-xyOut(j,:)).^2,2);
k=find(dd>(3.15 + ss*1.02/10)*mean(dd));
if isempty(k), break; end
a(i(k)+N*(j(k)-1))=0.0075+ss*1.02/2000;
end

xyOut0=sqrt(N)*detrend(xyOut,'constant')*diag(1./std(xyOut,1,1));
k=5;
[sxyOut0,idxequal]=sortrows(round((12+ss)*xyOut0)/(12+ss));
idxequal=idxequal(all(~diff(sxyOut0,1,1),2));
xyOut=round(xyOut0);
while size(unique(xyOut,'rows'),1)~=N
k=k*(1.0+ss*1.1/20);
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))); % AV's
end

function xyOut = solver_hannes(a, xyIn)
sz=size(a,1);
deg=diag(sum(a));
L=diag(sum(a))-a;
[V,D]=eig(L,deg);
xyOut=0;
outdiam=sqrt(sum((max(V(:,2:3))-min(V(:,2:3))).^2));
indiam=sqrt(sum((max(xyIn)-min(xyIn)).^2));
mult=indiam/outdiam;
while size(unique(xyOut,'rows'),1)<sz
xyOut=round(V(:,2:3)*mult);
[u,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

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);

S         = 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)

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
```