From f3393ef7f56a2b270646cc1e0e47b9b31fcc2916 Mon Sep 17 00:00:00 2001 From: Thomas Forgione Date: Tue, 19 Mar 2019 17:00:43 +0100 Subject: [PATCH] Progressing --- .gitignore | 1 + assets/test.json | 8 ----- pytron_run/run.py | 30 +++++++++------- src/lib.rs | 72 +++++++++++++++++++-------------------- src/main.rs | 2 +- templates/index.html.tera | 24 ++++++------- 6 files changed, 68 insertions(+), 69 deletions(-) delete mode 100644 assets/test.json diff --git a/.gitignore b/.gitignore index 6936990..28f5928 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /target **/*.rs.bk Cargo.lock +assets/ diff --git a/assets/test.json b/assets/test.json deleted file mode 100644 index 81b0741..0000000 --- a/assets/test.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "ias": ["ia1", "ia2", "ia3"], - "battles": { - "ia1/ia2": 0.3, - "ia1/ia3": 0.7, - "ia2/ia3": 0.5 - } -} diff --git a/pytron_run/run.py b/pytron_run/run.py index 92a05c0..9557e9d 100755 --- a/pytron_run/run.py +++ b/pytron_run/run.py @@ -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() diff --git a/src/lib.rs b/src/lib.rs index 9df9a19..57d247f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,19 +8,19 @@ use std::io::Read; #[derive(Serialize, Deserialize)] /// The data contained from the json file generated by python. pub struct JsonData { - /// The names of the IAs. - pub ias: Vec, + /// The names of the AIs. + pub ais: Vec, /// The results of the battles. pub battles: HashMap, } impl JsonData { - /// Creates some test data. - pub fn test() -> JsonData { + /// Creates some data data. + pub fn data() -> JsonData { // Open the file - let mut file = File::open("./assets/test.json") + let mut file = File::open("./assets/data.json") .expect("Couldn't open file"); let mut content = String::new(); @@ -33,46 +33,46 @@ impl JsonData { } #[derive(Serialize, Deserialize, Clone)] -/// An IA. -pub struct Ia { - /// The name of the ia. +/// An AI. +pub struct Ai { + /// The name of the ai. pub name: String, - /// The number of battles won by the ia. + /// The number of battles won by the ai. pub victory_count: usize, - /// The number of battles lost by the ia. + /// The number of battles lost by the ai. pub defeat_count: usize, } #[derive(Serialize, Deserialize)] pub struct Data { - /// The IAs - pub ias: Vec, + /// The AIs + pub ais: Vec, /// The battles. /// /// This hashmap is symmetric. pub battles: HashMap, - /// The ias sorted by victory count. - pub sorted_ias: Vec, + /// The ais sorted by victory count. + pub sorted_ais: Vec, } impl Data { - /// Creates some test data. - pub fn test() -> Data { - JsonData::test().into() + /// Creates some data data. + pub fn data() -> Data { + JsonData::data().into() } } impl From for Data { fn from(d: JsonData) -> Data { - let mut ias = vec![]; + let mut ais = vec![]; - for ia in d.ias { - ias.push(Ia { - name: ia, + for ai in d.ais { + ais.push(Ai { + name: ai, victory_count: 0, defeat_count: 0, }) @@ -82,42 +82,42 @@ impl From for Data { for (key, val) in d.battles { - let playing_ias = key.split("/") + let playing_ais = key.split("/") .collect::>(); - let key1 = format!("{}/{}", playing_ias[0], playing_ias[1]); - let key2 = format!("{}/{}", playing_ias[1], playing_ias[0]); + let key1 = format!("{}/{}", playing_ais[0], playing_ais[1]); + let key2 = format!("{}/{}", playing_ais[1], playing_ais[0]); battles.insert(key1, val); battles.insert(key2, val); if val != 0.5 { let (winner, loser) = if val > 0.5 { - (playing_ias[0], playing_ias[1]) + (playing_ais[0], playing_ais[1]) } else { - (playing_ias[1], playing_ias[0]) + (playing_ais[1], playing_ais[0]) }; - for ia in &mut ias { - if ia.name == winner { - ia.victory_count += 1; + for ai in &mut ais { + if ai.name == winner { + ai.victory_count += 1; } - if ia.name == loser { - ia.defeat_count += 1; + if ai.name == loser { + ai.defeat_count += 1; } } } } - let mut sorted_ias = ias.clone(); - sorted_ias.sort_by_key(|ia| { - (std::usize::MAX - ia.victory_count, ia.defeat_count) + let mut sorted_ais = ais.clone(); + sorted_ais.sort_by_key(|ai| { + (std::usize::MAX - ai.victory_count, ai.defeat_count) }); Data { - ias, - sorted_ias, + ais, + sorted_ais, battles, } } diff --git a/src/main.rs b/src/main.rs index 38888bb..22194ea 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,7 +10,7 @@ use pytron_web::Data; #[get("/")] fn hello() -> Template { - Template::render("index", &Data::test()) + Template::render("index", &Data::data()) } fn main() { diff --git a/templates/index.html.tera b/templates/index.html.tera index 1a5bcdd..10458d4 100644 --- a/templates/index.html.tera +++ b/templates/index.html.tera @@ -7,21 +7,21 @@ - {% for ia in ias %} - + {% for ai in ais %} + {% endfor %} - {% for ia1 in ias %} + {% for ai1 in ais %} - - {% for ia2 in ias %} - {% if ia1.name == ia2.name %} + + {% for ai2 in ais %} + {% if ai1.name == ai2.name %} {% else %} - {% set key = ia1.name ~ "/" ~ ia2.name %} + {% set key = ai1.name ~ "/" ~ ai2.name %} {% set value = battles | get(key=key) %} {% if value > 0.5 %} @@ -32,8 +32,8 @@ {% endif %} {% endif %} {% endfor %} - - + + {% endfor %}
{{ ia.name }}{{ ai.name }}
{{ ia1.name }}{{ ai1.name }}{{ value }}{{ ia1.victory_count }}{{ ia1.defeat_count }}{{ ai1.victory_count }}{{ ai1.defeat_count }}
@@ -41,10 +41,10 @@
- {% for ia in sorted_ias %} + {% for ai in sorted_ais %} - - + + {% endfor %}
AIScore
{{ ia.name }}{{ ia.victory_count - ia.defeat_count }}{{ ai.name }}{{ ai.victory_count - ai.defeat_count }}