From be9af4c7b1c5140aee7074b6c0349caf55306972 Mon Sep 17 00:00:00 2001 From: fabolous005 Date: Tue, 7 Oct 2025 12:16:58 +0200 Subject: [PATCH] config file + first network layer --- Cargo.toml | 2 ++ src/config.rs | 44 ++++++++++++++++++++++++++++++++++++++++ src/main.rs | 16 ++++++++++++++- src/sensors/sight.rs | 7 +++++-- src/structure/mod.rs | 1 + src/structure/network.rs | 36 ++++++++++++++++++++++++++++++++ src/structure/neuron.rs | 12 +++++++++++ 7 files changed, 115 insertions(+), 3 deletions(-) create mode 100644 src/config.rs create mode 100644 src/structure/network.rs diff --git a/Cargo.toml b/Cargo.toml index 748d461..10d929a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,3 +4,5 @@ version = "0.1.0" edition = "2024" [dependencies] +serde = { version = "1.0.228", features = ["derive"] } +toml = "0.9.7" diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..bc7652d --- /dev/null +++ b/src/config.rs @@ -0,0 +1,44 @@ +use serde::Deserialize; +use std::collections::HashMap; +use std::fs; + +#[derive(Deserialize, Clone)] +pub struct BrainRegionConfig { + name: String, + input_size: usize, + connection_likeliness: i8, +} + +impl BrainRegionConfig { + pub fn set_name(&mut self, name: String) { + self.name = name + } +} + +impl BrainRegionConfig { + pub fn input_size(&self) -> usize { + self.input_size + } +} + +#[derive(Deserialize, Clone)] +pub struct BrainConfig { + region: HashMap, + + network_file: String +} + +impl BrainConfig { + pub fn from_string(path: &str) -> Self { + let toml_str = fs::read_to_string(path).unwrap(); + toml::from_str(&toml_str).unwrap() + } + + pub fn regions(self) -> HashMap { + std::mem::take(&mut self.region.clone()) + } + + pub fn network_file(self) -> String { + self.network_file + } +} diff --git a/src/main.rs b/src/main.rs index 52b37c0..a865679 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,20 @@ mod structure; mod sensors; +mod config; + +use crate::structure::network::Network; +use std::{path::Path}; +use crate::config::BrainConfig; + fn main() { - println!("Hello, world!"); + let config_file = "./config.toml"; + + let config = BrainConfig::from_string(config_file); + + let network = if ! Path::exists(Path::new(&config_file)) { + Network::new(config.regions()) + } else { + Network::from_file(config.network_file()) + }; } diff --git a/src/sensors/sight.rs b/src/sensors/sight.rs index a2d674c..d733a1c 100644 --- a/src/sensors/sight.rs +++ b/src/sensors/sight.rs @@ -12,11 +12,14 @@ change rate of structures focus <= prediction */ -pub struct Sight; +pub enum Senses { + Sight, + Hearing, +} const SIZE: usize = 1024; -impl Sensor for Sight { +impl Sensor for Senses { fn input(&self, input: [f32; SIZE]) { todo!() diff --git a/src/structure/mod.rs b/src/structure/mod.rs index 8c64d44..864c669 100644 --- a/src/structure/mod.rs +++ b/src/structure/mod.rs @@ -1,2 +1,3 @@ pub mod neuron; pub mod sensoric; +pub mod network; diff --git a/src/structure/network.rs b/src/structure/network.rs new file mode 100644 index 0000000..46be1f9 --- /dev/null +++ b/src/structure/network.rs @@ -0,0 +1,36 @@ +use std::str; +use std::array; +use crate::structure::neuron::Neuron; +use crate::config::BrainRegionConfig; +use std::collections::HashMap; + + +pub struct BrainRegion { + config: BrainRegionConfig, + nodes: Vec +} + +impl BrainRegion { + fn new(mut config: BrainRegionConfig, name: String) -> Self { + config.set_name(name); + Self { + nodes: vec![Neuron::default(); config.input_size()], + config, + } + } +} + +pub struct Network { + parts: Vec +} + +impl Network { + pub fn new(region_config: HashMap) -> Self { + let parts = region_config.into_iter().map(|config| BrainRegion::new(config.1, config.0)).collect(); + Self { parts } + } + + pub fn from_file(path: String) -> Self { + todo!() + } +} diff --git a/src/structure/neuron.rs b/src/structure/neuron.rs index b75f0b6..1f383cf 100644 --- a/src/structure/neuron.rs +++ b/src/structure/neuron.rs @@ -17,4 +17,16 @@ systems */ +#[derive(Clone)] +struct Connection { + weight: u8, + id: usize +} +#[derive(Default, Clone)] +pub struct Neuron { + id: usize, + connections: Vec, + state: f32, + difference: f32 +}