Compare commits

...

11 Commits

Author SHA1 Message Date
Leonora Tindall 3fd3d53be0 Use raw score for fitness 2023-04-03 22:55:59 -05:00
Leonora Tindall f74fed7638 Add extra layer to default config 2023-04-03 22:55:46 -05:00
Leonora Tindall 5378d3a4fb Output max, mean, median, and minimum fitness values 2023-04-03 22:55:10 -05:00
Leonora Tindall 01517cc61c Let asteroids wrap around the screen
They are now killed once having travelled the entire diagonal dimension
of the screen, which removes the last dependence on "true" x/y position.
Now, only relative position matters.
2023-04-03 22:54:02 -05:00
Leonora Tindall 007eb03050 Add shift register memory 2023-04-03 22:52:54 -05:00
Leonora Tindall e63fbf74ba Accept multiple asteroids and shot-time input 2023-04-03 22:52:45 -05:00
Leonora Tindall 36da3483ae Use a more lenient asteroid spawning strategy
Two large asteroids are created to start.
New asteroids are only created when the total asteroid area is below 12.
Large asteroids have 4 area, medium asteroids have 2, and small
asteroids have 1.
Asteroid true sizes are based on multiples of a base number.
2023-04-03 22:36:06 -05:00
Leonora Tindall 154517654e Improve crossover function
Rather than always choosing a parental weight verbatim, weights are
sometimes averaged between parents.
2023-04-03 22:34:28 -05:00
Leonora Tindall d71b437124 Permit larger layers (up to 64 neurons) 2023-04-03 22:34:05 -05:00
Leonora Tindall 49b720baec Use a normal distribution for mutations
Rather than StandardNormal, weights are generated with a Normal
distribution centered on 0 with a standard deviation of 0.75,
and mutations are performed by adding a number from that distribution to
the existing weight, rather than replacing the weight entirely.
2023-04-03 22:32:53 -05:00
Leonora Tindall f04309f678 Ignore testing files 2023-04-03 22:30:51 -05:00
9 changed files with 223 additions and 128 deletions

4
.gitignore vendored
View File

