Winner Paulo Uribe (turbf1)

2002-05-23 00:00:00 UTC

# turbut

Status: Passed
Results: Average strain = 2031.316
CPU Time: 122.405
Score: 7807.16
Submitted at: 2002-05-23 16:59:41 UTC
Scored at: 2002-05-24 17:58:15 UTC

Current Rank: 9th
Based on: More Useless Tweaking (diff)

Code
```function xy = solver(kd,bx)
npts = length(kd);
if ~any(kd(:)>0),
xy = zeros(npts,2);
return
end
dx=bx(2);
dy=bx(4);
D=dx+1i*dy;
pm=D/2;
xy=zeros(1,npts)+pm;

d=0.1*max(dx,dy);
x=0:d:dx;
y=0:d:dy;
[xx,yy] = ndgrid(x,1i*y);
[nx,ny]=size(xx);
onesx=ones(1,nx);
onesy=ones(1,ny);
XX=xx(:)+yy(:);

dr=abs(D);
kd(find(kd>dr))=dr;

S=(kd-eye(npts))>=0;
nX=length(XX);
xOnes=ones(nX,1);
xZeros=zeros(nX,1);
start=1;
iones=ones(1,npts);
sCut = 2;

olds = 1.0e30;
iterbigmax = 1;

for iterbig = 1:iterbigmax
[dum,p]=sort(-max(kd));
S=S(p,p);
kd=kd(p,p);
[v,w]=sort(p);
for iter=1:6
%	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.001) | s < sCut
break;
end
olds = s;
end
xy=xy.';
xy=xy(w,:);
kd=kd(w,w);
S=S(w,w);

olds = 1.0e50;
oldxy = xy;
alpha = 1;

for iter=1:100
[s,M]=findstrain(S,xy,kd);

if( iter > 5 & abs(s-olds) / (s+1) < 0.01 ) | s < sCut
xy = oldxy;
break;
end

if( s > olds )
xy = oldxy;
break;
end
oldxy = xy;
olds = s;
for indx = 1:npts
Dx=xy-xy(indx);
N = abs(Dx);
N(N==0)=1;
Dx=Dx./N .* M(:,indx);
Dx = mean(Dx);

xy(indx) = xy(indx) + alpha*Dx;
xy(indx) = min(dx,max(0,real(xy(indx))))+1i*min(dy,max(0,imag(xy(indx))));
end
end

if( iterbig < iterbigmax )
xy=xy.';
end
end

[objnew,sM,dM]=findstrain(S,xy,kd);
obj(1) = objnew;
xybig(:,1) = xy;

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

obj(ij) = s;
xybig(:,ij) = xy;
if( obj(ij) > obj(ij-1) )
xy = xybig(:,ij-1);
alpha = alpha / 3;
end
[bestobj,index] = min(obj);
if bestobj < sCut
break
end
end

[bestobj,index] = min(obj);
xy = xybig(:,index);
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)));```