Removed commented out code
All checks were successful
Copy Documentation to webserver / copy-documentation (push) Successful in 22s

This commit is contained in:
Pascal Serrarens 2026-05-08 08:58:07 +02:00
parent ce1ff796b8
commit 4235f260b4
3 changed files with 37 additions and 369 deletions

View File

@ -194,87 +194,6 @@ namespace NanoBrain {
} }
} }
// /// <summary>
// /// Sort the nuclei in a correct evaluation order
// /// </summary>
// /// <param name="nodes"></param>
// /// <returns></returns>
// /// <exception cref="InvalidOperationException"></exception>
// private List<Nucleus> TopologicalSort(List<Nucleus> nodes) {
// Dictionary<Nucleus, int> inDegree = new();
// foreach (Nucleus node in nodes)
// inDegree[node] = 0; // Initialize in-degree to zero
// // Calculate in-degrees
// foreach (Nucleus node in nodes) {
// if (node is Cluster cluster) {
// foreach (Nucleus receiver in cluster.CollectReceivers())
// inDegree[receiver]++;
// }
// else if (node is Neuron neuron) {
// foreach (Nucleus receiver in neuron.receivers)
// inDegree[receiver]++;
// }
// }
// Queue<Nucleus> queue = new();
// foreach (Nucleus node in nodes) {
// if (inDegree[node] == 0) // Nodes with no dependencies
// queue.Enqueue(node);
// }
// // The queue basically stores all input nuclei?
// List<Nucleus> sortedOrder = new();
// while (queue.Count > 0) {
// Nucleus current = queue.Dequeue();
// sortedOrder.Add(current); // Process the node
// if (current is Neuron neuron) {
// foreach (Nucleus receiver in neuron.receivers) {
// inDegree[receiver]--;
// if (inDegree[receiver] == 0) // If all dependencies resolved
// queue.Enqueue(receiver);
// }
// }
// else if (current is Cluster cluster) {
// foreach (Nucleus receiver in cluster.CollectReceivers()) {
// inDegree[receiver]--;
// if (inDegree[receiver] == 0) // If all dependencies resolved
// queue.Enqueue(receiver);
// }
// }
// }
// // Check for cycles in the graph
// if (sortedOrder.Count != nodes.Count)
// throw new InvalidOperationException("Graph is not a DAG; a cycle exists.");
// return sortedOrder;
// }
// public override Nucleus Clone(ClusterPrefab parent) {
// Cluster clone = new(this.prefab, parent);
// foreach (Nucleus nucleus in this.nuclei) {
// if (nucleus is Neuron output) {
// foreach (Nucleus receiver in output.receivers) {
// int ix = GetNucleusIndex(this.nuclei, output);
// Debug.Log($"{output.name} -> {receiver.name}: {ix}");
// if (ix < 0)
// continue;
// if (clone.nuclei[ix] is not Neuron clonedOutput)
// continue;
// clonedOutput.AddReceiver(receiver);
// }
// }
// }
// return clone;
// }
/// \copydoc NanoBrain::Nucleus::ShallowCloneTo /// \copydoc NanoBrain::Nucleus::ShallowCloneTo
public override Nucleus ShallowCloneTo(Cluster parent) { public override Nucleus ShallowCloneTo(Cluster parent) {
// Clusters should not be cloned, but instantiated from the prefab.... // Clusters should not be cloned, but instantiated from the prefab....
@ -442,46 +361,8 @@ namespace NanoBrain {
thingClusters.Remove(thingId); thingClusters.Remove(thingId);
} }
// public bool SameSiblingsAs(Cluster[] otherSiblingClusters) {
// if (this.siblingClusters == null)
// return false;
// for (int ix = 0; ix < this.siblingClusters.Length; ix++) {
// if (this.siblingClusters[ix] != otherSiblingClusters[ix])
// return false;
// }
// return true;
// }
// public void AddArrayReceiver(Nucleus receiverToAdd, float weight = 1) {
// this.defaultOutput.AddReceiver(receiverToAdd, weight);
// // foreach (Cluster cluster in this.siblingClusters) {
// // cluster.defaultOutput.AddReceiver(receiverToAdd, weight);
// // }
// }
#endregion ClusterArray #endregion ClusterArray
// private List<Nucleus> _inputs = null;
// public virtual List<Nucleus> inputs {
// get {
// if (this._inputs == null) {
// this._inputs = new();
// foreach (Nucleus nucleus in this.nuclei) {
// if (nucleus is not Neuron neuron)
// continue;
// // inputs have no synapses
// if (neuron.synapses.Count == 0)
// this._inputs.Add(nucleus);
// }
// RefreshComputeOrders();
// }
// return this._inputs;
// }
// }
/// <summary> /// <summary>
/// This gives the order in which nuclei should be computed when a nucleus is updated /// This gives the order in which nuclei should be computed when a nucleus is updated
/// </summary> /// </summary>
@ -700,30 +581,6 @@ namespace NanoBrain {
selectedCluster.name = baseName + ": " + thingName; selectedCluster.name = baseName + ": " + thingName;
thingClusters[thingId] = selectedCluster; thingClusters[thingId] = selectedCluster;
return lowestNeuron; return lowestNeuron;
/*
// Find a sleeping cluster
// foreach (Cluster cluster in this.siblingClusters) {
// if (cluster.defaultOutput.isSleeping) {
// RemoveThingCluster(cluster);
// return cluster;
// }
// }
// Find longest unused cluster
// Note this uses the default output...
Cluster unusedCluster = this.siblingClusters[0];
for (int ix = 1; ix < this.siblingClusters.Length; ix++) {
if (this.siblingClusters[ix].defaultOutput.lastUpdate < unusedCluster.defaultOutput.lastUpdate)
unusedCluster = this.siblingClusters[ix];
}
RemoveThingCluster(unusedCluster);
//return unusedCluster;
Cluster cluster = GetThingCluster(thingId, thingName);
Neuron neuron = cluster?.GetNeuron(neuronName);
return neuron;
*/
} }
/// <summary> /// <summary>
@ -774,26 +631,6 @@ namespace NanoBrain {
return receivers; return receivers;
} }
/// <summary>
/// Collect all connections to receivers of signals from this cluster
/// </summary>
/// <returns>A list of pairs of the sending neuron in this cluster and the matching receiving nucleus</returns>
// public List<(Neuron, Nucleus)> CollectConnections() {
// List<(Neuron, Nucleus)> connections = new();
// foreach (Nucleus outputNucleus in this.nuclei) {
// if (outputNucleus is not Neuron output)
// continue;
// foreach (Nucleus receiver in output.receivers) {
// // Only add receivers outside this cluster
// if (receiver.parent.prefab != this.prefab)
// connections.Add((output, receiver));
// }
// }
// return connections;
// }
/// <summary> /// <summary>
/// Collect all synapses of senders in another cluster of signals to this cluster /// Collect all synapses of senders in another cluster of signals to this cluster
/// </summary> /// </summary>
@ -813,35 +650,6 @@ namespace NanoBrain {
return collectedSynapses; return collectedSynapses;
} }
// public void MoveReceivers(Cluster newCluster) {
// Debug.Log($"Move receivers for {this.name} to {newCluster.name}");
// foreach (Nucleus outputNucleus in this.nuclei) {
// if (outputNucleus is not Neuron output)
// continue;
// // Find the existing output in the new cluster
// if (newCluster.GetNucleus(output.name) is not Neuron newOutput) {
// Debug.LogWarning($"Could not find output {this.name}.{output.name} in {newCluster.name}");
// continue;
// }
// Debug.Log($"Check {this.name}.{output.name} receivers");
// Nucleus[] receivers = output.receivers.ToArray();
// foreach (Nucleus receiver in receivers) {
// if (receiver.parent.prefab != this.prefab) {
// // Replace synapse with new synapse
// // to the new cluster
// Debug.Log($"move {receiver.name} from {this.name}.{output.name} to {newCluster.name}.{newOutput.name}");
// if (receiver is not Neuron receiverNeuron)
// continue;
// Synapse synapse = receiverNeuron.GetSynapse(output);
// newOutput.AddReceiver(receiver, synapse.weight);
// output.RemoveReceiver(receiver);
// }
// }
// }
// }
#endregion Receivers #endregion Receivers
#region Update #region Update
@ -872,7 +680,6 @@ namespace NanoBrain {
} }
} }
} }
//UpdateNuclei();
} }
/// \copydoc NanoBrain::Nucleus::UpdateStateIsolated /// \copydoc NanoBrain::Nucleus::UpdateStateIsolated
@ -880,12 +687,6 @@ namespace NanoBrain {
throw new Exception("Cluster should not be updated!"); throw new Exception("Cluster should not be updated!");
} }
// Don't think this does anything anymore...
// public override void UpdateNuclei() {
// foreach (Nucleus nucleus in this.nuclei)
// nucleus.UpdateNuclei();
// }
#endregion Update #endregion Update
/// <summary> /// <summary>
@ -895,8 +696,6 @@ namespace NanoBrain {
public void Refresh() { public void Refresh() {
// This should not be needed, but somehow somewhere the parent is changed... // This should not be needed, but somehow somewhere the parent is changed...
foreach (Nucleus nucleus in this.nuclei) { foreach (Nucleus nucleus in this.nuclei) {
// if (nucleus is not Neuron neuron)
// continue;
nucleus.parent = this; nucleus.parent = this;
} }
RefreshOutputs(); RefreshOutputs();

View File

@ -25,21 +25,6 @@ namespace NanoBrain {
this.name = name; this.name = name;
this.parent?.nuclei.Add(this); this.parent?.nuclei.Add(this);
} }
/// <summary>
/// Create a new Neuron in a Cluster Prefab
/// </summary>
/// <param name="prefab">The Cluster Preafb in which the new Neuron should be created</param>
/// <param name="name">The name of the new Neuron</param>
// public Neuron(ClusterPrefab prefab, string name) {
// this.clusterPrefab = prefab;
// this.name = name;
// if (this.clusterPrefab != null) {
// this.clusterPrefab.cluster.nuclei.Add(this);
// this.clusterPrefab.cluster.RefreshOutputs();
// }
// else
// Debug.LogError("No prefab when adding neuron to prefab");
// }
#region Serialization #region Serialization
@ -72,10 +57,6 @@ namespace NanoBrain {
return synapse; return synapse;
} }
// public Synapse AddSynapse(ClusterPrefab clusterPrefab, string neuronName, float weight = 1) {
// }
/// <summary> /// <summary>
/// Find a synapse /// Find a synapse
/// </summary> /// </summary>
@ -394,20 +375,6 @@ namespace NanoBrain {
return clone; return clone;
} }
// \copydoc NanoBrain::Nucleus::Clone
// public override Nucleus Clone(ClusterPrefab prefab) {
// Neuron clone = new(prefab.cluster, this.name);
// CloneFields(clone);
// foreach (Synapse synapse in this.synapses) {
// Synapse clonedSynapse = clone.AddSynapse(synapse.neuron);
// clonedSynapse.weight = synapse.weight;
// }
// foreach (Nucleus receiver in this.receivers) {
// clone.AddReceiver(receiver);
// }
// return clone;
// }
/// <summary> /// <summary>
/// Copy relevant fields of this neuron to the given neuron /// Copy relevant fields of this neuron to the given neuron
/// </summary> /// </summary>
@ -474,11 +441,6 @@ namespace NanoBrain {
this.lastUpdate = Time.time; this.lastUpdate = Time.time;
} }
// protected void CheckSleepingSynapses() {
// foreach (Synapse synapse in this.synapses)
// synapse.neuron.SleepCheck();
// }
#region Combinator #region Combinator
#if UNITY_MATHEMATICS #if UNITY_MATHEMATICS

