summaryrefslogtreecommitdiff
path: root/src/strategy/minimax.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/strategy/minimax.rs')
-rw-r--r--src/strategy/minimax.rs59
1 files changed, 19 insertions, 40 deletions
diff --git a/src/strategy/minimax.rs b/src/strategy/minimax.rs
index ce5e574..6c07b07 100644
--- a/src/strategy/minimax.rs
+++ b/src/strategy/minimax.rs
@@ -7,33 +7,13 @@ use std::collections::HashMap;
use std::ops::*;
use time::{Duration, PreciseTime};
-pub fn choose_move(
- state: &GameBoard,
- previous_root: Option<Node>,
- start_time: PreciseTime,
- max_time: Duration,
-) -> (Command, Node) {
- let mut root_node = match previous_root {
- None => Node {
- score_sum: ScoreSum::new(),
- player_score_sums: [HashMap::new(), HashMap::new()],
- unexplored: move_combos(state),
- children: HashMap::new(),
- },
- Some(mut node) => node
- .children
- .drain()
- .map(|(_k, n)| n)
- .find(|_n| false) // TODO: Use the last player / opponent move and worm positions to use this cache.
- .unwrap_or_else(|| {
- eprintln!("Previous round did not appear in the cache");
- Node {
- score_sum: ScoreSum::new(),
- player_score_sums: [HashMap::new(), HashMap::new()],
- unexplored: move_combos(state),
- children: HashMap::new(),
- }
- }),
+// TODO: Cache results from last round based on player / opponent move and worm positions
+pub fn choose_move(state: &GameBoard, start_time: PreciseTime, max_time: Duration) -> Command {
+ let mut root_node = Node {
+ score_sum: ScoreSum::new(),
+ player_score_sums: [HashMap::new(), HashMap::new()],
+ unexplored: move_combos(state),
+ children: HashMap::new(),
};
while start_time.to(PreciseTime::now()) < max_time {
@@ -50,13 +30,7 @@ pub fn choose_move(
);
}
- let chosen_command = best_player_move(&root_node);
-
- root_node
- .children
- .retain(|[c1, _], _| *c1 == chosen_command);
-
- (chosen_command, root_node)
+ best_player_move(&root_node)
}
pub struct Node {
@@ -243,17 +217,20 @@ fn score(state: &GameBoard) -> Score {
let snowballs = state.players[0].snowballs() as f32 - state.players[1].snowballs() as f32;
let bombs = state.players[0].bombs() as f32 - state.players[1].bombs() as f32;
+ // TODO: Calibrate these weightings somehow? Some sort of generate and sort based on playing against each other?
+ // What about:
+ // - Creating a list (mins and maxes)
+ // - Keep adding a new guess, run against all, and sort the list by fitness.
+ // - Repeat until list has many values
+ // - Somehow prioritize sticking new items in based on what's going well? Or maximally different? Keep dividing all the ranges in half?
const MAX_HEALTH_WEIGHT: f32 = 1.;
const TOTAL_HEALTH_WEIGHT: f32 = 1.;
const POINTS_WEIGHT: f32 = 0.;
const VICTORY_WEIGHT: f32 = 3000.;
-
const SNOWBALL_WEIGHT: f32 = 100.;
const BOMB_WEIGHT: f32 = 100.;
- // TODO: Try adding new features here. Something about board position and ammo remaining? Ammo isn't zero sum, so is it right to represent it as such here?
- // TODO: Calibrate these weightings somehow?
- // TODO: Distance to dirt heatmap? Probably less relevant these days.
+ // TODO: Try adding new features here. Something about board position?
Score {
val: max_health * MAX_HEALTH_WEIGHT
+ total_health * TOTAL_HEALTH_WEIGHT
@@ -318,11 +295,13 @@ fn pruned_moves(state: &GameBoard, player_index: usize) -> Vec<Command> {
.valid_moves(player_index)
.into_iter()
.filter(|command| {
- // TODO: Filtering out of snowball moves to only freeze opponents
+ // TODO: Some of these filters could be done with better
+ // performance by running them while generating the list
+ // of valid moves.
// NB: These rules should pass for doing nothing, otherwise
// we need some other mechanism for sticking in a do
- // nothing option. Unfortunately, sitting in lava is a situation where this prunes all moves currently :(
+ // nothing option.
let idle_opponent_state = sim_with_idle_opponent(*command);
let hurt_self = idle_opponent_state.players[player_index].health() < my_starting_health;