Progressing

This commit is contained in:
2019-03-19 17:00:43 +01:00
parent fcf466275a
commit f3393ef7f5
6 changed files with 68 additions and 69 deletions
+18 -12
View File
@@ -1,10 +1,13 @@
#!/usr/bin/env python3
import sys
import json
import random
random.seed()
sys.path.append('pytron')
from tron.map import Map
from tron.game import Game, PositionPlayer, Winner
from tron.game import Game, PositionPlayer
from tron.player import Direction, ConstantPlayer
from importlib import import_module
@@ -37,8 +40,8 @@ def run_battle(ai1, ai2):
ai2_victories = 0
for _ in range(50):
initial_position_one = [0, 0]
initial_position_two = [width - 1, height - 1]
initial_position_one = [random.randint(0, width - 1), random.randint(0, height - 1)]
initial_position_two = [random.randint(0, width - 1), random.randint(0, height - 1)]
game = Game(width, height, [
PositionPlayer(1, ai1.builder(), initial_position_one),
@@ -46,9 +49,9 @@ def run_battle(ai1, ai2):
])
game.main_loop()
if game.winner == Winner.PLAYER_ONE:
if game.winner == 1:
ai1_victories += 1
elif game.winner == Winner.PLAYER_TWO:
elif game.winner == 2:
ai2_victories += 1
# Inverse positions and replay to be symmetrical
@@ -58,25 +61,28 @@ def run_battle(ai1, ai2):
])
game.main_loop()
if game.winner == Winner.PLAYER_ONE:
if game.winner == 1:
ai2_victories += 1
elif game.winner == Winner.PLAYER_TWO:
elif game.winner == 2:
ai1_victories += 1
return (ai1_victories, ai2_victories)
def main():
# Prepare the size for the game.
# Those values may be good if you want to play, they might not be so good
# to train your AI. Decreasing them will make the learning faster.
dictionnary = {"ais": list(map(lambda x: x.name, ais)), "battles": {}}
for (id1, ai1) in enumerate(ais):
for (id2, ai2) in enumerate(ais):
if id1 >= id2:
continue
print("Battling {} vs {}".format(ai1.name, ai2.name))
score = run_battle(ai1, ai2)
print(score)
(score1, score2) = run_battle(ai1, ai2)
dictionnary["battles"][ai1.name + "/" + ai2.name] = score1 / (score1 + score2)
with open("assets/data.json", "w") as f:
f.write(json.dumps(dictionnary))
if __name__ == '__main__':
main()