It works again (without nucleus array)

This commit is contained in:
Pascal Serrarens 2026-01-12 12:30:08 +01:00
parent beb88f8214
commit 90350b625b
21 changed files with 693 additions and 492 deletions

View File

@ -79,6 +79,7 @@
<Compile Include="Assets/NanoBrain/LinearAlgebra/src/SwingTwist.cs" /> <Compile Include="Assets/NanoBrain/LinearAlgebra/src/SwingTwist.cs" />
<Compile Include="Assets/NanoBrain/LinearAlgebra/test/SphericalTest.cs" /> <Compile Include="Assets/NanoBrain/LinearAlgebra/test/SphericalTest.cs" />
<Compile Include="Assets/NanoBrain/Cluster.cs" /> <Compile Include="Assets/NanoBrain/Cluster.cs" />
<Compile Include="Assets/NanoBrain/Synapse.cs" />
<Compile Include="Assets/NanoBrain/LinearAlgebra/test/Vector3IntTest.cs" /> <Compile Include="Assets/NanoBrain/LinearAlgebra/test/Vector3IntTest.cs" />
<Compile Include="Assets/NanoBrain/Neuroid.cs" /> <Compile Include="Assets/NanoBrain/Neuroid.cs" />
<Compile Include="Assets/NanoBrain/LinearAlgebra/src/Angle.cs" /> <Compile Include="Assets/NanoBrain/LinearAlgebra/src/Angle.cs" />

View File

@ -4,27 +4,29 @@ using UnityEngine;
[CreateAssetMenu(menuName = "Passer/Cluster")] [CreateAssetMenu(menuName = "Passer/Cluster")]
public class Cluster : ScriptableObject, INucleus { public class Cluster : ScriptableObject, INucleus {
// private string _name;
// public new string name {
// get { return _name; }
// set { _name = value; }
// }
public Cluster cluster => this; public Cluster cluster => this;
public List<Nucleus> nuclei = new(); [SerializeReference]
public List<INucleus> nuclei = new();
public Nucleus output => this.nuclei[0]; public INucleus output => this.nuclei[0];
[SerializeField] //private readonly List<INucleus> _inputs = new();
private readonly List<Synapse> _synapses = new(); // = inputs, compare receptors in NanoBrain public List<INucleus> inputs { // = compare receptors in NanoBrain
// for now all nuclei are inputs
get { return this.nuclei; }
}
// The synapses of all inputs
private readonly List<Synapse> _synapses = new();
public List<Synapse> synapses => _synapses; public List<Synapse> synapses => _synapses;
// Call this function to ensure that there is at least one nucleus // 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 // This is an invariant and should be ensured before the nucleus is used
// because output requires it. // because output requires it.
public void EnsureInitialization() { public void EnsureInitialization() {
nuclei ??= new List<Nucleus>(); nuclei ??= new List<INucleus>();
if (nuclei.Count == 0) if (nuclei.Count == 0)
new Neuroid(this, "Output"); // Every cluster should have at least 1 neuroid new Neuroid(this, "Output"); // Every cluster should have at least 1 neuroid
} }
@ -36,12 +38,12 @@ public class Cluster : ScriptableObject, INucleus {
public void RemoveReceiver(INucleus receiver) { public void RemoveReceiver(INucleus receiver) {
output.RemoveReceiver(receiver); output.RemoveReceiver(receiver);
} }
public List<Receiver> receivers { public List<INucleus> receivers {
get => output.receivers; get => output.receivers;
} }
public void AddSynapse(INucleus sender) { public void AddSynapse(IReceptor sender) {
Synapse synapse = new (sender, 1.0f); Synapse synapse = new(sender, 1.0f);
synapses.Add(synapse); synapses.Add(synapse);
} }
@ -63,17 +65,18 @@ public class Cluster : ScriptableObject, INucleus {
foreach (Synapse synapse in nucleus.synapses) { foreach (Synapse synapse in nucleus.synapses) {
if (synapse != null && synapse.nucleus != null) { if (synapse != null && synapse.nucleus != null) {
visitedSynapses.Add(synapse); visitedSynapses.Add(synapse);
MarkNuclei(visitedNuclei, synapse.nucleus); if (synapse.nucleus is INucleus synapse_nucleus)
MarkNuclei(visitedNuclei, synapse_nucleus);
} }
} }
nucleus.synapses.RemoveAll(synapse => visitedSynapses.Contains(synapse) == false); nucleus.synapses.RemoveAll(synapse => visitedSynapses.Contains(synapse) == false);
} }
if (nucleus.receivers != null) { if (nucleus.receivers != null) {
HashSet<Receiver> visitedReceivers = new(); HashSet<INucleus> visitedReceivers = new();
foreach (Receiver receiver in nucleus.receivers) { foreach (INucleus receiver in nucleus.receivers) {
if (receiver != null && receiver.nucleus != null) { if (receiver != null && receiver != null) {
visitedReceivers.Add(receiver); visitedReceivers.Add(receiver);
visitedNuclei.Add(receiver.nucleus); visitedNuclei.Add(receiver);
} }
} }
nucleus.receivers.RemoveAll(receiver => visitedReceivers.Contains(receiver) == false); nucleus.receivers.RemoveAll(receiver => visitedReceivers.Contains(receiver) == false);
@ -91,5 +94,17 @@ public class Cluster : ScriptableObject, INucleus {
// it is not, I should take inputs from the synapses and route them to the right internal nuclei // it is not, I should take inputs from the synapses and route them to the right internal nuclei
} }
public void UpdateNuclei() {
foreach (INucleus nucleus in nuclei)
nucleus.IncreaseAge();
}
public void IncreaseAge() {
foreach (INucleus nucleus in nuclei)
nucleus.IncreaseAge();
}
// ha ha ha
#endregion Dynamics #endregion Dynamics
} }

View File

@ -1,33 +1,45 @@
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
public interface INucleus { public interface INucleus : IReceptor {
#region static struct #region static struct
public string name { get; set; }
// Cluster // Cluster
public Cluster cluster { get; } public Cluster cluster { get; }
// Receivers
public List<Receiver> receivers { get; }
public void AddReceiver(INucleus receiver);
public void RemoveReceiver(INucleus receiverNucleus);
// Senders // Senders
public List<Synapse> synapses { get; } public List<Synapse> synapses { get; }
public void AddSynapse(INucleus sender); public void AddSynapse(IReceptor sender);
#endregion static struct #endregion static struct
#region dynamic state #region dynamic state
public bool isSleeping { get; }
public Vector3 outputValue { get; }
public void UpdateState(); public void UpdateState();
public void IncreaseAge();
#endregion dynamic state #endregion dynamic state
} }
public interface IReceptor {
#region static
public string name { get; set; }
// Receivers
public List<INucleus> receivers { get; }
public void AddReceiver(INucleus receiver);
public void RemoveReceiver(INucleus receiverNucleus);
#endregion static
#region dynamic
public Vector3 outputValue { get; }
public bool isSleeping { get; }
#endregion dynamic
}

