From b0455769297964f0b82ef0df78dad8da74e9bca9 Mon Sep 17 00:00:00 2001 From: Justin Worthe Date: Mon, 14 May 2018 22:12:05 +0200 Subject: Reduced number of needless allocations to improve perf Current iterations: 26486 in 10 seconds --- src/strategy/monte_carlo.rs | 58 +++++++++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 26 deletions(-) (limited to 'src/strategy') diff --git a/src/strategy/monte_carlo.rs b/src/strategy/monte_carlo.rs index 7e62cae..83627b0 100644 --- a/src/strategy/monte_carlo.rs +++ b/src/strategy/monte_carlo.rs @@ -1,5 +1,6 @@ use engine::settings::GameSettings; use engine::command::*; +use engine::geometry::*; use engine::{GameState, GameStatus}; use rand::{thread_rng, Rng}; @@ -64,12 +65,29 @@ fn simulate_to_endstate(command_score: &mut CommandScore, settings: &Gam } fn random_player_move(settings: &GameSettings, state: &GameState, rng: &mut R) -> Command { - let all_commands = enumerate_player_commands(settings, state); - rng.choose(&all_commands).cloned().unwrap_or(Command::Nothing) + let all_positions = state.unoccupied_player_cells(settings); + let all_buildings = state.player_affordable_buildings(settings); + random_move(&all_positions, &all_buildings, rng) } + fn random_opponent_move(settings: &GameSettings, state: &GameState, rng: &mut R) -> Command { - let all_commands = enumerate_opponent_commands(settings, state); - rng.choose(&all_commands).cloned().unwrap_or(Command::Nothing) + let all_positions = state.unoccupied_opponent_cells(settings); + let all_buildings = state.opponent_affordable_buildings(settings); + random_move(&all_positions, &all_buildings, rng) +} + +fn random_move(all_positions: &[Point], all_buildings: &[BuildingType], rng: &mut R) -> Command { + let number_of_commands = all_positions.len()*all_buildings.len()+1; + let choice_index = rng.gen_range(0, number_of_commands); + + if choice_index == number_of_commands - 1 { + Command::Nothing + } else { + Command::Build( + all_positions[choice_index/all_buildings.len()], + all_buildings[choice_index%all_buildings.len()] + ) + } } #[derive(Debug)] @@ -128,27 +146,15 @@ impl CommandScore { fn enumerate_player_commands(settings: &GameSettings, state: &GameState) -> Vec { let all_positions = state.unoccupied_player_cells(settings); let all_buildings = state.player_affordable_buildings(settings); - - let build_commands = all_positions.iter() - .flat_map(|&pos| all_buildings.iter() - .map(|&building| Command::Build(pos, building)).collect::>() - ); - let other_commands = vec!(Command::Nothing); - - build_commands.chain(other_commands) - .collect() -} -fn enumerate_opponent_commands(settings: &GameSettings, state: &GameState) -> Vec { - let all_positions = state.unoccupied_opponent_cells(settings); - let all_buildings = state.opponent_affordable_buildings(settings); - - let build_commands = all_positions.iter() - .flat_map(|&pos| all_buildings.iter() - .map(|&building| Command::Build(pos, building)).collect::>() - ); - let other_commands = vec!(Command::Nothing); - - build_commands.chain(other_commands) - .collect() + let mut commands = Vec::with_capacity(all_positions.len()*all_buildings.len()+1); + commands.push(Command::Nothing); + + for position in all_positions { + for &building in &all_buildings { + commands.push(Command::Build(position, building)); + } + } + + commands } -- cgit v1.2.3