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 nn;
mod player;
mod population;
mod world;
use macroquad::prelude::*;
@ -15,11 +16,8 @@ async fn main() {
..Default::default()
};
set_camera(&cam);
let mut world = World::new();
// let mut world = World::new();
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 {
// clear_background(BLACK);

View File

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

View File

@ -2,7 +2,7 @@ use std::{f32::consts::PI, path::Iter};
use macroquad::{prelude::*, rand::gen_range};
use crate::asteroids::Asteroid;
use crate::{asteroids::Asteroid, nn::NN};
#[derive(Default)]
pub struct Player {
pos: Vec2,
@ -13,6 +13,7 @@ pub struct Player {
bullets: Vec<Bullet>,
last_shot: f32,
shot_interval: f32,
brain: Option<NN>,
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 {
if asteroid.check_collision(self.pos, 8.) {
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::{
asteroids::{Asteroid, AsteroidSize},
nn::NN,
player::Player,
};
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) {
self.player.update();
let mut to_add: Vec<Asteroid> = Vec::new();