@ -8,3 +8,7 @@ Cargo.lock
# These are backup files generated by rustfmt
**/*.rs.bk
# Gathered data
data/
charts/

7
Cargo.lock generated
View File

@ -142,6 +142,7 @@ dependencies = [
name = "genetic"
version = "0.1.0"
dependencies = [
"lazy_static",
"macroquad",
"nalgebra",
"rand",
@ -203,6 +204,12 @@ version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fad582f4b9e86b6caa621cabeb0963332d92eea04729ab12892c2533951e6440"
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "lewton"
version = "0.9.4"

View File

@ -13,6 +13,7 @@ rand_distr = "0.4.3"
serde = { version = "1.0.152", features = ["derive"] }
serde_json = "1.0.91"
tinyfiledialogs = "3.9.1"
lazy_static = "1.4"
[profile.dev]
opt-level = 3

View File

@ -1,6 +1,6 @@
use crate::{HEIGHT, WIDTH};
use macroquad::{prelude::*, rand::gen_range};
#[derive(Clone)]
#[derive(Clone, PartialEq, Eq)]
pub enum AsteroidSize {
Large,
Medium,
@ -19,12 +19,14 @@ pub struct Asteroid {
pub alive: bool,
}
const BASE_SIZE: f32 = 20.;
impl Asteroid {
pub fn new(size: AsteroidSize) -> Self {
let (sides, radius) = match size {
AsteroidSize::Large => (gen_range(6, 10), gen_range(50., 65.)),
AsteroidSize::Medium => (gen_range(5, 6), gen_range(35., 50.)),
AsteroidSize::Small => (gen_range(3, 5), 25.),
AsteroidSize::Large => (gen_range(6, 10), BASE_SIZE * 4.),
AsteroidSize::Medium => (gen_range(5, 6), BASE_SIZE * 2.),
AsteroidSize::Small => (gen_range(3, 5), BASE_SIZE),
};
let mut r = vec2(
if gen_range(0., 1.) > 0.5 { -1. } else { 1. },

View File

@ -70,7 +70,7 @@ async fn main() {
let mut size: u32 = 100;
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 mut_rate = 0.05;
@ -94,9 +94,8 @@ async fn main() {
);
let ui_thick = 34.;
let nums = &[
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16",
];
let nums_owning: Vec<String> = (0..=64).into_iter().map(|i| format!("{:00}", i)).collect();
let nums: Vec<&str> = nums_owning.iter().map(|v| v.as_str()).collect();
let skin = skins::get_ui_skin();
let skin2 = skins::get_white_buttons_skin();
let skin3 = skins::get_green_buttons_skin();
@ -390,9 +389,9 @@ async fn main() {
ui.label(None, "Hidden Layers");
ui.label(None, "Neurons Config");
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 3", nums, &mut hlayers[2]);
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 3", &nums, &mut hlayers[2]);
if prev_hlayers != hlayers {
pop = Population::new(
size as usize,

View File

@ -1,10 +1,14 @@
use macroquad::{prelude::*, rand::gen_range};
use nalgebra::*;
use r::Rng;
use rand_distr::StandardNormal;
use rand_distr::Normal;
use serde::{Deserialize, Serialize};
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)]
pub enum ActivationFunc {
@ -39,8 +43,12 @@ impl NN {
.zip(config.iter().skip(1))
.map(|(&curr, &last)| {
// DMatrix::from_fn(last, curr + 1, |_, _| gen_range(-1., 1.))
DMatrix::<f32>::from_distribution(last, curr + 1, &StandardNormal, &mut rng)
* (2. / last as f32).sqrt()
DMatrix::<f32>::from_distribution(
last,
curr + 1,
&*CONNECTION_DISTRIBUTION,
&mut rng,
) * (2. / last as f32).sqrt()
})
.collect(),
@ -60,10 +68,16 @@ impl NN {
.iter()
.zip(b.weights.iter())
.map(|(m1, m2)| {
m1.zip_map(
m2,
|ele1, ele2| if gen_range(0., 1.) < 0.5 { ele1 } else { ele2 },
)
m1.zip_map(m2, |ele1, ele2| {
let choice = gen_range(0., 3.);
if choice < 1. {
ele1
} else if choice < 2. {
ele2
} else {
(ele1 + ele2) / 2.
}
})
})
.collect(),
}
@ -75,7 +89,9 @@ impl NN {
if gen_range(0., 1.) < self.mut_rate {
// *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);
let mut y = DMatrix::from_vec(inputs.len(), 1, inputs.to_vec());
for i in 0..self.config.len() - 1 {
y = (&self.weights[i] * y.insert_row(self.config[i] - 1, 1.)).map(|x| {
match self.activ_func {
ActivationFunc::ReLU => x.max(0.),
ActivationFunc::Sigmoid => 1. / (1. + (-x).exp()),
ActivationFunc::Tanh => x.tanh(),
}
let row = y.insert_row(self.config[i] - 1, 1.);
y = (&self.weights[i] * row).map(|x| match self.activ_func {
ActivationFunc::ReLU => x.max(0.),
ActivationFunc::Sigmoid => 1. / (1. + (-x).exp()),
ActivationFunc::Tanh => x.tanh(),
});
}
y.column(0).data.into_slice().to_vec()

View File

@ -7,6 +7,12 @@ use crate::{
nn::{ActivationFunc, NN},
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)]
pub struct Player {
pub pos: Vec2,
@ -16,17 +22,16 @@ pub struct Player {
rot: f32,
drag: f32,
bullets: Vec<Bullet>,
asteroid: Option<Asteroid>,
asteroids: Vec<Option<Asteroid>>,
inputs: Vec<f32>,
pub outputs: Vec<f32>,
// asteroid_data: Vec<(f32, f32, f32)>,
raycasts: Vec<f32>,
last_shot: u32,
shot_interval: u32,
pub brain: Option<NN>,
alive: bool,
pub lifespan: u32,
pub shots: u32,
memory: std::collections::VecDeque<f32>,
}
impl Player {
@ -40,9 +45,21 @@ impl Player {
Some(mut c) => {
c.retain(|&x| x != 0);
// 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
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()))
}
_ => None,
@ -55,51 +72,18 @@ impl Player {
shot_interval: 18,
alive: true,
shots: 4,
outputs: vec![0.; 4],
raycasts: vec![0.; 8],
// 4 outputs, 1 for memory
outputs: vec![0.; NUM_KEYS + VALUES_PER_MEMORY],
memory: vec![0.; VALUES_PER_MEMORY * NUM_MEMORIES].into(),
..Default::default()
}
}
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() {
self.alive = false;
return true;
@ -107,12 +91,28 @@ impl Player {
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 {
for bullet in &mut self.bullets {
if asteroid.check_collision(bullet.pos, 0.) {
asteroid.alive = false;
bullet.alive = false;
self.asteroid = None;
return true;
}
}
@ -125,46 +125,55 @@ impl Player {
self.acc = 0.;
self.outputs = vec![0.; 4];
let mut keys = vec![false; 4];
if let Some(ast) = self.asteroid.as_ref() {
self.inputs = vec![
(ast.pos - self.pos).length() / HEIGHT,
self.dir.angle_between(ast.pos - self.pos),
(ast.vel - self.vel).x * 0.6,
(ast.vel - self.vel).y * 0.6,
self.rot / TAU as f32,
// self.vel.x / 8.,
// self.vel.y / 8.,
// self.rot / TAU as f32,
];
// self.inputs.append(self.raycasts.as_mut());
// self.asteroid_data
// .sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap());
// self.asteroid_data.resize(1, (0., 0., 0.));
// inputs.append(
// &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();
self.inputs = vec![];
// Insert all the asteroid data
Self::consider_asteroids(self.pos, &mut self.asteroids);
for ast in &self.asteroids {
if let Some(ast) = ast {
self.inputs.extend_from_slice(&[
// Distance to asteroid
dist_wrapping(ast.pos, self.pos, ast.radius),
// Angle to asteroid
self.dir.angle_between(ast.pos - self.pos),
// Asteroid velocity x
(ast.vel - self.vel).x * 0.6,
// Asteroid velocity y
(ast.vel - self.vel).y * 0.6,
]);
} else {
self.inputs.extend_from_slice(&[0., 0., 0., 0.]);
}
}
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) {
// RIGHT
self.rot = (self.rot + 0.1 + TAU as f32) % TAU as f32;
@ -187,6 +196,7 @@ impl Player {
pos: self.pos + self.dir * 20.,
vel: self.dir * 8.5 + self.vel,
alive: true,
travelled: Vec2::new(0., 0.),
});
}
}
@ -203,11 +213,8 @@ impl Player {
for bullet in &mut self.bullets {
bullet.update();
}
self.bullets
.retain(|b| b.alive && b.pos.x.abs() * 2. < WIDTH && b.pos.y.abs() * 2. < HEIGHT);
self.asteroid = None;
// self.asteroid_data.clear();
// self.raycasts = vec![0.; 8];
self.bullets.retain(|b| b.alive);
self.asteroids = vec![];
}
pub fn draw(&self, color: Color, debug: bool) {
@ -226,13 +233,17 @@ impl Player {
draw_triangle_lines(p6, p7, p8, 2., color);
}
if debug {
if let Some(ast) = self.asteroid.as_ref() {
draw_circle_lines(ast.pos.x, ast.pos.y, ast.radius, 1., RED);
// let p = self.pos
// + 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);
let mut debug_asteroids = self.asteroids.clone();
Self::consider_asteroids(self.pos, &mut debug_asteroids);
for asteroid in &debug_asteroids {
if let Some(ast) = asteroid {
draw_circle_lines(ast.pos.x, ast.pos.y, ast.radius, 1., RED);
// let p = self.pos
// + 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
@ -266,13 +277,39 @@ struct Bullet {
pos: Vec2,
vel: Vec2,
alive: bool,
travelled: Vec2,
}
impl Bullet {
fn update(&mut self) {
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) {
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()
}

View File

@ -150,10 +150,25 @@ impl Population {
let total = self.worlds.iter().fold(0., |acc, x| acc + x.fitness);
self.worlds
.sort_by(|a, b| b.fitness.partial_cmp(&a.fitness).unwrap());
for i in &self.worlds {
println!("Fitness: {}", i.fitness);
let mut scores: std::collections::VecDeque<_> =
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))
.map(|i| World::simulate(self.worlds[i].see_brain().to_owned()))
.collect::<Vec<_>>();

View File

@ -29,9 +29,6 @@ impl World {
asteroids: vec![
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),
],
..Default::default()
}
@ -102,8 +99,7 @@ impl World {
self.over = true;
}
}
self.fitness =
(self.score / self.player.shots as f32).powi(2) * self.player.lifespan as f32;
self.fitness = self.score;
self.asteroids.append(&mut to_add);
self.asteroids.retain(|asteroid| asteroid.alive);
// if self.asteroids.iter().fold(0, |acc, x| {
@ -115,7 +111,26 @@ impl World {
// }) < self.max_asteroids
// || 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
.push(Asteroid::new_to(self.player.pos, 1.5, AsteroidSize::Large));
}