Foraging is working, but not correct yet

This commit is contained in:
Pascal Serrarens 2026-02-09 13:04:14 +01:00
parent f5ed87f9e9
commit 1e2726ecd4
5 changed files with 31 additions and 32 deletions

View File

@ -271,7 +271,7 @@ public class Cluster : Nucleus {
public override void UpdateNuclei() { public override void UpdateNuclei() {
this.stale++; this.stale++;
if (this.stale > 5) if (this.stale > staleValueForSleep)
_outputValue = Vector3.zero; _outputValue = Vector3.zero;
foreach (Nucleus nucleus in this.nuclei) foreach (Nucleus nucleus in this.nuclei)

View File

@ -17,20 +17,20 @@ public class ClusterInspector : Editor {
#region Start #region Start
public override VisualElement CreateInspectorGUI() { public override VisualElement CreateInspectorGUI() {
ClusterPrefab cluster = target as ClusterPrefab; ClusterPrefab prefab = target as ClusterPrefab;
if (cluster != null) if (prefab != null)
cluster.EnsureInitialization(); prefab.EnsureInitialization();
serializedObject.Update(); serializedObject.Update();
VisualElement root = new(); VisualElement root = new();
CreateInspector(root, cluster); CreateInspector(root, prefab, prefab.output);
serializedObject.ApplyModifiedProperties(); serializedObject.ApplyModifiedProperties();
return root; return root;
} }
public static GraphView CreateInspector(VisualElement root, ClusterPrefab cluster) { public static GraphView CreateInspector(VisualElement root, ClusterPrefab cluster, Nucleus output) {
root.style.paddingLeft = 0; root.style.paddingLeft = 0;
root.style.paddingRight = 0; root.style.paddingRight = 0;
root.style.paddingTop = 0; root.style.paddingTop = 0;
@ -61,14 +61,14 @@ public class ClusterInspector : Editor {
mainContainer.Add(inspectorContainer); mainContainer.Add(inspectorContainer);
root.Add(mainContainer); root.Add(mainContainer);
graph.SetGraph(null, cluster.output, inspectorContainer); graph.SetGraph(null, output, inspectorContainer);
return graph; return graph;
} }
public class GraphView : VisualElement { public class GraphView : VisualElement {
readonly ClusterPrefab cluster; readonly ClusterPrefab prefab;
SerializedObject serializedBrain; SerializedObject serializedBrain;
Nucleus currentNucleus; Nucleus currentNucleus;
GameObject gameObject; GameObject gameObject;
@ -80,7 +80,7 @@ public class ClusterInspector : Editor {
PopupField<string> outputsField; PopupField<string> outputsField;
public GraphView(ClusterPrefab prefab) { public GraphView(ClusterPrefab prefab) {
this.cluster = prefab; this.prefab = prefab;
name = "content"; name = "content";
style.flexGrow = 1; style.flexGrow = 1;
@ -100,7 +100,7 @@ public class ClusterInspector : Editor {
} }
}; };
List<string> names = this.cluster.outputs.Select(output => output.name).ToList(); List<string> names = this.prefab.outputs.Select(output => output.name).ToList();
outputsField = new(names, names.First()) { outputsField = new(names, names.First()) {
style = { flexGrow = 1 } style = { flexGrow = 1 }
}; };
@ -120,13 +120,18 @@ public class ClusterInspector : Editor {
} }
void OnOutputChanged(string outputName) { void OnOutputChanged(string outputName) {
this.currentNucleus = this.cluster.GetNucleus(outputName); if (this.currentNucleus.parent != null)
// Get nucleus in the parent instance
this.currentNucleus = this.currentNucleus.parent.GetNucleus(outputName);
else
// Get nucleus in the prefab
this.currentNucleus = this.prefab.GetNucleus(outputName);
} }
void OnAddClusterOutput() { void OnAddClusterOutput() {
Nucleus newOutput = new Neuron(this.cluster, "New Output"); Nucleus newOutput = new Neuron(this.prefab, "New Output");
outputsField.choices = this.cluster.outputs.Select(output => output.name).ToList(); outputsField.choices = this.prefab.outputs.Select(output => output.name).ToList();
outputsField.value = newOutput.name; outputsField.value = newOutput.name;
this.currentNucleus = newOutput; this.currentNucleus = newOutput;
@ -150,7 +155,7 @@ public class ClusterInspector : Editor {
this.gameObject = gameObject; this.gameObject = gameObject;
//this.cluster = brain; //this.cluster = brain;
if (Application.isPlaying == false) if (Application.isPlaying == false)
this.serializedBrain = new SerializedObject(this.cluster); this.serializedBrain = new SerializedObject(this.prefab);
this.currentNucleus = nucleus; this.currentNucleus = nucleus;
Rebuild(inspectorContainer); Rebuild(inspectorContainer);
} }
@ -165,7 +170,7 @@ public class ClusterInspector : Editor {
if (currentWrapper != null) if (currentWrapper != null)
DestroyImmediate(currentWrapper); DestroyImmediate(currentWrapper);
currentWrapper = CreateInstance<ClusterWrapper>().Init(this.currentNucleus, cluster); currentWrapper = CreateInstance<ClusterWrapper>().Init(this.currentNucleus, prefab);
DrawInspector(inspectorContainer); DrawInspector(inspectorContainer);
} }
@ -277,13 +282,6 @@ public class ClusterInspector : Editor {
DrawNucleus(nucleus, pos, maxValue, size); DrawNucleus(nucleus, pos, maxValue, size);
row++; row++;
} }
// GUIStyle style = new(EditorStyles.label) {
// alignment = TextAnchor.UpperCenter,
// normal = { textColor = Color.white },
// fontStyle = FontStyle.Bold,
// };
// Vector3 labelPos = new Vector3(150, yMax, 0) - Vector3.down * (size + 25); // below disc along up axis
// Handles.Label(labelPos, this.currentNucleus.name, style);
} }
else { else {
Handles.color = Color.white; Handles.color = Color.white;
@ -547,7 +545,7 @@ public class ClusterInspector : Editor {
EditorGUILayout.BeginHorizontal(); EditorGUILayout.BeginHorizontal();
EditorGUILayout.IntField("Array size", neuroid.array.nuclei.Count()); EditorGUILayout.IntField("Array size", neuroid.array.nuclei.Count());
if (GUILayout.Button("Add")) if (GUILayout.Button("Add"))
neuroid.array.AddNucleus(this.cluster); neuroid.array.AddNucleus(this.prefab);
if (GUILayout.Button("Del")) if (GUILayout.Button("Del"))
neuroid.array.RemoveNucleus(); neuroid.array.RemoveNucleus();
EditorGUILayout.EndHorizontal(); EditorGUILayout.EndHorizontal();
@ -560,8 +558,8 @@ public class ClusterInspector : Editor {
showSynapses = EditorGUILayout.BeginFoldoutHeaderGroup(showSynapses, "Synapses"); showSynapses = EditorGUILayout.BeginFoldoutHeaderGroup(showSynapses, "Synapses");
if (showSynapses) { if (showSynapses) {
ConnectNucleus(this.cluster, this.currentNucleus); ConnectNucleus(this.prefab, this.currentNucleus);
AddSynapse(this.cluster, this.currentNucleus); AddSynapse(this.prefab, this.currentNucleus);
if (this.currentNucleus.synapses.Count > 0) { if (this.currentNucleus.synapses.Count > 0) {
Synapse[] synapses = this.currentNucleus.synapses.ToArray(); Synapse[] synapses = this.currentNucleus.synapses.ToArray();
@ -645,7 +643,7 @@ public class ClusterInspector : Editor {
protected virtual void AddInputNeuron(Nucleus nucleus) { protected virtual void AddInputNeuron(Nucleus nucleus) {
//Neuron newNeuroid = new(this.cluster, "New neuron"); //Neuron newNeuroid = new(this.cluster, "New neuron");
Neuron newNeuroid = new(this.cluster, "New neuron"); Neuron newNeuroid = new(this.prefab, "New neuron");
newNeuroid.AddReceiver(nucleus); newNeuroid.AddReceiver(nucleus);
this.currentNucleus = newNeuroid; this.currentNucleus = newNeuroid;
BuildLayers(); BuildLayers();
@ -667,14 +665,14 @@ public class ClusterInspector : Editor {
} }
protected void AddSelectorInput(Nucleus nucleus) { protected void AddSelectorInput(Nucleus nucleus) {
Selector newSelector = new(this.cluster, "New Selector"); Selector newSelector = new(this.prefab, "New Selector");
newSelector.AddReceiver(nucleus); newSelector.AddReceiver(nucleus);
this.currentNucleus = newSelector; this.currentNucleus = newSelector;
BuildLayers(); BuildLayers();
} }
protected virtual void AddInputMemoryCell(Nucleus nucleus) { protected virtual void AddInputMemoryCell(Nucleus nucleus) {
MemoryCell newMemory = new(this.cluster, "New memory cell"); MemoryCell newMemory = new(this.prefab, "New memory cell");
newMemory.AddReceiver(nucleus); newMemory.AddReceiver(nucleus);
this.currentNucleus = newMemory; this.currentNucleus = newMemory;
BuildLayers(); BuildLayers();
@ -685,7 +683,7 @@ public class ClusterInspector : Editor {
} }
private void OnClusterPicked(Nucleus nucleus, ClusterPrefab prefab) { private void OnClusterPicked(Nucleus nucleus, ClusterPrefab prefab) {
Cluster subclusterInstance = new(prefab, this.cluster); Cluster subclusterInstance = new(prefab, this.prefab);
subclusterInstance.AddReceiver(nucleus); subclusterInstance.AddReceiver(nucleus);
// This does not work somehow // This does not work somehow
// this.currentNucleus = subclusterInstance; // this.currentNucleus = subclusterInstance;

View File

@ -27,7 +27,7 @@ public class NanoBrainComponent_Editor : Editor {
VisualElement root = new(); VisualElement root = new();
ClusterInspector.CreateInspector(root, brain.prefab); ClusterInspector.CreateInspector(root, brain.prefab, brain.output);
if (Application.isPlaying == false) if (Application.isPlaying == false)
serializedObject.ApplyModifiedProperties(); serializedObject.ApplyModifiedProperties();

View File

@ -226,7 +226,7 @@ public class Neuron : Nucleus {
result = normalize(sum) * activatedValue; result = normalize(sum) * activatedValue;
break; break;
} }
if (this.stale > 5) if (this.stale > staleValueForSleep)
this.outputValue = new float3(0,0,0); this.outputValue = new float3(0,0,0);
else else
this.outputValue = result; this.outputValue = result;

View File

@ -32,6 +32,7 @@ public abstract class Nucleus {
public bool isSleeping => lengthsq(this.outputValue) == 0; public bool isSleeping => lengthsq(this.outputValue) == 0;
[NonSerialized] [NonSerialized]
public int stale = 1000; public int stale = 1000;
public readonly int staleValueForSleep = 20;
public abstract Nucleus ShallowCloneTo(Cluster parent); public abstract Nucleus ShallowCloneTo(Cluster parent);
public abstract Nucleus Clone(ClusterPrefab prefab); public abstract Nucleus Clone(ClusterPrefab prefab);
@ -98,7 +99,7 @@ public abstract class Nucleus {
public virtual void UpdateNuclei() { public virtual void UpdateNuclei() {
this.stale++; this.stale++;
if (this.stale > 5) { if (this.stale > staleValueForSleep) {
//Debug.Log($"{this.name} goes to sleep, stale = {this.stale}"); //Debug.Log($"{this.name} goes to sleep, stale = {this.stale}");
_outputValue = Vector3.zero; _outputValue = Vector3.zero;
} }