summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJustin Worthe <justin@worthe-it.co.za>2018-09-02 21:52:14 +0200
committerJustin Worthe <justin@worthe-it.co.za>2018-09-02 21:52:14 +0200
commite7e948d8861661880d2d283954288d9dba25014f (patch)
treec542285cc60cd52ec7469d00d72e773944e2f95d
parent1cb761aefe4d7b33a02c76803d19f48e7929d52c (diff)
Improved performance of heuristic calcs by caching common calcs
-rw-r--r--Cargo.toml2
-rw-r--r--src/strategy/monte_carlo.rs16
2 files changed, 15 insertions, 3 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 6f19b1c..e502dd6 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -29,7 +29,7 @@ energy-cutoff = []
discard-poor-performers = []
heuristic-random = ["lazy_static"]
-default = ["energy-cutoff", "discard-poor-performers"]
+default = ["energy-cutoff", "discard-poor-performers", "heuristic-random", "debug-decisions"]
[profile.release]
debug = true \ No newline at end of file
diff --git a/src/strategy/monte_carlo.rs b/src/strategy/monte_carlo.rs
index be53ef1..6180fce 100644
--- a/src/strategy/monte_carlo.rs
+++ b/src/strategy/monte_carlo.rs
@@ -190,6 +190,17 @@ fn random_move<R: Rng>(player: &Player, opponent: &Player, rng: &mut R) -> Comma
let mut cdf_defence = [0; NUMBER_OF_MAP_POSITIONS];
let mut cdf_attack = [0; NUMBER_OF_MAP_POSITIONS];
let mut cdf_tesla = [0; NUMBER_OF_MAP_POSITIONS];
+
+ let mut opponent_energy_per_row = [0; MAP_HEIGHT as usize];
+ let mut opponent_attack_per_row = [0; MAP_HEIGHT as usize];
+ let mut opponent_towers_per_row = [0; MAP_HEIGHT as usize];
+ let mut player_attack_per_row = [0; MAP_HEIGHT as usize];
+ for y in 0..MAP_HEIGHT {
+ opponent_energy_per_row[y as usize] = opponent.count_energy_towers_in_row(y);
+ opponent_attack_per_row[y as usize] = opponent.count_attack_towers_in_row(y);
+ opponent_towers_per_row[y as usize] = opponent.count_towers_in_row(y);
+ player_attack_per_row[y as usize] = player.count_attack_towers_in_row(y);
+ }
let mut other_end: u16 = 0;
@@ -238,7 +249,7 @@ fn random_move<R: Rng>(player: &Player, opponent: &Player, rng: &mut R) -> Comma
0
} else {
//TODO: Favour the front and rows with something to defend
- opponent.count_attack_towers_in_row(point.y())
+ opponent_attack_per_row[usize::from(point.y())]
};
defence_end += weight;
@@ -255,7 +266,8 @@ fn random_move<R: Rng>(player: &Player, opponent: &Player, rng: &mut R) -> Comma
0
} else {
// TODO: take into account opponent attacks and defence in row?
- 8 + opponent.count_energy_towers_in_row(point.y()) + opponent.count_towers_in_row(point.y()) - player.count_attack_towers_in_row(point.y())
+ let y = usize::from(point.y());
+ 8 + opponent_energy_per_row[y] + opponent_towers_per_row[y] - player_attack_per_row[y]
};
attack_end += weight;