-
Notifications
You must be signed in to change notification settings - Fork 0
/
heuristic_GG.py
368 lines (289 loc) · 13.8 KB
/
heuristic_GG.py
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
import numpy as np
from utils import *
import math
from typing import Tuple
from typing import List, Tuple
# ------------------------------ CONSTANT ------------------------------
NEAR_MONSTERS_RANGE = 4 # range to consider a monster dangerous
ETA_NEAR_MONSTERS_TARGET = 0.5 # weight of near monsters
DISTANCE_MIN = 1 # minimum distance
DISTANCE_MAX = 18 # maximum distance
NEAR_MONSTER_MIN = 1 # minimum number of monster
BRAVE_PLAYER = 0.6 # brave of the player (value between 0 and 1)
ANGLES = [(1,33),(18,33),(1,50),(18,50)] # position of angles in the map
TRAP_RANGE=math.pi/4 # range to consider a trap
TRAP_DISTANCE=4 # distance to consider monsters
# ------------------------------ ---------- ------------------------------
class MonsterInfo:
"""
Represents information about a monster.
Attributes:
position (Tuple[int, int]): The position of the monster.
distance (int): The distance of the monster from a certain point.
near_monsters (List[Tuple[int, int]]): A list of positions of nearby monsters.
"""
def __init__(self, position: Tuple[int, int], distance: int, near_monsters: List[Tuple[int, int]]):
self._position = position
self._distance = distance
self._near_monsters = near_monsters
@property
def position(self) -> Tuple[int, int]:
return self._position
@position.setter
def position(self, value: Tuple[int, int]):
self._position = value
@property
def distance(self) -> int:
return self._distance
@distance.setter
def distance(self, value: int):
self._distance = value
@property
def near_monsters(self) -> List[Tuple[int, int]]:
return self._near_monsters
@near_monsters.setter
def near_monsters(self, value: List[Tuple[int, int]]):
self._near_monsters = value
# heuristic function that return a score for the move
def heuristic_gg(game_map: np.ndarray, move: Tuple[int, int], end_target: Tuple[int, int], hp_rate: int, weapon_in_hand: bool) -> float:
# initial target is the end target
target = end_target
# get the position of the monsters, the player and the weapons
monsters_position: List[Tuple[int, int]]= get_monster_location(game_map)
player_position: Tuple[int, int] = get_player_location(game_map)
weapons: List[Tuple[int, int]] = get_weapon_location(game_map)
# if the player is in the end target and there are monsters, the score is infinite
if move == end_target and len(monsters_position)!=0:
return math.inf
# list with information about the monsters
info_monsters: List[MonsterInfo] = Heuristic_utils.get_info_monsters(player_position, monsters_position)
# if the player is not brave and there are monsters, the player will escape
if BRAVE_PLAYER <= (1-hp_rate) and len(monsters_position) > 0:
# check if the player is in a trap
if Heuristic_utils.is_trap(player_position,info_monsters):
return Heuristic_utils.escape_trap(move,info_monsters)
# check if there is a monster near the player
nearest_monster = Heuristic_utils.nearest_monster(info_monsters)
if nearest_monster.distance < 3:
return Heuristic_utils.escape_near_monster(move, nearest_monster, info_monsters)
# default strategy to escape from the monsters
return Heuristic_utils.default_escape(monsters_position, player_position, move)
# if the player brave and there are monsters, the player will fight
if len(monsters_position) > 0:
# the player go to the weapon
if not weapon_in_hand and len(weapons) > 0:
target = Heuristic_utils.best_weapon(player_position, weapons)
# the player fight the monsters
else:
# select the best monster target to fight
target = Heuristic_utils.best_monster_to_fight(info_monsters)
# the player go to the target
return Heuristic_utils.score(move,target,info_monsters)
# ------------------------------ Heurstic UTILS ------------------------------
class Heuristic_utils:
@staticmethod
def nearest_monster(info_monsters: [MonsterInfo]) -> MonsterInfo:
"""
Finds the nearest monster from a list of monster information.
Args:
info_monsters (list): A list of MonsterInfo objects representing the information of each monster.
Returns:
MonsterInfo: The MonsterInfo object representing the nearest monster.
"""
min = math.inf
near_monster = None
for info_monster in info_monsters:
if info_monster.distance < min:
min = info_monster.distance
near_monster = info_monster
return near_monster
@staticmethod
def normalize(value: int, min_val: int, max_val: int) -> float:
"""
Normalize a value between a minimum and maximum value.
Args:
value (int): The value to be normalized.
min_val (int): The minimum value of the range.
max_val (int): The maximum value of the range.
Returns:
float: The normalized value.
"""
# Controllo se il denominatore è zero
if max_val - min_val == 0:
return 0.0
return (value - min_val) / (max_val - min_val)
@staticmethod
def score(move:Tuple[int, int],target:Tuple[int, int], info_monsters:[MonsterInfo]) -> float:
"""
Calculates the score for a given move based on the target position and information about monsters.
Parameters:
move (Tuple[int, int]): The coordinates of the move.
target (Tuple[int, int]): The coordinates of the target position.
info_monsters ([MonsterInfo]): A list of MonsterInfo objects containing information about the monsters.
Returns:
float: The calculated score for the move.
"""
if len(info_monsters)==0:
return euclidean_distance(move,target)
sum=1
count=0
for info_monster in info_monsters:
if info_monster.position!=target:
sum+=(DISTANCE_MAX-euclidean_distance(move,info_monster.position))
count+=1
return math.ceil(euclidean_distance(move,target)) + (Heuristic_utils.normalize(sum,0,(DISTANCE_MAX-1)*count))
@staticmethod
def near_monsters(cell: Tuple[int, int], monsters_position: [Tuple[int, int]]):
"""
Counts the number of monsters within the given range of vision.
Parameters:
game_map (np.ndarray): The game map.
cell (Tuple[int, int]): The current cell coordinates.
monsters_position ([Tuple[int, int]]): List of monster coordinates.
Returns:
int: The number of monsters within the range of vision.
"""
num_monsters = 0
for monster in monsters_position:
if euclidean_distance(cell, monster) <= NEAR_MONSTERS_RANGE:
num_monsters += 1
return num_monsters
@staticmethod
def is_trap(player_position: Tuple[int, int], info_monsters: List[MonsterInfo]) -> bool:
"""
Determines if the current position is a trap based on the given information about the monsters.
Args:
player_position (Tuple[int, int]): The current position of the player.
info_monsters (List[MonsterInfo]): The information about the monsters.
Returns:
bool: True if the current position is a trap, False otherwise.
"""
#remove from info_mosters the monsters that are too far distance 4 from the player
info_monsters_reduced=[]
for monster in info_monsters:
if monster.distance <= TRAP_DISTANCE:
info_monsters_reduced.append(monster)
# check if there are at least 2 monsters
if len(info_monsters_reduced) < 2:
return False
# calculate the angle between the player and the monsters
angles = [math.atan2(monster.position[1] - player_position[1], monster.position[0] - player_position[0]) for monster in info_monsters_reduced]
# sort the angles
angles.sort()
# check the interval between the angles
count = 0
for i in range(1, len(angles)):
if abs(angles[i] - angles[i - 1]) > TRAP_RANGE:
count += 1
if count >= 2:
# return trap
return True
# check the interval between the first and the last angle
if abs(angles[0] + 2 * math.pi - angles[-1]) > TRAP_RANGE:
count += 1
# return True if there are at least 2 intervals
return count >= 2
@staticmethod
def get_info_monsters(player_position:Tuple[int,int],monsters_position:List[Tuple[int,int]]) -> List[MonsterInfo]:
"""
Get information about monsters.
Args:
player_position (Tuple[int, int]): The position of the player.
monsters_position (List[Tuple[int, int]]): The positions of the monsters.
Returns:
List[MonsterInfo]: A list of MonsterInfo objects containing information about each monster.
"""
info_monsters: List[MonsterInfo] = []
for monster in monsters_position:
info_monsters.append(
MonsterInfo(monster, euclidean_distance(player_position, monster), Heuristic_utils.near_monsters(monster,monsters_position))
)
return info_monsters
@staticmethod
def best_weapon(player_position:Tuple[int, int], weapons:[Tuple[int, int]]):
"""
Finds the best weapon for the player based on the player's position and the available weapons.
Args:
player_position (Tuple[int, int]): The position of the player.
weapons ([Tuple[int, int]]): A list of weapon positions.
Returns:
Tuple[int, int]: The position of the best weapon.
"""
min = math.inf
best_weapon = weapons[0]
for weapon in weapons:
distance = euclidean_distance(player_position, weapon)
if distance < min:
min = distance
best_weapon = weapon
return best_weapon
@staticmethod
def escape_trap(move:Tuple[int,int], info_monsters:List[MonsterInfo]):
"""
Calculates the heuristic score for escaping a trap.
Args:
move (Tuple[int,int]): The move to evaluate.
info_monsters (List[MonsterInfo]): List of information about the monsters.
Returns:
float: The heuristic score for the move.
"""
if move in ANGLES:
return math.inf
max_monster_distance=0
for angle in ANGLES:
new_distance=0
for monster in info_monsters:
new_distance+=euclidean_distance(angle, monster.position)
if new_distance > max_monster_distance:
max_monster_distance=new_distance
target = angle
return Heuristic_utils.score(move,target,info_monsters)
@staticmethod
def escape_near_monster(move:Tuple[int,int], near_monster:Tuple[int,int], info_monsters:List[MonsterInfo]):
"""
Calculates the escape score for a given move based on the distance from the nearest monster and the positions of other monsters.
Args:
move (Tuple[int, int]): The move to evaluate.
near_monster (Tuple[int, int]): The position of the nearest monster.
info_monsters (List[MonsterInfo]): A list of MonsterInfo objects containing information about other monsters.
Returns:
float: The escape score for the move.
"""
if move in ANGLES:
return math.inf
sum=1
for info_monster in info_monsters:
sum+=(DISTANCE_MAX-euclidean_distance(move,info_monster.position))
return -math.ceil(euclidean_distance(move,near_monster.position)) + (Heuristic_utils.normalize(sum,0,(DISTANCE_MAX-1)*len(info_monsters)))
@staticmethod
def default_escape(monsters_position:List[Tuple[int,int]], player_position:Tuple[int,int], move:Tuple[int,int]):
"""
Calculates the escape score for a given move based on the positions of monsters and the player.
Parameters:
monsters_position (List[Tuple[int,int]]): A list of tuples representing the positions of monsters.
player_position (Tuple[int,int]): A tuple representing the position of the player.
move (Tuple[int,int]): A tuple representing the move to be evaluated.
Returns:
int: The escape score for the move.
"""
sum = 0
for monster in monsters_position:
sum -= euclidean_distance(move, monster) * ((DISTANCE_MAX - euclidean_distance(player_position, monster)))
return sum
@staticmethod
def best_monster_to_fight(info_monsters:List[MonsterInfo]):
"""
Finds the best monster to fight based on a heuristic calculation.
Args:
info_monsters (List[MonsterInfo]): A list of MonsterInfo objects containing information about each monster.
Returns:
Tuple[int, int]: The position of the best monster to fight.
"""
NEAR_MONSTER_MAX = len(info_monsters)
min = math.inf
for monster in info_monsters:
weight = (Heuristic_utils.normalize(monster.near_monsters, NEAR_MONSTER_MIN, NEAR_MONSTER_MAX) * ETA_NEAR_MONSTERS_TARGET) + Heuristic_utils.normalize(monster.distance, DISTANCE_MIN, DISTANCE_MAX)
if weight < min:
min = weight
target = monster.position
return target