Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions analysis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from halma import Halma
from time import perf_counter

def minmax_vs_alpha_beta(file_name, depth):
halma = Halma()

with open(file_name, 'w') as file:
file.write("Iteration,time_minmax,time_alpha,depth\n")
for i in range(10):
halma.generate_random_board()
start_time = perf_counter()
a = halma.player1.minmax(([], halma.board), depth, True, halma.player1.evaluate)
end_time = perf_counter()
execution_time_minmax = end_time - start_time
halma.generate_random_board()
start_time = perf_counter()
alpha = halma.player1.minimax_alpha_beta(([], halma.board), depth, True, halma.player1.evaluate, float('-inf'), float('inf'))
end_time = perf_counter()
execution_time_alpha_beta = end_time - start_time
file.write(f"{i+1},{execution_time_minmax},{execution_time_alpha_beta},{depth}\n")
def number_of_nodes(depth):
minmax_nodes = []
alpha_nodes = []
halma = Halma()
for i in range(10):
halma.generate_random_board()
a = halma.player1.minmax(([], halma.board), depth, True, halma.player1.evaluate)
minmax_nodes.append(halma.player1.nodes)
halma.player1.nodes = 0
alpha = halma.player1.minimax_alpha_beta(([], halma.board), depth, True, halma.player1.evaluate, float('-inf'),
float('inf'))
alpha_nodes.append(halma.player1.nodes)
halma.player1.nodes = 0
# print(minmax_nodes)
# print(alpha_nodes)
print(sum(minmax_nodes) / len(minmax_nodes))
print(sum(alpha_nodes) / len(alpha_nodes))

def analysis_heurystic(file_name):
halma = Halma()
with open(file_name, 'w') as file:
file.write("Iteration,winner,depth\n")
for i in range(10):
# halma.read_board_from_file('halmaWinner.txt')
halma.generate_random_board()
halma.print_board()
winner = halma.game_alpha_beta(2, halma.player1.startegy_all, halma.player1.strategy_go_in_group)
print(i+1)
file.write(f"{i + 1},{winner}\n")







if __name__ == "__main__":
# minmax_vs_alpha_beta('minmax_vs_alpha_3.csv', 3)
# number_of_nodes(2)
analysis_heurystic('eall_vs_egroup_2v2_random.csv')
82 changes: 35 additions & 47 deletions evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,66 +2,54 @@ def manhatan(point_x, point_y, x, y):
return abs(point_x - x) + abs(point_y - y)


def manhatan_2(point_x, point_y, x, y, board, base, player_type):
def random_empty_base(board, base, x, y, player_type):
if player_type == 1:
empty_base = all(board[x][y] == 0 or board[x][y] == 2 for x, y in base)
pos_x = 15
pos_y = 15
else:
empty_base = all(board[x][y] == 0 or board[x][y] == 1 for x, y in base)
if empty_base:
# print('empty',player_type)
return manhatan(point_x, point_y, x, y)
else:
if board[x][y] in base:
return abs(point_x - x) + abs(point_y - y)
else:
return abs(point_x - x) + abs(point_y - y) * 5
pos_y = 0
pos_x = 0


def if_in_base(player_type, base, board, x, y):
if (x, y) in base:
if player_type == 1:
return -100
else:
return 100
return 0
for field in base:
x_1, x_2 = field
if board[x_1][x_2] != 0:
pos_x = x_1
pos_y = x_2
break
# print(pos_x, pos_y)
return manhatan(pos_x, pos_y, x, y)


def leave_base(player_type, state, base):
if state[0]:
if state[0][0][0] in base:
if state[0][0][1] not in base:
if player_type == 1:
return 4
else:
return -4
return 0
def rival_base(base, x, y):
if (x, y) in base:
return 5
return 0


def rival_base(player_type, board, base, x, y):
def leave_base1(player_type, x, y, base):
if (x, y) in base:
if player_type == 1:
return 5
d = (abs(-int(x) - int(y) + 6)) / (2 ** 0.5)
return d * 10
else:
return -5
d = (abs(-int(x) - int(y) + 24)) / (2 ** 0.5)
return d * 10
return 0


def go_in_group(player_type, board, base, x, y):



def go_in_group1(player_type, board, x, y, base, bonus):
value = 0
if (x, y) not in base:
for i in range(-1, 2):
for j in range(-1, 2):
if board[i][j] == player_type:
if player_type == 1:
value += 1
else:
value -= 1
else:
if player_type == 1:
value -= 1
else:
value += 1
# else:
# value = 4
if (x, y) in base:
# print('tal')
return 0
for i in range(-1, 2):
for j in range(-1, 2):
x_new = x + i
y_new = y + j
if 0 < x_new < 16 and 0 < y_new < 16:
if board[1][x_new][y_new] == player_type and (x_new, y_new) not in base:
value += bonus
return value
97 changes: 34 additions & 63 deletions halma.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ class Halma:
def __init__(self):

self.board = []
self.player2 = Player(2)
self.player1 = Player(1)
self.player2 = Player()
self.player1 = Player()
self.moves = set()
self.base1 = {(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (1, 0), (1, 1), (1, 2), (1, 3), (1, 4), (2, 0), (2, 1),
(2, 2), (2, 3), (3, 0), (3, 1), (3, 2), (4, 0), (4, 1)}
Expand All @@ -19,6 +19,7 @@ def __init__(self):
(13, 15), (13, 14), (13, 13), (13, 12), (12, 15), (12, 14), (12, 13), (11, 15), (11, 14)}

