mod firing_functions;
use rand::{self, Rng};
use firing_functions::sigmoid;
#[derive(Debug, Clone)]
pub struct Perceptron {
pub weight: Vec<f64>,
bias: f64,
}
impl Perceptron {
pub fn new(inputs: usize, bias: f64) -> Self {
Self {
weight: vec![0.0; inputs + 1],
bias,
}
}
pub fn set_weights(&mut self, w_init: f64) {
for i in 0..self.weight.len() {
let mut rng = rand::thread_rng();
self.weight[i] = rng.gen_range(0.0..w_init);
}
}
pub fn run(&mut self, x: Vec<f64>) -> f64 {
let mut node_value = 0.0;
for i in 0..self.weight.len() - 1 {
node_value += self.weight[i] * x[i];
}
node_value += self.bias * self.weight.last().unwrap();
sigmoid(node_value)
}
#[allow(unused)]
pub fn run_or_gate_perceptron(x: Vec<f64>) {
let mut and_gate = Perceptron::new(2, 0.0);
and_gate.set_weights(10.0);
let result = and_gate.run(x);
println!("Outlet {:?}:", result);
}
}