| Code: | function [dRow,dCol,action,mark] = solver(mainMap,foodMap,myAntMap,opAntMap, ...
myScentMap,opScentMap,myDeathMap,opDeathMap)
JustDeadTeamMateValue=90; %To decide if the dead of a team mate is really dangerous
LeftSugarValue=10; %To weight how important left sugar is
my_result=zeros(1,4); %Preallocate result value array
if isempty(find(foodMap>0))==1 % No food available!
my_result=NoFoodAvailable(myScentMap,myDeathMap,opAntMap);
else %Food is available!
my_result=FoodIsAvailable(foodMap,mainMap,myDeathMap,opAntMap,myAntMap);
end %End If Else Statement
%Final assignment
dRow = my_result(1);
dCol = my_result(2);
action = my_result(3);
mark = my_result(4);
%////////////////
%Nested functions
%////////////////
%--------------------------------------------------------------------------
function output=NoFoodAvailable(myScentMap,myDeathMap,opAntMap)
if isempty(find(myScentMap>0))==1 % No chemical mark is available
if IsThisPlaceDangerous(myDeathMap,opAntMap)==1
[dangerous_row,dangerous_col]=find(myDeathMap >= JustDeadTeamMateValue);
output=FindMyMove(3,3,dangerous_row(1),dangerous_col(1),mainMap);
output=-output; %I need to change direction!
output(3)=0; %No action
output(4)=0; %No mark
else
output=zeros(1,4); %No move, No action, No mark
end
else % Chemical mark is available
[target_row,target_col]=find(myScentMap==max(max(myScentMap)));
if (target_row ~= 3)&(target_col ~= 3)
output=FindMyMove(3,3,target_row(1),target_col(1),mainMap);
output(3)=0; %No action
output(4)=0; %No mark
else
output=zeros(1,4); %No move, No action, No mark
end %End If Else Statement
end %End If Else Statement
end %End function NoFoodAvailable
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
function output=IsThisPlaceDangerous(myDeathMap_Resized,opAntMap_Resized)
if (isempty(find(opAntMap_Resized >= 1))==0)&(isempty(find(myDeathMap_Resized >= JustDeadTeamMateValue))==0)
output=true;
else
output=false;
end %End If Else Statement
end %End function IsThisPlaceDangerous
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
function [DeltaRow,DeltaCol]=FindMyMove(ref_row,ref_col,target_row,target_col,Impassable)
if (ref_row==target_row)&(ref_col==target_col) %Strange situation detected
DeltaRow=0;
DeltaCol=0;
else
target_path=path_finder(ref_row,ref_col,target_row,target_col);
if (ref_row==target_row)|(ref_col==target_col) %The movement will have to take place along a row or a column
DeltaRow=target_path(1,3)-target_path(1,1);
DeltaCol=target_path(1,4)-target_path(1,2);
%Check if you can get to the calculated position
if (Impassable(ref_row+DeltaRow,ref_col+DeltaCol)==NaN)&(DeltaRow==0) %Forbidden movement along a row
DeltaRow=DeltaCol;
DeltaCol=0;
elseif (Impassable(ref_row+DeltaRow,ref_col+DeltaCol)==NaN)&(DeltaCol==0) %Forbidden movement along a column
DeltaCol=DeltaRow;
DeltaRow=0;
end
else %The movement will have to take place diagonally
DeltaRow=target_path(2,3)-target_path(1,1);
DeltaCol=target_path(2,4)-target_path(1,2);
%Check if you can get to the calculated position
if (Impassable(ref_row+DeltaRow,ref_col+DeltaCol)==NaN) %Forbidden diagonal movement
DeltaRow=0;
end
end %End If Else Statement
end %End If Else Statement
end %End function FindMyMove
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
function output=FoodIsAvailable(foodMap,mainMap,myDeathMap,opAntMap,myAntMap)
if (foodMap(3,3)>=1) %Food is available in the place where we are!
%I need to find my nearest anthill
[PossibleMyNearestAnthillRow, PossibleMyNearestAnthillCol]=...
find(mainMap==1);
[MyNearestAnthillRow, MyNearestAnthillCol]=...
FindNearest(3,3,mainMap,PossibleMyNearestAnthillRow,PossibleMyNearestAnthillCol,1);
output=FindMyMove(3,3,MyNearestAnthillRow,MyNearestAnthillCol,mainMap);
output(3)=1; %Carry
%Mark considering the amout of sugar
output(4)=min(100,LeftSugarValue*(foodMap(3,3)-1));
else %Food is available but not where I am
%I need to find the biggest amount of sugar cubes
[PossibleMyNearestFoodRow, PossibleMyNearestFoodCol]=...
find(foodMap>=1);
[MyNearestFoodRow, MyNearestFoodCol]=...
FindNearest(3,3,foodMap,PossibleMyNearestFoodRow,PossibleMyNearestFoodCol,1);
%I need to get further information regarding the situation we
%have very close to the food position
myDeathMap_Resized=MyResize(MyNearestFoodRow,MyNearestFoodCol,myDeathMap);
opAntMap_Resized=MyResize(MyNearestFoodRow,MyNearestFoodCol,opAntMap);
myAntMap_Resized=MyResize(MyNearestFoodRow,MyNearestFoodCol,myAntMap);
%Different cases are now possible
if (IsThisPlaceDangerous(myDeathMap_Resized,opAntMap_Resized)==false) %This place is not dangerous
if isempty(find(myAntMap_Resized >= 1))==1 %Case 1 (No danger - No team mates)
output=FindMyMove(3,3,MyNearestFoodRow,MyNearestFoodCol,mainMap);
output(3)=0; %No action
%Mark considering the amout of sugar
output(4)=min(100,LeftSugarValue*(foodMap(MyNearestFoodRow,MyNearestFoodCol)));
else %Case 2 (No danger - Yes team mates)
if (foodMap(MyNearestFoodRow,MyNearestFoodCol) > 1) %More than one sugar cube is available
output=FindMyMove(3,3,MyNearestFoodRow,MyNearestFoodCol,mainMap);
output(3)=0; %No action
%Mark considering the amout of sugar
output(4)=min(100,LeftSugarValue*(foodMap(MyNearestFoodRow,MyNearestFoodCol)));
else %Moving is not necessary
output=zeros(1,4); %No move, No action, No mark
end %End If Then Else Statement
end %End If Then Else Statement
else %This place is dangerous
%I need to move towards the second closest sugar cube
[MySecondNearestFoodRow, MySecondNearestFoodCol]=...
FindNearest(3,3,foodMap,PossibleMyNearestFoodRow,PossibleMyNearestFoodCol,2);
output=FindMyMove(3,3,MySecondNearestFoodRow,MySecondNearestFoodCol,mainMap);
output(3)=0; %No action
if isempty(find(myAntMap_Resized >= 1))==0 %Case 3 (Yes danger - Yes team mates)
output(4)=0; %No mark
else %Case 4 (Yes danger - No team mates)
output(4)=min(100,LeftSugarValue*(foodMap(MySecondNearestFoodRow,MySecondNearestFoodCol)));
end %End If Then Else Statement
end %End If Then Else Statement
end %End If Then Else Statement
end %End function FoodIsAvailable
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
function [NearestPosRow, NearestPosCol]=FindNearest(ref_row,ref_col,matrix,candidates_pos_row,candidates_pos_col,pos)
distances=zeros(1,length(candidates_pos_row)); %Preallocate array
if (length(candidates_pos_row)~=0)&(length(candidates_pos_col)~=0) %No strange situation detected
for k=1:length(candidates_pos_row)
%The distance is calculated and weighted thanks to the matrix
%values
distances(k)=matrix(candidates_pos_row(k),candidates_pos_col(k))*...
sqrt((candidates_pos_col(k)-ref_col)^2+(candidates_pos_row(k)-ref_row)^2);
end
if length(distances) > 1
[distances_val_sorted,distances_ind_sorted]=sort(distances,2,'ascend');
%When pos=1, the closest candidate is picked up
NearestPosRow=candidates_pos_row(distances_ind_sorted(pos));
NearestPosCol=candidates_pos_col(distances_ind_sorted(pos));
else
NearestPosRow=candidates_pos_row(1);
NearestPosCol=candidates_pos_col(1);
end %End If Then Else Statement
else %There is no candidate!
NearestPosRow=ref_row;
NearestPosCol=ref_col;
end %End If Then Else Statement
end %End function FindNearest
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
function ResizedMatrix=MyResize(ref_row,ref_col,matrix)
ResizedMatrix=matrix(max(1,ref_row-1):min(size(matrix,1),ref_row+1),...
max(1,ref_col-1):min(size(matrix,2),ref_col+1));
end %End function ResizedMatrix
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
function output=path_finder(row_1,col_1,row_2,col_2)
%Number of necessary unitary segments
segment_portion_num=abs(row_2-row_1)+abs(col_2-col_1);
for k=1:segment_portion_num
if k==1
output(k,:)=[row_1 col_1...
row_1+(col_1==col_2)*sign(row_2-row_1)...
col_1+sign(col_2-col_1)];
elseif rem(k,2)==0 %Even index -> row index to be modified
output(k,:)=[output(k-1,3) output(k-1,4) ...
output(k-1,3)+sign(row_2-output(k-1,3))...
output(k-1,4)+(output(k-1,3)==row_2)*sign(col_2-output(k-1,4))];
else %Odd index -> column index to be modified
output(k,:)=[output(k-1,3) output(k-1,4)...
output(k-1,3)+(output(k-1,4)==col_2)*sign(row_2-output(k-1,3))...
output(k-1,4)+sign(col_2-output(k-1,4))];
end
end
end %path_finder Function End
%--------------------------------------------------------------------------
end %End function ant |