ID:50003
Title:sjh2
Author:sjh
Date:2008-11-06 11:58:17
Score:21735.2014
Result:21695.44 (cyc: 18, node: 657)
CPU Time:86.0982
Status:Passed
Comments:
Based on:none
Code:
function [dr,dc,act,mark] = sjh(m,f,a,ao,s,so,d,do) %#ok<INUSD,FNDEF>

% move algs
% if can't move up/down/left/right, try diags, if can't move diag/ try straight
% if can't move in desired direction, use function to check for path to target


%% in
% m main map
% f food map
% a ant map
% ao op ant map
% s scent
% so op scent
% d death
% do op death

%% out
% dr -1 up, 1 down
% dc -1 left, 1 right
% act -1 attack, 1 carry
% mark scent


%% defaults
dr = 0;
dc = 0;
act = -1;
mark = 0;


%% fight
if ao(13)
    return;
end


%% init
hillmark = 500;
m = m(:);
f = f(:);
s = s(:);
a = a(:);
f(m == 1) = 0; % ignore food in my anthill
rs = [-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];
cs = [-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1];
% bndx = isnan(m); % blocked
if isnan(m(7))
    m([1 2 6]) = NaN;
end
if isnan(m(8))
    m(3) = NaN;
end
if isnan(m(9))
    m([4 5 10]) = NaN;
end
if isnan(m(12))
    m(11) = NaN;
end
if isnan(m(14))
    m(15) = NaN;
end
if isnan(m(17))
    m([16 21 22]) = NaN;
end
if isnan(m(18))
    m(23) = NaN;
end
if isnan(m(19))
    m([20 24 25]) = NaN;
end
bndx = isnan(m); % blocked
f(bndx) = 0;
s(bndx) = 0;


%% mask off positions that can't be moved to


%% carry food
if rand < f(13)/a(13) % if there are more ants than food, not all ants need to carry
    act = 1;
    % carry food toward strongest scent to get to anthill
    % try direction of largest scent, fall back on largest adjacent scent
    [mm,mndx] = max(s);
    dr = rs(mndx);
    dc = cs(mndx);
    % one less than max scent I can see
    mark = floor((max(s) - s(13) - 2)/a(13));
    return
end


%% get food
f(13) = 0; % don't pay attention to food at current location
fe = f - a; % food with no ants to carry it
fe(fe<0) = 0;
if any(fe)
    % move towards food that needs carrying based on probability
    [fe,fndx] = sort(fe,'descend'); % sort most food to least
    fp = fe / sum(fe);
    cndx = find(rand < cumsum(fp),1);
    fndx = fndx(cndx);
    dr = rs(fndx);
    dc = cs(fndx);
    if m(13) == 1
        mark = floor((hillmark - s(13))/a(13));
    else
        % one less than max scent I can see
        mark = floor((max(s) - s(13) - 2 - 3)/a(13));
    end
    return
end


%% search
% move toward lowest scent marking
% sometimes min, sometimes max? how do I get back to food source and explore new area?
% if there are zeros, go there, else, find min scent marking -- will get stuck in corners
% if on min scent, mark = 100 and move to next smallest scent
mm = min(s(~bndx));
if mm == 0
    mndx = find(s==mm & ~bndx);
else
    mm = max(s(s<s(13) & ~bndx)); % max scent less than current scent
    if isempty(mm) % no scent less than current scent
        mm = min(s(s>s(13))); % min scent greater than current scent
        mndx = find(s==mm);
        % one less than max scent I can see
        mark = floor((max(s) - s(13) - 2 - 6)/a(13));
    else
        mndx = find(s==mm & ~bndx);
    end
end
mndx = mndx(ceil(rand*length(mndx)));
dr = rs(mndx);
dc = cs(mndx);
if mark > 0
    % do nothing
elseif m(13) == 1
    mark = floor((hillmark - s(13))/a(13));
else
    % one less than min scent I can see
    mark = floor((min(s(s>0)) - s(13) - 2 - 6)/a(13));
    if isempty(mark)
        mark = 0; % no scent, so what do I do?
    end
end