def read_board_from_file(self, file):
self.board = []
try:
with open(file, 'r') as f:
lines = f.readlines()
Expand All @@ -27,7 +28,6 @@ def read_board_from_file(self, file):
self.board.append(row)
except FileNotFoundError:
print('nie ma pliku')
# print(type(self.board[0][0]))

def check_winner(self):
win2 = all(self.board[x][y] == 2 for x, y in self.base1)
Expand All @@ -51,6 +51,7 @@ def make_move1(self, move):
x, y = pos_old
x_new, y_new = pos_new
temp = self.board[x][y]
# print(temp, 'ruch')
self.board[x][y] = self.board[x_new][y_new]
self.board[x_new][y_new] = temp

Expand All @@ -74,86 +75,56 @@ def generate_random_board(self):
if self.board[x][y] == 0:
self.board[x][y] = 2
number_of_2 += 1
self.print_board()

def game(self):
while self.check_winner() is None:
move_2 = self.player2.minmax(([], self.board), 1, False,self.player2.evaluate)
move_2 = self.player2.minmax(([], self.board), 1, True, self.player2.strategy_offensive)
self.make_move1(move_2)
print('ruch 2')
move_1 = self.player1.minmax(([], self.board), 1, True, self.player1.evaluate)
move_1 = self.player1.minmax(([], self.board), 1, False, self.player1.strategy_offensive)
self.make_move1(move_1)
print('ruch 1')
self.print_board()
# move_2 = self.player2.minmax(([], self.board), 1, False)
# self.make_move1(move_2)
# print('ruch 2')
print(self.check_winner())

def game_alpha_beta(self, depth):
rounds = 0;
def game_alpha_beta(self, depth, h1, h2):
rounds = 0
while self.check_winner() is None:
# move_2 = self.player2.minimax_alpha_beta(([], self.board), depth, False, self.player2.evaluate1,
# float('-inf'), float('inf'))
# self.make_move1(move_2)
move_2 = self.player2.minimax_alpha_beta(([], self.board), depth, False, h2, float('-inf'), float('inf'))
self.make_move1(move_2)
# print('ruch 2')
# self.print_board()
move_1 = self.player1.minimax_alpha_beta(([], self.board), depth, True, self.player1.evaluate3, float('-inf'), float('inf'))
move_1 = self.player1.minimax_alpha_beta(([], self.board), depth, True, h1, float('-inf'), float('inf'))
self.make_move1(move_1)
print('ruch 1')
self.print_board()
move_2 = self.player2.minimax_alpha_beta(([], self.board), depth, False, self.player2.evaluate1, float('-inf'), float('inf'))
self.make_move1(move_2)
print('ruch 2')
self.print_board()
# move_2 = self.player2.minmax(([], self.board), 1, False)
# print('ruch 1')
# self.print_board()
# move_2 = self.player2.minimax_alpha_beta(([], self.board), depth, False, h2, float('-inf'), float('inf'))
# self.make_move1(move_2)
# print('ruch 2')
# self.print_board()
rounds += 1
print(rounds)
print(self.check_winner())
print(rounds)

def test(self):
self.player1.minimax_alpha_beta(([], self.board), 4, True, self.player1.evaluate, float('-inf'), float('inf'))
rounds += 1
if rounds > 300:
points_1 = self.player1.count_pawns_in_base(self.board, self.player1.base2, 1)
points_2 = self.player1.count_pawns_in_base(self.board, self.player1.base1, 2)
if points_1 > points_2:
return 1
elif points_2 > points_1:
return 2
else:
return 0

# print(rounds)
# print(self.check_winner())
# print(rounds)
return self.check_winner()


if __name__ == "__main__":
halma = Halma()
# halma.read_board_from_file('halmaWinner.txt')
halma.generate_random_board()
# halma.possile_moves(4, 0, 0)
# # print(halma.moves)
# # print(halma.check_winner())
# # halma.game()
# halma.all_possible_moves(1)
# halma.print_board()# print(len(halma.base1))
# # halma.make_move((4,0), (5,0))
# print('nowa')
# halma.print_board()
# halma.player.make_move(halma.board, (4,0), (5,0))
# halma.player.all_possible_moves(halma.board, 1)
# a = halma.player1.minmax(([], halma.board), 2, True)
# print(a)
# alpha = halma.player1.minimax_alpha_beta(([], halma.board),2, True, float('-inf'), float('inf'))
# print(alpha)

start_time = time.time()
a = halma.player1.minmax(([], halma.board), 2, True, halma.player1.evaluate)
end_time = time.time()
execution_time_minmax = end_time - start_time
print(a)
halma.generate_random_board()

start_time = time.time()
alpha = halma.player1.minimax_alpha_beta(([], halma.board), 2, True, halma.player1.evaluate, float('-inf'), float('inf'))
end_time = time.time()
execution_time_alpha_beta = end_time - start_time
print(alpha)
print("Czas wykonania algorytmu Minimax:", execution_time_minmax)
print("Czas wykonania algorytmu Minimax z cięciami alfa-beta:", execution_time_alpha_beta)
# halma.game_alpha_beta(2)
# halma.player1.minimax_alpha_beta(([],halma.board), 2,True,halma.player1.evaluate, float('-inf'), float('inf'))
halma.read_board_from_file('halmaWinner.txt')
# halma.generate_random_board()
# halma.player1.test(([],halma.board), halma.player1.evaluate)

a = halma.game_alpha_beta(2, halma.player1.strategy_ofensivev2, halma.player1.strategy_go_in_group)
print(a)

4 changes: 2 additions & 2 deletions halma1.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0
1 1 1 1 0 0 2 0 0 0 0 0 0 0 0 0
1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0
1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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