ID:50195
Title:8_1_7
Author:Abhisek Ukil
Date:2008-11-07 08:38:18
Score:33765.5895
Result:31726.03 (cyc: 47, node: 2062)
CPU Time:148.5561
Status:Passed
Comments:
Based on:none
Code:
function [dRow,dCol,action,mark] = solver(mainMap,foodMap,myAntMap,opAntMap, ...
    myScentMap,opScentMap,myDeathMap,opDeathMap)

dRow   = round(rand*2) - 1;
dCol   = round(rand*2) - 1;
action=0;
mark=0;

%look for home
[hi,hj]=find(mainMap==1);
if isempty(hi),hi=0;end
if isempty(hj),hj=0;end
path=[hi hj];

flag=0;
%if there is enemy, stay still and kill, effective
if opAntMap(3,3)>0 & sum(opAntMap(:))>sum(myAntMap(:))
    flag=flag+1;
    dRow=0;
    dCol=0;
    action=-1;
    mark=5;
end

% if we start at food and with no enemy
if foodMap(3,3)>0 & opAntMap(3,3)==0
    %flag=flag+1;
    if path(1,1)>0 & path(1,1)~=3 & path(1,2)>0 & path(1,2)~=3 %try to move to home
        %if ~isempty(hi) & hi~=3 & ~isempty(hj) & hj~=3 %try to move to home
        [dRow,dCol]=definitemoveadv(path(1,1),path(1,2),mainMap);
    else
        [dRow,dCol,action,mark]=randommoveadv(mainMap,opAntMap,myDeathMap);
    end
    action=1;
    mark=50;
end

% if we start at home, explore further
if hi==3 & hj==3 %current position is home
    flag=flag+1;
    [dRow,dCol,action,mark]=foodsearch(foodMap,mainMap,opAntMap,myDeathMap);
end


%do something for other conditions
if flag==0
    m=mainMap;
    m(isnan(m))=-1;
    [dRow,dCol,mark,action] =mysolver(m,foodMap,myAntMap,myScentMap);
end

end



function [dRow,dCol,action,mark]=foodsearch(foodMap,mainMap,opAntMap,myDeathMap)

[foodR,foodC]=find(foodMap>0);
if ~isempty(foodR) %and there is food, go for it
    dist=distance([3,3],[foodR,foodC]);
    if numel(dist)>1
        dist(dist==0)=10000; %needed for min, max

        [foodval,foodindex]=min(dist); %nearest food, good 23604

        %foodindex=ceil(rand*numel(foodR)); %random food, good 23573

        %[foodval,foodindex]=max(dist); %farthest food, not good, 24191

        fi=foodR(foodindex);
        fj=foodC(foodindex);

        [dRow,dCol]=definitemoveadv(fi,fj,mainMap); action=0; mark=10;
        %[dRow,dCol,action,mark]=randommoveadv(mainMap,opAntMap,myDeathMap);
    else

        [dRow,dCol,action,mark]=randommoveadv(mainMap,opAntMap,myDeathMap);
    end
else %there is no food in sight
    [dRow,dCol,action,mark]=randommoveadv(mainMap,opAntMap,myDeathMap);
end
end


function d=distance(x,y)

d=[];
for i=1:size(y,1)
    d(end+1)=(x(1)-y(i,1))^2+(x(2)-y(i,2))^2;
end
end



% function [dRow,dCol,action,mark]=randommoveadv(mainMap,opAntMap,myDeathMap)
% action = 0;
% mark   = 10;
% dRow=-10000; dCol=-10000;
% iter=0;
% deathrate=mean(myDeathMap(:));
% while dRow==-10000 & dCol==-10000 & iter<100
%     iter=iter+1;
%     dRow   = round(rand*2) - 1;
%     dCol   = round(rand*2) - 1;
%     if isnan(mainMap(dRow+3,dCol+3)) | opAntMap(dRow+3,dCol+3)>0 | myDeathMap(dRow+3,dCol+3)>deathrate %not suitable
%         dRow=-10000; dCol=-10000;
%     end
% end
% 
% if iter==100 & dRow==-10000 & dCol==-10000
%     dRow=0; dCol=0;
% end
% end

