148 lines
4.5 KiB
C#
148 lines
4.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
/// <summary>
|
|
/// The Nanobrain namespace
|
|
/// </summary>
|
|
namespace NanoBrain {
|
|
|
|
/// <summary>
|
|
/// A Nucleus is a basic element in a brain cluster
|
|
/// </summary>
|
|
[Serializable]
|
|
public abstract class Nucleus {
|
|
/// <summary>
|
|
/// The name of the Nucleus
|
|
/// </summary>
|
|
public string name;
|
|
|
|
/// <summary>
|
|
/// The cluster prefab in which the nucleus is located
|
|
/// </summary>
|
|
[SerializeReference]
|
|
public ClusterPrefab clusterPrefab;
|
|
/// <summary>
|
|
/// The cluster instance in which the nucleus is located
|
|
/// </summary>
|
|
[SerializeReference]
|
|
public Cluster parent;
|
|
|
|
/// <summary>
|
|
/// Toggle for printing debugging trace data
|
|
/// </summary>
|
|
public bool trace = false;
|
|
|
|
/// <summary>
|
|
/// Function to make a partial clone of this nucleus
|
|
/// </summary>
|
|
/// <param name="parent">The cluster in which the cloned nucleus should be placed</param>
|
|
/// <returns></returns>
|
|
public abstract Nucleus ShallowCloneTo(Cluster parent);
|
|
/// <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,
|
|
}
|
|
|
|
#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;
|
|
}
|
|
|
|
/// <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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Process an external stimulus
|
|
/// </summary>
|
|
/// <param name="inputValue">The value of the stimulus</param>
|
|
/// <param name="thingId">The id of the thing causing the stimulus</param>
|
|
/// <param name="thingName">The name of the thing causing the stimulus</param>
|
|
public virtual void ProcessStimulus(Vector3 inputValue, int thingId = 0, string thingName = "") {
|
|
}
|
|
|
|
#endregion Update
|
|
|
|
}
|
|
|
|
} |