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 122 additions and 49 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,14 +70,14 @@ async fn main() {
let mut size: u32 = 100;
let mut world: World = World::new(None, None, None);
let mut hlayers: Vec<usize> = vec![20, 6, 0];
let mut hlayers: Vec<usize> = vec![6, 6, 6];
let mut prev_hlayers = hlayers.clone();
let mut mut_rate = 0.05;
let mut prev_mut_rate = 0.05;
let mut activ: usize = 2;
let mut prev_activ: usize = 2;
let mut activ: usize = 0;
let mut prev_activ: usize = 0;
let activs = [
ActivationFunc::ReLU,
ActivationFunc::Sigmoid,
@ -94,7 +94,7 @@ async fn main() {
);
let ui_thick = 34.;
let nums_owning: Vec<String> = (0..=20).into_iter().map(|i| format!("{:00}", i)).collect();
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();

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);
}
}
}
@ -86,12 +102,10 @@ impl NN {
let mut y = DMatrix::from_vec(inputs.len(), 1, inputs.to_vec());
for i in 0..self.config.len() - 1 {
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 = (&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

@ -9,10 +9,10 @@ use crate::{
};
const NUM_KEYS: usize = 4;
const INPUTS_PER_ASTEROID: usize = 4;
const NUM_ASTEROIDS: usize = 5;
const INPUTS_FOR_SHIP: usize = 1;
const NUM_ASTEROIDS: usize = 1;
const INPUTS_FOR_SHIP: usize = 2;
const VALUES_PER_MEMORY: usize = 1;
const NUM_MEMORIES: usize = 3;
const NUM_MEMORIES: usize = 0;
#[derive(Default)]
pub struct Player {
pub pos: Vec2,
@ -52,7 +52,14 @@ impl Player {
+ (VALUES_PER_MEMORY * NUM_MEMORIES),
);
// Number of outputs
c.push(NUM_KEYS + VALUES_PER_MEMORY);
c.push(
NUM_KEYS
+ if NUM_MEMORIES > 0 {
VALUES_PER_MEMORY
} else {
0
},
);
Some(NN::new(c, mut_rate.unwrap(), activ.unwrap()))
}
_ => None,
@ -88,7 +95,7 @@ impl Player {
// Consider the closest asteroids first
asteroids.sort_by_key(|ast| match ast {
None => i32::MAX,
Some(ast) => (dist_wrapping(ast.pos, pos) * 100.) as i32,
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();
@ -125,7 +132,7 @@ impl Player {
if let Some(ast) = ast {
self.inputs.extend_from_slice(&[
// Distance to asteroid
dist_wrapping(ast.pos, self.pos),
dist_wrapping(ast.pos, self.pos, ast.radius),
// Angle to asteroid
self.dir.angle_between(ast.pos - self.pos),
// Asteroid velocity x
@ -140,6 +147,9 @@ impl Player {
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.));
@ -148,8 +158,10 @@ impl Player {
if let Some(brain) = &self.brain {
assert_eq!(self.inputs.len(), brain.config[0] - 1);
self.outputs = brain.feed_forward(&self.inputs);
self.memory.push_back(self.outputs[self.outputs.len() - 1]);
self.memory.pop_front();
if NUM_MEMORIES > 0 {
self.memory.push_back(self.outputs[self.outputs.len() - 1]);
self.memory.pop_front();
}
keys = self
.outputs
.iter()
@ -201,8 +213,7 @@ impl Player {
for bullet in &mut self.bullets {
bullet.update();
}
self.bullets
.retain(|b| b.alive);
self.bullets.retain(|b| b.alive);
self.asteroids = vec![];
}
@ -225,14 +236,14 @@ impl Player {
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);
}
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
@ -279,7 +290,7 @@ impl Bullet {
self.pos.y *= -1.;
}
self.travelled += self.vel;
if self.travelled.length() >= (WIDTH*WIDTH + HEIGHT*HEIGHT).sqrt() / 2. {
if self.travelled.length() >= (WIDTH * WIDTH + HEIGHT * HEIGHT).sqrt() / 2. {
self.alive = false;
}
}
@ -290,7 +301,7 @@ impl Bullet {
// 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) -> f32 {
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();
@ -300,5 +311,5 @@ fn dist_wrapping(a: Vec2, b: Vec2) -> f32 {
if dy > (HEIGHT as f32 / 2.) {
dy = HEIGHT as f32 - dy;
}
(dx*dx + dy*dy).sqrt() / (WIDTH*WIDTH + HEIGHT*HEIGHT).sqrt()
((dx * dx + dy * dy).sqrt() - r) / (WIDTH * WIDTH + HEIGHT * HEIGHT).sqrt()
}

View File

@ -150,7 +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());
println!("{}", self.worlds[0].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();
}
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,7 +99,7 @@ impl World {
self.over = true;
}
}
self.fitness = (self.score 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| {
@ -114,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));
}