Compare commits
11 Commits
91eb44f4bd
...
3fd3d53be0
Author | SHA1 | Date |
---|---|---|
Leonora Tindall | 3fd3d53be0 | |
Leonora Tindall | f74fed7638 | |
Leonora Tindall | 5378d3a4fb | |
Leonora Tindall | 01517cc61c | |
Leonora Tindall | 007eb03050 | |
Leonora Tindall | e63fbf74ba | |
Leonora Tindall | 36da3483ae | |
Leonora Tindall | 154517654e | |
Leonora Tindall | d71b437124 | |
Leonora Tindall | 49b720baec | |
Leonora Tindall | f04309f678 |
|
@ -8,3 +8,7 @@ Cargo.lock
|
||||||
|
|
||||||
# These are backup files generated by rustfmt
|
# These are backup files generated by rustfmt
|
||||||
**/*.rs.bk
|
**/*.rs.bk
|
||||||
|
|
||||||
|
# Gathered data
|
||||||
|
data/
|
||||||
|
charts/
|
||||||
|
|
|
@ -142,6 +142,7 @@ dependencies = [
|
||||||
name = "genetic"
|
name = "genetic"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"lazy_static",
|
||||||
"macroquad",
|
"macroquad",
|
||||||
"nalgebra",
|
"nalgebra",
|
||||||
"rand",
|
"rand",
|
||||||
|
@ -203,6 +204,12 @@ version = "1.0.5"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "fad582f4b9e86b6caa621cabeb0963332d92eea04729ab12892c2533951e6440"
|
checksum = "fad582f4b9e86b6caa621cabeb0963332d92eea04729ab12892c2533951e6440"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "lazy_static"
|
||||||
|
version = "1.4.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "lewton"
|
name = "lewton"
|
||||||
version = "0.9.4"
|
version = "0.9.4"
|
||||||
|
|
|
@ -13,6 +13,7 @@ rand_distr = "0.4.3"
|
||||||
serde = { version = "1.0.152", features = ["derive"] }
|
serde = { version = "1.0.152", features = ["derive"] }
|
||||||
serde_json = "1.0.91"
|
serde_json = "1.0.91"
|
||||||
tinyfiledialogs = "3.9.1"
|
tinyfiledialogs = "3.9.1"
|
||||||
|
lazy_static = "1.4"
|
||||||
|
|
||||||
[profile.dev]
|
[profile.dev]
|
||||||
opt-level = 3
|
opt-level = 3
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use crate::{HEIGHT, WIDTH};
|
use crate::{HEIGHT, WIDTH};
|
||||||
use macroquad::{prelude::*, rand::gen_range};
|
use macroquad::{prelude::*, rand::gen_range};
|
||||||
#[derive(Clone)]
|
#[derive(Clone, PartialEq, Eq)]
|
||||||
pub enum AsteroidSize {
|
pub enum AsteroidSize {
|
||||||
Large,
|
Large,
|
||||||
Medium,
|
Medium,
|
||||||
|
@ -19,12 +19,14 @@ pub struct Asteroid {
|
||||||
pub alive: bool,
|
pub alive: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const BASE_SIZE: f32 = 20.;
|
||||||
|
|
||||||
impl Asteroid {
|
impl Asteroid {
|
||||||
pub fn new(size: AsteroidSize) -> Self {
|
pub fn new(size: AsteroidSize) -> Self {
|
||||||
let (sides, radius) = match size {
|
let (sides, radius) = match size {
|
||||||
AsteroidSize::Large => (gen_range(6, 10), gen_range(50., 65.)),
|
AsteroidSize::Large => (gen_range(6, 10), BASE_SIZE * 4.),
|
||||||
AsteroidSize::Medium => (gen_range(5, 6), gen_range(35., 50.)),
|
AsteroidSize::Medium => (gen_range(5, 6), BASE_SIZE * 2.),
|
||||||
AsteroidSize::Small => (gen_range(3, 5), 25.),
|
AsteroidSize::Small => (gen_range(3, 5), BASE_SIZE),
|
||||||
};
|
};
|
||||||
let mut r = vec2(
|
let mut r = vec2(
|
||||||
if gen_range(0., 1.) > 0.5 { -1. } else { 1. },
|
if gen_range(0., 1.) > 0.5 { -1. } else { 1. },
|
||||||
|
|
13
src/main.rs
13
src/main.rs
|
@ -70,7 +70,7 @@ async fn main() {
|
||||||
let mut size: u32 = 100;
|
let mut size: u32 = 100;
|
||||||
let mut world: World = World::new(None, None, None);
|
let mut world: World = World::new(None, None, None);
|
||||||
|
|
||||||
let mut hlayers: Vec<usize> = vec![6, 6, 0];
|
let mut hlayers: Vec<usize> = vec![6, 6, 6];
|
||||||
let mut prev_hlayers = hlayers.clone();
|
let mut prev_hlayers = hlayers.clone();
|
||||||
|
|
||||||
let mut mut_rate = 0.05;
|
let mut mut_rate = 0.05;
|
||||||
|
@ -94,9 +94,8 @@ async fn main() {
|
||||||
);
|
);
|
||||||
|
|
||||||
let ui_thick = 34.;
|
let ui_thick = 34.;
|
||||||
let nums = &[
|
let nums_owning: Vec<String> = (0..=64).into_iter().map(|i| format!("{:00}", i)).collect();
|
||||||
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16",
|
let nums: Vec<&str> = nums_owning.iter().map(|v| v.as_str()).collect();
|
||||||
];
|
|
||||||
let skin = skins::get_ui_skin();
|
let skin = skins::get_ui_skin();
|
||||||
let skin2 = skins::get_white_buttons_skin();
|
let skin2 = skins::get_white_buttons_skin();
|
||||||
let skin3 = skins::get_green_buttons_skin();
|
let skin3 = skins::get_green_buttons_skin();
|
||||||
|
@ -390,9 +389,9 @@ async fn main() {
|
||||||
ui.label(None, "Hidden Layers");
|
ui.label(None, "Hidden Layers");
|
||||||
ui.label(None, "Neurons Config");
|
ui.label(None, "Neurons Config");
|
||||||
|
|
||||||
ui.combo_box(hash!(), "Layer 1", nums, &mut hlayers[0]);
|
ui.combo_box(hash!(), "Layer 1", &nums, &mut hlayers[0]);
|
||||||
ui.combo_box(hash!(), "Layer 2", nums, &mut hlayers[1]);
|
ui.combo_box(hash!(), "Layer 2", &nums, &mut hlayers[1]);
|
||||||
ui.combo_box(hash!(), "Layer 3", nums, &mut hlayers[2]);
|
ui.combo_box(hash!(), "Layer 3", &nums, &mut hlayers[2]);
|
||||||
if prev_hlayers != hlayers {
|
if prev_hlayers != hlayers {
|
||||||
pop = Population::new(
|
pop = Population::new(
|
||||||
size as usize,
|
size as usize,
|
||||||
|
|
43
src/nn.rs
43
src/nn.rs
|
@ -1,10 +1,14 @@
|
||||||
use macroquad::{prelude::*, rand::gen_range};
|
use macroquad::{prelude::*, rand::gen_range};
|
||||||
use nalgebra::*;
|
use nalgebra::*;
|
||||||
use r::Rng;
|
use r::Rng;
|
||||||
use rand_distr::StandardNormal;
|
use rand_distr::Normal;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
extern crate rand as r;
|
extern crate rand as r;
|
||||||
|
|
||||||
|
lazy_static::lazy_static! {
|
||||||
|
static ref CONNECTION_DISTRIBUTION: Normal<f32> = Normal::new(0.0, 0.75).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(PartialEq, Debug, Clone, Copy, Serialize, Deserialize)]
|
#[derive(PartialEq, Debug, Clone, Copy, Serialize, Deserialize)]
|
||||||
|
|
||||||
pub enum ActivationFunc {
|
pub enum ActivationFunc {
|
||||||
|
@ -39,8 +43,12 @@ impl NN {
|
||||||
.zip(config.iter().skip(1))
|
.zip(config.iter().skip(1))
|
||||||
.map(|(&curr, &last)| {
|
.map(|(&curr, &last)| {
|
||||||
// DMatrix::from_fn(last, curr + 1, |_, _| gen_range(-1., 1.))
|
// DMatrix::from_fn(last, curr + 1, |_, _| gen_range(-1., 1.))
|
||||||
DMatrix::<f32>::from_distribution(last, curr + 1, &StandardNormal, &mut rng)
|
DMatrix::<f32>::from_distribution(
|
||||||
* (2. / last as f32).sqrt()
|
last,
|
||||||
|
curr + 1,
|
||||||
|
&*CONNECTION_DISTRIBUTION,
|
||||||
|
&mut rng,
|
||||||
|
) * (2. / last as f32).sqrt()
|
||||||
})
|
})
|
||||||
.collect(),
|
.collect(),
|
||||||
|
|
||||||
|
@ -60,10 +68,16 @@ impl NN {
|
||||||
.iter()
|
.iter()
|
||||||
.zip(b.weights.iter())
|
.zip(b.weights.iter())
|
||||||
.map(|(m1, m2)| {
|
.map(|(m1, m2)| {
|
||||||
m1.zip_map(
|
m1.zip_map(m2, |ele1, ele2| {
|
||||||
m2,
|
let choice = gen_range(0., 3.);
|
||||||
|ele1, ele2| if gen_range(0., 1.) < 0.5 { ele1 } else { ele2 },
|
if choice < 1. {
|
||||||
)
|
ele1
|
||||||
|
} else if choice < 2. {
|
||||||
|
ele2
|
||||||
|
} else {
|
||||||
|
(ele1 + ele2) / 2.
|
||||||
|
}
|
||||||
|
})
|
||||||
})
|
})
|
||||||
.collect(),
|
.collect(),
|
||||||
}
|
}
|
||||||
|
@ -75,7 +89,9 @@ impl NN {
|
||||||
if gen_range(0., 1.) < self.mut_rate {
|
if gen_range(0., 1.) < self.mut_rate {
|
||||||
// *ele += gen_range(-1., 1.);
|
// *ele += gen_range(-1., 1.);
|
||||||
// *ele = gen_range(-1., 1.);
|
// *ele = gen_range(-1., 1.);
|
||||||
*ele = r::thread_rng().sample::<f32, StandardNormal>(StandardNormal);
|
*ele +=
|
||||||
|
r::thread_rng().sample::<f32, Normal<f32>>(CONNECTION_DISTRIBUTION.clone());
|
||||||
|
*ele = ele.min(10.0).max(-10.0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -85,12 +101,11 @@ impl NN {
|
||||||
// println!("inputs: {:?}", inputs);
|
// println!("inputs: {:?}", inputs);
|
||||||
let mut y = DMatrix::from_vec(inputs.len(), 1, inputs.to_vec());
|
let mut y = DMatrix::from_vec(inputs.len(), 1, inputs.to_vec());
|
||||||
for i in 0..self.config.len() - 1 {
|
for i in 0..self.config.len() - 1 {
|
||||||
y = (&self.weights[i] * y.insert_row(self.config[i] - 1, 1.)).map(|x| {
|
let row = y.insert_row(self.config[i] - 1, 1.);
|
||||||
match self.activ_func {
|
y = (&self.weights[i] * row).map(|x| match self.activ_func {
|
||||||
ActivationFunc::ReLU => x.max(0.),
|
ActivationFunc::ReLU => x.max(0.),
|
||||||
ActivationFunc::Sigmoid => 1. / (1. + (-x).exp()),
|
ActivationFunc::Sigmoid => 1. / (1. + (-x).exp()),
|
||||||
ActivationFunc::Tanh => x.tanh(),
|
ActivationFunc::Tanh => x.tanh(),
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
y.column(0).data.into_slice().to_vec()
|
y.column(0).data.into_slice().to_vec()
|
||||||
|
|
225
src/player.rs
225
src/player.rs
|
@ -7,6 +7,12 @@ use crate::{
|
||||||
nn::{ActivationFunc, NN},
|
nn::{ActivationFunc, NN},
|
||||||
HEIGHT, WIDTH,
|
HEIGHT, WIDTH,
|
||||||
};
|
};
|
||||||
|
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)]
|
#[derive(Default)]
|
||||||
pub struct Player {
|
pub struct Player {
|
||||||
pub pos: Vec2,
|
pub pos: Vec2,
|
||||||
|
@ -16,17 +22,16 @@ pub struct Player {
|
||||||
rot: f32,
|
rot: f32,
|
||||||
drag: f32,
|
drag: f32,
|
||||||
bullets: Vec<Bullet>,
|
bullets: Vec<Bullet>,
|
||||||
asteroid: Option<Asteroid>,
|
asteroids: Vec<Option<Asteroid>>,
|
||||||
inputs: Vec<f32>,
|
inputs: Vec<f32>,
|
||||||
pub outputs: Vec<f32>,
|
pub outputs: Vec<f32>,
|
||||||
// asteroid_data: Vec<(f32, f32, f32)>,
|
|
||||||
raycasts: Vec<f32>,
|
|
||||||
last_shot: u32,
|
last_shot: u32,
|
||||||
shot_interval: u32,
|
shot_interval: u32,
|
||||||
pub brain: Option<NN>,
|
pub brain: Option<NN>,
|
||||||
alive: bool,
|
alive: bool,
|
||||||
pub lifespan: u32,
|
pub lifespan: u32,
|
||||||
pub shots: u32,
|
pub shots: u32,
|
||||||
|
memory: std::collections::VecDeque<f32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Player {
|
impl Player {
|
||||||
|
@ -40,9 +45,21 @@ impl Player {
|
||||||
Some(mut c) => {
|
Some(mut c) => {
|
||||||
c.retain(|&x| x != 0);
|
c.retain(|&x| x != 0);
|
||||||
// Number of inputs
|
// Number of inputs
|
||||||
c.insert(0, 5);
|
c.insert(
|
||||||
|
0,
|
||||||
|
(INPUTS_PER_ASTEROID * NUM_ASTEROIDS)
|
||||||
|
+ INPUTS_FOR_SHIP
|
||||||
|
+ (VALUES_PER_MEMORY * NUM_MEMORIES),
|
||||||
|
);
|
||||||
// Number of outputs
|
// Number of outputs
|
||||||
c.push(4);
|
c.push(
|
||||||
|
NUM_KEYS
|
||||||
|
+ if NUM_MEMORIES > 0 {
|
||||||
|
VALUES_PER_MEMORY
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
},
|
||||||
|
);
|
||||||
Some(NN::new(c, mut_rate.unwrap(), activ.unwrap()))
|
Some(NN::new(c, mut_rate.unwrap(), activ.unwrap()))
|
||||||
}
|
}
|
||||||
_ => None,
|
_ => None,
|
||||||
|
@ -55,51 +72,18 @@ impl Player {
|
||||||
shot_interval: 18,
|
shot_interval: 18,
|
||||||
alive: true,
|
alive: true,
|
||||||
shots: 4,
|
shots: 4,
|
||||||
outputs: vec![0.; 4],
|
// 4 outputs, 1 for memory
|
||||||
raycasts: vec![0.; 8],
|
outputs: vec![0.; NUM_KEYS + VALUES_PER_MEMORY],
|
||||||
|
memory: vec![0.; VALUES_PER_MEMORY * NUM_MEMORIES].into(),
|
||||||
|
|
||||||
..Default::default()
|
..Default::default()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn check_player_collision(&mut self, asteroid: &Asteroid) -> bool {
|
pub fn check_player_collision(&mut self, asteroid: &Asteroid) -> bool {
|
||||||
// To give more near asteroids data:
|
// Save the asteroid to our asteroids
|
||||||
|
self.asteroids.push(Some(asteroid.clone()));
|
||||||
|
|
||||||
// self.asteroid_data.push((
|
|
||||||
// ((asteroid.pos - self.pos).length() - asteroid.radius) / WIDTH,
|
|
||||||
// self.dir.angle_between(asteroid.pos - self.pos),
|
|
||||||
// (asteroid.vel - self.vel).length(),
|
|
||||||
// ));
|
|
||||||
|
|
||||||
// Single asteroid data:
|
|
||||||
if self.asteroid.is_none()
|
|
||||||
|| (asteroid.pos).distance_squared(self.pos)
|
|
||||||
< self
|
|
||||||
.asteroid
|
|
||||||
.as_ref()
|
|
||||||
.unwrap()
|
|
||||||
.pos
|
|
||||||
.distance_squared(self.pos)
|
|
||||||
{
|
|
||||||
self.asteroid = Some(asteroid.clone());
|
|
||||||
}
|
|
||||||
|
|
||||||
// Try raycasting below:
|
|
||||||
|
|
||||||
// let v = asteroid.pos - self.pos;
|
|
||||||
// for i in 0..4 {
|
|
||||||
// let dir = Vec2::from_angle(PI / 4. * i as f32).rotate(self.dir);
|
|
||||||
// let cross = v.perp_dot(dir);
|
|
||||||
// let dot = v.dot(dir);
|
|
||||||
// if cross.abs() <= asteroid.radius {
|
|
||||||
// self.raycasts[if dot >= 0. { i } else { i + 4 }] = *partial_max(
|
|
||||||
// &self.raycasts[if dot >= 0. { i } else { i + 4 }],
|
|
||||||
// &(100.
|
|
||||||
// / (dot.abs() - (asteroid.radius * asteroid.radius - cross * cross).sqrt())),
|
|
||||||
// )
|
|
||||||
// .unwrap();
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
if asteroid.check_collision(self.pos, 8.) || self.lifespan > 3600 && self.brain.is_some() {
|
if asteroid.check_collision(self.pos, 8.) || self.lifespan > 3600 && self.brain.is_some() {
|
||||||
self.alive = false;
|
self.alive = false;
|
||||||
return true;
|
return true;
|
||||||
|
@ -107,12 +91,28 @@ impl Player {
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn consider_asteroids(pos: Vec2, asteroids: &mut Vec<Option<Asteroid>>) {
|
||||||
|
// Consider the closest asteroids first
|
||||||
|
asteroids.sort_by_key(|ast| match ast {
|
||||||
|
None => i32::MAX,
|
||||||
|
Some(ast) => (dist_wrapping(ast.pos, pos, ast.radius) * 100.) as i32,
|
||||||
|
});
|
||||||
|
// Cull if there are too may asteroids
|
||||||
|
*asteroids = asteroids.iter().cloned().take(NUM_ASTEROIDS).collect();
|
||||||
|
// Insert if there are not enought asteroids
|
||||||
|
if asteroids.len() < NUM_ASTEROIDS {
|
||||||
|
for _ in 0..NUM_ASTEROIDS - asteroids.len() {
|
||||||
|
asteroids.push(None);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert_eq!(asteroids.len(), NUM_ASTEROIDS);
|
||||||
|
}
|
||||||
|
|
||||||
pub fn check_bullet_collisions(&mut self, asteroid: &mut Asteroid) -> bool {
|
pub fn check_bullet_collisions(&mut self, asteroid: &mut Asteroid) -> bool {
|
||||||
for bullet in &mut self.bullets {
|
for bullet in &mut self.bullets {
|
||||||
if asteroid.check_collision(bullet.pos, 0.) {
|
if asteroid.check_collision(bullet.pos, 0.) {
|
||||||
asteroid.alive = false;
|
asteroid.alive = false;
|
||||||
bullet.alive = false;
|
bullet.alive = false;
|
||||||
self.asteroid = None;
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -125,46 +125,55 @@ impl Player {
|
||||||
self.acc = 0.;
|
self.acc = 0.;
|
||||||
self.outputs = vec![0.; 4];
|
self.outputs = vec![0.; 4];
|
||||||
let mut keys = vec![false; 4];
|
let mut keys = vec![false; 4];
|
||||||
if let Some(ast) = self.asteroid.as_ref() {
|
self.inputs = vec![];
|
||||||
self.inputs = vec![
|
// Insert all the asteroid data
|
||||||
(ast.pos - self.pos).length() / HEIGHT,
|
Self::consider_asteroids(self.pos, &mut self.asteroids);
|
||||||
self.dir.angle_between(ast.pos - self.pos),
|
for ast in &self.asteroids {
|
||||||
(ast.vel - self.vel).x * 0.6,
|
if let Some(ast) = ast {
|
||||||
(ast.vel - self.vel).y * 0.6,
|
self.inputs.extend_from_slice(&[
|
||||||
self.rot / TAU as f32,
|
// Distance to asteroid
|
||||||
// self.vel.x / 8.,
|
dist_wrapping(ast.pos, self.pos, ast.radius),
|
||||||
// self.vel.y / 8.,
|
// Angle to asteroid
|
||||||
// self.rot / TAU as f32,
|
self.dir.angle_between(ast.pos - self.pos),
|
||||||
];
|
// Asteroid velocity x
|
||||||
// self.inputs.append(self.raycasts.as_mut());
|
(ast.vel - self.vel).x * 0.6,
|
||||||
|
// Asteroid velocity y
|
||||||
// self.asteroid_data
|
(ast.vel - self.vel).y * 0.6,
|
||||||
// .sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap());
|
]);
|
||||||
// self.asteroid_data.resize(1, (0., 0., 0.));
|
} else {
|
||||||
// inputs.append(
|
self.inputs.extend_from_slice(&[0., 0., 0., 0.]);
|
||||||
// &mut self
|
|
||||||
// .asteroid_data
|
|
||||||
// .iter()
|
|
||||||
// .map(|(d, a, h)| vec![*d, *a, *h])
|
|
||||||
// .flatten()
|
|
||||||
// .collect::<Vec<_>>(),
|
|
||||||
// );
|
|
||||||
|
|
||||||
if let Some(brain) = &self.brain {
|
|
||||||
self.outputs = brain.feed_forward(&self.inputs);
|
|
||||||
keys = self
|
|
||||||
.outputs
|
|
||||||
.iter()
|
|
||||||
.map(|&x| {
|
|
||||||
x > if brain.activ_func == ActivationFunc::Sigmoid {
|
|
||||||
0.85
|
|
||||||
} else {
|
|
||||||
0.
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.collect();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
assert_eq!(self.inputs.len(), NUM_ASTEROIDS * INPUTS_PER_ASTEROID);
|
||||||
|
// Insert the ship data
|
||||||
|
self.inputs.push(self.rot / TAU as f32);
|
||||||
|
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()
|
||||||
|
.map(|&x| {
|
||||||
|
x > if brain.activ_func == ActivationFunc::Sigmoid {
|
||||||
|
0.85
|
||||||
|
} else {
|
||||||
|
0.
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
}
|
||||||
if keys[0] || self.brain.is_none() && is_key_down(KeyCode::Right) {
|
if keys[0] || self.brain.is_none() && is_key_down(KeyCode::Right) {
|
||||||
// RIGHT
|
// RIGHT
|
||||||
self.rot = (self.rot + 0.1 + TAU as f32) % TAU as f32;
|
self.rot = (self.rot + 0.1 + TAU as f32) % TAU as f32;
|
||||||
|
@ -187,6 +196,7 @@ impl Player {
|
||||||
pos: self.pos + self.dir * 20.,
|
pos: self.pos + self.dir * 20.,
|
||||||
vel: self.dir * 8.5 + self.vel,
|
vel: self.dir * 8.5 + self.vel,
|
||||||
alive: true,
|
alive: true,
|
||||||
|
travelled: Vec2::new(0., 0.),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -203,11 +213,8 @@ impl Player {
|
||||||
for bullet in &mut self.bullets {
|
for bullet in &mut self.bullets {
|
||||||
bullet.update();
|
bullet.update();
|
||||||
}
|
}
|
||||||
self.bullets
|
self.bullets.retain(|b| b.alive);
|
||||||
.retain(|b| b.alive && b.pos.x.abs() * 2. < WIDTH && b.pos.y.abs() * 2. < HEIGHT);
|
self.asteroids = vec![];
|
||||||
self.asteroid = None;
|
|
||||||
// self.asteroid_data.clear();
|
|
||||||
// self.raycasts = vec![0.; 8];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn draw(&self, color: Color, debug: bool) {
|
pub fn draw(&self, color: Color, debug: bool) {
|
||||||
|
@ -226,13 +233,17 @@ impl Player {
|
||||||
draw_triangle_lines(p6, p7, p8, 2., color);
|
draw_triangle_lines(p6, p7, p8, 2., color);
|
||||||
}
|
}
|
||||||
if debug {
|
if debug {
|
||||||
if let Some(ast) = self.asteroid.as_ref() {
|
let mut debug_asteroids = self.asteroids.clone();
|
||||||
draw_circle_lines(ast.pos.x, ast.pos.y, ast.radius, 1., RED);
|
Self::consider_asteroids(self.pos, &mut debug_asteroids);
|
||||||
// let p = self.pos
|
for asteroid in &debug_asteroids {
|
||||||
// + self.dir.rotate(Vec2::from_angle(self.asteroid_data[0].1))
|
if let Some(ast) = asteroid {
|
||||||
// * self.asteroid_data[0].0
|
draw_circle_lines(ast.pos.x, ast.pos.y, ast.radius, 1., RED);
|
||||||
// * WIDTH;
|
// let p = self.pos
|
||||||
draw_line(self.pos.x, self.pos.y, ast.pos.x, ast.pos.y, 1., RED);
|
// + self.dir.rotate(Vec2::from_angle(self.asteroid_data[0].1))
|
||||||
|
// * self.asteroid_data[0].0
|
||||||
|
// * WIDTH;
|
||||||
|
draw_line(self.pos.x, self.pos.y, ast.pos.x, ast.pos.y, 1., RED);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Draw raycasts
|
// Draw raycasts
|
||||||
|
@ -266,13 +277,39 @@ struct Bullet {
|
||||||
pos: Vec2,
|
pos: Vec2,
|
||||||
vel: Vec2,
|
vel: Vec2,
|
||||||
alive: bool,
|
alive: bool,
|
||||||
|
travelled: Vec2,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Bullet {
|
impl Bullet {
|
||||||
fn update(&mut self) {
|
fn update(&mut self) {
|
||||||
self.pos += self.vel;
|
self.pos += self.vel;
|
||||||
|
if self.pos.x.abs() > WIDTH * 0.5 {
|
||||||
|
self.pos.x *= -1.;
|
||||||
|
}
|
||||||
|
if self.pos.y.abs() > HEIGHT * 0.5 {
|
||||||
|
self.pos.y *= -1.;
|
||||||
|
}
|
||||||
|
self.travelled += self.vel;
|
||||||
|
if self.travelled.length() >= (WIDTH * WIDTH + HEIGHT * HEIGHT).sqrt() / 2. {
|
||||||
|
self.alive = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
fn draw(&self, c: Color) {
|
fn draw(&self, c: Color) {
|
||||||
draw_circle(self.pos.x, self.pos.y, 2., Color::new(c.r, c.g, c.b, 0.9));
|
draw_circle(self.pos.x, self.pos.y, 2., Color::new(c.r, c.g, c.b, 0.9));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Distance in a toroidal space:
|
||||||
|
// https://blog.demofox.org/2017/10/01/calculating-the-distance-between-points-in-wrap-around-toroidal-space/
|
||||||
|
fn dist_wrapping(a: Vec2, b: Vec2, r: f32) -> f32 {
|
||||||
|
let mut dx = (a.x - b.x).abs();
|
||||||
|
let mut dy = (a.y - b.y).abs();
|
||||||
|
|
||||||
|
if dx > (WIDTH as f32 / 2.) {
|
||||||
|
dx = WIDTH as f32 - dx;
|
||||||
|
}
|
||||||
|
if dy > (HEIGHT as f32 / 2.) {
|
||||||
|
dy = HEIGHT as f32 - dy;
|
||||||
|
}
|
||||||
|
((dx * dx + dy * dy).sqrt() - r) / (WIDTH * WIDTH + HEIGHT * HEIGHT).sqrt()
|
||||||
|
}
|
||||||
|
|
|
@ -150,10 +150,25 @@ impl Population {
|
||||||
let total = self.worlds.iter().fold(0., |acc, x| acc + x.fitness);
|
let total = self.worlds.iter().fold(0., |acc, x| acc + x.fitness);
|
||||||
self.worlds
|
self.worlds
|
||||||
.sort_by(|a, b| b.fitness.partial_cmp(&a.fitness).unwrap());
|
.sort_by(|a, b| b.fitness.partial_cmp(&a.fitness).unwrap());
|
||||||
for i in &self.worlds {
|
let mut scores: std::collections::VecDeque<_> =
|
||||||
println!("Fitness: {}", i.fitness);
|
self.worlds.iter().map(|w| w.fitness as f32).collect();
|
||||||
|
let mean: f32 = scores.iter().sum::<f32>() / scores.len() as f32;
|
||||||
|
while scores.len() > 2 {
|
||||||
|
scores.pop_front();
|
||||||
|
scores.pop_back();
|
||||||
}
|
}
|
||||||
println!("Gen: {}, Fitness: {}", self.gen, self.worlds[0].fitness);
|
if scores.len() == 2 {
|
||||||
|
scores.pop_front();
|
||||||
|
}
|
||||||
|
let median = scores[0];
|
||||||
|
|
||||||
|
println!(
|
||||||
|
"{} {} {} {}",
|
||||||
|
self.worlds[0].fitness,
|
||||||
|
mean,
|
||||||
|
median,
|
||||||
|
self.worlds[self.worlds.len() - 1].fitness
|
||||||
|
);
|
||||||
let mut new_worlds = (0..std::cmp::max(1, self.size / 20))
|
let mut new_worlds = (0..std::cmp::max(1, self.size / 20))
|
||||||
.map(|i| World::simulate(self.worlds[i].see_brain().to_owned()))
|
.map(|i| World::simulate(self.worlds[i].see_brain().to_owned()))
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
27
src/world.rs
27
src/world.rs
|
@ -29,9 +29,6 @@ impl World {
|
||||||
asteroids: vec![
|
asteroids: vec![
|
||||||
Asteroid::new_to(vec2(0., 0.), 1.5, AsteroidSize::Large),
|
Asteroid::new_to(vec2(0., 0.), 1.5, AsteroidSize::Large),
|
||||||
Asteroid::new(AsteroidSize::Large),
|
Asteroid::new(AsteroidSize::Large),
|
||||||
Asteroid::new(AsteroidSize::Large),
|
|
||||||
Asteroid::new(AsteroidSize::Large),
|
|
||||||
Asteroid::new(AsteroidSize::Large),
|
|
||||||
],
|
],
|
||||||
..Default::default()
|
..Default::default()
|
||||||
}
|
}
|
||||||
|
@ -102,8 +99,7 @@ impl World {
|
||||||
self.over = true;
|
self.over = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.fitness =
|
self.fitness = self.score;
|
||||||
(self.score / self.player.shots as f32).powi(2) * self.player.lifespan as f32;
|
|
||||||
self.asteroids.append(&mut to_add);
|
self.asteroids.append(&mut to_add);
|
||||||
self.asteroids.retain(|asteroid| asteroid.alive);
|
self.asteroids.retain(|asteroid| asteroid.alive);
|
||||||
// if self.asteroids.iter().fold(0, |acc, x| {
|
// if self.asteroids.iter().fold(0, |acc, x| {
|
||||||
|
@ -115,7 +111,26 @@ impl World {
|
||||||
// }) < self.max_asteroids
|
// }) < self.max_asteroids
|
||||||
// || self.player.lifespan % 200 == 0
|
// || self.player.lifespan % 200 == 0
|
||||||
// {
|
// {
|
||||||
if self.player.lifespan % 200 == 0 {
|
let num_small: usize = self
|
||||||
|
.asteroids
|
||||||
|
.iter()
|
||||||
|
.filter(|a| a.size == AsteroidSize::Small)
|
||||||
|
.map(|_| 1)
|
||||||
|
.sum();
|
||||||
|
let num_medium: usize = self
|
||||||
|
.asteroids
|
||||||
|
.iter()
|
||||||
|
.filter(|a| a.size == AsteroidSize::Medium)
|
||||||
|
.map(|_| 1)
|
||||||
|
.sum();
|
||||||
|
let num_large: usize = self
|
||||||
|
.asteroids
|
||||||
|
.iter()
|
||||||
|
.filter(|a| a.size == AsteroidSize::Large)
|
||||||
|
.map(|_| 1)
|
||||||
|
.sum();
|
||||||
|
let area = num_small + num_medium * 2 + num_large * 4;
|
||||||
|
if self.player.lifespan % 200 == 0 && area < 12 {
|
||||||
self.asteroids
|
self.asteroids
|
||||||
.push(Asteroid::new_to(self.player.pos, 1.5, AsteroidSize::Large));
|
.push(Asteroid::new_to(self.player.pos, 1.5, AsteroidSize::Large));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue