Boid training first steps

This commit is contained in:
Pascal Serrarens 2026-07-10 14:42:46 +02:00
parent 90f8065a39
commit a229ee21f0
2 changed files with 21 additions and 22 deletions

View File

@ -25,7 +25,7 @@ namespace NanoBrain.Unity {
return height; return height;
SerializedProperty prefabProp = property.FindPropertyRelative(nameof(Cluster.prefab)); SerializedProperty prefabProp = property.FindPropertyRelative(nameof(Cluster.prefab));
if (//prefabProp.objectReferenceValue != null && if (prefabProp.objectReferenceValue != null &&
Cluster_Drawer.currentClusterView.isOpen) { Cluster_Drawer.currentClusterView.isOpen) {
height = graphHeight; height = graphHeight;
} }

View File

@ -99,7 +99,6 @@ namespace NanoBrain {
ClonePrefab(); ClonePrefab();
if (instanceCount > 1) { if (instanceCount > 1) {
Debug.Log($" InstanceCount = {instanceCount}");
List<Cluster> siblings = new() { this }; List<Cluster> siblings = new() { this };
for (int instanceIx = 1; instanceIx < instanceCount; instanceIx++) { for (int instanceIx = 1; instanceIx < instanceCount; instanceIx++) {
// Create another sibling // Create another sibling
@ -474,27 +473,27 @@ namespace NanoBrain {
return allInstances; return allInstances;
} }
public void Backpropagation(Func<Cluster, float> Observer, float target, float learningRate) { // public void Backpropagation(Func<Cluster, float> Observer, float target, float learningRate) {
foreach (Nucleus nucleus in this.instances[0].nuclei) { // foreach (Nucleus nucleus in this.instances[0].nuclei) {
if (nucleus is not Neuron neuron) // if (nucleus is not Neuron neuron)
continue; // continue;
foreach (Synapse synapse in neuron.synapses) { // foreach (Synapse synapse in neuron.synapses) {
List<Neuron> allSynapseNeurons = GetAllInstances(synapse.neuron); // List<Neuron> allSynapseNeurons = GetAllInstances(synapse.neuron);
Vector3 dSSRdW = Vector3.zero; // Vector3 dSSRdW = Vector3.zero;
for (int clusterIx = 0; clusterIx < this.instances.Length; clusterIx++) { // for (int clusterIx = 0; clusterIx < this.instances.Length; clusterIx++) {
Cluster clusterInstance = this.instances[clusterIx]; // Cluster clusterInstance = this.instances[clusterIx];
Neuron neuronInstance = allSynapseNeurons[clusterIx]; // Neuron neuronInstance = allSynapseNeurons[clusterIx];
// Simple case, without receivers... // // Simple case, without receivers...
dSSRdW += (Vector3)(-2 * (Observer(clusterInstance) - target) * neuronInstance.activation); // dSSRdW += (Vector3)(-2 * (Observer(clusterInstance) - target) * neuronInstance.activation);
} // }
synapse.weight += learningRate * dSSRdW.magnitude; // synapse.weight += learningRate * dSSRdW.magnitude;
} // }
} // }
} // }
#endregion ClusterArray #endregion ClusterArray
@ -867,13 +866,13 @@ namespace NanoBrain {
ClusterData data = new(this); //this.ToJSON(); ClusterData data = new(this); //this.ToJSON();
string json = JsonUtility.ToJson(data, prettyPrint: true); string json = JsonUtility.ToJson(data, prettyPrint: true);
Debug.Log($"Exporting json to {path}"); // Debug.Log($"Exporting json to {path}");
System.IO.File.WriteAllText(path, json); File.WriteAllText(path, json);
} }
public static ClusterData Import(string path) { public static ClusterData Import(string path) {
string json = File.ReadAllText(path); string json = File.ReadAllText(path);
Debug.Log($"Importing json from {path}"); // Debug.Log($"Importing json from {path}");
return JsonUtility.FromJson<ClusterData>(json); return JsonUtility.FromJson<ClusterData>(json);
} }