Compare commits
No commits in common. "4e44ac548cf8a8a0b3c76befa30561657b27c5d3" and "e545f09bb43bc4227aa7b85e4797aedceee5679b" have entirely different histories.
4e44ac548c
...
e545f09bb4
@ -19,7 +19,7 @@ use std::{
|
|||||||
|
|
||||||
use log::{error, info, warn};
|
use log::{error, info, warn};
|
||||||
use ordered_float::OrderedFloat;
|
use ordered_float::OrderedFloat;
|
||||||
use rand::{prelude::*, thread_rng};
|
use rand::thread_rng;
|
||||||
use serde_json::{json, Value};
|
use serde_json::{json, Value};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
@ -172,8 +172,7 @@ pub fn get_move(
|
|||||||
let chosen = actions
|
let chosen = actions
|
||||||
.iter()
|
.iter()
|
||||||
.max_by_key(|(_, stat)| OrderedFloat(stat.won as f64 / stat.played as f64))
|
.max_by_key(|(_, stat)| OrderedFloat(stat.won as f64 / stat.played as f64))
|
||||||
.map(|(direction, _)| *direction)
|
.map(|(direction, _)| *direction)?;
|
||||||
.or_else(|| possible_actions.iter().choose(&mut thread_rng()).copied())?;
|
|
||||||
|
|
||||||
info!(
|
info!(
|
||||||
"DIRECTION {turn}: {chosen:?} after {}ms ({})",
|
"DIRECTION {turn}: {chosen:?} after {}ms ({})",
|
||||||
@ -206,7 +205,7 @@ struct DeadlineError;
|
|||||||
struct Node {
|
struct Node {
|
||||||
statistic: Statistics,
|
statistic: Statistics,
|
||||||
child_statistics: BTreeMap<SnakeToken, BTreeMap<Direction, ActionStatistic>>,
|
child_statistics: BTreeMap<SnakeToken, BTreeMap<Direction, ActionStatistic>>,
|
||||||
childs: BTreeMap<BTreeMap<SnakeToken, (Direction, usize)>, Node>,
|
childs: BTreeMap<BTreeMap<SnakeToken, Direction>, Node>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Node {
|
impl Node {
|
||||||
@ -259,13 +258,9 @@ impl Node {
|
|||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
board.simulate_actions(&actions, &mut thread_rng());
|
board.simulate_actions(&actions, &mut thread_rng());
|
||||||
let map_actions = actions
|
|
||||||
.iter()
|
|
||||||
.map(|(&snake, &action)| (snake, (action, board.snake_length(snake).unwrap_or(0))))
|
|
||||||
.collect();
|
|
||||||
let winner = self
|
let winner = self
|
||||||
.childs
|
.childs
|
||||||
.entry(map_actions)
|
.entry(actions.clone())
|
||||||
.or_default()
|
.or_default()
|
||||||
.monte_carlo_step(board, deadline)?;
|
.monte_carlo_step(board, deadline)?;
|
||||||
|
|
||||||
@ -357,13 +352,9 @@ impl Node {
|
|||||||
return Err(DeadlineError);
|
return Err(DeadlineError);
|
||||||
}
|
}
|
||||||
board.simulate_actions(&actions, &mut thread_rng());
|
board.simulate_actions(&actions, &mut thread_rng());
|
||||||
let map_actions = actions
|
|
||||||
.iter()
|
|
||||||
.map(|(&snake, &action)| (snake, (action, board.snake_length(snake).unwrap_or(0))))
|
|
||||||
.collect();
|
|
||||||
let lengths = self
|
let lengths = self
|
||||||
.childs
|
.childs
|
||||||
.entry(map_actions)
|
.entry(actions.clone())
|
||||||
.or_default()
|
.or_default()
|
||||||
.monte_carlo_step_solo(board, deadline)?;
|
.monte_carlo_step_solo(board, deadline)?;
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
#![allow(clippy::needless_pass_by_value)]
|
#![allow(clippy::needless_pass_by_value)]
|
||||||
|
|
||||||
use battlesnake::{logic, Action, Direction, GameState};
|
use battlesnake::{logic, Action, GameState};
|
||||||
use log::{error, info};
|
use log::{error, info};
|
||||||
use rocket::{
|
use rocket::{
|
||||||
fairing::AdHoc, get, http::Status, launch, post, routes, serde::json::Json, tokio::task,
|
fairing::AdHoc, get, http::Status, launch, post, routes, serde::json::Json, tokio::task,
|
||||||
@ -26,7 +26,7 @@ fn handle_start(start_req: Json<GameState>) -> Status {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[post("/move", format = "json", data = "<move_req>")]
|
#[post("/move", format = "json", data = "<move_req>")]
|
||||||
async fn handle_move(move_req: Json<GameState>) -> Json<Action> {
|
async fn handle_move(move_req: Json<GameState>) -> Option<Json<Action>> {
|
||||||
let start = Instant::now();
|
let start = Instant::now();
|
||||||
let response = task::spawn_blocking(move || {
|
let response = task::spawn_blocking(move || {
|
||||||
logic::get_move(
|
logic::get_move(
|
||||||
@ -40,13 +40,9 @@ async fn handle_move(move_req: Json<GameState>) -> Json<Action> {
|
|||||||
.await
|
.await
|
||||||
.inspect_err(|e| error!("failed to join compute thread: {e}"))
|
.inspect_err(|e| error!("failed to join compute thread: {e}"))
|
||||||
.ok()
|
.ok()
|
||||||
.flatten()
|
.flatten()?;
|
||||||
.unwrap_or(Action {
|
|
||||||
r#move: Direction::Up,
|
|
||||||
shout: Some("I am so dead".to_owned()),
|
|
||||||
});
|
|
||||||
|
|
||||||
Json(response)
|
Some(Json(response))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[post("/end", format = "json", data = "<end_req>")]
|
#[post("/end", format = "json", data = "<end_req>")]
|
||||||
|
Loading…
Reference in New Issue
Block a user