-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagentAstar.m
More file actions
255 lines (213 loc) · 9.32 KB
/
agentAstar.m
File metadata and controls
255 lines (213 loc) · 9.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
%% Rules with A* search implemented in simulator D2DSS
% Responsible for make a good static policy in game soccer
% Based in using ifs for defining rules and making a search A* to find and include more rules
% function G = agentAstar(limit,M)
%%
classdef agentAstar < handle
properties
M % problem settings
coord %array of positions of players and ball
limit %limit max for A* search treee
end
methods
%Constructor of agent of type agentAstar
function G = agentAstar(limit,M)
G.M = M;
G.limit = limit;
G.coord = cell(1,M.Ta-1);
for i=1:(M.Ta-1)
G.coord{i} = floor(G.M.Nx*(1:i)/(i+1));
end
end
%Function responsible for choose next best state based on rules strategy
function sNext = nextState(G,s,a,Map)
switch a
case 1 %Move north
sNext = s+[0, -1];
case 2 %Move south
sNext = s+[0, 1];
case 3 %Move east
sNext = s+[1, 0];
case 4 %Move west
sNext = s+[-1, 0];
end
sNext = min(sNext,[G.M.Nx, G.M.Ny]);
sNext = max(sNext,[1, 1]);
if Map(sNext(1),sNext(2))
sNext = s;
end
end
%Function with an additional planning using search A* concepts
function [d a] = planning(G,pos,goal,Map,depth)
border = zeros(G.M.Nx,G.M.Ny);
analyzed = zeros(G.M.Nx,G.M.Ny);
h = inf(G.M.Nx,G.M.Ny);
f = zeros(G.M.Nx,G.M.Ny);
actions = zeros(G.M.Nx,G.M.Ny);
border(pos(1),pos(2)) = 1;
h(pos(1),pos(2)) = norm(pos-goal,1);
f(pos(1),pos(2)) = 0;
nSearch = 0;
while any(border(:))
nSearch = nSearch+1;
[a b] = min(h(:)+f(:));
if (a == inf)
d = inf;
a = 0;
return;
end
aux = find(h(:)+f(:)==a);
b = aux(randi(length(aux)));
[I,J] = ind2sub(size(h),b);
previousA = actions(I,J);
previousF = f(I,J);
previousH = h(I,J);
h(I,J) = inf;
border(I,J) = 0;
analyzed(I,J) = 1;
if previousH == 0 || nSearch > depth
d = previousF + previousH;
a = previousA;
return;
end
if previousA
for i=1:4
sNext = G.nextState([I J],i,Map);
if ~analyzed(sNext(1),sNext(2))
border(sNext(1),sNext(2)) = 1;
h(sNext(1),sNext(2)) = norm(sNext-goal,1);
f(sNext(1),sNext(2)) = previousF+1;
actions(sNext(1),sNext(2)) = previousA;
end
end
else
for i=1:4
sNext = G.nextState([I J],i,Map);
if ~analyzed(sNext(1),sNext(2))
border(sNext(1),sNext(2)) = 1;
h(sNext(1),sNext(2)) = norm(sNext-goal,1);
f(sNext(1),sNext(2)) = previousF+1;
actions(sNext(1),sNext(2)) = i;
end
end
end
end
d = inf;
a = randi(4);
end
%Pre planning for more a set of rules extended more actions than 4 basics of moves
function aa = prePlanning(G, dist, positions,aa,goal,Map,BallorPos)
[d a] = min(dist);
pos = positions(a,:);
undecided = true;
if d == 0
if ~BallorPos
aa(a)=13; %tackle for recovery ball
undecided = false;
else
positions(a,:) = 0;
if pos(2)<(ceil(G.M.Ny/2)-G.M.goalWidth) && max(abs(pos-[G.M.Nx (ceil(G.M.Ny/2)-G.M.goalWidth)])) <= G.M.ProbBallMoveDiagonal*G.M.KickRange
aa(a) = 10; %kick Southeast
undecided = false;
elseif pos(2)>(ceil(G.M.Ny/2)+G.M.goalWidth) && max(abs(pos-[G.M.Nx (ceil(G.M.Ny/2)+G.M.goalWidth)])) <= G.M.ProbBallMoveDiagonal*G.M.KickRange
aa(a) = 9; %kick Northeast
undecided = false;
elseif pos(2)>=(ceil(G.M.Ny/2)-G.M.goalWidth) && pos(2)<=(ceil(G.M.Ny/2)+G.M.goalWidth) && (G.M.Nx-pos(1)) <= G.M.ProbBallWithPlayer*G.M.KickRange || any(positions(1:G.M.Ta,1)>pos(1))
aa(a) = 7; %ball pass or kick East
undecided = false;
elseif pos(2)>=(ceil(G.M.Ny/2)-G.M.goalWidth) && pos(2)<=(ceil(G.M.Ny/2)+G.M.goalWidth)
goal = [min(pos(1)+G.limit,G.M.Nx) pos(2)]; %go East
elseif pos(2)<(ceil(G.M.Ny/2)-G.M.goalWidth)
goal = [min(pos(1)+G.limit,G.M.Nx) (ceil(G.M.Ny/2)-G.M.goalWidth)];%go Southeast
elseif pos(2)>(ceil(G.M.Ny/2)+G.M.goalWidth)
goal = [min(pos(1)+G.limit,G.M.Nx) (ceil(G.M.Ny/2)+G.M.goalWidth)];%go Northeast
end
end
end
%if not could decide, keep with next rules
if undecided
if goal(1) < pos(1)
intX = max(goal(1),pos(1)-G.limit):pos(1);
else
intX = pos(1):min(goal(1),pos(1)+G.limit);
end
if goal(2) < pos(2)
intY = max(goal(2),pos(2)-G.limit):pos(2);
else
intY = pos(2):min(goal(2),pos(2)+G.limit);
end
if (sum(sum(Map(intX,intY))) == 1) && ~BallorPos || (sum(sum(Map(intX,intY))) == 0) && BallorPos;
d = abs(goal-pos);
if d(1)>d(2)
if goal(1)>pos(1) %East
aa(a) = 3;
else %West
aa(a) = 4;
end
elseif d(1)<d(2)
if goal(2)>pos(2) %South
aa(a) = 2;
else %North
aa(a) = 1;
end
else
if rand > 0.5
if goal(1)>pos(1) %East
aa(a) = 3;
else %West
aa(a) = 4;
end
else
if goal(2)>pos(2) %South
aa(a) = 2;
else %North
aa(a) = 1;
end
end
end
else
[d act] = G.planning(pos,goal,Map,5); % A* call for find new rules for take in decision for actual position in state
if act > 0
aa(a) = act;
else
aa(a) = 4 + randi(7);
end
end
end
end
%Mapping state in action
function aa = action(G,s)
ball = s(1:2);
Map = zeros(G.M.Nx,G.M.Ny);
positions = reshape(s(5:end),2,(G.M.Ta+G.M.Tb));
positions = positions';
for i=1:(G.M.Ta+G.M.Tb)
Map(positions(i,1),positions(i,2)) = 1;
end
Map(ball(1),ball(2)) = 0;
aa = zeros(1,G.M.Ta);
dist = sum(abs(positions(1:G.M.Ta,:) - ones(G.M.Ta,1)*ball),2);
if any(sum(abs(positions((G.M.Ta+1):(G.M.Ta+G.M.Tb),:) - ones(G.M.Tb,1)*ball),2)==0) && any(dist==1)
for a=find(dist==1)
aa(a) = 13;
end
else
%Decides who will go to ball?
dist = sum(abs(positions(1:G.M.Ta,:) - ones(G.M.Ta,1)*ball),2);
dist(aa~=0) = inf;
aa = prePlanning(G, dist, positions,aa,ball,Map,true);
end
%Decides who will keep in each direction?
for j=randperm(G.M.Ta-1)
if sum(aa==0)==0
break;
end
ball(1) = G.coord{G.M.Ta-1}(j);
ball(2) = round((1-max(s(1)-ball(1),0)/G.M.Nx)*s(2)+max(s(1)-ball(1),0)/G.M.Nx*G.M.Ny/2);
dist = sum(abs(positions(1:G.M.Ta,:) - ones(G.M.Ta,1)*ball),2);
dist(aa~=0) = inf;
aa = prePlanning(G, dist, positions,aa,ball,Map,false); %call for a pre planning step for using all first static rules
end
end
end
end