View File

@ -1,5 +1,4 @@
using System; using System;
using System.Collections.Generic;
using UnityEngine; using UnityEngine;
/// <summary> /// <summary>
@ -7,143 +6,51 @@ using UnityEngine;
/// </summary> /// </summary>
namespace NanoBrain { namespace NanoBrain {
/// <summary>
/// A Nucleus is a basic element in a brain cluster
/// </summary>
[Serializable]
public abstract class Nucleus {
/// <summary> /// <summary>
/// The name of the Nucleus /// A Nucleus is a basic element in a brain cluster
/// </summary> /// </summary>
public string name; [Serializable]
public abstract class Nucleus {
/// <summary>
/// The name of the Nucleus
/// </summary>
public string name;
// [NonSerialized] /// <summary>
// public Nucleus prefabNucleus; /// The cluster instance in which the nucleus is located
/// </summary>
[SerializeReference]
public Cluster parent;
/// <summary> /// <summary>
/// The cluster prefab in which the nucleus is located /// Function to make a partial clone of this nucleus
/// </summary> /// </summary>
// [SerializeReference] /// <param name="parent">The cluster in which the cloned nucleus should be placed</param>
// public ClusterPrefab clusterPrefab; /// <returns></returns>
public abstract Nucleus ShallowCloneTo(Cluster parent);
/// <summary> /// <summary>
/// The cluster instance in which the nucleus is located /// The types of Nucleus
/// </summary> /// </summary>
[SerializeReference] public enum Type {
public Cluster parent; None,
Neuron,
MemoryCell,
Cluster,
//Receptor,
//ClusterReceptor,
//ClusterArray,
}
/// <summary> #region Update
/// Toggle for printing debugging trace data
/// </summary>
//public bool trace = false;
/// <summary> /// <summary>
/// Function to make a partial clone of this nucleus /// Update the state without updating other Nuclei
/// </summary> /// </summary>
/// <param name="parent">The cluster in which the cloned nucleus should be placed</param> public abstract void UpdateStateIsolated();
/// <returns></returns>
public abstract Nucleus ShallowCloneTo(Cluster parent); #endregion Update
/// <summary>
/// Function to clone a nucleus to a Cluster prefab
/// </summary>
/// <param name="prefab"></param>
/// <returns></returns>
// public abstract Nucleus Clone(ClusterPrefab prefab);
/// <summary>
/// The types of Nucleus
/// </summary>
public enum Type {
None,
Neuron,
MemoryCell,
Cluster,
//Receptor,
//ClusterReceptor,
//ClusterArray,
} }
// public virtual void Initialize() {}
// #region Synapses
// /// <summary>
// /// The bias of the nucleus
// /// </summary>
// /// The bias which a value which is always added to the combined value of the nucleus
// /// It does not have a synapse and therefore no weight of source nucleus
// //public Vector3 bias = Vector3.zero;
// [SerializeField]
// private List<Synapse> _synapses = new();
// /// <summary>
// /// The synapses of the nucleus
// /// </summary>
// public List<Synapse> synapses => _synapses;
// /// <summary>
// /// Add a new synapse to this nuclues
// /// </summary>
// /// <param name="sendingNucleus">The nucleus from which the signals may originate</param>
// /// <param name="weight">The weight applied to the input. Default value = 1</param>
// /// <returns>The created Synapse</returns>
// /// This will add a new input to this nucleus with the given weight.
// public Synapse AddSynapse(Neuron sendingNucleus, float weight = 1) {
// Synapse synapse = new(sendingNucleus, weight);
// this.synapses.Add(synapse);
// return synapse;
// }
// // public Synapse AddSynapse(ClusterPrefab clusterPrefab, string neuronName, float weight = 1) {
// // }
// /// <summary>
// /// Find a synapse
// /// </summary>
// /// <param name="sender">The sender of the input to the Synapse</param>
// /// <returns>The found Synapse or null when the sender has no synapse to this nucleus.</returns>
// public Synapse GetSynapse(Nucleus sender) {
// foreach (Synapse synapse in this.synapses)
// if (synapse.neuron == sender)
// return synapse;
// return null;
// }
// /// <summary>
// /// Remove a synapse from a Nucleus
// /// </summary>
// /// <param name="sendingNucleus">Remote the synapse connecting to this Nucleus</param>
// public void RemoveSynapse(Nucleus sendingNucleus) {
// this.synapses.RemoveAll(synapse => synapse.neuron == sendingNucleus);
// }
// #endregion Synapses
#region Update
/// <summary>
/// Update the state without updating other Nuclei
/// </summary>
public abstract void UpdateStateIsolated();
/// <summary>
/// Update the state and recursively all Nuclei receiving data from this Nucleus
/// </summary>
public virtual void UpdateNuclei() {
}
// /// <summary>
// /// Set the bias, recalculate the output and update all Nuclei receiving from this Nucleus
// /// </summary>
// /// <param name="inputValue"></param>
// public virtual void SetBias(Vector3 inputValue) {
// this.bias = inputValue;
// this.parent.UpdateFromNucleus(this);
// }
#endregion Update
}
} }