Progressing

This commit is contained in:
Thomas Forgione 2019-03-19 17:00:43 +01:00
parent fcf466275a
commit f3393ef7f5
No known key found for this signature in database
GPG Key ID: 203DAEA747F48F41
6 changed files with 68 additions and 69 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
/target
**/*.rs.bk
Cargo.lock
assets/

View File

@ -1,8 +0,0 @@
{
"ias": ["ia1", "ia2", "ia3"],
"battles": {
"ia1/ia2": 0.3,
"ia1/ia3": 0.7,
"ia2/ia3": 0.5
}
}

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()

View File

@ -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<String>,
/// The names of the AIs.
pub ais: Vec<String>,
/// The results of the battles.
pub battles: HashMap<String, f64>,
}
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<Ia>,
/// The AIs
pub ais: Vec<Ai>,
/// The battles.
///
/// This hashmap is symmetric.
pub battles: HashMap<String, f64>,
/// The ias sorted by victory count.
pub sorted_ias: Vec<Ia>,
/// The ais sorted by victory count.
pub sorted_ais: Vec<Ai>,
}
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<JsonData> 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<JsonData> for Data {
for (key, val) in d.battles {
let playing_ias = key.split("/")
let playing_ais = key.split("/")
.collect::<Vec<_>>();
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,
}
}

View File

@ -10,7 +10,7 @@ use pytron_web::Data;
#[get("/")]
fn hello() -> Template {
Template::render("index", &Data::test())
Template::render("index", &Data::data())
}
fn main() {

View File

@ -7,21 +7,21 @@
<table class="table is-bordered is-striped is-narrow is-hoverable">
<tr>
<th></th>
{% for ia in ias %}
<th class="has-text-centered">{{ ia.name }}</th>
{% for ai in ais %}
<th class="has-text-centered">{{ ai.name }}</th>
{% endfor %}
<th class="has-text-success has-text-centered">✓</th>
<th class="has-text-danger has-text-centered">✗</th>
</tr>
{% for ia1 in ias %}
{% for ai1 in ais %}
<tr>
<th class="has-text-centered">{{ ia1.name }}</th>
{% for ia2 in ias %}
{% if ia1.name == ia2.name %}
<th class="has-text-centered">{{ ai1.name }}</th>
{% for ai2 in ais %}
{% if ai1.name == ai2.name %}
<td></td>
{% else %}
{% set key = ia1.name ~ "/" ~ ia2.name %}
{% set key = ai1.name ~ "/" ~ ai2.name %}
{% set value = battles | get(key=key) %}
{% if value > 0.5 %}
<td class="has-text-success">{{ value }}</td>
@ -32,8 +32,8 @@
{% endif %}
{% endif %}
{% endfor %}
<td class="has-text-centered"><strong>{{ ia1.victory_count }}</strong></td>
<td class="has-text-centered"><strong>{{ ia1.defeat_count }}</strong></td>
<td class="has-text-centered"><strong>{{ ai1.victory_count }}</strong></td>
<td class="has-text-centered"><strong>{{ ai1.defeat_count }}</strong></td>
</tr>
{% endfor %}
</table>
@ -41,10 +41,10 @@
<div class="column is-narrow">
<table class="table is-bordered is-striped is-narrow is-hoverable">
<tr><th class="has-text-centered">AI</th><th>Score</th></tr>
{% for ia in sorted_ias %}
{% for ai in sorted_ais %}
<tr>
<td class="has-text-centered"><strong>{{ ia.name }}</strong></td>
<td class="has-text-centered">{{ ia.victory_count - ia.defeat_count }}</td>
<td class="has-text-centered"><strong>{{ ai.name }}</strong></td>
<td class="has-text-centered">{{ ai.victory_count - ai.defeat_count }}</td>
</tr>
{% endfor %}
</table>