-
Notifications
You must be signed in to change notification settings - Fork 0
/
Heuristics.py
102 lines (90 loc) · 3.17 KB
/
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
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
from typing import Sized
from MancalaBoard import *
def beans_difference(board: BoardState):
return (board.player_stores[0]-board.player_stores[1])
def beans_hoarded(board: BoardState):
player1_beans = 0
player2_beans = 0
for i in range(board.pockets):
player1_beans += board.player_side[0][i]
player2_beans += board.player_side[1][i]
return (player1_beans-player2_beans)
def repeat_turns(max_side):
max_chain = 0
side_copy = max_side[:]
no_pockets = len(side_copy)
for i in range(no_pockets):
inc = (side_copy[i])//(2*no_pockets+1)
res = (side_copy[i])%(2*no_pockets+1)
if res==no_pockets-i:
side_copy[i] = 0
for j in range(no_pockets):
if j<=i:
side_copy[j] += inc
else:
side_copy[j] += inc+1
max_chain = max(max_chain,repeat_turns(side_copy)+1)
return max_chain
def maximize_capture(max_side,min_side):
max_cap = 0
side_copy1 = max_side[:]
side_copy2 = min_side[:]
no_pockets = len(side_copy1)
for i in range(no_pockets):
beans_amount = side_copy1[i]
if beans_amount!=0:
j = i+beans_amount
if j<no_pockets:
if side_copy1[j]==0:
max_cap = max(max_cap,side_copy2[no_pockets-j-1]+1)
else:
j -= (2*no_pockets+1)
if j>=0 and j<=i:
if j==i or side_copy1[j]==0:
max_cap = max(max_cap,side_copy2[no_pockets-j-1]+2)
return max_cap
def heuristic1(board: BoardState):
w = 1
return w*beans_difference(board)
def heurisric2(board: BoardState):
w1 = 1
w2 = 0.5
return w1*beans_difference(board) + w2*beans_hoarded(board)
def heuristic3(board: BoardState):
w1 = 1
w2 = 0.5
w3 = 0.5
return w1*beans_difference(board) + w2*beans_hoarded(board) + w3*repeat_turns(board.player_side[0])
def heuristic4(board: BoardState):
w1 = 1
w2 = 0.5
w3 = 0.7
w4 = -0.3
return w1*beans_difference(board) + w2*beans_hoarded(board) + w3*repeat_turns(board.player_side[0]) \
+ w4*repeat_turns(board.player_side[1])
def heuristic5(board: BoardState):
w1 = 1
w2 = 0.5
w3 = 0.7
w4 = -0.3
w5 = 0.7
return w1*beans_difference(board) + w2*beans_hoarded(board) + w3*repeat_turns(board.player_side[0]) \
+ w4*repeat_turns(board.player_side[1]) + w5*maximize_capture(board.player_side[0],board.player_side[1])
def heuristic6(board: BoardState):
w1 = 1
w2 = 0.5
w3 = 0.7
w4 = -0.3
w5 = 0.7
w6 = -0.3
return w1*beans_difference(board) + w2*beans_hoarded(board) + w3*repeat_turns(board.player_side[0]) \
+ w4*repeat_turns(board.player_side[1]) + w5*maximize_capture(board.player_side[0],board.player_side[1]) \
+ w6*maximize_capture(board.player_side[1],board.player_side[0])
heu_dict = {
"heuristic1": heuristic1,
"heuristic2": heurisric2,
"heuristic3": heuristic3,
"heuristic4": heuristic4,
"heuristic5": heuristic5,
"heuristic6": heuristic6
}