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

Maximus jiggle 10 110

by Richard Zapor

Status: Passed
Results: 1205 (cyc: 16, node: 1244)
CPU Time: 142.069
Score: 17989200.0
Submitted at: 2012-11-07 15:59:55 UTC
Scored at: 2012-11-07 23:41:50 UTC

Current Rank: 1834th (Highest: 1834th )

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(nnz(triu(a,1))),-1));

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

parfor ss=1:9
    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)));

ni_Lim=60;
if N>=100,ni_Lim=40;end


sprev = rng(0,'twister');

for ii=1:2
    ni=0;
    for jiggle=1:180
       xyOutZ=xyOut+randi([-ii ii],N,2);
      if size(unique(xyOutZ,'rows'),1)==N
       gradeIt(p1i,p2i,pickr,pickc,xyOutZ);
       if ans<ZBest 
        ZBest=ans;
        xyOut=xyOutZ;
        ni=0;
       end
      end
     ni=ni+1;
     if ni>ni_Lim,break;end
    end
end

for ii=1:2
    ni=0;
    for jiggle=1:10
       xyOutZ=xyOut+randi([-ii ii],N,2);
      if size(unique(xyOutZ,'rows'),1)==N
       gradeIt(p1i,p2i,pickr,pickc,xyOutZ);
       if ans<ZBest 
        ZBest=ans;
        xyOut=xyOutZ;
        ni=0;
       end
      end
     ni=ni+1;
     if ni>ni_Lim,break;end
    end
end




rng(sprev);

for jiggle=2:min(40,N)
    xyOutJ1=xyOut;
    xyOutJ2=xyOut;
    idx=jiggle:jiggle:2*N;
    
    xyOutJ1(idx)=xyOut(idx)+1;
    grade1=gradeIt(p1i,p2i,pickr,pickc,xyOutJ1);
    xyOutJ2(idx)=xyOut(idx)-1;
    grade2=gradeIt(p1i,p2i,pickr,pickc,xyOutJ2);
    [ZBest,xyOut]=calc(ZBest,xyOutJ1,grade1,xyOutJ2,grade2,N,xyOut);
end

%Methodical
for idx=1:2:min(110,N)
    xyOutJ1=xyOut;
    xyOutJ2=xyOut;
    
    xyOutJ1(idx,:)=xyOut(idx,:)+4;
    grade1=gradeIt(p1i,p2i,pickr,pickc,xyOutJ1);
    xyOutJ2(idx,:)=xyOut(idx,:)-4;
    grade2=gradeIt(p1i,p2i,pickr,pickc,xyOutJ2);
    [ZBest,xyOut]=calc(ZBest,xyOutJ1,grade1,xyOutJ2,grade2,N,xyOut);
end



end

function [ZBest,xyOut]=calc(ZBest,xyOutJ1,grade1,xyOutJ2,grade2,N,xyOut)
if grade1<ZBest && size(unique(xyOutJ1,'rows'),1)==N
    xyOut=xyOutJ1; % Use jiggle enhanced for more jiggling
    %break % Greedy
    ZBest=grade1;
elseif grade2<ZBest && size(unique(xyOutJ2,'rows'),1)==N
    xyOut=xyOutJ2; % Use jiggle enhanced for more jiggling
    %break % Greedy
    ZBest=grade2;
end
end

function [xyOut xyOut1] = solver_alfonsoz(a, ss, N, V)
randn('seed',ss);
xyOut=V(:,2:3);
sN=sqrt(N);
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);
        [c1,D]=svd(xc');
        xyOut=xc*c1*diag(sN./(.1+D([1 4])))*c1';
    end
    [i,j]=find(a>0.002+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.002+ss*5.1e-4;
end

xyOutH=xyOut;
single_idx = find(sum(a)==1);
[singles_link, ~] = find(a(:,single_idx)==1);
xyOut(single_idx,:)=xyOut(singles_link,:);

for zsingle=1:2
    xyOut0=sqrt(N)*detrend(xyOut,'constant')*diag(1./std(xyOut,1,1));
    k=5;
    [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
    if zsingle==1
        xyOut1=xyOut;
        xyOut=xyOutH;
    end
end
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);
    [~,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 ans = gradeIt(p1i,p2i,pickr,pickc,XYnew)
p1 = XYnew(p1i,:);
p2 = XYnew(p2i,:);
P = [p1 p2 p1-p2 p1(:,1).*p2(:,2)-p2(:,1).*p1(:,2)];
a = P(pickr,:);
b = P(pickc,:);
A = (a(:,1).*b(:,6) - a(:,2).*b(:,5) + b(:,7)) ...
    .*(a(:,3).*b(:,6) - a(:,4).*b(:,5) + b(:,7));
B = (b(:,1).*a(:,6) - b(:,2).*a(:,5) + a(:,7)) ...
    .*(b(:,3).*a(:,6) - b(:,4).*a(:,5) + a(:,7));

  sum((A <=0)  & (B <=0));
end