1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
use crate::Scalar;
use serde::{Deserialize, Serialize};
use std::ops::Range;

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[repr(C)]
pub struct Config {
    pub propagation_speed: Scalar,
    pub neuron_potential_decay: Scalar,
    pub action_potential_treshold: Scalar,
    pub receptors_excitation: Scalar,
    pub receptors_inhibition: Scalar,
    pub default_receptors: Range<Scalar>,
    pub synapse_inactivity_time: Scalar,
    pub synapse_reconnection_range: Option<Scalar>,
    pub synapse_overdose_receptors: Option<Scalar>,
    pub synapse_propagation_decay: Scalar,
    pub synapse_new_connection_receptors: Option<Scalar>,
}

impl Default for Config {
    fn default() -> Self {
        Self {
            propagation_speed: 1.0,
            neuron_potential_decay: 1.0,
            action_potential_treshold: 1.0,
            receptors_excitation: 1.0,
            receptors_inhibition: 0.05,
            default_receptors: 0.5..1.5,
            synapse_inactivity_time: 0.05,
            synapse_reconnection_range: None,
            synapse_overdose_receptors: None,
            synapse_propagation_decay: 0.0,
            synapse_new_connection_receptors: None,
        }
    }
}

impl Config {
    pub fn merge(&self, other: &Self) -> Self {
        Self {
            propagation_speed: merge_scalar(self.propagation_speed, other.propagation_speed),
            neuron_potential_decay: merge_scalar(
                self.neuron_potential_decay,
                other.neuron_potential_decay,
            ),
            action_potential_treshold: merge_scalar(
                self.action_potential_treshold,
                other.action_potential_treshold,
            ),
            receptors_excitation: merge_scalar(
                self.receptors_excitation,
                other.receptors_excitation,
            ),
            receptors_inhibition: merge_scalar(
                self.receptors_inhibition,
                other.receptors_inhibition,
            ),
            default_receptors: Range {
                start: merge_scalar(self.default_receptors.start, other.default_receptors.start),
                end: merge_scalar(self.default_receptors.end, other.default_receptors.end),
            },
            synapse_inactivity_time: merge_scalar(
                self.synapse_inactivity_time,
                other.synapse_inactivity_time,
            ),
            synapse_reconnection_range: match (
                self.synapse_reconnection_range,
                other.synapse_reconnection_range,
            ) {
                (Some(a), Some(b)) => Some(merge_scalar(a, b)),
                (Some(a), None) => Some(a),
                (None, Some(b)) => Some(b),
                _ => None,
            },
            synapse_overdose_receptors: match (
                self.synapse_overdose_receptors,
                other.synapse_overdose_receptors,
            ) {
                (Some(a), Some(b)) => Some(merge_scalar(a, b)),
                (Some(a), None) => Some(a),
                (None, Some(b)) => Some(b),
                _ => None,
            },
            synapse_propagation_decay: merge_scalar(
                self.synapse_propagation_decay,
                other.synapse_propagation_decay,
            ),
            synapse_new_connection_receptors: match (
                self.synapse_new_connection_receptors,
                other.synapse_new_connection_receptors,
            ) {
                (Some(a), Some(b)) => Some(merge_scalar(a, b)),
                (Some(a), None) => Some(a),
                (None, Some(b)) => Some(b),
                _ => None,
            },
        }
    }
}

fn merge_scalar(a: Scalar, b: Scalar) -> Scalar {
    (a + b) * 0.5
}