Progressing
This commit is contained in:
parent
fcf466275a
commit
f3393ef7f5
|
@ -1,3 +1,4 @@
|
||||||
/target
|
/target
|
||||||
**/*.rs.bk
|
**/*.rs.bk
|
||||||
Cargo.lock
|
Cargo.lock
|
||||||
|
assets/
|
||||||
|
|
|
@ -1,8 +0,0 @@
|
||||||
{
|
|
||||||
"ias": ["ia1", "ia2", "ia3"],
|
|
||||||
"battles": {
|
|
||||||
"ia1/ia2": 0.3,
|
|
||||||
"ia1/ia3": 0.7,
|
|
||||||
"ia2/ia3": 0.5
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,10 +1,13 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
|
import json
|
||||||
|
import random
|
||||||
|
random.seed()
|
||||||
sys.path.append('pytron')
|
sys.path.append('pytron')
|
||||||
|
|
||||||
from tron.map import Map
|
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 tron.player import Direction, ConstantPlayer
|
||||||
|
|
||||||
from importlib import import_module
|
from importlib import import_module
|
||||||
|
@ -37,8 +40,8 @@ def run_battle(ai1, ai2):
|
||||||
ai2_victories = 0
|
ai2_victories = 0
|
||||||
|
|
||||||
for _ in range(50):
|
for _ in range(50):
|
||||||
initial_position_one = [0, 0]
|
initial_position_one = [random.randint(0, width - 1), random.randint(0, height - 1)]
|
||||||
initial_position_two = [width - 1, height - 1]
|
initial_position_two = [random.randint(0, width - 1), random.randint(0, height - 1)]
|
||||||
|
|
||||||
game = Game(width, height, [
|
game = Game(width, height, [
|
||||||
PositionPlayer(1, ai1.builder(), initial_position_one),
|
PositionPlayer(1, ai1.builder(), initial_position_one),
|
||||||
|
@ -46,9 +49,9 @@ def run_battle(ai1, ai2):
|
||||||
])
|
])
|
||||||
game.main_loop()
|
game.main_loop()
|
||||||
|
|
||||||
if game.winner == Winner.PLAYER_ONE:
|
if game.winner == 1:
|
||||||
ai1_victories += 1
|
ai1_victories += 1
|
||||||
elif game.winner == Winner.PLAYER_TWO:
|
elif game.winner == 2:
|
||||||
ai2_victories += 1
|
ai2_victories += 1
|
||||||
|
|
||||||
# Inverse positions and replay to be symmetrical
|
# Inverse positions and replay to be symmetrical
|
||||||
|
@ -58,25 +61,28 @@ def run_battle(ai1, ai2):
|
||||||
])
|
])
|
||||||
game.main_loop()
|
game.main_loop()
|
||||||
|
|
||||||
if game.winner == Winner.PLAYER_ONE:
|
if game.winner == 1:
|
||||||
ai2_victories += 1
|
ai2_victories += 1
|
||||||
elif game.winner == Winner.PLAYER_TWO:
|
elif game.winner == 2:
|
||||||
ai1_victories += 1
|
ai1_victories += 1
|
||||||
|
|
||||||
return (ai1_victories, ai2_victories)
|
return (ai1_victories, ai2_victories)
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
# Prepare the size for the game.
|
|
||||||
# Those values may be good if you want to play, they might not be so good
|
dictionnary = {"ais": list(map(lambda x: x.name, ais)), "battles": {}}
|
||||||
# to train your AI. Decreasing them will make the learning faster.
|
|
||||||
for (id1, ai1) in enumerate(ais):
|
for (id1, ai1) in enumerate(ais):
|
||||||
for (id2, ai2) in enumerate(ais):
|
for (id2, ai2) in enumerate(ais):
|
||||||
if id1 >= id2:
|
if id1 >= id2:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
print("Battling {} vs {}".format(ai1.name, ai2.name))
|
print("Battling {} vs {}".format(ai1.name, ai2.name))
|
||||||
score = run_battle(ai1, ai2)
|
(score1, score2) = run_battle(ai1, ai2)
|
||||||
print(score)
|
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__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|
72
src/lib.rs
72
src/lib.rs
|
@ -8,19 +8,19 @@ use std::io::Read;
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
/// The data contained from the json file generated by python.
|
/// The data contained from the json file generated by python.
|
||||||
pub struct JsonData {
|
pub struct JsonData {
|
||||||
/// The names of the IAs.
|
/// The names of the AIs.
|
||||||
pub ias: Vec<String>,
|
pub ais: Vec<String>,
|
||||||
|
|
||||||
/// The results of the battles.
|
/// The results of the battles.
|
||||||
pub battles: HashMap<String, f64>,
|
pub battles: HashMap<String, f64>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl JsonData {
|
impl JsonData {
|
||||||
/// Creates some test data.
|
/// Creates some data data.
|
||||||
pub fn test() -> JsonData {
|
pub fn data() -> JsonData {
|
||||||
|
|
||||||
// Open the file
|
// Open the file
|
||||||
let mut file = File::open("./assets/test.json")
|
let mut file = File::open("./assets/data.json")
|
||||||
.expect("Couldn't open file");
|
.expect("Couldn't open file");
|
||||||
|
|
||||||
let mut content = String::new();
|
let mut content = String::new();
|
||||||
|
@ -33,46 +33,46 @@ impl JsonData {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Clone)]
|
#[derive(Serialize, Deserialize, Clone)]
|
||||||
/// An IA.
|
/// An AI.
|
||||||
pub struct Ia {
|
pub struct Ai {
|
||||||
/// The name of the ia.
|
/// The name of the ai.
|
||||||
pub name: String,
|
pub name: String,
|
||||||
|
|
||||||
/// The number of battles won by the ia.
|
/// The number of battles won by the ai.
|
||||||
pub victory_count: usize,
|
pub victory_count: usize,
|
||||||
|
|
||||||
/// The number of battles lost by the ia.
|
/// The number of battles lost by the ai.
|
||||||
pub defeat_count: usize,
|
pub defeat_count: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
pub struct Data {
|
pub struct Data {
|
||||||
/// The IAs
|
/// The AIs
|
||||||
pub ias: Vec<Ia>,
|
pub ais: Vec<Ai>,
|
||||||
|
|
||||||
/// The battles.
|
/// The battles.
|
||||||
///
|
///
|
||||||
/// This hashmap is symmetric.
|
/// This hashmap is symmetric.
|
||||||
pub battles: HashMap<String, f64>,
|
pub battles: HashMap<String, f64>,
|
||||||
|
|
||||||
/// The ias sorted by victory count.
|
/// The ais sorted by victory count.
|
||||||
pub sorted_ias: Vec<Ia>,
|
pub sorted_ais: Vec<Ai>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Data {
|
impl Data {
|
||||||
/// Creates some test data.
|
/// Creates some data data.
|
||||||
pub fn test() -> Data {
|
pub fn data() -> Data {
|
||||||
JsonData::test().into()
|
JsonData::data().into()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<JsonData> for Data {
|
impl From<JsonData> for Data {
|
||||||
fn from(d: JsonData) -> Data {
|
fn from(d: JsonData) -> Data {
|
||||||
let mut ias = vec![];
|
let mut ais = vec![];
|
||||||
|
|
||||||
for ia in d.ias {
|
for ai in d.ais {
|
||||||
ias.push(Ia {
|
ais.push(Ai {
|
||||||
name: ia,
|
name: ai,
|
||||||
victory_count: 0,
|
victory_count: 0,
|
||||||
defeat_count: 0,
|
defeat_count: 0,
|
||||||
})
|
})
|
||||||
|
@ -82,42 +82,42 @@ impl From<JsonData> for Data {
|
||||||
|
|
||||||
for (key, val) in d.battles {
|
for (key, val) in d.battles {
|
||||||
|
|
||||||
let playing_ias = key.split("/")
|
let playing_ais = key.split("/")
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
let key1 = format!("{}/{}", playing_ias[0], playing_ias[1]);
|
let key1 = format!("{}/{}", playing_ais[0], playing_ais[1]);
|
||||||
let key2 = format!("{}/{}", playing_ias[1], playing_ias[0]);
|
let key2 = format!("{}/{}", playing_ais[1], playing_ais[0]);
|
||||||
|
|
||||||
battles.insert(key1, val);
|
battles.insert(key1, val);
|
||||||
battles.insert(key2, val);
|
battles.insert(key2, val);
|
||||||
|
|
||||||
if val != 0.5 {
|
if val != 0.5 {
|
||||||
let (winner, loser) = if val > 0.5 {
|
let (winner, loser) = if val > 0.5 {
|
||||||
(playing_ias[0], playing_ias[1])
|
(playing_ais[0], playing_ais[1])
|
||||||
} else {
|
} else {
|
||||||
(playing_ias[1], playing_ias[0])
|
(playing_ais[1], playing_ais[0])
|
||||||
};
|
};
|
||||||
|
|
||||||
for ia in &mut ias {
|
for ai in &mut ais {
|
||||||
if ia.name == winner {
|
if ai.name == winner {
|
||||||
ia.victory_count += 1;
|
ai.victory_count += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ia.name == loser {
|
if ai.name == loser {
|
||||||
ia.defeat_count += 1;
|
ai.defeat_count += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut sorted_ias = ias.clone();
|
let mut sorted_ais = ais.clone();
|
||||||
sorted_ias.sort_by_key(|ia| {
|
sorted_ais.sort_by_key(|ai| {
|
||||||
(std::usize::MAX - ia.victory_count, ia.defeat_count)
|
(std::usize::MAX - ai.victory_count, ai.defeat_count)
|
||||||
});
|
});
|
||||||
|
|
||||||
Data {
|
Data {
|
||||||
ias,
|
ais,
|
||||||
sorted_ias,
|
sorted_ais,
|
||||||
battles,
|
battles,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,7 +10,7 @@ use pytron_web::Data;
|
||||||
|
|
||||||
#[get("/")]
|
#[get("/")]
|
||||||
fn hello() -> Template {
|
fn hello() -> Template {
|
||||||
Template::render("index", &Data::test())
|
Template::render("index", &Data::data())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
|
|
@ -7,21 +7,21 @@
|
||||||
<table class="table is-bordered is-striped is-narrow is-hoverable">
|
<table class="table is-bordered is-striped is-narrow is-hoverable">
|
||||||
<tr>
|
<tr>
|
||||||
<th></th>
|
<th></th>
|
||||||
{% for ia in ias %}
|
{% for ai in ais %}
|
||||||
<th class="has-text-centered">{{ ia.name }}</th>
|
<th class="has-text-centered">{{ ai.name }}</th>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
<th class="has-text-success has-text-centered">✓</th>
|
<th class="has-text-success has-text-centered">✓</th>
|
||||||
<th class="has-text-danger has-text-centered">✗</th>
|
<th class="has-text-danger has-text-centered">✗</th>
|
||||||
</tr>
|
</tr>
|
||||||
|
|
||||||
{% for ia1 in ias %}
|
{% for ai1 in ais %}
|
||||||
<tr>
|
<tr>
|
||||||
<th class="has-text-centered">{{ ia1.name }}</th>
|
<th class="has-text-centered">{{ ai1.name }}</th>
|
||||||
{% for ia2 in ias %}
|
{% for ai2 in ais %}
|
||||||
{% if ia1.name == ia2.name %}
|
{% if ai1.name == ai2.name %}
|
||||||
<td></td>
|
<td></td>
|
||||||
{% else %}
|
{% else %}
|
||||||
{% set key = ia1.name ~ "/" ~ ia2.name %}
|
{% set key = ai1.name ~ "/" ~ ai2.name %}
|
||||||
{% set value = battles | get(key=key) %}
|
{% set value = battles | get(key=key) %}
|
||||||
{% if value > 0.5 %}
|
{% if value > 0.5 %}
|
||||||
<td class="has-text-success">{{ value }}</td>
|
<td class="has-text-success">{{ value }}</td>
|
||||||
|
@ -32,8 +32,8 @@
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
<td class="has-text-centered"><strong>{{ ia1.victory_count }}</strong></td>
|
<td class="has-text-centered"><strong>{{ ai1.victory_count }}</strong></td>
|
||||||
<td class="has-text-centered"><strong>{{ ia1.defeat_count }}</strong></td>
|
<td class="has-text-centered"><strong>{{ ai1.defeat_count }}</strong></td>
|
||||||
</tr>
|
</tr>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</table>
|
</table>
|
||||||
|
@ -41,10 +41,10 @@
|
||||||
<div class="column is-narrow">
|
<div class="column is-narrow">
|
||||||
<table class="table is-bordered is-striped is-narrow is-hoverable">
|
<table class="table is-bordered is-striped is-narrow is-hoverable">
|
||||||
<tr><th class="has-text-centered">AI</th><th>Score</th></tr>
|
<tr><th class="has-text-centered">AI</th><th>Score</th></tr>
|
||||||
{% for ia in sorted_ias %}
|
{% for ai in sorted_ais %}
|
||||||
<tr>
|
<tr>
|
||||||
<td class="has-text-centered"><strong>{{ ia.name }}</strong></td>
|
<td class="has-text-centered"><strong>{{ ai.name }}</strong></td>
|
||||||
<td class="has-text-centered">{{ ia.victory_count - ia.defeat_count }}</td>
|
<td class="has-text-centered">{{ ai.victory_count - ai.defeat_count }}</td>
|
||||||
</tr>
|
</tr>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</table>
|
</table>
|
||||||
|
|
Loading…
Reference in New Issue