public static void SerializeNodes(ANNNetwork net) { string path = GLOBALS.NETS.Path; string file = ""; // nodes foreach (ANNNode n in net.InputNodes) { file += NODES.ToStream(n) + GLOBALS.NETS.NodeSeparator; } foreach (ANNNode n in net.OutputNodes) { file += NODES.ToStream(n) + GLOBALS.NETS.NodeSeparator; } if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } path += "/" + net.name; if(!Directory.Exists(path)) { Directory.CreateDirectory(path); } File.WriteAllText(path + "/" + net.name, file); }
// Serialization --- public static string ToStream(ANNNetwork net) { if (net == null) return ""; DATA_STRUCTS.NetworkDataStruct data = new DATA_STRUCTS.NetworkDataStruct(); data.Read(net); return JsonUtility.ToJson(data); }
public override void OnInspectorGUI() { if (target.GetType() != typeof(ANNAgent)) { GUILayout.Label(" ------ Script ------", EditorStyles.boldLabel); DrawDefaultInspector(); } GUILayout.Label(" ------ Agent ------", EditorStyles.boldLabel); ANNAgent comp = (ANNAgent)target; ANNNetwork auxNet = null; auxNet = (ANNNetwork)EditorGUILayout.ObjectField("Network:", comp.network, typeof(ANNNetwork), true); if (auxNet != comp.network) { comp.network = auxNet; if (auxNet != null) { comp.network.agent = comp; } } GUILayout.Label(" ------ Network ------", EditorStyles.boldLabel); showNetInspector = EditorGUILayout.Toggle("Show Network Inpector:", showNetInspector); if (showNetInspector) { if (comp.network != null) { ANNNetworkInspector.DrawInspector(comp.network); } } }
public virtual void Copy(ANNNode target) { this.name = target.name + GetNextNumAppendixFromName(target.name); network = target.network; Bias = target.Bias; ActivationMethodType = target.ActivationMethodType; ActivationDelegate = target.ActivationDelegate; }
/// !_USER METHODS /// ------------------------------------------------------------------------------------------------ public ANNNetwork CreateChild(ANNAgent agent) { ANNNetwork child = this.Instanciate(); child.LoadNetwork(false, true); child.agent = agent; children.Add(child); return(child); }
public ANNNetwork Instanciate() { ANNNetwork auxNet = ScriptableObject.CreateInstance <ANNNetwork>(); auxNet.Copy(this); instances.Add(auxNet); auxNet.instanceParent = this.instanceParent == null ? this : this.instanceParent; auxNet.IsInstance = true; return(auxNet); }
public void Write(ref ANNNetwork net) { if(net == null) { Debug.LogWarning("Trying to Write a 'null' net."); return; } net.name = name; net.SetGeneration(generation); }
// references public void Read(ANNNetwork net) { if (net == null) { Debug.LogWarning("Trying to Read a 'null' net."); return; } name = net.name; generation = net.generation; }
public void Copy(ANNNetwork net) { this.name = net.name + GetNextNumAppendixFromName(net.name); NHiddenLayers = net.NHiddenLayers; Generation = net.Generation; AgentsPerGeneration = net.AgentsPerGeneration; GeneticAlgorithmDelegate = net.GeneticAlgorithmDelegate; tickType = net.tickType; ManualNHiddenNodes = net.ManualNHiddenNodes; NHiddenNodesPerLayer = net.NHiddenNodesPerLayer; }
/// OnGUI /// public override void OnInspectorGUI() { if (curve.length <= 0) { curve.AddKey(-1, 0); } curve.keys[0].value = 0; ANNNetwork net = (ANNNetwork)target; DrawInspector(net); }
/// Methods /// // Read --- public void Read(ANNNetwork net, float fitness) { // Net --- this.net = net.Parent(); this.fitness = fitness; generation = net.generation; // Nodes --- // input nodes --> not needed for the Thought Processes // hidden nodes for (int i = 0; i < net.HiddenNodes.Count; ++i) { thoughts.Add(new ANNNodeThought(net.HiddenNodes[i])); } // output nodes for (int i = 0; i < net.OutputNodes.Count; ++i) { thoughts.Add(new ANNNodeThought(net.OutputNodes[i])); } }
public static void SerializeValues(ANNNetwork net) { string path = GLOBALS.NETS.Path; string file = ""; // Network file += NETS.ToStream(net); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } path += "/" + net.name; if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } File.WriteAllText(path + "/" + GLOBALS.NETS.NetValuesFileName, file); }
// Use this for initialization void Start() { if (Application.isPlaying) { base_agent = AgentPrefab.GetComponent <ANNAgent>(); if (base_agent == null) { Debug.LogError("ANNAcademy => Trying to use an AgentPrefab without an ANNAgent Component."); return; } base_net = base_agent.network; base_net.topBest = 0; switch (base_net.TrainingType) { case ANNNetwork.ANNNetworkTrainingType.MultiAgent: // MULTI ReplicateAgent(AgentPrefab); break; case ANNNetwork.ANNNetworkTrainingType.SingleAgent: // SINGLE AgentPrefab.SetActive(true); StartAgent(base_agent); break; } } else if (Application.isEditor) { if (AgentPrefab != null) { base_agent = AgentPrefab.GetComponent <ANNAgent>(); if (base_agent != null) { base_net = base_agent.network; base_net.LoadNetwork(true, true); } } } }
public static void DrawInspector(ANNNetwork net) { if (curve.length <= 0) { curve.AddKey(-1, 0); } curve.keys[0].value = 0; // Cycles per Generation net.AgentsPerGeneration = EditorGUILayout.IntField("Cycles per Generation:", net.AgentsPerGeneration); // Net State net.TrainingType = (ANNNetwork.ANNNetworkTrainingType)EditorGUILayout.EnumPopup("Network Training Type:", net.TrainingType); // Hidden Layers EditorGUILayout.Space(); net.NHiddenLayers = EditorGUILayout.IntField("Number of Hidden Layers", net.NHiddenLayers); // Hidden Nodes net.ManualNHiddenNodes = EditorGUILayout.Toggle("Manual Number of Hidden Nodes:", net.ManualNHiddenNodes); if (net.ManualNHiddenNodes) { net.NHiddenNodesPerLayer = EditorGUILayout.IntField("Number of Hidden Nodes:", net.NHiddenNodesPerLayer); } // Add Nodes GUILayout.Label("-- Add Nodes --", EditorStyles.boldLabel); InNodeAux = (ANNInputNode)EditorGUILayout.ObjectField("Add Input Node:", InNodeAux, typeof(ANNInputNode), true); //nodetype = (NodeType)EditorGUILayout.EnumPopup("New Node Type: ", nodetype); //nodename = EditorGUILayout.TextField("New Node Name: ", nodename); GUILayout.BeginHorizontal(); inputnumber = EditorGUILayout.IntField("Amount to Add: ", inputnumber); if (inputnumber < 0) { inputnumber = 0; } if (GUILayout.Button("Add Input Node") && InNodeAux != null) { if (inputnumber <= 0) { } else if (inputnumber == 1) { net.AddNode(InNodeAux, true, false, false, false); } else { for (int i = 0; i < inputnumber; ++i) { ANNNode n = net.AddNode(InNodeAux, true, false, false, false); n.name = InNodeAux.Parent().name + "_" + i; } net.ResetNet(); net.ResetGeneration(); } InNodeAux = null; net.Serialize(); } GUILayout.EndHorizontal(); if (inputnumber > 99) { //EditorStyles.colorField = Color.yellow; GUILayout.Label("The number of nodes you want to add is too high, could lead to performance issues.", EditorStyles.miniLabel); } // Add Output Nodes OutNodeAux = (ANNOutputNode)EditorGUILayout.ObjectField("Add Output Node:", OutNodeAux, typeof(ANNOutputNode), true); GUILayout.BeginHorizontal(); outputnumber = EditorGUILayout.IntField("Amount to Add: ", outputnumber); if (outputnumber < 0) { outputnumber = 0; } if (GUILayout.Button("Add Output Node") && OutNodeAux != null) { if (outputnumber <= 0) { } else if (outputnumber == 1) { net.AddNode(OutNodeAux, true, false, false, false); } else { for (int i = 0; i < outputnumber; ++i) { ANNNode n = net.AddNode(OutNodeAux, true, false, false, false); n.name = OutNodeAux.Parent().name + "_" + i; } net.ResetNet(); net.ResetGeneration(); } OutNodeAux = null; net.Serialize(); } GUILayout.EndHorizontal(); if (outputnumber > 99) { //EditorStyles.colorField = Color.yellow; GUILayout.Label("The number of nodes you want to add is too high, could lead to performance issues.", EditorStyles.miniLabel); } GUILayout.BeginHorizontal(); EditorGUILayout.IntField("Number of Input Nodes", net.InputNodes.Count, EditorStyles.boldLabel); hideInNodeNames = EditorGUILayout.Toggle("Hide Names:", hideInNodeNames); GUILayout.EndHorizontal(); if (!hideInNodeNames) { for (int i = 0; i < net.InputNodes.Count; ++i) { GUILayout.BeginHorizontal(); if (net.InputNodes[i] != null) { GUILayout.Label(" - " + net.InputNodes[i].name); } else { GUILayout.Label(" - ¡¡ error reading name !!"); } GUILayout.Space(50f); if (GUILayout.Button("Delete", GUILayout.Height(20f), GUILayout.Width(50f))) { if (net.InputNodes[i] == null) { net.InputNodes.RemoveAt(i--); continue; } net.RemoveNode(net.InputNodes[i]); } GUILayout.FlexibleSpace(); GUILayout.EndHorizontal(); } } GUILayout.BeginHorizontal(); EditorGUILayout.IntField("Number of Output Nodes", net.OutputNodes.Count, EditorStyles.boldLabel); hideOutNodeNames = EditorGUILayout.Toggle("Hide Names:", hideOutNodeNames); GUILayout.EndHorizontal(); if (!hideOutNodeNames) { for (int i = 0; i < net.OutputNodes.Count; ++i) { GUILayout.BeginHorizontal(); if (net.OutputNodes[i] != null) { GUILayout.Label(" - " + net.OutputNodes[i].name); } else { GUILayout.Label(" - ¡¡ error reading name !!"); } GUILayout.Space(50f); if (GUILayout.Button("Delete", GUILayout.Height(20f), GUILayout.Width(50f))) { if (net.OutputNodes[i] == null) { net.OutputNodes.RemoveAt(i--); continue; } net.RemoveNode(net.OutputNodes[i]); } GUILayout.FlexibleSpace(); GUILayout.EndHorizontal(); } } net.tickType = (ANNNetwork.TickType)EditorGUILayout.EnumPopup("Tick Type", net.tickType); EditorGUILayout.Separator(); if (GUILayout.Button("Network Tab", GUILayout.Height(25f))) { ANNNetworkTab window = ANNNetworkTab.GetWindow <ANNNetworkTab>("ANNetwork Tab"); window.network = net; window.UpdateGraphics(); } if (GUILayout.Button("Save Network", GUILayout.Height(25f))) { net.Serialize(); } EditorGUILayout.IntField("Generation: ", net.Parent().generation, EditorStyles.boldLabel); GUILayout.BeginHorizontal(); generationToChange = EditorGUILayout.IntField("Change Generation: ", generationToChange); if (GUILayout.Button("Change Generation")) { net.SetGeneration(generationToChange); } GUILayout.EndHorizontal(); EditorGUILayout.FloatField("Best Fitness: ", net.topBest, EditorStyles.label); EditorGUILayout.FloatField("Last Fitness: ", curveKeys.Count > 0 ? curveKeys[curveKeys.Count - 1] : 0, EditorStyles.label); for (int i = 0; i < curve.length; ++i) { curve.RemoveKey(i); } for (int i = 0; i < curveKeys.Count; ++i) { curve.AddKey(i, curveKeys[i]); } EditorGUILayout.CurveField(curve, GUILayout.Height(350f)); }
public ANNNode(ANNNetwork net) { network = net; }