Compare commits
100 Commits
a64ff841ac
...
91c4500b0a
| Author | SHA1 | Date | |
|---|---|---|---|
| 91c4500b0a | |||
| b4f8e5a4d8 | |||
| 9ae3607911 | |||
| 8a414816e5 | |||
| 728ef1767c | |||
| 5cb9e788a4 | |||
| b60fc19d96 | |||
| d754457a14 | |||
| 5aedb30d36 | |||
| e7dd1e50cb | |||
| 92c301b850 | |||
| 5ab15926e4 | |||
| 866dee0b56 | |||
| 33f5af32d1 | |||
| 96ed53312d | |||
| 296b80453f | |||
| 8d1a4c3b72 | |||
| f4559efe40 | |||
| c1e6df8df3 | |||
| f0da7c9c9b | |||
| 8b8e5fc33c | |||
| b556aa62fc | |||
| c6d9f727c0 | |||
| f309d4cec6 | |||
| 4b72e6c712 | |||
| 14436cbfba | |||
| f2d20dd462 | |||
| e568759d40 | |||
| 78e750925d | |||
| 394df2167d | |||
| 4f2f230335 | |||
| 5f6ec71c7b | |||
| 3a67652578 | |||
| b3823ac2e6 | |||
| e477ce4814 | |||
| 90350b625b | |||
| beb88f8214 | |||
| c64ccb246c | |||
| 1b5a86ce55 | |||
|
|
f8dc4dc5d3 | ||
|
|
de7503bb4a | ||
|
|
5206469764 | ||
|
|
600ecd5406 | ||
|
|
b9b942760b | ||
|
|
cd74c312dc | ||
|
|
5dbb5654a1 | ||
|
|
6341d827e9 | ||
|
|
cd6ec2ee1e | ||
|
|
1855aa0705 | ||
|
|
19318796da | ||
|
|
ccf0b16136 | ||
|
|
f8fc9dabe6 | ||
|
|
841d923fed | ||
|
|
28c1fac3f8 | ||
|
|
d026996ce2 | ||
|
|
1e1e5b1344 | ||
|
|
a22e4b149a | ||
|
|
2134495045 | ||
|
|
14a786246c | ||
|
|
11ecb905ee | ||
|
|
61bdc36e84 | ||
|
|
cb8c9dbb7c | ||
|
|
bb39435892 | ||
|
|
c86b01ae57 | ||
|
|
a1d1f7ba24 | ||
|
|
d102fc7b80 | ||
|
|
3611de5142 | ||
|
|
a91bd6dfee | ||
|
|
91950a2d19 | ||
|
|
220e1e4ead | ||
|
|
e693ee5ceb | ||
|
|
de74789cb5 | ||
|
|
a500a9e16e | ||
| 65359ecbfa | |||
| 0cee652110 | |||
| 76af037a01 | |||
| 0adb71f306 | |||
| 4e8d10f1bd | |||
| dc8ac7ef9b | |||
| 21751f8cea | |||
| 86b5053383 | |||
| 605bdfc629 | |||
| 5a9aa7161b | |||
| 69c6b1e2b7 | |||
| ca48381dc2 | |||
| fc4e1ea889 | |||
| a3d49e330f | |||
| 145e033d4c | |||
| 9a6ae0e071 | |||
| 88bf20b9c2 | |||
| 2bae70fae2 | |||
| bb5939b6ab | |||
| fdabad2895 | |||
| 7ce787f5db | |||
| 1771ab7d23 | |||
| 2e803179e3 | |||
| cb04a7645b | |||
| d91057cf97 | |||
| 6ded599bb8 | |||
| ad410fd193 |
346
Cluster.cs
346
Cluster.cs
@ -4,12 +4,9 @@ using UnityEngine;
|
||||
using Unity.Mathematics;
|
||||
using static Unity.Mathematics.math;
|
||||
|
||||
[System.Serializable]
|
||||
[Serializable]
|
||||
public class Cluster : INucleus {
|
||||
// The ScriptableObject asset from which the runtime object has been created
|
||||
public readonly ClusterPrefab prefab;
|
||||
|
||||
public ClusterPrefab cluster { get; set; }
|
||||
|
||||
[SerializeField]
|
||||
protected string _name;
|
||||
@ -18,17 +15,113 @@ public class Cluster : INucleus {
|
||||
set => _name = value;
|
||||
}
|
||||
|
||||
// Hmm, a cluster instance can never be part of a scriptable object...(Cluster)
|
||||
public Cluster(ClusterPrefab parent, ClusterPrefab prefab) {
|
||||
#region Init
|
||||
|
||||
public Cluster(ClusterPrefab prefab, Cluster parent) {
|
||||
this.prefab = prefab;
|
||||
this.name = prefab.name;
|
||||
|
||||
this.parent = parent;
|
||||
this.parent?.nuclei.Add(this);
|
||||
|
||||
ClonePrefab();
|
||||
this.sortedNuclei = TopologicalSort(this.nuclei);
|
||||
}
|
||||
|
||||
public Cluster(ClusterPrefab prefab, ClusterPrefab parent = null) {
|
||||
this.prefab = prefab;
|
||||
this.name = prefab.name;
|
||||
this.cluster = parent;
|
||||
|
||||
if (this.cluster != null)
|
||||
this.cluster.nuclei.Add(this);
|
||||
|
||||
// foreach (IReceptor nucleus in this.prefab.nuclei) {
|
||||
// IReceptor clone = nucleus.CloneTo(null);
|
||||
// this.dynamicNuclei.Add(clone);
|
||||
// }
|
||||
ClonePrefab();
|
||||
this.sortedNuclei = TopologicalSort(this.nuclei);
|
||||
}
|
||||
|
||||
// public Cluster(ClusterPrefab parent, ClusterPrefab realPrefab) {
|
||||
// this.prefab = realPrefab;
|
||||
// this.name = realPrefab.name;
|
||||
// this.cluster = parent;
|
||||
// if (this.cluster != null)
|
||||
// this.cluster.nuclei.Add(this);
|
||||
|
||||
// ClonePrefab();
|
||||
// }
|
||||
|
||||
private void ClonePrefab() {
|
||||
IReceptor[] nucleiArray = this.prefab.nuclei.ToArray();
|
||||
// first clone the nuclei without their connections
|
||||
foreach (IReceptor nucleus in this.prefab.nuclei)
|
||||
nucleus.ShallowCloneTo(this);
|
||||
IReceptor[] clonedNuclei = this.nuclei.ToArray();
|
||||
|
||||
// Now clone the connections
|
||||
for (int nucleusIx = 0; nucleusIx < nucleiArray.Length; nucleusIx++) {
|
||||
IReceptor receptor = nucleiArray[nucleusIx];
|
||||
IReceptor clonedSender = clonedNuclei[nucleusIx];
|
||||
if (clonedSender == null)
|
||||
continue;
|
||||
|
||||
// Copy the receivers, which will also create the synapses
|
||||
foreach (INucleus receiver in receptor.receivers) {
|
||||
int ix = GetNucleusIndex(nucleiArray, receiver);
|
||||
if (ix < 0)
|
||||
continue;
|
||||
|
||||
if (clonedNuclei[ix] is not INucleus clonedReceiver)
|
||||
continue;
|
||||
|
||||
// Find the synapse for the weight
|
||||
float weight = 1;
|
||||
foreach (Synapse synapse in receiver.synapses) {
|
||||
if (synapse.nucleus == receptor) {
|
||||
weight = synapse.weight;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
clonedSender.AddReceiver(clonedReceiver, weight);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Sort the nuclei in a correct evaluation order
|
||||
private List<IReceptor> TopologicalSort(List<IReceptor> nodes) {
|
||||
Dictionary<IReceptor, int> inDegree = new();
|
||||
foreach (IReceptor node in nodes)
|
||||
inDegree[node] = 0; // Initialize in-degree to zero
|
||||
|
||||
// Calculate in-degrees
|
||||
foreach (IReceptor node in nodes) {
|
||||
foreach (INucleus receiver in node.receivers)
|
||||
inDegree[receiver]++;
|
||||
}
|
||||
|
||||
Queue<IReceptor> queue = new();
|
||||
foreach (IReceptor node in nodes) {
|
||||
if (inDegree[node] == 0) // Nodes with no dependencies
|
||||
queue.Enqueue(node);
|
||||
}
|
||||
|
||||
List<IReceptor> sortedOrder = new();
|
||||
while (queue.Count > 0) {
|
||||
IReceptor current = queue.Dequeue();
|
||||
sortedOrder.Add(current); // Process the node
|
||||
|
||||
foreach (INucleus receiver in current.receivers) {
|
||||
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 virtual IReceptor Clone() {
|
||||
@ -46,6 +139,59 @@ public class Cluster : INucleus {
|
||||
return clone;
|
||||
}
|
||||
|
||||
public IReceptor ShallowCloneTo(Cluster parent) {
|
||||
Cluster clone = new(this.prefab, parent) {
|
||||
name = this.name,
|
||||
};
|
||||
return clone;
|
||||
}
|
||||
|
||||
private int GetNucleusIndex(IReceptor[] nucleiArray, IReceptor nucleus) {
|
||||
for (int i = 0; i < nucleiArray.Length; i++) {
|
||||
if (nucleus == nucleiArray[i])
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
#endregion Init
|
||||
|
||||
public ClusterPrefab prefab;
|
||||
|
||||
public ClusterPrefab cluster { get; set; }
|
||||
public Cluster parent { get; set; }
|
||||
|
||||
[SerializeReference]
|
||||
public List<IReceptor> nuclei = new();
|
||||
// the nuclei sorted using topological sorting
|
||||
// to ensure that the cluster is computer in the right order
|
||||
public List<IReceptor> sortedNuclei;
|
||||
|
||||
public List<INucleus> _inputs = null;
|
||||
public virtual List<INucleus> inputs {
|
||||
get {
|
||||
if (this._inputs == null) {
|
||||
this._inputs = new();
|
||||
foreach (IReceptor receptor in this.nuclei) {
|
||||
if (receptor is INucleus nucleus) {
|
||||
// inputs have no incoming synapses yet.
|
||||
if (nucleus.synapses.Count == 0)
|
||||
this._inputs.Add(nucleus);
|
||||
}
|
||||
}
|
||||
}
|
||||
return this._inputs;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual INucleus output {//=> this.nuclei[0] as INucleus;
|
||||
get {
|
||||
if (this.nuclei.Count > 0)
|
||||
return this.nuclei[0] as INucleus;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// Not sure if this belongs here...
|
||||
[SerializeReference]
|
||||
private NucleusArray _array;
|
||||
@ -56,27 +202,14 @@ public class Cluster : INucleus {
|
||||
|
||||
#region Synapses
|
||||
|
||||
// class ClusterSynapse : Synapse {
|
||||
// public IReceptor receptor;
|
||||
// public ClusterSynapse(IReceptor nucleus, INucleus receptor, float weight = 1.0f) : base(nucleus, weight) {
|
||||
// this.receptor = receptor;
|
||||
// }
|
||||
// }
|
||||
[SerializeField]
|
||||
private List<Synapse> _synapses = new();
|
||||
public List<Synapse> synapses => _synapses;
|
||||
|
||||
public Synapse AddSynapse(IReceptor sendingNucleus) {
|
||||
Synapse synapse = new(sendingNucleus); //, this.prefab.inputs[0]);
|
||||
public Synapse AddSynapse(IReceptor sendingNucleus, float weight = 1.0f) {
|
||||
Synapse synapse = new(sendingNucleus, weight);
|
||||
this._synapses.Add(synapse);
|
||||
return synapse;
|
||||
// else {
|
||||
// INucleus receptor = (INucleus)this.prefab.nuclei.Find(nucleus => nucleus is INucleus n && nucleus.name == nucleusName);
|
||||
// ClusterSynapse synapse = new(sendingNucleus, receptor);
|
||||
// receptor.AddSynapse(sendingNucleus);
|
||||
// }
|
||||
// // Add synapse to which neuron?
|
||||
// return null;
|
||||
}
|
||||
|
||||
// Does this even exist already?
|
||||
@ -95,9 +228,9 @@ public class Cluster : INucleus {
|
||||
set { _receivers = value; }
|
||||
}
|
||||
|
||||
public virtual void AddReceiver(INucleus receivingNucleus) {
|
||||
public virtual void AddReceiver(INucleus receivingNucleus, float weight = 1) {
|
||||
this._receivers.Add(receivingNucleus);
|
||||
receivingNucleus.AddSynapse(this);
|
||||
receivingNucleus.AddSynapse(this, weight);
|
||||
}
|
||||
|
||||
public void RemoveReceiver(INucleus receiverNucleus) {
|
||||
@ -105,48 +238,6 @@ public class Cluster : INucleus {
|
||||
receiverNucleus.synapses.RemoveAll(synapse => synapse.nucleus == this);
|
||||
}
|
||||
|
||||
// public void AddReceiver(INucleus receivingNucleus) {
|
||||
// int newLength = this._receivers.Count + 1;
|
||||
// INucleus[] newReceivers = new INucleus[newLength];
|
||||
|
||||
// // Copy the existing receivers
|
||||
// for (int ix = 0; ix < this._receivers.Count; ix++)
|
||||
// newReceivers[ix] = this._receivers[ix];
|
||||
// // Add the new receivers
|
||||
// newReceivers[this._receivers.Count] = receivingNucleus;
|
||||
// // Replace the receivers with the new receivers
|
||||
// this._receivers = new(newReceivers);
|
||||
|
||||
// receivingNucleus.AddSynapse(this);
|
||||
// }
|
||||
|
||||
// public void RemoveReceiver(INucleus receivingNucleus) {
|
||||
// Debug.Log("Clusterinstance. remote receiver");
|
||||
// int newLength = this._receivers.Count - 1;
|
||||
// if (newLength < 0)
|
||||
// // Array was empty, so we cannot remove anything
|
||||
// return;
|
||||
|
||||
// INucleus[] newReceivers = new INucleus[newLength];
|
||||
|
||||
// int newIx = 0;
|
||||
// // Copy all receivers except receivingNucleus
|
||||
// for (int ix = 0; ix < this._receivers.Count; ix++) {
|
||||
// if (this._receivers[ix] == receivingNucleus)
|
||||
// // skip the receiver we want to remote
|
||||
// continue;
|
||||
|
||||
// if (newIx >= newLength)
|
||||
// // We want to copy more elements than expected
|
||||
// // the receivingNucleus is not found
|
||||
// // and the original array is returned
|
||||
// return;
|
||||
// newReceivers[newIx] = this._receivers[ix];
|
||||
// newIx++;
|
||||
// }
|
||||
// this._receivers = new(newReceivers);
|
||||
// }
|
||||
|
||||
#endregion Receivers
|
||||
|
||||
#region Runtime
|
||||
@ -171,89 +262,68 @@ public class Cluster : INucleus {
|
||||
UpdateState(new float3(0, 0, 0));
|
||||
}
|
||||
|
||||
public void UpdateState(float3 inputValue) {
|
||||
float3 sum = inputValue; // new(0, 0, 0);
|
||||
public void UpdateState(float3 bias) {
|
||||
float3 sum = bias; // new(0, 0, 0);
|
||||
|
||||
//Applying the weight factgors
|
||||
//Applying the weight factors
|
||||
foreach (Synapse synapse in this.synapses) {
|
||||
sum += synapse.weight * synapse.nucleus.outputValue;
|
||||
}
|
||||
|
||||
// This does not work because the prefab nucleus does not have a state
|
||||
this.prefab.inputs[0].UpdateState(sum);
|
||||
|
||||
//this.inputs[0].UpdateState(sum);
|
||||
this.inputs[0].UpdateStateIsolated(sum);
|
||||
foreach (IReceptor receptor in this.sortedNuclei) {
|
||||
if (receptor is INucleus nucleus && nucleus != this.inputs[0])
|
||||
nucleus.UpdateStateIsolated();
|
||||
}
|
||||
|
||||
UpdateResult(this.output.outputValue);
|
||||
}
|
||||
|
||||
public void UpdateStateIsolated() {
|
||||
float3 bias = new(0,0,0);
|
||||
UpdateStateIsolated(bias);
|
||||
}
|
||||
public void UpdateStateIsolated(float3 bias) {
|
||||
float3 sum = bias; // new(0, 0, 0);
|
||||
|
||||
//Applying the weight factors
|
||||
foreach (Synapse synapse in this.synapses) {
|
||||
sum += synapse.weight * synapse.nucleus.outputValue;
|
||||
}
|
||||
|
||||
//this.inputs[0].UpdateState(sum);
|
||||
this.inputs[0].UpdateStateIsolated(sum);
|
||||
foreach (IReceptor receptor in this.sortedNuclei) {
|
||||
if (receptor is INucleus nucleus && nucleus != this.inputs[0])
|
||||
nucleus.UpdateStateIsolated();
|
||||
}
|
||||
this.outputValue = this.output.outputValue;
|
||||
}
|
||||
|
||||
public virtual void UpdateResult(Vector3 result) {
|
||||
// float d = Vector3.Distance(result, this.outputValue);
|
||||
// if (d < 0.5f) {
|
||||
// //Debug.Log($"insignificant update: {d}");
|
||||
// return;
|
||||
// }
|
||||
|
||||
this.outputValue = result;
|
||||
foreach (INucleus receiver in this.receivers)
|
||||
receiver.UpdateState();
|
||||
}
|
||||
|
||||
public void UpdateNuclei() {
|
||||
this.stale++;
|
||||
if (this.stale > 2)
|
||||
if (this.stale > 2)
|
||||
_outputValue = Vector3.zero;
|
||||
|
||||
foreach (IReceptor nucleus in this.prefab.nuclei)
|
||||
//foreach (IReceptor nucleus in this.prefab.nuclei)
|
||||
foreach (IReceptor nucleus in this.nuclei)
|
||||
nucleus.UpdateNuclei();
|
||||
}
|
||||
|
||||
#endregion Update
|
||||
|
||||
#endregion Runtime
|
||||
|
||||
/*
|
||||
[SerializeField]
|
||||
private List<IReceptor> _dynamicNuclei;
|
||||
public List<IReceptor> dynamicNuclei {// = new();
|
||||
get {
|
||||
if (_dynamicNuclei == null) {
|
||||
this._dynamicNuclei = new();
|
||||
foreach (IReceptor nucleus in this.prefab.nuclei) {
|
||||
IReceptor clone = nucleus.CloneTo(null);
|
||||
this._dynamicNuclei.Add(clone);
|
||||
}
|
||||
}
|
||||
return this._dynamicNuclei;
|
||||
}
|
||||
}
|
||||
|
||||
public List<INucleus> _inputs = null;
|
||||
public List<INucleus> inputs {
|
||||
get {
|
||||
this._inputs = new();
|
||||
if (this.dynamicNuclei != null) {
|
||||
foreach (IReceptor receptor in this.dynamicNuclei) {
|
||||
if (receptor is INucleus nucleus)
|
||||
this._inputs.Add(nucleus);
|
||||
}
|
||||
}
|
||||
return this._inputs;
|
||||
}
|
||||
}
|
||||
|
||||
public INucleus output => this.dynamicNuclei[0] as INucleus;
|
||||
|
||||
public float3 outputValue => this.output.outputValue;
|
||||
|
||||
|
||||
public IReceptor CloneTo(ClusterPrefab parent) {
|
||||
Cluster clone = new(parent, this.prefab);
|
||||
return clone;
|
||||
}
|
||||
public IReceptor Clone() {
|
||||
Cluster clone = new(this.cluster, this.prefab);
|
||||
return clone;
|
||||
}
|
||||
|
||||
#region Properties
|
||||
|
||||
public string name {
|
||||
get { return prefab.name; }
|
||||
set { prefab.name = value; }
|
||||
}
|
||||
|
||||
public bool isSleeping => lengthsq(this.outputValue) == 0;
|
||||
|
||||
public NucleusArray array { get; set; }
|
||||
|
||||
#endregion Properties
|
||||
|
||||
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
@ -4,18 +4,11 @@ using UnityEngine;
|
||||
[CreateAssetMenu(menuName = "Passer/Cluster")]
|
||||
public class ClusterPrefab : ScriptableObject {
|
||||
|
||||
//public virtual Cluster cluster {get;set;}
|
||||
|
||||
// The ScriptableObject asset from which the runtime object has been created
|
||||
//public Cluster asset;
|
||||
|
||||
[SerializeReference]
|
||||
public List<IReceptor> nuclei = new();
|
||||
|
||||
// public List<Cluster> subClusters = new();
|
||||
// public void AddSubCluster(ClusterInstance subCluster) {
|
||||
// this.nuclei.Add(subCluster);
|
||||
// }
|
||||
|
||||
public virtual INucleus output => this.nuclei[0] as INucleus;
|
||||
|
||||
@ -25,8 +18,11 @@ public class ClusterPrefab : ScriptableObject {
|
||||
if (this._inputs == null) {
|
||||
this._inputs = new();
|
||||
foreach (IReceptor receptor in this.nuclei) {
|
||||
if (receptor is INucleus nucleus)
|
||||
this._inputs.Add(nucleus);
|
||||
if (receptor is INucleus nucleus) {
|
||||
// inputs have no incoming synapses yet.
|
||||
if (nucleus.synapses.Count == 0)
|
||||
this._inputs.Add(nucleus);
|
||||
}
|
||||
}
|
||||
}
|
||||
return this._inputs;
|
||||
|
||||
@ -10,7 +10,7 @@ public interface INucleus : IReceptor {
|
||||
|
||||
// Senders
|
||||
public List<Synapse> synapses { get; }
|
||||
public Synapse AddSynapse(IReceptor sender);
|
||||
public Synapse AddSynapse(IReceptor sender, float weight = 1.0f);
|
||||
|
||||
public NucleusArray array { get; set; }
|
||||
|
||||
@ -20,7 +20,8 @@ public interface INucleus : IReceptor {
|
||||
|
||||
public void UpdateState();
|
||||
public void UpdateState(float3 inputValue);
|
||||
|
||||
public void UpdateStateIsolated();
|
||||
public void UpdateStateIsolated(float3 inputValue);
|
||||
|
||||
#endregion dynamic state
|
||||
}
|
||||
@ -33,7 +34,7 @@ public interface IReceptor {
|
||||
// Receivers
|
||||
public List<INucleus> receivers { get; set; }
|
||||
|
||||
public void AddReceiver(INucleus receiver);
|
||||
public void AddReceiver(INucleus receiver, float weight = 1);
|
||||
public void RemoveReceiver(INucleus receiverNucleus);
|
||||
|
||||
#endregion static
|
||||
@ -48,7 +49,7 @@ public interface IReceptor {
|
||||
|
||||
#endregion dynamic
|
||||
|
||||
//public IReceptor CloneTo(ClusterPrefab parent);
|
||||
public IReceptor ShallowCloneTo(Cluster parent);
|
||||
public IReceptor Clone();
|
||||
}
|
||||
|
||||
|
||||
@ -4,35 +4,38 @@ using Unity.Mathematics;
|
||||
using static Unity.Mathematics.math;
|
||||
|
||||
[Serializable]
|
||||
public class MemoryCell : Neuron {
|
||||
public class MemoryCell : Neuron, INucleus {
|
||||
|
||||
public MemoryCell(ClusterPrefab cluster, string name) : base(cluster, name) {}
|
||||
public MemoryCell(ClusterPrefab cluster, string name) : base(cluster, name) { }
|
||||
public MemoryCell(Cluster parent, string name) : base(parent, name) { }
|
||||
// this.parent = parent;
|
||||
// this.name = name;
|
||||
// this.parent?.nuclei.Add(this);
|
||||
// }
|
||||
|
||||
#region Parameters
|
||||
|
||||
// Returns the memorized value weighted by time
|
||||
// return lastValue * (current time - last time)
|
||||
[SerializeField]
|
||||
public bool deltaValue = false;
|
||||
|
||||
#endregion Parameters
|
||||
public override IReceptor ShallowCloneTo(Cluster newParent) {
|
||||
MemoryCell clone = new(newParent, this.name) {
|
||||
array = this.array,
|
||||
curve = this.curve,
|
||||
curvePreset = this.curvePreset,
|
||||
curveMax = this.curveMax,
|
||||
average = this.average
|
||||
};
|
||||
return clone;
|
||||
}
|
||||
|
||||
#region State
|
||||
|
||||
private float3 _memorizedValue;
|
||||
private float _memorizedTime;
|
||||
|
||||
public override void UpdateState() {
|
||||
public override void UpdateState(float3 bias) {
|
||||
// A memorycell does not have an activation function
|
||||
float3 result = new(0, 0, 0);
|
||||
float3 result = bias;
|
||||
int n = 0;
|
||||
|
||||
//Applying the weight factgors
|
||||
foreach (Synapse synapse in this.synapses) {
|
||||
if (synapse.nucleus == this) {
|
||||
float deltaTime = Time.time - this.lastTime;
|
||||
synapse.weight = deltaTime;
|
||||
}
|
||||
result += synapse.weight * synapse.nucleus.outputValue;
|
||||
if (lengthsq(synapse.nucleus.outputValue) != 0)
|
||||
n++;
|
||||
@ -44,14 +47,40 @@ public class MemoryCell : Neuron {
|
||||
UpdateResult(result);
|
||||
}
|
||||
|
||||
public override void UpdateStateIsolated() {
|
||||
float3 bias = new(0, 0, 0);
|
||||
UpdateStateIsolated(bias);
|
||||
}
|
||||
public override void UpdateStateIsolated(float3 bias) {
|
||||
// A memorycell does not have an activation function
|
||||
float3 result = bias;
|
||||
int n = 0;
|
||||
|
||||
//Applying the weight factgors
|
||||
foreach (Synapse synapse in this.synapses) {
|
||||
result += synapse.weight * synapse.nucleus.outputValue;
|
||||
if (lengthsq(synapse.nucleus.outputValue) != 0)
|
||||
n++;
|
||||
}
|
||||
|
||||
if (this.average)
|
||||
result /= n;
|
||||
|
||||
this.outputValue = this._memorizedValue;
|
||||
|
||||
// Store the result for the next time
|
||||
this._memorizedValue = result;
|
||||
this._memorizedTime = Time.time;
|
||||
}
|
||||
|
||||
public override void UpdateResult(Vector3 result) {
|
||||
// output value is the previous value
|
||||
if (this.deltaValue) {
|
||||
float deltaTime = Time.time - this._memorizedTime;
|
||||
this._outputValue = this._memorizedValue * deltaTime;
|
||||
}
|
||||
else
|
||||
this._outputValue = this._memorizedValue;
|
||||
// if (this.deltaValue) {
|
||||
// float deltaTime = Time.time - this._memorizedTime;
|
||||
// this._outputValue = this._memorizedValue * deltaTime;
|
||||
// }
|
||||
//else
|
||||
this.outputValue = this._memorizedValue;
|
||||
|
||||
// Store the result for the next time
|
||||
this._memorizedValue = result;
|
||||
|
||||
99
Neuron.cs
99
Neuron.cs
@ -88,6 +88,7 @@ public class Neuron : INucleus {
|
||||
#region Runtime state (not serialized)
|
||||
|
||||
public ClusterPrefab cluster { get; set; }
|
||||
public Cluster parent { get; set; }
|
||||
|
||||
#region Activation
|
||||
|
||||
@ -153,18 +154,22 @@ public class Neuron : INucleus {
|
||||
// private bool _isSleeping = false;
|
||||
// public bool isSleeping => _isSleeping;
|
||||
public bool isSleeping => lengthsq(this.outputValue) == 0;
|
||||
public float lastTime { get; private set; }
|
||||
|
||||
public void UpdateNuclei() {
|
||||
this.stale++;
|
||||
// this._isSleeping = this.stale > 2;
|
||||
// if (isSleeping)
|
||||
if (this.stale > 2)
|
||||
if (this.stale > 2)
|
||||
_outputValue = Vector3.zero;
|
||||
}
|
||||
|
||||
#endregion Runtime state
|
||||
|
||||
public Neuron(Cluster parent, string name) {
|
||||
this.parent = parent;
|
||||
this.name = name;
|
||||
this.parent?.nuclei.Add(this);
|
||||
}
|
||||
public Neuron(ClusterPrefab parent, string name) {
|
||||
this.cluster = parent;
|
||||
this.name = name;
|
||||
@ -175,9 +180,27 @@ public class Neuron : INucleus {
|
||||
// Debug.LogError("No neuroid network");
|
||||
}
|
||||
|
||||
// public Neuron(string name) {
|
||||
// this._name = name;
|
||||
// }
|
||||
// this clone the nucleus without the synapses and receivers
|
||||
public virtual IReceptor ShallowCloneTo(Cluster newParent) {
|
||||
Neuron clone = new(newParent, this.name) {
|
||||
array = this.array,
|
||||
curve = this.curve,
|
||||
curvePreset = this.curvePreset,
|
||||
curveMax = this.curveMax,
|
||||
average = this.average
|
||||
};
|
||||
return clone;
|
||||
}
|
||||
public virtual IReceptor ShallowCloneTo(ClusterPrefab newParent) {
|
||||
Neuron clone = new(newParent, this.name) {
|
||||
array = this.array,
|
||||
curve = this.curve,
|
||||
curvePreset = this.curvePreset,
|
||||
curveMax = this.curveMax,
|
||||
average = this.average
|
||||
};
|
||||
return clone;
|
||||
}
|
||||
|
||||
public virtual IReceptor CloneTo(ClusterPrefab parent) {
|
||||
Neuron clone = new(parent, this.name) {
|
||||
@ -187,8 +210,6 @@ public class Neuron : INucleus {
|
||||
curveMax = this.curveMax,
|
||||
average = this.average
|
||||
};
|
||||
// if (clone.cluster != null)
|
||||
// clone.cluster.nuclei.Add(clone);
|
||||
|
||||
foreach (Synapse synapse in this.synapses) {
|
||||
Synapse clonedSynapse = clone.AddSynapse(synapse.nucleus);
|
||||
@ -220,9 +241,9 @@ public class Neuron : INucleus {
|
||||
return clone;
|
||||
}
|
||||
|
||||
public virtual void AddReceiver(INucleus receivingNucleus) {
|
||||
public virtual void AddReceiver(INucleus receivingNucleus, float weight = 1) {
|
||||
this._receivers.Add(receivingNucleus);
|
||||
receivingNucleus.AddSynapse(this);
|
||||
receivingNucleus.AddSynapse(this, weight);
|
||||
}
|
||||
|
||||
public void RemoveReceiver(INucleus receiverNucleus) {
|
||||
@ -254,27 +275,25 @@ public class Neuron : INucleus {
|
||||
}
|
||||
}
|
||||
|
||||
public Synapse AddSynapse(IReceptor sendingNucleus) {
|
||||
Synapse synapse = new(sendingNucleus);
|
||||
public Synapse AddSynapse(IReceptor sendingNucleus, float weight = 1.0f) {
|
||||
Synapse synapse = new(sendingNucleus, weight);
|
||||
this.synapses.Add(synapse);
|
||||
return synapse;
|
||||
}
|
||||
|
||||
public virtual void UpdateState() {
|
||||
UpdateState(new float3(0, 0, 0));
|
||||
//UpdateState(new float3(0, 0, 0));
|
||||
this.parent?.UpdateState();
|
||||
}
|
||||
|
||||
public virtual void UpdateState(float3 inputValue) {
|
||||
float3 sum = inputValue;//new(0, 0, 0);
|
||||
float3 sum = inputValue;
|
||||
int n = 0;
|
||||
|
||||
//Applying the weight factgors
|
||||
foreach (Synapse synapse in this.synapses) {
|
||||
if (synapse.nucleus == this) {
|
||||
float deltaTime = Time.time - this.lastTime;
|
||||
synapse.weight = deltaTime;
|
||||
}
|
||||
sum += synapse.weight * synapse.nucleus.outputValue;
|
||||
|
||||
// Perhaps synapses should be removed when the output value goes to 0....
|
||||
if (lengthsq(synapse.nucleus.outputValue) != 0)
|
||||
n++;
|
||||
@ -305,6 +324,47 @@ public class Neuron : INucleus {
|
||||
UpdateResult(result);
|
||||
}
|
||||
|
||||
public virtual void UpdateStateIsolated() {
|
||||
UpdateStateIsolated(new float3(0, 0, 0));
|
||||
}
|
||||
public virtual void UpdateStateIsolated(float3 bias) {
|
||||
float3 sum = bias;
|
||||
int n = 0;
|
||||
|
||||
//Applying the weight factgors
|
||||
foreach (Synapse synapse in this.synapses) {
|
||||
sum += synapse.weight * synapse.nucleus.outputValue;
|
||||
|
||||
// Perhaps synapses should be removed when the output value goes to 0....
|
||||
if (lengthsq(synapse.nucleus.outputValue) != 0)
|
||||
n++;
|
||||
}
|
||||
if (this.average && n > 0)
|
||||
sum /= n;
|
||||
|
||||
// Activation function
|
||||
Vector3 result;
|
||||
switch (this.curvePreset) {
|
||||
case CurvePresets.Linear:
|
||||
result = sum;
|
||||
break;
|
||||
case CurvePresets.Sqrt:
|
||||
result = normalize(sum) * System.MathF.Sqrt(length(sum));
|
||||
break;
|
||||
case CurvePresets.Power:
|
||||
result = normalize(sum) * System.MathF.Pow(length(sum), 2);
|
||||
break;
|
||||
case CurvePresets.Reciprocal:
|
||||
result = normalize(sum) * (1 / length(sum));
|
||||
break;
|
||||
default:
|
||||
float activatedValue = this.curve.Evaluate(length(sum));
|
||||
result = normalize(sum) * activatedValue;
|
||||
break;
|
||||
}
|
||||
this.outputValue = result;
|
||||
}
|
||||
|
||||
public virtual void UpdateResult(Vector3 result) {
|
||||
// float d = Vector3.Distance(result, this.outputValue);
|
||||
// if (d < 0.5f) {
|
||||
@ -313,7 +373,10 @@ public class Neuron : INucleus {
|
||||
// }
|
||||
|
||||
this.outputValue = result;
|
||||
this.lastTime = Time.time;
|
||||
if (lengthsq(outputValue) != 0) {
|
||||
Debug.Log($"{this.parent.name}.{this.name}: {this.outputValue}");
|
||||
}
|
||||
|
||||
foreach (INucleus receiver in this.receivers)
|
||||
receiver.UpdateState();
|
||||
|
||||
|
||||
26
Receptor.cs
26
Receptor.cs
@ -1,3 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using Unity.Mathematics;
|
||||
@ -6,6 +7,7 @@ using static Unity.Mathematics.math;
|
||||
public class Receptor : IReceptor {
|
||||
|
||||
private ClusterPrefab cluster;
|
||||
private Cluster parent;
|
||||
|
||||
[SerializeField]
|
||||
protected string _name;
|
||||
@ -14,6 +16,11 @@ public class Receptor : IReceptor {
|
||||
set => _name = value;
|
||||
}
|
||||
|
||||
public Receptor(Cluster parent) {
|
||||
this.parent = parent;
|
||||
if (parent != null)
|
||||
parent.nuclei.Add(this);
|
||||
}
|
||||
public Receptor(ClusterPrefab cluster) {
|
||||
this.cluster = cluster;
|
||||
if (cluster != null)
|
||||
@ -27,15 +34,13 @@ public class Receptor : IReceptor {
|
||||
this.AddReceiver(nucleus);
|
||||
}
|
||||
|
||||
public static Receptor CreateReceptor(ClusterPrefab cluster, string nucleusName) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
@ -45,6 +50,15 @@ public class Receptor : IReceptor {
|
||||
return receptor;
|
||||
}
|
||||
|
||||
public virtual IReceptor ShallowCloneTo(Cluster parent) {
|
||||
Receptor clone = new(parent);
|
||||
return clone;
|
||||
}
|
||||
public virtual IReceptor ShallowCloneTo(ClusterPrefab parent) {
|
||||
Receptor clone = new(parent);
|
||||
return clone;
|
||||
}
|
||||
|
||||
public virtual IReceptor CloneTo(ClusterPrefab parent) {
|
||||
Receptor clone = new(parent);
|
||||
|
||||
@ -84,9 +98,9 @@ public class Receptor : IReceptor {
|
||||
|
||||
protected int[] thingIds; // every receiver can handle a thing with this id
|
||||
|
||||
public virtual void AddReceiver(INucleus receivingNucleus) {
|
||||
public virtual void AddReceiver(INucleus receivingNucleus, float weight = 1) {
|
||||
this._receivers.Add(receivingNucleus);
|
||||
receivingNucleus.AddSynapse(this);
|
||||
receivingNucleus.AddSynapse(this, weight);
|
||||
}
|
||||
|
||||
public void RemoveReceiver(INucleus receiverNucleus) {
|
||||
@ -122,6 +136,8 @@ public class Receptor : IReceptor {
|
||||
|
||||
public virtual void ProcessStimulus(int thingId, Vector3 newLocalPositionVector, string thingName = null) {
|
||||
this.localPosition = newLocalPositionVector;
|
||||
if (this._receivers == null)
|
||||
return;
|
||||
|
||||
thingIds ??= new int[this._receivers.Count];
|
||||
|
||||
|
||||
@ -11,26 +11,20 @@ MonoBehaviour:
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: 60a957541c24c57e78018c202ebb1d9b, type: 3}
|
||||
m_Name: Velocity
|
||||
m_EditorClassIdentifier: Assembly-CSharp::Cluster
|
||||
m_EditorClassIdentifier: Assembly-CSharp::ClusterPrefab
|
||||
nuclei:
|
||||
- rid: 2243601403683012671
|
||||
- rid: 2243601403683012676
|
||||
- rid: 2243601425629446539
|
||||
references:
|
||||
version: 2
|
||||
RefIds:
|
||||
- rid: -2
|
||||
type: {class: , ns: , asm: }
|
||||
- rid: 2243601403683012671
|
||||
- rid: 2243601425629446539
|
||||
type: {class: Neuron, ns: , asm: Assembly-CSharp}
|
||||
data:
|
||||
_name: Output
|
||||
_synapses:
|
||||
- nucleus:
|
||||
rid: -2
|
||||
weight: 1
|
||||
_synapses: []
|
||||
_receivers: []
|
||||
_array:
|
||||
rid: 2243601403683012672
|
||||
rid: 2243601425629446540
|
||||
_curvePreset: 0
|
||||
curve:
|
||||
serializedVersion: 2
|
||||
@ -58,51 +52,9 @@ MonoBehaviour:
|
||||
m_RotationOrder: 4
|
||||
curveMax: 1
|
||||
average: 0
|
||||
- rid: 2243601403683012672
|
||||
- rid: 2243601425629446540
|
||||
type: {class: NucleusArray, ns: , asm: Assembly-CSharp}
|
||||
data:
|
||||
_nuclei:
|
||||
- rid: 2243601403683012671
|
||||
- rid: 2243601425629446539
|
||||
name: Output
|
||||
- rid: 2243601403683012676
|
||||
type: {class: Neuron, ns: , asm: Assembly-CSharp}
|
||||
data:
|
||||
_name: Position
|
||||
_synapses: []
|
||||
_receivers:
|
||||
- rid: 2243601403683012671
|
||||
_array:
|
||||
rid: 2243601403683012677
|
||||
_curvePreset: 0
|
||||
curve:
|
||||
serializedVersion: 2
|
||||
m_Curve:
|
||||
- serializedVersion: 3
|
||||
time: 0
|
||||
value: 0
|
||||
inSlope: 0
|
||||
outSlope: 1
|
||||
tangentMode: 0
|
||||
weightedMode: 0
|
||||
inWeight: 0
|
||||
outWeight: 0
|
||||
- serializedVersion: 3
|
||||
time: 1000
|
||||
value: 1000
|
||||
inSlope: 1
|
||||
outSlope: 0
|
||||
tangentMode: 0
|
||||
weightedMode: 0
|
||||
inWeight: 0
|
||||
outWeight: 0
|
||||
m_PreInfinity: 2
|
||||
m_PostInfinity: 2
|
||||
m_RotationOrder: 4
|
||||
curveMax: 1
|
||||
average: 0
|
||||
- rid: 2243601403683012677
|
||||
type: {class: NucleusArray, ns: , asm: Assembly-CSharp}
|
||||
data:
|
||||
_nuclei:
|
||||
- rid: 2243601403683012676
|
||||
name: New neuron
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
fileFormatVersion: 2
|
||||
guid: dd622ac7ed09e70ea8edac595047ac82
|
||||
guid: c61aecac62c26de4aaefb2612bcc9a5d
|
||||
NativeFormatImporter:
|
||||
externalObjects: {}
|
||||
mainObjectFileID: 11400000
|
||||
|
||||
@ -3,14 +3,14 @@ using UnityEngine;
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
public class BrainPickerWindow : EditorWindow {
|
||||
public class ClusterPickerWindow : EditorWindow {
|
||||
private Vector2 scroll;
|
||||
private ClusterPrefab[] items = new ClusterPrefab[0];
|
||||
private Action<ClusterPrefab> onPicked;
|
||||
private string search = "";
|
||||
|
||||
public static void ShowPicker(Action<ClusterPrefab> onPicked, string title = "Select Cluster") {
|
||||
var w = CreateInstance<BrainPickerWindow>();
|
||||
var w = CreateInstance<ClusterPickerWindow>();
|
||||
w.titleContent = new GUIContent(title);
|
||||
w.minSize = new Vector2(360, 320);
|
||||
w.onPicked = onPicked;
|
||||
@ -21,7 +21,7 @@ public class BrainPickerWindow : EditorWindow {
|
||||
private void OnEnable() => RefreshList();
|
||||
|
||||
private void RefreshList() {
|
||||
var guids = AssetDatabase.FindAssets("t:Cluster");
|
||||
var guids = AssetDatabase.FindAssets("t:ClusterPrefab");
|
||||
items = guids
|
||||
.Select(g => AssetDatabase.LoadAssetAtPath<ClusterPrefab>(AssetDatabase.GUIDToAssetPath(g)))
|
||||
.Where(b => b != null)
|
||||
|
||||
@ -617,22 +617,22 @@ public class ClusterInspector : Editor {
|
||||
}
|
||||
|
||||
protected virtual void AddCluster(INucleus nucleus) {
|
||||
BrainPickerWindow.ShowPicker(brain => OnClusterPicked(nucleus, brain), "Select Cluster");
|
||||
ClusterPickerWindow.ShowPicker(brain => OnClusterPicked(nucleus, brain), "Select Cluster");
|
||||
}
|
||||
|
||||
private void OnClusterPicked(INucleus nucleus, ClusterPrefab subCluster) {
|
||||
Cluster subclusterInstance = new(this.cluster, subCluster);
|
||||
//this.cluster.AddSubCluster(subclusterInstance);
|
||||
//this.cluster.nuclei.Add(subclusterInstance);
|
||||
private void OnClusterPicked(INucleus nucleus, ClusterPrefab prefab) {
|
||||
Cluster subclusterInstance = new(prefab, this.cluster);
|
||||
subclusterInstance.AddReceiver(nucleus);
|
||||
// This does not work somehow
|
||||
// this.currentNucleus = subclusterInstance;
|
||||
// BuildLayers();
|
||||
}
|
||||
|
||||
private void EditCluster(Cluster subCluster) {
|
||||
//var currentActiveObject = Selection.activeObject;
|
||||
// May be used with storedPrefab...
|
||||
Selection.activeObject = subCluster.prefab;
|
||||
EditorGUIUtility.PingObject(subCluster.prefab);
|
||||
var editor = Editor.CreateEditor(subCluster.prefab);
|
||||
//Selection.activeObject = currentActiveObject;
|
||||
}
|
||||
|
||||
// Connect to another nucleus in the same cluster
|
||||
|
||||
@ -22,8 +22,8 @@ public class NanoBrainComponent_Editor : Editor {
|
||||
}
|
||||
|
||||
public override VisualElement CreateInspectorGUI() {
|
||||
//NanoBrainComponent component = target as NanoBrainComponent;
|
||||
ClusterPrefab brain = Application.isPlaying ? component.brain : component.defaultBrain;
|
||||
//ClusterPrefab brain = Application.isPlaying ? component.brain.prefab : component.defaultBrain;
|
||||
Cluster brain = component.brain;
|
||||
|
||||
if (Application.isPlaying == false)
|
||||
serializedObject.Update();
|
||||
@ -79,7 +79,7 @@ public class NanoBrainComponent_Editor : Editor {
|
||||
});
|
||||
|
||||
if (brain != null && board != null)
|
||||
board.SetGraph(component.gameObject, brain, brain.output, inspectorContainer);
|
||||
board.SetGraph(component.gameObject, brain.prefab, brain.output, inspectorContainer);
|
||||
// else
|
||||
// Debug.LogWarning(" No brain!");
|
||||
|
||||
|
||||
@ -1,14 +1,15 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
|
||||
public class NanoBrainComponent : MonoBehaviour {
|
||||
public ClusterPrefab defaultBrain;
|
||||
private ClusterPrefab brainInstance;
|
||||
|
||||
public INucleus root => brainInstance.output;
|
||||
public ClusterPrefab brain {
|
||||
|
||||
[NonSerialized]
|
||||
private Cluster brainInstance;
|
||||
public Cluster brain {
|
||||
get {
|
||||
if (brainInstance == null && defaultBrain != null) {
|
||||
brainInstance = Instantiate(defaultBrain);
|
||||
brainInstance = new Cluster(defaultBrain); //Instantiate(defaultBrain);
|
||||
brainInstance.name = defaultBrain.name + " (Instance)";
|
||||
|
||||
SwarmControl sc = FindFirstObjectByType<SwarmControl>();
|
||||
@ -23,7 +24,7 @@ public class NanoBrainComponent : MonoBehaviour {
|
||||
}
|
||||
}
|
||||
|
||||
public static void UpdateWeight(ClusterPrefab brain, string name, float weight) {
|
||||
public static void UpdateWeight(Cluster brain, string name, float weight) {
|
||||
INucleus root = brain.output;
|
||||
foreach (Synapse synapse in root.synapses) {
|
||||
if (synapse.nucleus.name == name) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user