function [dRow,dCol,action,mark]=randommoveadv(mainMap,opAntMap,myDeathMap)
dRow   = round(rand*2) - 1;
dCol   = round(rand*2) - 1;
action = 0;
mark   = 0; % noscent

% dR=dRow; dC=dCol;
% if isnan(mainMap(dRow+3,dCol+3)) %it won't be a move
%     dRow=dR; dCol=0;
% else
%     return
% end
% 
% if isnan(mainMap(dRow+3,dCol+3)) %it won't be a move
%     dRow=0; dCol=dC;
% else
%     return
% end
% 
% if isnan(mainMap(dRow+3,dCol+3)) %it won't be a move
%     dRow=dR; dCol=dC; %can be better
% else
%     return
% end

end



function [dRow,dCol]=definitemoveadv(fi,fj,mainMap)
di=fi-3;
dj=fj-3;
if di~=0, dRow=di/abs(di); end
if di==0, dRow=di; end
if dj~=0, dCol=dj/abs(dj); end
if dj==0,dCol=dj; end

dR=dRow; dC=dCol;
if isnan(mainMap(dRow+3,dCol+3)) %it won't be a move
    dRow=dR; dCol=0;
else
    return
end

if isnan(mainMap(dRow+3,dCol+3)) %it won't be a move
    dRow=0; dCol=dC;
else
    return
end

if isnan(mainMap(dRow+3,dCol+3)) %it won't be a move
    dRow=dR; dCol=dC; %can be better
else
    return
end
end



function [dy,dx,mark,carry]=mysolver(main,food,ants,scent)