View File

@ -2,49 +2,8 @@ using UnityEngine;
[System.Serializable] [System.Serializable]
public class Neuroid : Nucleus { public class Neuroid : Nucleus {
public enum CurvePresets {
Linear,
Power,
Sqrt,
Reciprocal,
Custom
}
[SerializeField]
private CurvePresets _curvePreset;
public CurvePresets curvePreset {
get { return _curvePreset; }
set {
_curvePreset = value;
this.curve = GenerateCurve();
}
}
public AnimationCurve curve;
public float curveMax = 1.0f;
public AnimationCurve GenerateCurve() {
switch (this.curvePreset) {
case CurvePresets.Linear:
this.curveMax = 1;
return Synapse.Presets.Linear(1);
case CurvePresets.Power:
this.curveMax = 1;
return Synapse.Presets.Power(2.0f, 1);
case CurvePresets.Sqrt:
this.curveMax = 1;
return Synapse.Presets.Power(0.5f, 1);
case CurvePresets.Reciprocal:
this.curveMax = 1 / 0.01f * 1;
return Synapse.Presets.Reciprocal(1);
default:
this.curveMax = 1;
return this.curve;
}
}
public bool average = false; public bool average = false;
public bool inverse = false;
public float exponent = 1.0f;
public Neuroid(Cluster brain, string name) : base(name) { public Neuroid(Cluster brain, string name) : base(name) {
this.cluster = brain; this.cluster = brain;

View File

@ -19,26 +19,65 @@ public class Nucleus : INucleus {
private List<Synapse> _synapses = new(); private List<Synapse> _synapses = new();
public List<Synapse> synapses => _synapses; public List<Synapse> synapses => _synapses;
[SerializeField] [SerializeReference]
private List<Receiver> _receivers = new(); private List<INucleus> _receivers = new();
public List<Receiver> receivers => _receivers; public List<INucleus> receivers => _receivers;
#region Serialization #region Serialization
[SerializeField] [SerializeField]
protected string nucleusType; protected string nucleusType;
public enum CurvePresets {
Linear,
Power,
Sqrt,
Reciprocal,
Custom
}
[SerializeField]
private CurvePresets _curvePreset;
public CurvePresets curvePreset {
get { return _curvePreset; }
set {
_curvePreset = value;
this.curve = GenerateCurve();
}
}
public AnimationCurve curve;
public float curveMax = 1.0f;
public AnimationCurve GenerateCurve() {
switch (this.curvePreset) {
case CurvePresets.Linear:
this.curveMax = 1;
return Synapse.Presets.Linear(1);
case CurvePresets.Power:
this.curveMax = 1;
return Synapse.Presets.Power(2.0f, 1);
case CurvePresets.Sqrt:
this.curveMax = 1;
return Synapse.Presets.Power(0.5f, 1);
case CurvePresets.Reciprocal:
this.curveMax = 1 / 0.01f * 1;
return Synapse.Presets.Reciprocal(1);
default:
this.curveMax = 1;
return this.curve;
}
}
public virtual void Rebuild(NanoBrain brain) { public virtual void Rebuild(NanoBrain brain) {
if (this.synapses != null) { if (this.synapses != null) {
foreach (Synapse synapse in synapses) foreach (Synapse synapse in synapses)
synapse.Rebuild(brain); synapse.Rebuild(brain);
} }
foreach (Receiver receiver in receivers.ToArray()) { // foreach (INucleus receiver in receivers.ToArray()) {
if (receiver.Rebuild(brain) == false) { // if (receiver.Rebuild(brain) == false) {
Debug.Log("Rebuilding failed, removing receiver."); // Debug.Log("Rebuilding failed, removing receiver.");
receivers.Remove(receiver); // receivers.Remove(receiver);
} // }
} // }
} }
public static Nucleus RebuildType(NanoBrain brain, Nucleus nucleus) { public static Nucleus RebuildType(NanoBrain brain, Nucleus nucleus) {
@ -63,8 +102,7 @@ public class Nucleus : INucleus {
public Cluster cluster { get; set; } public Cluster cluster { get; set; }
private Vector3 _outputValue; private Vector3 _outputValue;
public Vector3 outputValue public Vector3 outputValue {
{
get { return _outputValue; } get { return _outputValue; }
set { set {
this.stale = 0; this.stale = 0;
@ -96,30 +134,33 @@ public class Nucleus : INucleus {
} }
public virtual void AddReceiver(INucleus receivingNucleus) { public virtual void AddReceiver(INucleus receivingNucleus) {
this.receivers.Add(new Receiver(receivingNucleus)); // this.receivers.Add(new Receiver(receivingNucleus));
this.receivers.Add(receivingNucleus);
//receivingNucleus.SetWeight(this, 1.0f); //receivingNucleus.SetWeight(this, 1.0f);
receivingNucleus.AddSynapse(this); receivingNucleus.AddSynapse(this);
} }
public void RemoveReceiver(INucleus receiverNucleus) { public void RemoveReceiver(INucleus receiverNucleus) {
this.receivers.RemoveAll(receiver => receiver.nucleus == receiverNucleus); this.receivers.RemoveAll(receiver => receiver == receiverNucleus);
receiverNucleus.synapses.RemoveAll(synapse => synapse.nucleus == this); receiverNucleus.synapses.RemoveAll(synapse => synapse.nucleus == this);
} }
public static void Delete(INucleus nucleus) { public static void Delete(INucleus nucleus) {
foreach (Synapse synapse in nucleus.synapses) { foreach (Synapse synapse in nucleus.synapses) {
if (synapse.nucleus.receivers.Count > 1) { if (synapse.nucleus is Nucleus synapse_nucleus) {
// there is another nucleus feeding into this input nucleus if (synapse_nucleus.receivers.Count > 1) {
synapse.nucleus.receivers.RemoveAll(r => r.nucleus == nucleus); // there is another nucleus feeding into this input nucleus
} synapse_nucleus.receivers.RemoveAll(r => r == nucleus);
else { }
// No other links, delete it. else {
Nucleus.Delete(synapse.nucleus); // No other links, delete it.
Nucleus.Delete(synapse_nucleus);
}
} }
} }
foreach (Receiver receiver in nucleus.receivers) { foreach (INucleus receiver in nucleus.receivers) {
if (receiver.nucleus != null && receiver.nucleus.synapses != null) if (receiver != null && receiver.synapses != null)
receiver.nucleus.synapses.RemoveAll(s => s.nucleus == nucleus); receiver.synapses.RemoveAll(s => s.nucleus == nucleus);
} }
if (nucleus.cluster != null) { if (nucleus.cluster != null) {
@ -141,7 +182,7 @@ public class Nucleus : INucleus {
return false; return false;
} }
public void AddSynapse(INucleus sendingNucleus) { public void AddSynapse(IReceptor sendingNucleus) {
Synapse synapse = new(sendingNucleus, 1.0f); Synapse synapse = new(sendingNucleus, 1.0f);
this.synapses.Add(synapse); this.synapses.Add(synapse);
} }
@ -166,146 +207,36 @@ public class Nucleus : INucleus {
// } // }
this.outputValue = result; this.outputValue = result;
foreach (Receiver receiver in this.receivers) foreach (INucleus receiver in this.receivers)
receiver.nucleus.UpdateState(); receiver.UpdateState();
} }
} }
[Serializable] // [Serializable]
public class Synapse { // public class Receiver {
[NonSerialized] // [NonSerialized]
public INucleus nucleus; // public INucleus nucleus;
public Cluster cluster; // //public int nucleusId;
public int nucleusId;
public float weight;
public enum CurvePresets { // public Receiver(INucleus nucleus) {
Linear, // this.nucleus = nucleus;
Power, // //this.nucleusId = nucleus.id;
Sqrt, // }
Reciprocal,
Custom
}
// public CurvePresets curvePreset;
// public AnimationCurve curve;
public float curveMax = 1.0f;
public Synapse(INucleus nucleus, float weight) { // public bool Rebuild(NanoBrain brain) {
this.nucleus = nucleus; // if (brain == null) {
//this.nucleusId = nucleus.id; // return false;
this.weight = weight; // }
}
public void Rebuild(NanoBrain brain) { // // Use SerializedReference instead?
if (brain == null) { // // foreach (Nucleus nucleus in brain.nuclei) {
return; // // if (nucleus.id == this.nucleusId) {
} // // this.nucleus = nucleus;
// // return true;
foreach (Nucleus nucleus in brain.nuclei) { // // }
if (nucleus.id == this.nucleusId) { // // }
this.nucleus = nucleus; // //Debug.LogWarning($"Receiver deserialization error: could not find nucleus with id {this.nucleusId}");
return; // return false;
} // }
} // }
foreach (Perceptoid perceptoid in brain.perceptei) {
if (perceptoid.id == this.nucleusId) {
this.nucleus = perceptoid;
return;
}
}
Debug.LogError($"Synapse deserialization error: could not find nucleus with id {this.nucleusId}");
}
// public AnimationCurve GenerateCurve() {
// switch (this.curvePreset) {
// case CurvePresets.Linear:
// this.curveMax = this.weight;
// return Presets.Linear(this.weight);
// case CurvePresets.Power:
// this.curveMax = this.weight;
// return Presets.Power(2.0f, this.weight);
// case CurvePresets.Sqrt:
// this.curveMax = this.weight;
// return Presets.Power(0.5f, this.weight);
// case CurvePresets.Reciprocal:
// this.curveMax = 1 / 0.01f * this.weight;
// return Presets.Reciprocal(this.weight);
// default:
// this.curveMax = weight;
// return AnimationCurve.Constant(0, 1, weight);
// }
// }
public static class Presets {
private const int samples = 32;
public static AnimationCurve Linear(float weight) {
return AnimationCurve.Linear(0f, 0f, 1000f, weight * 1000);
}
public static AnimationCurve Power(float exponent, float weight) {
// build keyframes
Keyframe[] keys = new Keyframe[samples];
for (int i = 0; i < samples; i++) {
float t = i / (float)(samples - 1);
float v = Mathf.Pow(t, exponent) * weight;
keys[i] = new Keyframe(t, v);
}
AnimationCurve curve = new(keys);
// set tangent modes for each key to Auto (smooth). Use Linear if you prefer straight segments.
for (int i = 0; i < curve.length; i++) {
AnimationUtility.SetKeyLeftTangentMode(curve, i, AnimationUtility.TangentMode.Auto);
AnimationUtility.SetKeyRightTangentMode(curve, i, AnimationUtility.TangentMode.Auto);
}
return curve;
}
public static AnimationCurve Reciprocal(float weight) {
int samples = 128;
float xMin = 0.001f;
float xMax = 1;
var keys = new Keyframe[samples];
for (int i = 0; i < samples; i++) {
float t = i / (float)(samples - 1);
float x = Mathf.Lerp(xMin, xMax, t);
float y = 1f / x * weight;
keys[i] = new Keyframe(x, y);
}
var curve = new AnimationCurve(keys);
for (int i = 0; i < curve.length; i++) {
AnimationUtility.SetKeyLeftTangentMode(curve, i, AnimationUtility.TangentMode.Linear);
AnimationUtility.SetKeyRightTangentMode(curve, i, AnimationUtility.TangentMode.Linear);
}
return curve;
}
}
}
[Serializable]
public class Receiver {
[NonSerialized]
public INucleus nucleus;
//public int nucleusId;
public Receiver(INucleus nucleus) {
this.nucleus = nucleus;
//this.nucleusId = nucleus.id;
}
public bool Rebuild(NanoBrain brain) {
if (brain == null) {
return false;
}
// Use SerializedReference instead?
// foreach (Nucleus nucleus in brain.nuclei) {
// if (nucleus.id == this.nucleusId) {
// this.nucleus = nucleus;
// return true;
// }
// }
//Debug.LogWarning($"Receiver deserialization error: could not find nucleus with id {this.nucleusId}");
return false;
}
}

View File

@ -36,19 +36,19 @@ public class Perceptoid : Neuroid {
this.receptor.thingType = perceptoid.thingType; this.receptor.thingType = perceptoid.thingType;
// Point all receivers to this perceptoid instead of the default nucleus // Point all receivers to this perceptoid instead of the default nucleus
foreach (Receiver receiver in nucleus.receivers) { foreach (INucleus receiver in nucleus.receivers) {
foreach (Synapse synapse in receiver.nucleus.synapses) { foreach (Synapse synapse in receiver.synapses) {
if (synapse.nucleus == nucleus) if (synapse.nucleus == nucleus)
synapse.nucleus = this; synapse.nucleus = this;
} }
} }
// Point all synapses to this perceptoid instead of the default nucleus // Point all synapses to this perceptoid instead of the default nucleus
foreach (Synapse synapse in nucleus.synapses) { // foreach (Synapse synapse in nucleus.synapses) {
foreach (Receiver receiver in synapse.nucleus.receivers) { // foreach (INucleus r in synapse.nucleus.receivers) {
if (receiver.nucleus == nucleus) // if (r == nucleus)
receiver.nucleus = this; // this.receiver = this;
} // }
} // }
// Copying disabled for now // Copying disabled for now
// // Copy all the synapses // // Copy all the synapses
// this.synapses = nucleus.synapses; // this.synapses = nucleus.synapses;

View File

@ -2,7 +2,36 @@ using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using LinearAlgebra; using LinearAlgebra;
public class Receptor { public class Receptor : IReceptor {
[SerializeField]
protected string _name;
public virtual string name {
get => _name;
set => _name = value;
}
public Cluster cluster;
//public INucleus nucleus;
//[SerializeField]
[SerializeReference]
private List<INucleus> _receivers = new();
public List<INucleus> receivers => _receivers;
public virtual void AddReceiver(INucleus receivingNucleus) {
//this.receivers.Add(new Receiver(receivingNucleus));
this.receivers.Add(receivingNucleus);
//receivingNucleus.SetWeight(this, 1.0f);
receivingNucleus.AddSynapse(this);
}
public void RemoveReceiver(INucleus receiverNucleus) {
this.receivers.RemoveAll(receiver => receiver == receiverNucleus);
receiverNucleus.synapses.RemoveAll(synapse => synapse.nucleus == this);
}
public bool isSleeping => false;
/// <summary> /// <summary>
/// The list of perceptoid which can process stimuli from this receptor /// The list of perceptoid which can process stimuli from this receptor
/// </summary> /// </summary>
@ -22,12 +51,26 @@ public class Receptor {
public float distanceResolution = 0.1f; public float distanceResolution = 0.1f;
public float directionResolution = 5; public float directionResolution = 5;
public Vector3 outputValue {
get { return localPosition; }
set {
localPosition = value;
}
}
public Receptor(NanoBrain brain, int thingType) { public Receptor(NanoBrain brain, int thingType) {
this.thingType = thingType; this.thingType = thingType;
//this.perceptei.Add(perceptoid); //this.perceptei.Add(perceptoid);
brain.receptors.Add(this); brain.receptors.Add(this);
} }
public Receptor(Cluster cluster, INucleus nucleus) {
this.cluster = cluster;
//nucleus.AddSynapse(this);
this.AddReceiver(nucleus);
}
public static Receptor GetReceptor(NanoBrain brain, int thingType) { public static Receptor GetReceptor(NanoBrain brain, int thingType) {
foreach (Receptor receptor in brain.receptors) { foreach (Receptor receptor in brain.receptors) {
if (thingType == 0 || receptor.thingType == thingType) if (thingType == 0 || receptor.thingType == thingType)
@ -37,46 +80,68 @@ public class Receptor {
return newReceptor; return newReceptor;
} }
public static Receptor CreateReceptor(Cluster cluster, string nucleusName) {
if (cluster == null)
return null;
foreach (INucleus nucleus in cluster.inputs) {
if (nucleus != null && nucleus.name == nucleusName) {
Receptor receptor = new(cluster, nucleus);
return receptor;
}
}
return null;
}
public virtual void ProcessStimulus(int thingId, Vector3 newLocalPositionVector, string thingName = null) { public virtual void ProcessStimulus(int thingId, Vector3 newLocalPositionVector, string thingName = null) {
this.localPosition = newLocalPositionVector; this.localPosition = newLocalPositionVector;
Perceptoid selectedPerceptoid = null; INucleus selectedReceiver = null;
foreach (Perceptoid perceptoid in this.perceptei) { foreach (INucleus receiver in this.receivers) {
if (perceptoid.thingId == thingId) { selectedReceiver = receiver;
// We found an existing perceptoid for this thing
selectedPerceptoid = perceptoid;
// Do not look any further
break;
}
else if (perceptoid.isSleeping) {
// A sleeping perceptoid is not active and can therefore always be reused
selectedPerceptoid = perceptoid;
// Look further because we could find a existing perceptoid for this thing
}
else if (selectedPerceptoid == null) {
// If we haven't found a perceptoid yet, just start by taking the first
selectedPerceptoid = perceptoid;
}
else if (selectedPerceptoid.isSleeping == false) {
// If no existing or sleeping perceptoid is found, we look for the perceptoid
// we the furthest (least interesting) stimulus
if (perceptoid.receptor.localPosition.magnitude < selectedPerceptoid.receptor.localPosition.magnitude) {
Debug.Log($"{selectedPerceptoid.name} {selectedPerceptoid.receptor.localPosition.magnitude} {perceptoid.receptor.localPosition.magnitude} ");
selectedPerceptoid = perceptoid;
}
}
} }
if (selectedPerceptoid == null) { // selectedReceiver.thingId = thingId;
Debug.Log("No perceptoid selected, stimulus is ignored"); // if (thingName != null)
return; // selectedReceiver.nucleus.name = selectedReceiver.nucleus.baseName + " " + thingName;
} selectedReceiver.UpdateState();
// Debug.Log($"Stimulus {thingType} {thingId} {selectedPerceptoid.name}");
selectedPerceptoid.thingId = thingId; // Perceptoid selectedPerceptoid = null;
if (thingName != null) // foreach (Perceptoid perceptoid in this.perceptei) {
selectedPerceptoid.name = selectedPerceptoid.baseName + " " + thingName; // if (perceptoid.thingId == thingId) {
selectedPerceptoid.UpdateState(); // // We found an existing perceptoid for this thing
// selectedPerceptoid = perceptoid;
// // Do not look any further
// break;
// }
// else if (perceptoid.isSleeping) {
// // A sleeping perceptoid is not active and can therefore always be reused
// selectedPerceptoid = perceptoid;
// // Look further because we could find a existing perceptoid for this thing
// }
// else if (selectedPerceptoid == null) {
// // If we haven't found a perceptoid yet, just start by taking the first
// selectedPerceptoid = perceptoid;
// }
// else if (selectedPerceptoid.isSleeping == false) {
// // If no existing or sleeping perceptoid is found, we look for the perceptoid
// // we the furthest (least interesting) stimulus
// if (perceptoid.receptor.localPosition.magnitude < selectedPerceptoid.receptor.localPosition.magnitude) {
// Debug.Log($"{selectedPerceptoid.name} {selectedPerceptoid.receptor.localPosition.magnitude} {perceptoid.receptor.localPosition.magnitude} ");
// selectedPerceptoid = perceptoid;
// }
// }
// }
// if (selectedPerceptoid == null) {
// Debug.Log("No perceptoid selected, stimulus is ignored");
// return;
// }
// // Debug.Log($"Stimulus {thingType} {thingId} {selectedPerceptoid.name}");
// selectedPerceptoid.thingId = thingId;
// if (thingName != null)
// selectedPerceptoid.name = selectedPerceptoid.baseName + " " + thingName;
// selectedPerceptoid.UpdateState();
} }
} }

115
Assets/NanoBrain/Synapse.cs Normal file
View File

@ -0,0 +1,115 @@
using System;
using UnityEngine;
using UnityEditor;
[Serializable]
public class Synapse {
//[NonSerialized]
[SerializeReference]
public IReceptor nucleus;
[SerializeReference]
public Cluster cluster;
//public int nucleusId;
public float weight;
public enum CurvePresets {
Linear,
Power,
Sqrt,
Reciprocal,
Custom
}
// public CurvePresets curvePreset;
// public AnimationCurve curve;
public float curveMax = 1.0f;
public Synapse(IReceptor nucleus, float weight) {
this.nucleus = nucleus;
//this.nucleusId = nucleus.id;
this.weight = weight;
}
public void Rebuild(NanoBrain brain) {
// if (brain == null) {
// return;
// }
// foreach (Nucleus nucleus in brain.nuclei) {
// if (nucleus.id == this.nucleusId) {
// this.nucleus = nucleus;
// return;
// }
// }
// foreach (Perceptoid perceptoid in brain.perceptei) {
// if (perceptoid.id == this.nucleusId) {
// this.nucleus = perceptoid;
// return;
// }
// }
// Debug.LogError($"Synapse deserialization error: could not find nucleus with id {this.nucleusId}");
}
// public AnimationCurve GenerateCurve() {
// switch (this.curvePreset) {
// case CurvePresets.Linear:
// this.curveMax = this.weight;
// return Presets.Linear(this.weight);
// case CurvePresets.Power:
// this.curveMax = this.weight;
// return Presets.Power(2.0f, this.weight);
// case CurvePresets.Sqrt:
// this.curveMax = this.weight;
// return Presets.Power(0.5f, this.weight);
// case CurvePresets.Reciprocal:
// this.curveMax = 1 / 0.01f * this.weight;
// return Presets.Reciprocal(this.weight);
// default:
// this.curveMax = weight;
// return AnimationCurve.Constant(0, 1, weight);
// }
// }
public static class Presets {
private const int samples = 32;
public static AnimationCurve Linear(float weight) {
return AnimationCurve.Linear(0f, 0f, 1000f, weight * 1000);
}
public static AnimationCurve Power(float exponent, float weight) {
// build keyframes
Keyframe[] keys = new Keyframe[samples];
for (int i = 0; i < samples; i++) {
float t = i / (float)(samples - 1);
float v = Mathf.Pow(t, exponent) * weight;
keys[i] = new Keyframe(t, v);
}
AnimationCurve curve = new(keys);
// set tangent modes for each key to Auto (smooth). Use Linear if you prefer straight segments.
for (int i = 0; i < curve.length; i++) {
AnimationUtility.SetKeyLeftTangentMode(curve, i, AnimationUtility.TangentMode.Auto);
AnimationUtility.SetKeyRightTangentMode(curve, i, AnimationUtility.TangentMode.Auto);
}
return curve;
}
public static AnimationCurve Reciprocal(float weight) {
int samples = 128;
float xMin = 0.001f;
float xMax = 1;
var keys = new Keyframe[samples];
for (int i = 0; i < samples; i++) {
float t = i / (float)(samples - 1);
float x = Mathf.Lerp(xMin, xMax, t);
float y = 1f / x * weight;
keys[i] = new Keyframe(x, y);
}
var curve = new AnimationCurve(keys);
for (int i = 0; i < curve.length; i++) {
AnimationUtility.SetKeyLeftTangentMode(curve, i, AnimationUtility.TangentMode.Linear);
AnimationUtility.SetKeyRightTangentMode(curve, i, AnimationUtility.TangentMode.Linear);
}
return curve;
}
}
}

View File

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 334a58eafccd60cbdb32f719e9e861c6

View File

@ -75,7 +75,7 @@ public class ClusterInspector : Editor {
INucleus currentNucleus; INucleus currentNucleus;
GameObject gameObject; GameObject gameObject;
private List<NeuroidLayer> layers = new(); private List<NeuroidLayer> layers = new();
private readonly Dictionary<INucleus, Vector2Int> neuroidPositions = new(); private readonly Dictionary<IReceptor, Vector2Int> neuroidPositions = new();
Vector2 pan = Vector2.zero; Vector2 pan = Vector2.zero;
//float zoom = 1f; //float zoom = 1f;
@ -101,7 +101,7 @@ public class ClusterInspector : Editor {
RegisterCallback<MouseUpEvent>(OnMouseUp); RegisterCallback<MouseUpEvent>(OnMouseUp);
} }
public void SetGraph(GameObject gameObject, Cluster brain, Nucleus nucleus, VisualElement inspectorContainer) { public void SetGraph(GameObject gameObject, Cluster brain, INucleus nucleus, VisualElement inspectorContainer) {
this.gameObject = gameObject; this.gameObject = gameObject;
this.cluster = brain; this.cluster = brain;
if (Application.isPlaying == false) if (Application.isPlaying == false)
@ -135,8 +135,8 @@ public class ClusterInspector : Editor {
NeuroidLayer currentLayer = new() { ix = layerIx }; NeuroidLayer currentLayer = new() { ix = layerIx };
if (selectedNucleus.receivers != null) { if (selectedNucleus.receivers != null) {
foreach (Receiver receiver in selectedNucleus.receivers) { foreach (INucleus receiver in selectedNucleus.receivers) {
INucleus outputNeuroid = receiver.nucleus; INucleus outputNeuroid = receiver;
if (outputNeuroid != null) { if (outputNeuroid != null) {
AddToLayer(currentLayer, outputNeuroid); AddToLayer(currentLayer, outputNeuroid);
// Debug.Log($"layer {layerIx} nucleus {outputNeuroid.name}"); // Debug.Log($"layer {layerIx} nucleus {outputNeuroid.name}");
@ -158,7 +158,7 @@ public class ClusterInspector : Editor {
if (selectedNucleus.synapses != null) { if (selectedNucleus.synapses != null) {
foreach (Synapse synapse in selectedNucleus.synapses) { foreach (Synapse synapse in selectedNucleus.synapses) {
INucleus input = synapse.nucleus; IReceptor input = synapse.nucleus;
AddToLayer(currentLayer, input); AddToLayer(currentLayer, input);
// Debug.Log($"layer {layerIx} nucleus {input.name}"); // Debug.Log($"layer {layerIx} nucleus {input.name}");
} }
@ -168,7 +168,7 @@ public class ClusterInspector : Editor {
} }
} }
private void AddToLayer(NeuroidLayer layer, INucleus nucleus) { private void AddToLayer(NeuroidLayer layer, IReceptor nucleus) {
if (nucleus == null) if (nucleus == null)
return; return;
layer.neuroids.Add(nucleus); layer.neuroids.Add(nucleus);
@ -225,8 +225,8 @@ public class ClusterInspector : Editor {
// Determine the maximum value in this layer // Determine the maximum value in this layer
// This is used to 'scale' the output value colors of the nuclei // This is used to 'scale' the output value colors of the nuclei
float maxValue = 0; float maxValue = 0;
foreach (Receiver receiver in nucleus.receivers) { foreach (INucleus receiver in nucleus.receivers) {
if (receiver.nucleus is Neuroid neuroid) { if (receiver is Neuroid neuroid) {
float value = neuroid.outputValue.magnitude; float value = neuroid.outputValue.magnitude;
if (value > maxValue) if (value > maxValue)
maxValue = value; maxValue = value;
@ -238,8 +238,8 @@ public class ClusterInspector : Editor {
float margin = 10 + spacing / 2; float margin = 10 + spacing / 2;
int row = 0; int row = 0;
foreach (Receiver receiver in nucleus.receivers) { foreach (INucleus receiver in nucleus.receivers) {
INucleus receiverNucleus = receiver.nucleus; INucleus receiverNucleus = receiver;
if (receiverNucleus == null) if (receiverNucleus == null)
continue; continue;
@ -286,13 +286,14 @@ public class ClusterInspector : Editor {
// } // }
// else { // else {
DrawNucleus(synapse.nucleus, pos, maxValue, size); if (synapse.nucleus != null)
DrawNucleus(synapse.nucleus, pos, maxValue, size);
row++; row++;
// } // }
} }
} }
private void DrawNucleus(INucleus nucleus, Vector3 position, float maxValue, float size) { private void DrawNucleus(IReceptor nucleus, Vector3 position, float maxValue, float size) {
if (nucleus.isSleeping) if (nucleus.isSleeping)
Handles.color = Color.darkRed; Handles.color = Color.darkRed;
else { else {
@ -360,7 +361,7 @@ public class ClusterInspector : Editor {
// To do: add HandleClick (see above) to expand the array // To do: add HandleClick (see above) to expand the array
} }
private void HandleMouseHover(INucleus nucleus, Rect rect) { private void HandleMouseHover(IReceptor nucleus, Rect rect) {
GUIContent tooltip; GUIContent tooltip;
if (nucleus is Perceptoid perceptoid) { if (nucleus is Perceptoid perceptoid) {
if (perceptoid.receptor != null) { if (perceptoid.receptor != null) {
@ -377,10 +378,14 @@ public class ClusterInspector : Editor {
$"\nValue: {nucleus.outputValue}"); $"\nValue: {nucleus.outputValue}");
} }
} }
else { else if (nucleus is INucleus n) {
tooltip = new(
$"{nucleus.name}" +
$"\nsynapse count {n.synapses.Count}" +
$"\nValue: {nucleus.outputValue}");
} else {
tooltip = new( tooltip = new(
$"{nucleus.name}" + $"{nucleus.name}" +
$"\nsynapse count {nucleus.synapses.Count}" +
$"\nValue: {nucleus.outputValue}"); $"\nValue: {nucleus.outputValue}");
} }
@ -393,9 +398,11 @@ public class ClusterInspector : Editor {
GUI.Box(tooltipRect, tooltip); GUI.Box(tooltipRect, tooltip);
} }
private void HandleClicked(INucleus nucleus) { private void HandleClicked(IReceptor nucleus) {
this.currentNucleus = nucleus; if (nucleus is INucleus n) {
this.currentNucleus = n;
BuildLayers(); BuildLayers();
}
} }
void DrawInspector(VisualElement inspectorContainer) { void DrawInspector(VisualElement inspectorContainer) {
@ -430,7 +437,7 @@ public class ClusterInspector : Editor {
perceptoid.array.RemovePerceptoid(); perceptoid.array.RemovePerceptoid();
EditorGUILayout.EndHorizontal(); EditorGUILayout.EndHorizontal();
} }
else if (this.currentNucleus is Neuroid neuroid) { else if (this.currentNucleus is Nucleus neuroid) {
EditorGUILayout.BeginHorizontal(); EditorGUILayout.BeginHorizontal();
EditorGUILayout.LabelField("Activation Curve", GUILayout.Width(150)); EditorGUILayout.LabelField("Activation Curve", GUILayout.Width(150));
if (neuroid.curveMax > 0) if (neuroid.curveMax > 0)
@ -517,9 +524,9 @@ public class ClusterInspector : Editor {
return; return;
if (nucleus.cluster != null) if (nucleus.cluster != null)
this.currentNucleus = nucleus.cluster.output; this.currentNucleus = nucleus.cluster.output;
foreach (Receiver receiver in nucleus.receivers) { foreach (INucleus receiver in nucleus.receivers) {
if (receiver.nucleus != null) { if (receiver != null) {
this.currentNucleus = receiver.nucleus; this.currentNucleus = receiver;
break; break;
} }
} }
@ -548,7 +555,7 @@ public class ClusterInspector : Editor {
if (cluster == null) if (cluster == null)
return; return;
IEnumerable<string> synapseNuclei = this.currentNucleus.synapses.Select(synapse => synapse.nucleus.name); IEnumerable<string> synapseNuclei = this.currentNucleus.synapses.Select(synapse => synapse.nucleus != null ? synapse.nucleus.name: "");
//IEnumerable<string> perceptei = this.currentNucleus.brain.perceptei.Select(i => i.name).Except(synapseNuclei); //IEnumerable<string> perceptei = this.currentNucleus.brain.perceptei.Select(i => i.name).Except(synapseNuclei);
IEnumerable<string> nuclei = cluster.nuclei.Select(i => i.name).Except(synapseNuclei); IEnumerable<string> nuclei = cluster.nuclei.Select(i => i.name).Except(synapseNuclei);
//string[] names = perceptei.Concat(nuclei).ToArray(); //string[] names = perceptei.Concat(nuclei).ToArray();
@ -564,7 +571,7 @@ public class ClusterInspector : Editor {
// Nucleus n = this.currentNucleus.brain.nuclei[selectedIndex - perceptei.Count()]; // Nucleus n = this.currentNucleus.brain.nuclei[selectedIndex - perceptei.Count()];
// n.AddReceiver(this.currentNucleus); // n.AddReceiver(this.currentNucleus);
// } // }
Nucleus n = cluster.nuclei[selectedIndex]; INucleus n = cluster.nuclei[selectedIndex];
n.AddReceiver(this.currentNucleus); n.AddReceiver(this.currentNucleus);
} }
} }
@ -606,7 +613,7 @@ public class ClusterInspector : Editor {
public class NeuroidLayer { public class NeuroidLayer {
public int ix = 0; public int ix = 0;
public List<INucleus> neuroids = new(); public List<IReceptor> neuroids = new();
} }
public class ClusterWrapper : ScriptableObject { public class ClusterWrapper : ScriptableObject {

View File

@ -20,7 +20,7 @@ public class NanoBrainComponent_Editor : Editor {
public override VisualElement CreateInspectorGUI() { public override VisualElement CreateInspectorGUI() {
//NanoBrainComponent component = target as NanoBrainComponent; //NanoBrainComponent component = target as NanoBrainComponent;
NanoBrain brain = Application.isPlaying ? component.brain : component.defaultBrain; Cluster brain = Application.isPlaying ? component.brain : component.defaultBrain;
if (Application.isPlaying == false) if (Application.isPlaying == false)
serializedObject.Update(); serializedObject.Update();
@ -52,8 +52,8 @@ public class NanoBrainComponent_Editor : Editor {
minHeight = 500, minHeight = 500,
} }
}; };
NanoBrainInspector.GraphView board; ClusterInspector.GraphView board;
board = new NanoBrainInspector.GraphView(); board = new ClusterInspector.GraphView();
board.style.flexGrow = 1; board.style.flexGrow = 1;
inspectorContainer = new VisualElement { inspectorContainer = new VisualElement {

View File

@ -73,7 +73,7 @@ public class NanoBrainInspector : Editor {
INucleus currentNucleus; INucleus currentNucleus;
GameObject gameObject; GameObject gameObject;
private List<NeuroidLayer> layers = new(); private List<NeuroidLayer> layers = new();
private readonly Dictionary<INucleus, Vector2Int> neuroidPositions = new(); private readonly Dictionary<IReceptor, Vector2Int> neuroidPositions = new();
Vector2 pan = Vector2.zero; Vector2 pan = Vector2.zero;
//float zoom = 1f; //float zoom = 1f;
@ -133,8 +133,8 @@ public class NanoBrainInspector : Editor {
NeuroidLayer currentLayer = new() { ix = layerIx }; NeuroidLayer currentLayer = new() { ix = layerIx };
if (selectedNucleus.receivers != null) { if (selectedNucleus.receivers != null) {
foreach (Receiver receiver in selectedNucleus.receivers) { foreach (INucleus receiver in selectedNucleus.receivers) {
INucleus outputNeuroid = receiver.nucleus; INucleus outputNeuroid = receiver;
if (outputNeuroid != null) { if (outputNeuroid != null) {
AddToLayer(currentLayer, outputNeuroid); AddToLayer(currentLayer, outputNeuroid);
// Debug.Log($"layer {layerIx} nucleus {outputNeuroid.name}"); // Debug.Log($"layer {layerIx} nucleus {outputNeuroid.name}");
@ -156,7 +156,7 @@ public class NanoBrainInspector : Editor {
if (selectedNucleus.synapses != null) { if (selectedNucleus.synapses != null) {
foreach (Synapse synapse in selectedNucleus.synapses) { foreach (Synapse synapse in selectedNucleus.synapses) {
INucleus input = synapse.nucleus; IReceptor input = synapse.nucleus;
AddToLayer(currentLayer, input); AddToLayer(currentLayer, input);
// Debug.Log($"layer {layerIx} nucleus {input.name}"); // Debug.Log($"layer {layerIx} nucleus {input.name}");
} }
@ -166,7 +166,7 @@ public class NanoBrainInspector : Editor {
} }
} }
private void AddToLayer(NeuroidLayer layer, INucleus nucleus) { private void AddToLayer(NeuroidLayer layer, IReceptor nucleus) {
if (nucleus == null) if (nucleus == null)
return; return;
layer.neuroids.Add(nucleus); layer.neuroids.Add(nucleus);
@ -223,8 +223,8 @@ public class NanoBrainInspector : Editor {
// Determine the maximum value in this layer // Determine the maximum value in this layer
// This is used to 'scale' the output value colors of the nuclei // This is used to 'scale' the output value colors of the nuclei
float maxValue = 0; float maxValue = 0;
foreach (Receiver receiver in nucleus.receivers) { foreach (INucleus receiver in nucleus.receivers) {
if (receiver.nucleus is Neuroid neuroid) { if (receiver is Neuroid neuroid) {
float value = neuroid.outputValue.magnitude; float value = neuroid.outputValue.magnitude;
if (value > maxValue) if (value > maxValue)
maxValue = value; maxValue = value;
@ -236,8 +236,8 @@ public class NanoBrainInspector : Editor {
float margin = 10 + spacing / 2; float margin = 10 + spacing / 2;
int row = 0; int row = 0;
foreach (Receiver receiver in nucleus.receivers) { foreach (INucleus receiver in nucleus.receivers) {
INucleus receiverNucleus = receiver.nucleus; INucleus receiverNucleus = receiver;
if (receiverNucleus == null) if (receiverNucleus == null)
continue; continue;
@ -290,7 +290,7 @@ public class NanoBrainInspector : Editor {
} }
} }
private void DrawNucleus(INucleus nucleus, Vector3 position, float maxValue, float size) { private void DrawNucleus(IReceptor nucleus, Vector3 position, float maxValue, float size) {
if (nucleus.isSleeping) if (nucleus.isSleeping)
Handles.color = Color.darkRed; Handles.color = Color.darkRed;
else { else {
@ -358,7 +358,7 @@ public class NanoBrainInspector : Editor {
// To do: add HandleClick (see above) to expand the array // To do: add HandleClick (see above) to expand the array
} }
private void HandleMouseHover(INucleus nucleus, Rect rect) { private void HandleMouseHover(IReceptor nucleus, Rect rect) {
GUIContent tooltip; GUIContent tooltip;
if (nucleus is Perceptoid perceptoid) { if (nucleus is Perceptoid perceptoid) {
if (perceptoid.receptor != null) { if (perceptoid.receptor != null) {
@ -375,10 +375,15 @@ public class NanoBrainInspector : Editor {
$"\nValue: {nucleus.outputValue}"); $"\nValue: {nucleus.outputValue}");
} }
} }
else if (nucleus is INucleus n) {
tooltip = new(
$"{nucleus.name}" +
$"\nsynapse count {n.synapses.Count}" +
$"\nValue: {nucleus.outputValue}");
}
else { else {
tooltip = new( tooltip = new(
$"{nucleus.name}" + $"{nucleus.name}" +
$"\nsynapse count {nucleus.synapses.Count}" +
$"\nValue: {nucleus.outputValue}"); $"\nValue: {nucleus.outputValue}");
} }
@ -391,9 +396,11 @@ public class NanoBrainInspector : Editor {
GUI.Box(tooltipRect, tooltip); GUI.Box(tooltipRect, tooltip);
} }
private void HandleClicked(INucleus nucleus) { private void HandleClicked(IReceptor nucleus) {
this.currentNucleus = nucleus; if (nucleus is INucleus n) {
BuildLayers(); this.currentNucleus = n;
BuildLayers();
}
} }
void DrawInspector(VisualElement inspectorContainer) { void DrawInspector(VisualElement inspectorContainer) {
@ -515,9 +522,9 @@ public class NanoBrainInspector : Editor {
return; return;
if (nucleus.cluster != null) if (nucleus.cluster != null)
this.currentNucleus = nucleus.cluster.output; this.currentNucleus = nucleus.cluster.output;
foreach (Receiver receiver in nucleus.receivers) { foreach (INucleus receiver in nucleus.receivers) {
if (receiver.nucleus != null) { if (receiver != null) {
this.currentNucleus = receiver.nucleus; this.currentNucleus = receiver;
break; break;
} }
} }
@ -561,8 +568,8 @@ public class NanoBrainInspector : Editor {
// Nucleus n = this.currentNucleus.brain.nuclei[selectedIndex - perceptei.Count()]; // Nucleus n = this.currentNucleus.brain.nuclei[selectedIndex - perceptei.Count()];
// n.AddReceiver(this.currentNucleus); // n.AddReceiver(this.currentNucleus);
// } // }
Nucleus n = this.currentNucleus.cluster.nuclei[selectedIndex]; INucleus n = this.currentNucleus.cluster.nuclei[selectedIndex];
n.AddReceiver(this.currentNucleus); n.AddReceiver(this.currentNucleus);
} }
} }

View File

@ -17,7 +17,7 @@ public class NanoBrain : ScriptableObject, ISerializationCallbackReceiver {
public NanoBrain() { public NanoBrain() {
// this.cluster = new(); // this.cluster = new();
// this.output = new Neuroid(this.cluster, "Root"); // this.output = new Neuroid(this.cluster, "Root");
} }
public Cluster cluster; public Cluster cluster;
@ -38,7 +38,9 @@ public class NanoBrain : ScriptableObject, ISerializationCallbackReceiver {
} }
public void OnBeforeSerialize() { public void OnBeforeSerialize() {
this.rootId = output.id; if (output != null) {
this.rootId = output.id;
}
} }
public void OnAfterDeserialize() { public void OnAfterDeserialize() {
try { try {

View File

@ -1,11 +1,11 @@
using UnityEngine; using UnityEngine;
public class NanoBrainComponent : MonoBehaviour { public class NanoBrainComponent : MonoBehaviour {
public NanoBrain defaultBrain; public Cluster defaultBrain;
private NanoBrain brainInstance; private Cluster brainInstance;
public Nucleus root => brainInstance.output; public INucleus root => brainInstance.output;
public NanoBrain brain { public Cluster brain {
get { get {
if (brainInstance == null && defaultBrain != null) { if (brainInstance == null && defaultBrain != null) {
brainInstance = Instantiate(defaultBrain); brainInstance = Instantiate(defaultBrain);
@ -21,8 +21,8 @@ public class NanoBrainComponent : MonoBehaviour {
} }
} }
public static void UpdateWeight(NanoBrain brain, string name, float weight) { public static void UpdateWeight(Cluster brain, string name, float weight) {
Nucleus root = brain.output; INucleus root = brain.output;
foreach (Synapse synapse in root.synapses) { foreach (Synapse synapse in root.synapses) {
if (synapse.nucleus.name == name) { if (synapse.nucleus.name == name) {
synapse.weight = weight; synapse.weight = weight;

View File

@ -1,20 +0,0 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 60a957541c24c57e78018c202ebb1d9b, type: 3}
m_Name: New Cluster
m_EditorClassIdentifier: Assembly-CSharp::Cluster
nuclei:
- id: 764290112
_name: Output
_synapses: []
_receivers: []
nucleusType:

View File

@ -0,0 +1,100 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 60a957541c24c57e78018c202ebb1d9b, type: 3}
m_Name: NewSwarm
m_EditorClassIdentifier: Assembly-CSharp::Cluster
nuclei:
- rid: 2243601034565648442
- rid: 2243601034565648443
references:
version: 2
RefIds:
- rid: 2243601034565648442
type: {class: Neuroid, ns: , asm: Assembly-CSharp}
data:
id: 322343360
_name: Output
_synapses:
- nucleus:
rid: 2243601034565648443
cluster: {fileID: 0}
weight: 1
curveMax: 1
_receivers: []
nucleusType:
_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
inverse: 0
exponent: 1
- rid: 2243601034565648443
type: {class: Neuroid, ns: , asm: Assembly-CSharp}
data:
id: -1924138416
_name: Avoidance
_synapses: []
_receivers:
- rid: 2243601034565648442
nucleusType:
_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
inverse: 0
exponent: 1

View File

@ -179,4 +179,4 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 92f34a5e4027a1dc39efd8ce63cf6aba, type: 3} m_Script: {fileID: 11500000, guid: 92f34a5e4027a1dc39efd8ce63cf6aba, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: Assembly-CSharp::NanoBrainComponent m_EditorClassIdentifier: Assembly-CSharp::NanoBrainComponent
defaultBrain: {fileID: 11400000, guid: fc1a4800a8c531eb4855b436bc9084ae, type: 2} defaultBrain: {fileID: 11400000, guid: eddc759ede59e66cd936ad6ae2c55c46, type: 2}

View File

@ -27,9 +27,14 @@ public class Boid : MonoBehaviour {
this.id = this.GetInstanceID(); this.id = this.GetInstanceID();
nanoBrain = GetComponent<NanoBrainComponent>(); nanoBrain = GetComponent<NanoBrainComponent>();
boundaryReceptor = Receptor.GetReceptor(nanoBrain.brain, BoundaryType); // boundaryReceptor = Receptor.GetReceptor(nanoBrain.brain, BoundaryType);
boidReceptor = Receptor.GetReceptor(nanoBrain.brain, BoidType); // boidReceptor = Receptor.GetReceptor(nanoBrain.brain, BoidType);
boidVelocityReceptor = Receptor.GetReceptor(nanoBrain.brain, BoidVelocityType); // boidVelocityReceptor = Receptor.GetReceptor(nanoBrain.brain, BoidVelocityType);
boundaryReceptor = Receptor.CreateReceptor(nanoBrain.brain, "Avoidance");
boundaryReceptor.name = "Boundary";
boundaryReceptor.receivers[0].synapses[0].weight = -1;
boidReceptor = Receptor.CreateReceptor(nanoBrain.brain, "Boid");
boidVelocityReceptor = Receptor.CreateReceptor(nanoBrain.brain, "Alignment");
sc = FindFirstObjectByType<SwarmControl>(); sc = FindFirstObjectByType<SwarmControl>();

View File

@ -10,9 +10,9 @@ public class SwarmControl_Editor : Editor {
if (EditorGUI.EndChangeCheck()) { if (EditorGUI.EndChangeCheck()) {
SwarmControl swarmControl = (SwarmControl)target; SwarmControl swarmControl = (SwarmControl)target;
NanoBrain[] nanoBrains = FindObjectsByType<NanoBrain>(FindObjectsSortMode.None); Cluster[] nanoBrains = FindObjectsByType<Cluster>(FindObjectsSortMode.None);
foreach (NanoBrain brain in nanoBrains) { foreach (Cluster brain in nanoBrains) {
NanoBrainComponent.UpdateWeight(brain, "Avoidance", swarmControl.avoidanceForce); NanoBrainComponent.UpdateWeight(brain, "Avoidance", swarmControl.avoidanceForce);
NanoBrainComponent.UpdateWeight(brain, "Cohesion", swarmControl.cohesionForce); NanoBrainComponent.UpdateWeight(brain, "Cohesion", swarmControl.cohesionForce);
NanoBrainComponent.UpdateWeight(brain, "Separation", swarmControl.separationForce); NanoBrainComponent.UpdateWeight(brain, "Separation", swarmControl.separationForce);

View File

@ -7478,8 +7478,8 @@ MonoBehaviour:
array: array:
rid: -2 rid: -2
thingType: 3 thingType: 3
cluster: {fileID: 0}
rootId: -1707533328 rootId: -1707533328
cluster: {fileID: 0}
references: references:
version: 2 version: 2
RefIds: RefIds: