Finish 2012-11-07 16:00:00 UTC

lasttry02

by Alfonso Nieto-Castanon

Status: Passed
Results: 1184 (cyc: 10, node: 1240)
CPU Time: 34.556
Score: 1230.93
Submitted at: 2012-11-07 15:41:56 UTC
Scored at: 2012-11-07 20:07:07 UTC

Current Rank: 7th (Highest: 3rd )
Based on: Paris NewSeq (diff)

Comments
Please login or create a profile.
Code
function xyOut = solver(a, xyIn, wts)
deg=diag(sum(a));
[V,~]=eig(deg-a,deg);
N = length(a);
[p1i,p2i] = find( triu(a,1));
[pickr,pickc] = find(tril(true(numel(p1i)),-1));

for ss=1:3
    [xy{ss+[0,3]}] = solver_alfonsoz(a, ss+1, N, V+rand(size(V))/25, 0); % (Biswas)
end
%[xy{7} xy{8}] = solver_alfonsoz (a, 1, N, V);
xy{7} = solver_hannes(xyIn, N, V(:,2:3),0);

Nss=2;
for ss=1:Nss
    [xy{7+ss+(0:2)*Nss}] = solver_alfonsoz(a, ss+1, N, V+rand(size(V))/25, 1);
end

NN=inf(1,numel(xy));
for ss=find(cellfun('length',xy))
 NN(ss)=gradeIt(p1i,p2i,pickr,pickc,xy{ss});
end

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

xyOut = bsxfun(@minus,xyOut,round(wts*(xyOut-xyIn)./sum(wts)));

sprev = rng(0,'twister');

%for ii=1:2
for jiggle=str2num('[ones(1,8) 2*ones(1,8) ones(1,7) 2*ones(1,7)]')
 xyOutZ=xyOut+randi([-jiggle jiggle],N,2);
 grade=gradeIt(p1i,p2i,pickr,pickc,xyOutZ);
 if grade<ZBest && size(unique(xyOutZ,'rows'),1)==N
  xyOut=xyOutZ;
  ZBest=grade;
 end
end
%end

rng(sprev);
end

function varargout = solver_alfonsoz(a, ss, N, V, newversion)
WXY0=2*pi*randn(3,200);
randn('seed',ss+10*newversion);
xyOut=V(:,2:3);
sN=sqrt(N);
XYOUT0=cell(1,3);
if newversion
    ww=(.05:.9/N:.9499999999)';
    ww=(ww-.5)./sqrt(ww.*(1-ww));
    thr=0.001+ss*5.1e-2;
else
    thr=0.002+ss*5.1e-4;
end
thr_d=(3.15 + ss*0.102);
for ncut=1:2+newversion
    Mbeta=((1-ss/15)*eye(N)+(ss/15)*bsxfun(@rdivide,a+eye(N),sum(a,2)+1))^10;
    for n1=1:(117+~newversion*ss)
        xyOut=Mbeta*xyOut;
        if newversion
            wxy0=WXY0(ncut,n1);
            wxy=[cos((0:3)/4*pi+wxy0);sin((0:3)/4*pi+wxy0)];
            [nill,wz]=sort(xyOut*wxy,1);
            for n2=1:size(wz,2), wz(wz(:,n2),n2)=ww; end
            xyOut=.5*xyOut+.5*wz*wxy';
        else
            xc = bsxfun(@minus,xyOut,sum(xyOut)/N);
            [c1,D]=svd(xc');
            xyOut=xc*c1*diag(sN./(.1+D([1 4])))*c1';
        end
    end
    XYOUT0{ncut}=xyOut;
    [i,j]=find(a>thr);
    dd=sum((xyOut(i,:)-xyOut(j,:)).^2,2);
    mdd=mean(dd);
    k=find(dd>thr_d*mdd);
    if isempty(k), break; end
    a(i(k)+N*(j(k)-1))=thr*(~newversion+newversion*mdd*thr_d./dd(k));
end

if ~newversion
    XYOUT0{2}=xyOut;
    single_idx = find(sum(a)==1);
    [singles_link, ~] = find(a(:,single_idx)==1);
    xyOut(single_idx,:)=xyOut(singles_link,:);
    XYOUT0{1}=xyOut;
    ncut=2;
end

for zsingle=1:ncut
    xyOut0=XYOUT0{zsingle};
    xyOut0=sqrt(N)*detrend(xyOut0,'constant')*diag(1./max(eps,std(xyOut0,1,1)));
    k=4+~newversion;
    [sxyOut0,idxequal]=sortrows(round(15*xyOut0));
    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
    XYOUT0{zsingle}=xyOut;
end 
varargout=XYOUT0;
end

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

function ans = gradeIt(p1i,p2i,pickr,pickc,XYnew)
  X = [XYnew(p1i,:) XYnew(p2i,:)];
  xa = X(pickr,:);
  xb = X(pickc,:);
  a = xa(:,1).*xa(:,4) - xa(:,2).*xa(:,3);
  b = xb(:,1).*xb(:,4) - xb(:,2).*xb(:,3);
  Px_n = a.*(xb(:,1) - xb(:,3)) - b.*(xa(:,1) - xa(:,3));
  Py_n = a.*(xb(:,2) - xb(:,4)) - b.*(xa(:,2) - xa(:,4));
  Pxy_d = (xa(:,1) - xa(:,3)).*(xb(:,2) - xb(:,4)) - ...
    (xa(:,2) - xa(:,4)).*(xb(:,1) - xb(:,3));
  
  sum(all([(xa(:,1).*Pxy_d - Px_n).*(Px_n - xa(:,3).*Pxy_d) >=0   ...
    (xa(:,2).*Pxy_d - Py_n).*(Py_n - xa(:,4).*Pxy_d) >=0  ...
    (xb(:,1).*Pxy_d - Px_n).*(Px_n - xb(:,3).*Pxy_d) >=0  ...
    (xb(:,2).*Pxy_d - Py_n).*(Py_n - xb(:,4).*Pxy_d) >=0],2));
end