population

This commit is contained in:
sparshg 2022-10-10 01:41:24 +05:30
parent 91bc4e27a4
commit 4084a43acc
5 changed files with 37 additions and 6 deletions

View File

@ -1,6 +1,7 @@
mod asteroids; mod asteroids;
mod nn; mod nn;
mod player; mod player;
mod population;
mod world; mod world;
use macroquad::prelude::*; use macroquad::prelude::*;
@ -15,11 +16,8 @@ async fn main() {
..Default::default() ..Default::default()
}; };
set_camera(&cam); set_camera(&cam);
let mut world = World::new(); // let mut world = World::new();
let mut nn = NN::new(vec![1, 2, 1]); let mut nn = NN::new(vec![1, 2, 1]);
println!("{} {}", nn.weights[0], nn.weights[1]);
nn.mutation();
println!("{} {}", nn.weights[0], nn.weights[1]);
loop { loop {
// clear_background(BLACK); // clear_background(BLACK);

View File

@ -11,7 +11,7 @@ enum ActivationFunc {
} }
pub struct NN { pub struct NN {
config: Vec<usize>, config: Vec<usize>,
pub weights: Vec<DMatrix<f32>>, weights: Vec<DMatrix<f32>>,
activ_func: ActivationFunc, activ_func: ActivationFunc,
mut_rate: f32, mut_rate: f32,
} }

View File

@ -2,7 +2,7 @@ use std::{f32::consts::PI, path::Iter};
use macroquad::{prelude::*, rand::gen_range}; use macroquad::{prelude::*, rand::gen_range};
use crate::asteroids::Asteroid; use crate::{asteroids::Asteroid, nn::NN};
#[derive(Default)] #[derive(Default)]
pub struct Player { pub struct Player {
pos: Vec2, pos: Vec2,
@ -13,6 +13,7 @@ pub struct Player {
bullets: Vec<Bullet>, bullets: Vec<Bullet>,
last_shot: f32, last_shot: f32,
shot_interval: f32, shot_interval: f32,
brain: Option<NN>,
alive: bool, alive: bool,
} }
@ -28,6 +29,12 @@ impl Player {
} }
} }
pub fn simulate(brain: NN) -> Self {
let mut p = Player::new();
p.brain = Some(brain);
p
}
pub fn check_player_collision(&mut self, asteroid: &mut Asteroid) -> bool { pub fn check_player_collision(&mut self, asteroid: &mut Asteroid) -> bool {
if asteroid.check_collision(self.pos, 8.) { if asteroid.check_collision(self.pos, 8.) {
self.alive = false; self.alive = false;

18
src/population.rs Normal file
View File

@ -0,0 +1,18 @@
use crate::world::World;
#[derive(Default)]
struct Population {
size: i32,
gen: i32,
worlds: Vec<World>,
}
impl Population {
// pub fn new(size: i32) -> Self {
// Self {
// size: size,
// worlds: vec![World::new(); size],
// ..Default::default(),
// }
// }
}

View File

@ -1,5 +1,6 @@
use crate::{ use crate::{
asteroids::{Asteroid, AsteroidSize}, asteroids::{Asteroid, AsteroidSize},
nn::NN,
player::Player, player::Player,
}; };
use macroquad::{prelude::*, rand::gen_range}; use macroquad::{prelude::*, rand::gen_range};
@ -20,6 +21,13 @@ impl World {
} }
} }
pub fn simulate(brain: NN) -> Self {
Self {
player: Player::simulate(brain),
..Default::default()
}
}
pub fn update(&mut self) { pub fn update(&mut self) {
self.player.update(); self.player.update();
let mut to_add: Vec<Asteroid> = Vec::new(); let mut to_add: Vec<Asteroid> = Vec::new();