From 007eb03050d05464921faf93477b40f8bc4346a9 Mon Sep 17 00:00:00 2001 From: Leonora Tindall Date: Mon, 3 Apr 2023 22:50:33 -0500 Subject: [PATCH] Add shift register memory --- src/player.rs | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/player.rs b/src/player.rs index 08223a0..9390df2 100644 --- a/src/player.rs +++ b/src/player.rs @@ -11,6 +11,8 @@ const NUM_KEYS: usize = 4; const INPUTS_PER_ASTEROID: usize = 4; const NUM_ASTEROIDS: usize = 1; const INPUTS_FOR_SHIP: usize = 2; +const VALUES_PER_MEMORY: usize = 1; +const NUM_MEMORIES: usize = 0; #[derive(Default)] pub struct Player { pub pos: Vec2, @@ -29,6 +31,7 @@ pub struct Player { alive: bool, pub lifespan: u32, pub shots: u32, + memory: std::collections::VecDeque, } impl Player { @@ -46,10 +49,16 @@ impl Player { 0, (INPUTS_PER_ASTEROID * NUM_ASTEROIDS) + INPUTS_FOR_SHIP + + (VALUES_PER_MEMORY * NUM_MEMORIES), ); // Number of outputs c.push( NUM_KEYS + + if NUM_MEMORIES > 0 { + VALUES_PER_MEMORY + } else { + 0 + }, ); Some(NN::new(c, mut_rate.unwrap(), activ.unwrap())) } @@ -63,8 +72,9 @@ impl Player { shot_interval: 18, alive: true, shots: 4, - // 4 outputs - outputs: vec![0.; NUM_KEYS], + // 4 outputs, 1 for memory + outputs: vec![0.; NUM_KEYS + VALUES_PER_MEMORY], + memory: vec![0.; VALUES_PER_MEMORY * NUM_MEMORIES].into(), ..Default::default() } @@ -140,10 +150,18 @@ impl Player { self.inputs.push( (self.shot_interval as f32 - self.last_shot as f32).max(0.) / self.shot_interval as f32, ); + // Insert the memories + for memory in &self.memory { + self.inputs.push(memory.min(1.).max(-1.)); + } // Run the brain if let Some(brain) = &self.brain { assert_eq!(self.inputs.len(), brain.config[0] - 1); self.outputs = brain.feed_forward(&self.inputs); + if NUM_MEMORIES > 0 { + self.memory.push_back(self.outputs[self.outputs.len() - 1]); + self.memory.pop_front(); + } keys = self .outputs .iter()