60 lines
2.1 KiB
C#

using System;
using UnityEngine;
namespace NanoBrain {
/// <summary>
/// The NanoBrain Unity Componnent
/// </summary>
/// This implements the top-level NanoBrain Cluster
public class Brain : MonoBehaviour {
/// <summary>
/// The Cluster prefab from which the cluster is created
/// </summary>
public ClusterPrefab brainPrefab;
[NonSerialized]
private Cluster brainInstance;
/// <summary>
/// The cluster isntance
/// </summary>
public Cluster brain {
get {
if (brainInstance == null && brainPrefab != null) {
brainInstance = new Cluster(brainPrefab) {
name = brainPrefab.name
};
} else if (brainInstance != null && brainPrefab == null) {
brainInstance = null;
}
return brainInstance;
}
}
// public Cluster InitializeBrain() {
// brainInstance = new Cluster(brainPrefab) {
// name = brainPrefab.name
// };
// return brainInstance;
// }
/// <summary>
/// Update the weight for all Synapses coming from the Neuron with the given name
/// </summary>
/// <param name="brain">The cluster in which the synapses are updated</param>
/// <param name="name">The name of the Neuron for which the weights are updated</param>
/// <param name="weight">The new Synapse weight</param>
public static void UpdateWeight(Cluster brain, string name, float weight) {
Nucleus root = brain.defaultOutput;
foreach (Synapse synapse in root.synapses) {
if (synapse.neuron.name == name) {
if (synapse.weight != weight) {
synapse.weight = weight;
// Debug.Log($"Updated weight for {name}");
}
}
}
}
}
}