-
Notifications
You must be signed in to change notification settings - Fork 0
/
AI_Heuristics.py
61 lines (54 loc) · 2.1 KB
/
AI_Heuristics.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
import numpy as np, math
from copy import deepcopy
smoothWeight = 0.5
monoWeight = 1.0
emptyWeight = 2.7
def heuristics(grid, num_empty):
'''
This function scores the grid based on the algorithm implemented
so that the maximize function of AI_Minimax can decide which branch
to follow.
'''# TODO: Implement your heuristics here.
# You are more than welcome to implement multiple heuristics
grid = np.array(grid)
#checking that the largest tile is bottom right
max_val = 0
max_r, max_c = 0, 0
large_tile_penalty = 0
for i in range(len(grid)):
for j in range(len(grid[i])):
if grid[i][j] >= max_val:
max_val = grid[i][j]
max_r = i
max_c = j
large_tile_penalty = (6 - max_r - max_c)# + math.log(max_val)/15
# checking monotonicity
# check that it's increasing from left to right
monolr = 0
for i in range(len(grid)):
for j in range(len(grid[i]) - 1):
if(grid[i][j] > grid[i][j + 1]):
monolr -= 1# + (math.log(grid[i][j] + 1) + math.log(grid[i][j + 1] + 1))/1000
elif(grid[i][j] < grid[i][j + 1]):
monolr += 1# + (math.log(grid[i][j] + 1) + math.log(grid[i][j + 1] + 1))/1000
# check that it's increasing from top to bottom
monotd = 0
for i in range(len(grid)):
for j in range(len(grid[i]) - 1):
if(grid[j][i] > grid[j + 1][i]):
monotd -= 1# + (math.log(grid[j][i] + 1) + math.log(grid[j + 1][i] + 1))/1000
elif(grid[j][i] < grid[j + 1][i]):
monotd += 1# + (math.log(grid[j][i] + 1) + math.log(grid[j + 1][i] + 1))/1000
# check smoothness
smoothness = 0
# check that it's smooth from left to right
for i in range(len(grid)):
for j in range(len(grid[i]) - 1):
if(grid[i][j] == grid[i][j + 1]):
smoothness += 1
# check that it's smooth from top to bottom
for i in range(len(grid)):
for j in range(len(grid[i]) - 1):
if(grid[j][i] == grid[j + 1][i]):
smoothness += 1
return (monoWeight * (monolr + monotd)) - (max(5, math.log(max_val,3)) * large_tile_penalty) + (smoothWeight * smoothness) + (emptyWeight * num_empty)