rand(1,5);
i35=3:5;
i24=2:4;
i02=0:2;
dirx = [-1,2,2,2,-1; 2,-1,-1,-1,2; 2,0,0,0,2; 2,1,1,1,2; 1,2,2,2,1];
diry = [-1,-1,0,1,1; -1,-1,0,1,1; -1,-1,0,1,1; -1,-1,0,1,1; -1,-1,0,1,1];
% find reachable regions
BAD=main<0;
if BAD(7)||BAD(9)||BAD(17)||BAD(19)
if BAD(7)
BAD(1)=1;
BAD(2)=BAD(2)||BAD(8);
BAD(3)=BAD(3)||(BAD(8)&&BAD(9));
BAD(6)=BAD(6)||BAD(12);
BAD(11)=BAD(11)||(BAD(12)&&BAD(17));
end
if BAD(9)
BAD(4)=BAD(4)||BAD(8);
BAD(5)=1;
BAD(10)=BAD(10)||BAD(14);
end
if BAD(17)
BAD(16)=BAD(16)||BAD(12);
BAD(21)=1;
BAD(22)=BAD(22)||BAD(18);
end
if BAD(19)
BAD(15)=BAD(15)||(BAD(9)&&BAD(14));
BAD(20)=BAD(20)||(BAD(14));
BAD(23)=BAD(23)||(BAD(17)&&BAD(18));
BAD(24)=BAD(24)||(BAD(18));
BAD(25)=1;
end
% modify current board based on reachable regions
main(BAD)=-1;
food(BAD)=0;
scent(BAD)=0;
end
BAD(13)=1;
% setup game stats
scents=scent(scent>0);
maxscent=max(scents(:));
% mark this field
if (max(main(:)))
mark=400-scent(13);
scent(13)=400;
init=0;
hills=find(main>0);
scent(hills)=1e7;
food(hills)=0;
if isempty(scents);maxscent=mark;end
elseif (maxscent==min(scents(:))) % init phase
mark=maxscent-scent(13);
scent(13)=maxscent;
init=1;
elseif isempty(scents) % nothing is marked
mark=98;
scent(13)=scent(13)+98;
maxscent=mark;
init=1;
else
mark=maxscent-1-scent(13);
scent(13)=maxscent-1;
init=0;
end
% look around for food
if max(food(:))<1 ||init % nothing found or no way home, search
target=maxscent-scent;target(BAD)=-1;
[y,x]=find(target==max(target(:)));
if (numel(y)>1)
r=ceil(rand*numel(y));
y=y(r);
x=x(r);
end
carry=food(13)>0;
dx = dirx(x,y);
if dx<2
dy = diry(x,y);
return;
end;
m(7,7)=0;
m(y+i02,x+i02)=1;
m=m(i35,i35);
t=target(i24,i24).*m+m;
[dy,dx]=find(t==max(t(:)));
r=ceil(rand*numel(dy));
dy=dy(r)-2;
dx=dx(r)-2;
return;
end;
if (food(13)>1)&&rand<.95 % sit on a lot of food, go home
target=scent;target(BAD)=-1;
[y,x]=find(target==max(target(:)));
if (numel(y)>1)
y=y(1);
x=x(1);
end
carry=1;
dx = dirx(x,y);
if dx<2
dy = diry(x,y);
return;
end;
m(7,7)=0;
m(y+i02,x+i02)=1;
m=m(i35,i35);
t=target(i24,i24).*m+m;
[dy,dx]=find(t==max(t(:)));
r=ceil(rand*numel(dy));
dy=dy(r)-2;
dx=dx(r)-2;
return;
end;
if (food(13) >= 1) % sit on one food, build track
target=scent;target(BAD)=-1;
[yhome,xhome]=find(target==max(target(:)));
lowfood=food;lowfood(scent>=scent(13))=0;
carry=1;
dhere=max(max(abs(yhome-3),abs(xhome-3)));
runs=numel(lowfood(lowfood(:)>0));
lowfood(i24,i24)=lowfood(i24,i24)*1e8;
target=lowfood;target(BAD)=-1;
while runs % fetch food behind
[y,x]=find(target==max(target(:)));
if (numel(y)>1)
    r=ceil(rand*numel(y));
    y=y(r);
    x=x(r);
end
dx = dirx(x,y);
if dx<2
    dy = diry(x,y);
else
    m(7,7)=0;
    m(y+i02,x+i02)=1;
    m=m(i35,i35);
    t=target(i24,i24).*m+m;
    [dy,dx]=find(t==max(t(:)));
    r=ceil(rand*numel(dy));
    dy=dy(r)-2;
    dx=dx(r)-2;
end
if ( min(max(abs(yhome-y),abs(xhome-x))) > dhere )
    carry=0;
    return;
end
target(y,x)=0;runs=runs-1;
end
if (carry) % go home
target=scent;target(BAD)=-1;
[y,x]=find(target==max(target(:)));
if (numel(y)>1)
    r=ceil(rand*numel(y));
    y=y(r);
    x=x(r);
end
dx = dirx(x,y);
if dx<2
    dy = diry(x,y);
    return;
end;
m(7,7)=0;
m(y+i02,x+i02)=1;
m=m(i35,i35);
t=target(i24,i24).*m+m;
[dy,dx]=find(t==max(t(:)));
r=ceil(rand*numel(dy));
dy=dy(r)-2;
dx=dx(r)-2;
return;
end
return;
end;
target=max(food-ants,0);target(BAD)=-1000;
[y,x]=find(target==max(target(:)));
if (numel(y)>1)
r=ceil(rand*numel(y));
y=y(r);
x=x(r);
end
carry=0;
dx = dirx(x,y);
if dx<2
dy = diry(x,y);
return;
end;
m(7,7)=0;
m(y+i02,x+i02)=1;
m=m(i35,i35);
t=target(i24,i24).*m+m;
[dy,dx]=find(t==max(t(:)));
r=ceil(rand*numel(dy));
dy=dy(r)-2;
dx=dx(r)-2;

end %function end