diff --git a/Assets/NanoBrain/Cluster.cs b/Assets/NanoBrain/Cluster.cs index cae5110..973ab4e 100644 --- a/Assets/NanoBrain/Cluster.cs +++ b/Assets/NanoBrain/Cluster.cs @@ -1,7 +1,5 @@ using System.Collections.Generic; using UnityEngine; -using Unity.Mathematics; -using static Unity.Mathematics.math; [CreateAssetMenu(menuName = "Passer/Cluster")] public class Cluster : ScriptableObject { @@ -35,30 +33,6 @@ public class Cluster : ScriptableObject { } } - // The synapses of all inputs - private readonly List _synapses = new(); - public List synapses => _synapses; - - public NucleusArray array { get; set; } - - public List receivers { - get { return this.output.receivers; } - set { this.output.receivers = value; } - } - // public List clusterReceivers { - // get { return this.output.clusterReceivers; } - // set { this.output.clusterReceivers = clusterReceivers; } - // } - // public IEnumerable allReceivers { - // get => output.allReceivers; - // } - - // public INucleus Clone() { - // Cluster clone = CreateInstance(); - // // Lots to add here... - // return clone; - // } - // Call this function to ensure that there is at least one nucleus // This is an invariant and should be ensured before the nucleus is used // because output requires it. @@ -67,38 +41,12 @@ public class Cluster : ScriptableObject { if (nuclei.Count == 0) new Neuron(this, "Output"); // Every cluster should have at least 1 neuron } -/* - public void AddReceiver(INucleus receivingNucleus) { - //output.AddReceiver(receiver); - this.output.receivers.Add(receivingNucleus); - receivingNucleus.AddClusterSynapse(this); - } - public void AddClusterReceiver(Cluster clusterReceiver) { - this.output.clusterReceivers.Add(clusterReceiver); - clusterReceiver.AddClusterSynapse(this); - } - public void RemoveReceiver(INucleus receiver) { - output.RemoveReceiver(receiver); - } - - public Synapse AddSynapse(IReceptor sender) { - Synapse synapse = new(sender); - synapses.Add(synapse); - return synapse; - } - public Synapse AddClusterSynapse(Cluster sender) { - Synapse synapse = new(sender); - synapses.Add(synapse); - return synapse; - } -*/ public void GarbageCollection() { HashSet visitedNuclei = new(); MarkNuclei(visitedNuclei, this.output); //Debug.Log($"Garbage collection found {visitedNuclei.Count} Nuclei"); this.nuclei.RemoveAll(nucleus => nucleus is INucleus n && visitedNuclei.Contains(n) == false); - //this.perceptei.RemoveAll(perceptoid => visitedNuclei.Contains(perceptoid) == false); } public void MarkNuclei(HashSet visitedNuclei, INucleus nucleus) { @@ -129,22 +77,8 @@ public class Cluster : ScriptableObject { } } - #region Dynamics - - public float3 outputValue => this.output.outputValue; - public bool isSleeping => lengthsq(this.outputValue) == 0; - - public void UpdateState() { - // Don't know if this is right - this.output.UpdateState(); - // it is not, I should take inputs from the synapses and route them to the right internal nuclei - } - public void UpdateNuclei() { foreach (IReceptor nucleus in this.nuclei) nucleus.UpdateNuclei(); - } - - #endregion Dynamics } \ No newline at end of file diff --git a/Assets/NanoBrain/ClusterInstance.cs b/Assets/NanoBrain/ClusterInstance.cs index 1dbb40d..7236fbe 100644 --- a/Assets/NanoBrain/ClusterInstance.cs +++ b/Assets/NanoBrain/ClusterInstance.cs @@ -1,4 +1,3 @@ -using System; using System.Collections.Generic; using UnityEngine; using Unity.Mathematics; @@ -11,14 +10,18 @@ public class ClusterInstance : INucleus { public Cluster cluster => this.asset; + public List dynamicNuclei = new(); + public INucleus output => this.asset.nuclei[0] as INucleus; public float3 outputValue => this.output.outputValue; public ClusterInstance(Cluster asset) { this.asset = asset; + foreach (IReceptor nucleus in this.asset.nuclei) + dynamicNuclei.Add(nucleus.Clone()); } - public INucleus Clone() { + public IReceptor Clone() { ClusterInstance clone = new(this.asset); return clone; } @@ -116,7 +119,15 @@ public class ClusterInstance : INucleus { #region Update - public void UpdateState() { } + public void UpdateState() { + float3 sum = new(0, 0, 0); + + //Applying the weight factgors + foreach (Synapse synapse in this.synapses) { + sum += synapse.weight * synapse.nucleus.outputValue; + } + this.asset.inputs[0].UpdateState(); + } public void UpdateNuclei() { foreach (IReceptor nucleus in this.asset.nuclei) diff --git a/Assets/NanoBrain/INucleus.cs b/Assets/NanoBrain/INucleus.cs index 8cc75de..a4b7657 100644 --- a/Assets/NanoBrain/INucleus.cs +++ b/Assets/NanoBrain/INucleus.cs @@ -22,8 +22,6 @@ public interface INucleus : IReceptor { #endregion dynamic state - - public INucleus Clone(); } public interface IReceptor { @@ -48,5 +46,7 @@ public interface IReceptor { public bool isSleeping { get; } #endregion dynamic + + public IReceptor Clone(); } diff --git a/Assets/NanoBrain/Neuron.cs b/Assets/NanoBrain/Neuron.cs index df4e965..17ed928 100644 --- a/Assets/NanoBrain/Neuron.cs +++ b/Assets/NanoBrain/Neuron.cs @@ -165,8 +165,9 @@ public class Neuron : INucleus { #endregion Runtime state - public Neuron(Cluster brain, string name) : this(name) { + public Neuron(Cluster brain, string name) { this.cluster = brain; + this.name = name; if (this.cluster != null) { this.cluster.nuclei.Add(this); } @@ -174,13 +175,12 @@ public class Neuron : INucleus { Debug.LogError("No neuroid network"); } - public Neuron(string name) { - this._name = name; - } + // public Neuron(string name) { + // this._name = name; + // } - public virtual INucleus Clone() { - Neuron clone = new(this.name) { - cluster = this.cluster, + public virtual IReceptor Clone() { + Neuron clone = new(this.cluster, this.name) { array = this.array, curve = this.curve, curvePreset = this.curvePreset, diff --git a/Assets/NanoBrain/NucleusArray.cs b/Assets/NanoBrain/NucleusArray.cs index bf46995..ed14af8 100644 --- a/Assets/NanoBrain/NucleusArray.cs +++ b/Assets/NanoBrain/NucleusArray.cs @@ -42,7 +42,8 @@ public class NucleusArray { for (int i = 0; i < this._nuclei.Length; i++) newArray[i] = this._nuclei[i]; - newArray[newLength - 1] = this._nuclei[0].Clone(); + if (this._nuclei[0] is INucleus nucleus) + newArray[newLength - 1] = (INucleus) nucleus.Clone(); this._nuclei = newArray; } diff --git a/Assets/NanoBrain/Receptor.cs b/Assets/NanoBrain/Receptor.cs index a743974..18443e5 100644 --- a/Assets/NanoBrain/Receptor.cs +++ b/Assets/NanoBrain/Receptor.cs @@ -1,11 +1,12 @@ -using System; -using System.Linq; using System.Collections.Generic; using UnityEngine; using Unity.Mathematics; using static Unity.Mathematics.math; public class Receptor : IReceptor { + + private Cluster cluster; + [SerializeField] protected string _name; public virtual string name { @@ -13,6 +14,47 @@ public class Receptor : IReceptor { set => _name = value; } + public Receptor(Cluster cluster) { + this.cluster = cluster; + if (cluster != null) + cluster.nuclei.Add(this); + } + + public Receptor(Cluster cluster, INucleus nucleus) { + this.cluster = cluster; + if (cluster != null) + cluster.nuclei.Add(this); + this.AddReceiver(nucleus); + } + + public static Receptor CreateReceptor(Cluster cluster, string nucleusName) { + if (cluster == null) + return null; + + Receptor receptor = new(cluster); + foreach (INucleus nucleus in cluster.inputs) { + if (nucleus != null && nucleus.name == nucleusName) { + // Receptor receptor = new(cluster, nucleus); + // return receptor; + receptor.AddReceiver(nucleus); + } + } + if (receptor._receivers.Count == 0) + return null; + else + return receptor; + } + + public virtual IReceptor Clone() { + Receptor clone = new(this.cluster); + + foreach (INucleus receiver in this.receivers) { + clone.AddReceiver(receiver); + } + + return clone; + } + class Receiver { public INucleus nucleus; public int thingId; @@ -69,35 +111,6 @@ public class Receptor : IReceptor { } } - public Receptor(Cluster cluster) { - if (cluster != null) - cluster.nuclei.Add(this); - } - - public Receptor(Cluster cluster, INucleus nucleus) { - if (cluster != null) - cluster.nuclei.Add(this); - this.AddReceiver(nucleus); - } - - public static Receptor CreateReceptor(Cluster cluster, string nucleusName) { - if (cluster == null) - return null; - - Receptor receptor = new(cluster); - foreach (INucleus nucleus in cluster.inputs) { - if (nucleus != null && nucleus.name == nucleusName) { - // Receptor receptor = new(cluster, nucleus); - // return receptor; - receptor.AddReceiver(nucleus); - } - } - if (receptor._receivers.Count == 0) - return null; - else - return receptor; - } - public virtual void ProcessStimulus(int thingId, Vector3 newLocalPositionVector, string thingName = null) { this.localPosition = newLocalPositionVector;