2005-05-18 09:00:00 UTC

# testg1

by Mihai

Status: Failed
Results:

Code
function [dy,dx,mark,carry] = solver(main,food,ants,scent)
%SOLVER The ant brain.

MF = 10;
MC = 50;
MM = 20;

m = zeros(3,3);
c = zeros(3,3);

o = -10*(main(2:4,2:4)==-1);

L = kron([1,1,1,0,0],ones(5,1));
U = L.';
TS1 = triu(ones(5));
TS2 = fliplr(TS1);

m(1,1) = sum(sum(L.*U.*scent));
m(1,2) = sum(sum(TS1.*TS2.*scent));
m(1,3) = sum(sum(fliplr(L).*U.*scent));

m(2,1) = sum(sum(TS1.'.*TS2.*scent));
m(2,2) = -10;
m(2,3) = sum(sum(TS1.*flipud(TS1).*scent));

m(3,1) = sum(sum(L.*flipud(U).*scent));
m(3,2) = sum(sum(TS1.'.*fliplr(TS1.').*scent));
m(3,3) = sum(sum(fliplr(L).*flipud(U).*scent));

if max(m) > 0
m = m/max(max(m));
end;

mL = kron(2:4,ones(3,1));
if main(3,3) < 1
if food(3,3) == 0
carry = logical(0);
[I,J] = find((food.*(food > 0)-(main > 0)-ants.*(ants > 0))>0);
if ~isempty(I)
for Inm = 1:length(I)
m = m + food(I(Inm),J(Inm)).*...
exp(-(I(Inm)-mL.').^2-(J(Inm)-mL).^2);
m(2,2) = 0;
end;
mFinal = m + o;

[I,J] = find(mFinal == max(max(mFinal)));
[dx,dy] = direction(I,J);

mark = MF;

else
mFinal = m + o;

[I,J] = find(mFinal.*mFinal == min(min(mFinal.*mFinal)));
[dx,dy] = direction(I,J);

mark = MM;
end;
else
carry = logical(1);
[I,J] = find(main > 0);
if ~isempty(I)
for Inc = 1:length(I)
c = c + food(I(Inc),J(Inc)).*...
exp(-(I(Inc)-mL.').^2-(J(Inc)-mL).^2);
c(2,2) = 0;
end;
mFinal = c + o;

[I,J] = find(mFinal == max(max(mFinal)));
[dx,dy] = direction(I,J);

mark = MC;

else
mFinal = m + o;

[I,J] = find(mFinal == max(max(mFinal)));
[dx,dy] = direction(I,J);

mark = MM;
end;
end;
else
carry = logical(0);
[I,J] = find((food.*(food > 0)-(main > 0)-ants.*(ants > 0))>0);
if ~isempty(I)
for Inm = 1:length(I)
m = m + food(I(Inm),J(Inm)).*...
exp(-(I(Inm)-mL.').^2-(J(Inm)-mL).^2);
m(2,2) = 0;
end;
mFinal = m + o;

[I,J] = find(mFinal == max(max(mFinal)));
[dx,dy] = direction(I,J);

mark = MF;

else
mFinal = m + o;

[I,J] = find(mFinal.*mFinal == min(min(mFinal.*mFinal)));
[dx,dy] = direction(I,J);

mark = MM;
end;
end;

function [dx,dy] = direction(I,J)
if length(I) > 1 % Daca sunt mai multe egale se duce aleator pe una
nP = floor(1+length(I)*rand);
else
nP = 1;
end;
dx = J(nP)-2;
dy = I(nP)-2;