using System; using UnityEngine; namespace NanoBrain.Unity { /// /// A NanoBrain which can be used to control a gameobject /// /// A NanoBrain is a small neural network which can be used to implement functional behaviour. /// The network consists of neurons which are connected together with synapses. /// The output values of the neurons are of type Vector3 to support spatial computing. /// /// This component is basically a Unity representation of a nanobrain cluster. /// \sa /// - \ref NanoBrain::Cluster "Cluster" /// - \ref NanoBrain::Neuron "Neuron" [HelpURL("https://passer.life/documentation/nanobrain/Documentation/html/class_nano_brain_1_1_unity_1_1_brain.html")] public class Brain : MonoBehaviour { /// /// The Cluster prefab from which the cluster is created /// public ClusterPrefab brainPrefab; [NonSerialized] private Cluster brainInstance; /// /// The cluster isntance /// 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; // } /// /// Update the weight for all Synapses coming from the Neuron with the given name /// /// The cluster in which the synapses are updated /// The name of the Neuron for which the weights are updated /// The new Synapse weight public static void UpdateWeight(Cluster brain, string name, float weight) { Neuron 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}"); } } } } } }