Fix external synapse persistence

This commit is contained in:
Pascal Serrarens 2026-08-12 12:47:06 +02:00
parent 8cb6b979bf
commit bcc6e95a44
3 changed files with 73 additions and 27 deletions

View File

@ -180,8 +180,33 @@ namespace NanoBrain {
foreach (NeuronData neuronData in clusterData.neurons) {
Neuron receiver = this.GetNeuron(neuronData.name);
foreach (SynapseData synapseData in neuronData.synapses) {
// If the synapse points to another cluster
if (synapseData.clusterName != this.name) {
// Add reference to external cluster
// Do we know the external cluster already?
Cluster extCluster = this.GetCluster(synapseData.clusterName);
if (extCluster == null) {
// if not: create new external cluster
Debug.Log("New ext cluster");
ExternalClusterData externalClusterData = clusterData.GetCluster(synapseData.clusterName);
ClusterPrefab extPrefab = Resources.Load(externalClusterData.prefabName) as ClusterPrefab;
extCluster = new(extPrefab, this, instanceCount) {
name = externalClusterData.name
};
}
// Do we have the synapse already?
Neuron extNeuron = extCluster.GetNeuron(synapseData.neuronName);
Synapse synapse = receiver.GetSynapse(extNeuron); //extCluster, synapseData.neuronName);
if (synapse == null) {
Debug.Log("new receiver for external");
// If not: create new synapse
extNeuron.AddReceiver(receiver);
}
//Cluster foundCluster = this.nuclei.Find(nucleus => nucleus is Cluster cluster && cluster.prefab.name == synapseData.clusterName) as Cluster;
/*
ClusterPrefab extPrefab = Resources.Load(synapseData.clusterName) as ClusterPrefab;
if (extPrefab == null)
Debug.LogError($"Could not find cluster Resource {synapseData.clusterName}");
@ -189,22 +214,28 @@ namespace NanoBrain {
uint instanceCount = 1;
ExternalClusterData externalClusterData = null;
foreach (ExternalClusterData externalCluster in clusterData.clusters) {
if (externalCluster.clusterName == synapseData.clusterName) {
if (externalCluster.prefabName == synapseData.clusterName) {
instanceCount = externalCluster.instanceCount;
externalClusterData = externalCluster;
}
}
if (this.nuclei.Find(nucleus => nucleus.name == synapseData.clusterName) is not Cluster extCluster) {
Nucleus foundNucleus = this.nuclei.Find(nucleus => nucleus is Cluster cluster && cluster.prefab.name == synapseData.clusterName);
if (foundNucleus is not Cluster extCluster) {
Debug.Log("New ext cluster");
extCluster = new(extPrefab, this, instanceCount);
if (externalClusterData != null)
extCluster.name = externalClusterData.name;
//extCluster.name = clusterData.name;
}
Neuron extNeuron = extCluster.GetNeuron(synapseData.neuronName);
Synapse synapse = receiver.GetSynapse(extCluster, synapseData.neuronName);
if (synapse == null) {
Debug.Log("Add receiver for external");
extNeuron.AddReceiver(receiver);
}
}
*/
}
else {
Neuron sender = this.GetNeuron(synapseData.neuronName);
sender?.AddReceiver(receiver, synapseData.weight, synapseData.trainable);
@ -951,17 +982,25 @@ namespace NanoBrain {
}
}
}
public ExternalClusterData GetCluster(string clusterName) {
foreach (ExternalClusterData cluster in this.clusters) {
if (cluster.name == clusterName)
return cluster;
}
return null;
}
}
[Serializable]
public class ExternalClusterData {
public string name;
public string clusterName;
public string prefabName;
public uint instanceCount;
public ExternalClusterData(Cluster cluster) {
this.name = cluster.name;
this.clusterName = cluster.prefab.name;
this.prefabName = cluster.prefab.name;
this.instanceCount = (uint)cluster.instanceCount;
}
}

View File

@ -95,6 +95,12 @@ namespace NanoBrain {
return this.GetSynapse(sender);
}
public Synapse GetSynapse(Cluster cluster, string senderNeuronName) {
Neuron sender = cluster.GetNeuron(senderNeuronName);
if (sender == null)
return null;
return this.GetSynapse(sender);
}
/// <summary>
/// Remove a synapse from a Nucleus

View File

@ -144,9 +144,10 @@ namespace NanoBrain {
public bool trainable;
public SynapseData(Synapse synapse) {
if (synapse.neuron.parent.prefab != null)
this.clusterName = synapse.neuron.parent.prefab.name;
else
// if (synapse.neuron.parent.prefab != null)
// this.clusterName = synapse.neuron.parent.prefab.name;
// else
// this.clusterName = synapse.neuron.parent.name;
this.clusterName = synapse.neuron.parent.name;
this.neuronName = synapse.neuron.name;
this.weight = synapse.weight;