Winner Paulo Uribe (turbf1)

Finish 2002-05-23 00:00:00 UTC

Best?

by Yi Cao

Status: Passed
Results: Average strain = 2031.562
CPU Time: 160.309
Score: 7807.87
Submitted at: 2002-05-23 16:57:22 UTC
Scored at: 2002-05-24 17:05:18 UTC

Current Rank: 28th
Based on: turbf (diff)

Comments
Please login or create a profile.
Code
function xy = solver(kd,bx)
npts = length(kd);
if ~any(kd(:)>0),
    xy = zeros(npts,2);
    return
end
S=(kd-eye(npts))>=0;
npts0=npts;
nzS = find(sum(S)>0);
S=S(nzS,nzS);
kd=kd(nzS,nzS);
npts = length(nzS);
nP=max(sum(S));
mx=max(kd(:))*(1+0.002*nP/npts);
dx=min(bx(2),mx);
dy=min(bx(4),mx);
D=dx+1i*dy;
pm=D/2;
xy=zeros(1,npts)+pm;
dr=abs(D);
kd(find(kd>dr))=dr;
minL = min(kd(find(triu(S,1))));
sCut = 0.5;
d=max([max(dx,dy)/10, 6*sCut/nP, minL/2]);
x=0:d:dx;
y=0:d:dy;
[xx,yy] = ndgrid(x,1i*y);
XX=xx(:)+yy(:);
nX=length(XX);
xOnes=ones(nX,1);
xZeros=zeros(nX,1);
iones=ones(1,npts);
Lp1=15;
ngrad=50;
olds = 1.0e30;
[dum,p]=sort(-max(kd));
S=S(p,p);
kd=kd(p,p);
[v,w]=sort(p);
for iter=1:Lp1
    %	for index = 1:npts
    for index = 1:npts
        I = find(S(:,index)); 
        if ~isempty(I)
            st = sum(abs(abs(XX(:,ones(size(I)))-xy(xOnes,I))-kd(xZeros+index,I)),2);
            [null,minloc] = min(st);
            xy(index) = XX(minloc);
        end
    end
    [s,M]=findstrain(S,xy,kd);
    if((abs(s-olds)/(s+1)) < 0.002) | s < sCut
        break;
    end
    olds = s;
end
xy=xy.';
xy=xy(w,:);
kd=kd(w,w);
S=S(w,w);
alpha = 0.13;
obj = 1.0e20*ones(ngrad,1);
xybig = zeros(npts,ngrad);
gradient = zeros(npts,1);
[objnew,sM,dM]=findstrain(S,xy,kd);
gradient(:) = sum(dM./(abs(dM)+eps).*sign(sM));
ograd=gradient;
obj(1) = objnew;
xybig(:,1) = xy;
for ij=2:ngrad  
   xy = xy - alpha  * gradient;
   xy = min(max(real(xy),0),dx) + j * min(max(imag(xy),0),dy);
   [s,sM,dM]=findstrain(S,xy,kd);
   gradient(:) = sum(dM./(abs(dM)+eps).*sign(sM));
   obj(ij) = s;
   xybig(:,ij) = xy;
   if( obj(ij) > obj(ij-1) )
       xy = xybig(:,ij-1);
       alpha = alpha / 3;
       gradient=ograd;
   end
   [bestobj,index] = min(obj);
   if bestobj < sCut
      break
   end
   ograd = gradient;
end
[bestobj,index] = min(obj);
xy = xybig(:,index);
xy=[xy(:);zeros(npts0-npts,1)];
xy=[real(xy) imag(xy)];

function [s,strainMatrix,dX]=findstrain(S,xy,kd)
sI=find(S);
strainMatrix=S;
dX=S;
[X1,X2]=meshgrid(xy); 
dX(sI)=X1(sI)-X2(sI);
strainMatrix(sI) = abs(dX(sI))-kd(sI);
s = sum(abs(strainMatrix(sI)));