From fcf466275ac9cb302d1c08e2f4357d6c6b6e2662 Mon Sep 17 00:00:00 2001 From: Thomas Forgione Date: Tue, 19 Mar 2019 16:44:34 +0100 Subject: [PATCH] Work --- .gitmodules | 3 ++ pytron | 1 + pytron_run/.gitignore | 1 + pytron_run/ai_manager/__init__.py | 4 ++ pytron_run/ai_manager/dummy/ai.py | 11 ++++ pytron_run/ai_manager/dummy2/ai.py | 8 +++ pytron_run/run.py | 83 ++++++++++++++++++++++++++++++ 7 files changed, 111 insertions(+) create mode 100644 .gitmodules create mode 160000 pytron create mode 100644 pytron_run/.gitignore create mode 100644 pytron_run/ai_manager/__init__.py create mode 100644 pytron_run/ai_manager/dummy/ai.py create mode 100644 pytron_run/ai_manager/dummy2/ai.py create mode 100755 pytron_run/run.py diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..918e4fb --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "pytron"] + path = pytron + url = git@gitea.tforgione.fr:tforgione/pytron diff --git a/pytron b/pytron new file mode 160000 index 0000000..fc14309 --- /dev/null +++ b/pytron @@ -0,0 +1 @@ +Subproject commit fc14309f01e6781c99d6c53c67049ce31d6b8167 diff --git a/pytron_run/.gitignore b/pytron_run/.gitignore new file mode 100644 index 0000000..0d20b64 --- /dev/null +++ b/pytron_run/.gitignore @@ -0,0 +1 @@ +*.pyc diff --git a/pytron_run/ai_manager/__init__.py b/pytron_run/ai_manager/__init__.py new file mode 100644 index 0000000..d2dd5e3 --- /dev/null +++ b/pytron_run/ai_manager/__init__.py @@ -0,0 +1,4 @@ +from os.path import dirname, basename, isfile +import glob +modules = glob.glob(dirname(__file__)+"/*/ai.py") +__all__ = ['.'.join(f.split('.')[:-1]) for f in modules if isfile(f)] diff --git a/pytron_run/ai_manager/dummy/ai.py b/pytron_run/ai_manager/dummy/ai.py new file mode 100644 index 0000000..c6eacd5 --- /dev/null +++ b/pytron_run/ai_manager/dummy/ai.py @@ -0,0 +1,11 @@ +"""An AI to try things""" + +from tron.player import Player, Direction + +class Ai(Player): + """" A basic AI""" + def __init__(self): + super(Ai, self).__init__() + + def action(self, game): + return Direction.RIGHT diff --git a/pytron_run/ai_manager/dummy2/ai.py b/pytron_run/ai_manager/dummy2/ai.py new file mode 100644 index 0000000..e15e769 --- /dev/null +++ b/pytron_run/ai_manager/dummy2/ai.py @@ -0,0 +1,8 @@ +from tron.player import Player, Direction + +class Ai(Player): + def __init__(self): + super(Ai, self).__init__() + + def action(self, game): + return Direction.LEFT diff --git a/pytron_run/run.py b/pytron_run/run.py new file mode 100755 index 0000000..92a05c0 --- /dev/null +++ b/pytron_run/run.py @@ -0,0 +1,83 @@ +#!/usr/bin/env python3 + +import sys +sys.path.append('pytron') + +from tron.map import Map +from tron.game import Game, PositionPlayer, Winner +from tron.player import Direction, ConstantPlayer + +from importlib import import_module + +import ai_manager + +# Find all the AIs +class AiClass: + def __init__(self, name, builder): + self.name = name + self.builder = builder + +ais = [] +for real_ai in ai_manager.__all__: + ai_name = real_ai.split('/')[-2] + ai_module = import_module('.'.join(real_ai.split('/')[-3:])) + ai_class = getattr(ai_module, "Ai") + ais.append(AiClass(ai_name, ai_class)) + + +# This script shows how to create a game with AI, that will run automatically. +# It is made to be fast and not to be used by humans. It especially doesn't +# display and window and doesn't listen to any keystrokes. + +width = 40 +height = 40 + +def run_battle(ai1, ai2): + ai1_victories = 0 + ai2_victories = 0 + + for _ in range(50): + initial_position_one = [0, 0] + initial_position_two = [width - 1, height - 1] + + game = Game(width, height, [ + PositionPlayer(1, ai1.builder(), initial_position_one), + PositionPlayer(2, ai2.builder(), initial_position_two), + ]) + game.main_loop() + + if game.winner == Winner.PLAYER_ONE: + ai1_victories += 1 + elif game.winner == Winner.PLAYER_TWO: + ai2_victories += 1 + + # Inverse positions and replay to be symmetrical + game = Game(width, height, [ + PositionPlayer(1, ai1.builder(), initial_position_two), + PositionPlayer(2, ai2.builder(), initial_position_one), + ]) + game.main_loop() + + if game.winner == Winner.PLAYER_ONE: + ai2_victories += 1 + elif game.winner == Winner.PLAYER_TWO: + 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. + 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) + +if __name__ == '__main__': + main() +