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() {
this.stale++;
if (this.stale > 5)
if (this.stale > staleValueForSleep)
_outputValue = Vector3.zero;
foreach (Nucleus nucleus in this.nuclei)

View File

@ -17,20 +17,20 @@ public class ClusterInspector : Editor {
#region Start
public override VisualElement CreateInspectorGUI() {
ClusterPrefab cluster = target as ClusterPrefab;
if (cluster != null)
cluster.EnsureInitialization();
ClusterPrefab prefab = target as ClusterPrefab;
if (prefab != null)
prefab.EnsureInitialization();
serializedObject.Update();
VisualElement root = new();
CreateInspector(root, cluster);
CreateInspector(root, prefab, prefab.output);
serializedObject.ApplyModifiedProperties();
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.paddingRight = 0;
root.style.paddingTop = 0;
@ -61,14 +61,14 @@ public class ClusterInspector : Editor {
mainContainer.Add(inspectorContainer);
root.Add(mainContainer);
graph.SetGraph(null, cluster.output, inspectorContainer);
graph.SetGraph(null, output, inspectorContainer);
return graph;
}
public class GraphView : VisualElement {
readonly ClusterPrefab cluster;
readonly ClusterPrefab prefab;
SerializedObject serializedBrain;
Nucleus currentNucleus;
GameObject gameObject;
@ -80,7 +80,7 @@ public class ClusterInspector : Editor {
PopupField<string> outputsField;
public GraphView(ClusterPrefab prefab) {
this.cluster = prefab;
this.prefab = prefab;
name = "content";
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()) {
style = { flexGrow = 1 }
};
@ -120,13 +120,18 @@ public class ClusterInspector : Editor {
}
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() {
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;
this.currentNucleus = newOutput;
@ -150,7 +155,7 @@ public class ClusterInspector : Editor {
this.gameObject = gameObject;
//this.cluster = brain;
if (Application.isPlaying == false)
this.serializedBrain = new SerializedObject(this.cluster);
this.serializedBrain = new SerializedObject(this.prefab);
this.currentNucleus = nucleus;
Rebuild(inspectorContainer);
}
@ -165,7 +170,7 @@ public class ClusterInspector : Editor {
if (currentWrapper != null)
DestroyImmediate(currentWrapper);
currentWrapper = CreateInstance<ClusterWrapper>().Init(this.currentNucleus, cluster);
currentWrapper = CreateInstance<ClusterWrapper>().Init(this.currentNucleus, prefab);
DrawInspector(inspectorContainer);
}
@ -277,13 +282,6 @@ public class ClusterInspector : Editor {
DrawNucleus(nucleus, pos, maxValue, size);
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 {
Handles.color = Color.white;
@ -547,7 +545,7 @@ public class ClusterInspector : Editor {
EditorGUILayout.BeginHorizontal();
EditorGUILayout.IntField("Array size", neuroid.array.nuclei.Count());
if (GUILayout.Button("Add"))
neuroid.array.AddNucleus(this.cluster);
neuroid.array.AddNucleus(this.prefab);
if (GUILayout.Button("Del"))
neuroid.array.RemoveNucleus();
EditorGUILayout.EndHorizontal();
@ -560,8 +558,8 @@ public class ClusterInspector : Editor {
showSynapses = EditorGUILayout.BeginFoldoutHeaderGroup(showSynapses, "Synapses");
if (showSynapses) {
ConnectNucleus(this.cluster, this.currentNucleus);
AddSynapse(this.cluster, this.currentNucleus);
ConnectNucleus(this.prefab, this.currentNucleus);
AddSynapse(this.prefab, this.currentNucleus);
if (this.currentNucleus.synapses.Count > 0) {
Synapse[] synapses = this.currentNucleus.synapses.ToArray();
@ -645,7 +643,7 @@ public class ClusterInspector : Editor {
protected virtual void AddInputNeuron(Nucleus nucleus) {
//Neuron newNeuroid = new(this.cluster, "New neuron");
Neuron newNeuroid = new(this.cluster, "New neuron");
Neuron newNeuroid = new(this.prefab, "New neuron");
newNeuroid.AddReceiver(nucleus);
this.currentNucleus = newNeuroid;
BuildLayers();
@ -667,14 +665,14 @@ public class ClusterInspector : Editor {
}
protected void AddSelectorInput(Nucleus nucleus) {
Selector newSelector = new(this.cluster, "New Selector");
Selector newSelector = new(this.prefab, "New Selector");
newSelector.AddReceiver(nucleus);
this.currentNucleus = newSelector;
BuildLayers();
}
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);
this.currentNucleus = newMemory;
BuildLayers();
@ -685,7 +683,7 @@ public class ClusterInspector : Editor {
}
private void OnClusterPicked(Nucleus nucleus, ClusterPrefab prefab) {
Cluster subclusterInstance = new(prefab, this.cluster);
Cluster subclusterInstance = new(prefab, this.prefab);
subclusterInstance.AddReceiver(nucleus);
// This does not work somehow
// this.currentNucleus = subclusterInstance;

View File

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

View File

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

View File

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