spawn blocking task for simulations
All checks were successful
Build / build (push) Successful in 2m2s
All checks were successful
Build / build (push) Successful in 2m2s
This commit is contained in:
parent
c26da8869c
commit
32883b2a1b
@ -82,46 +82,60 @@ async fn get_move(request: Json<Request>) -> response::Json<Response> {
|
|||||||
}
|
}
|
||||||
info!("valid actions: {:?}", actions);
|
info!("valid actions: {:?}", actions);
|
||||||
|
|
||||||
let mut action_data = [(0, 0); 4];
|
tokio::task::spawn_blocking(move || {
|
||||||
let mut total_simulations = 0;
|
let base_turns = board.turn();
|
||||||
let base_turns = board.turn();
|
let end_condition: &dyn Fn(&Board) -> Option<u32> = match &*request.game.ruleset.name {
|
||||||
while start.elapsed() < Duration::from_millis(250) {
|
"solo" => &|board: &Board| {
|
||||||
let mut board = board.clone();
|
if board.num_snakes() == 0
|
||||||
let action = *actions.choose(&mut thread_rng()).unwrap_or(&Direction::Up);
|
|| board.turn() > base_turns + u32::from(request.you.length) * 3
|
||||||
board.next_turn(&[(id, action)]);
|
{
|
||||||
let turns = board.simulate_random(|board| {
|
Some(board.turn())
|
||||||
if board.num_snakes() == 0
|
} else {
|
||||||
|| board.turn() > base_turns + u32::from(request.you.length) * 3
|
None
|
||||||
{
|
}
|
||||||
Some(board.turn())
|
},
|
||||||
} else {
|
_ => &|board: &Board| {
|
||||||
None
|
if !board.alive(id) || board.turn() > base_turns + u32::from(request.you.length) * 3
|
||||||
}
|
{
|
||||||
|
Some(board.turn())
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut action_data = [(0, 0); 4];
|
||||||
|
let mut total_simulations = 0;
|
||||||
|
while start.elapsed() < Duration::from_millis(250) {
|
||||||
|
let mut board = board.clone();
|
||||||
|
let action = *actions.choose(&mut thread_rng()).unwrap_or(&Direction::Up);
|
||||||
|
board.next_turn(&[(id, action)]);
|
||||||
|
let turns = board.simulate_random(end_condition);
|
||||||
|
let action_data = &mut action_data[usize::from(action)];
|
||||||
|
action_data.0 += turns - base_turns;
|
||||||
|
action_data.1 += 1;
|
||||||
|
total_simulations += 1;
|
||||||
|
}
|
||||||
|
debug!("action data: {action_data:?}");
|
||||||
|
|
||||||
|
let action = actions.into_iter().max_by_key(|action| {
|
||||||
|
let action_data = action_data[usize::from(*action)];
|
||||||
|
(action_data.0 / action_data.1, action_data.0 % action_data.1)
|
||||||
});
|
});
|
||||||
let action_data = &mut action_data[usize::from(action)];
|
|
||||||
action_data.0 += turns - base_turns;
|
|
||||||
action_data.1 += 1;
|
|
||||||
total_simulations += 1;
|
|
||||||
}
|
|
||||||
debug!("action data: {action_data:?}");
|
|
||||||
|
|
||||||
let action = actions.into_iter().max_by_key(|action| {
|
if let Some(action) = action {
|
||||||
let action_data = action_data[usize::from(*action)];
|
let action_data = action_data[usize::from(action)];
|
||||||
(action_data.0 / action_data.1, action_data.0 % action_data.1)
|
let avg_turns = action_data.0 / action_data.1;
|
||||||
});
|
info!("found action {action:?} after {total_simulations} simulations with an average of {avg_turns} turns.");
|
||||||
|
} else {
|
||||||
if let Some(action) = action {
|
warn!("unable to find a valid action");
|
||||||
let action_data = action_data[usize::from(action)];
|
}
|
||||||
let avg_turns = action_data.0 / action_data.1;
|
info!("chose {action:?}");
|
||||||
info!("found action {action:?} after {total_simulations} simulations with an average of {avg_turns} turns.");
|
response::Json(Response {
|
||||||
} else {
|
direction: action.unwrap_or(Direction::Up),
|
||||||
warn!("unable to find a valid action");
|
shout: None,
|
||||||
}
|
})
|
||||||
info!("chose {action:?}");
|
}).await.unwrap()
|
||||||
response::Json(Response {
|
|
||||||
direction: action.unwrap_or(Direction::Up),
|
|
||||||
shout: None,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn end(request: Json<Request>) {
|
async fn end(request: Json<Request>) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user