// Add --- public ANNNode AddNode(ANNNode n, bool instanciate = true, bool savenet = true, bool reset = true, bool resetGen = false) { if (n == null) { Debug.LogWarning(this.name + " => Trying to AddNode(node, bool) a 'null' node"); return(null); } ANNNode ret = null; if (n.GetType() == typeof(ANNInputNode)) { ret = AddInputNode((ANNInputNode)n, instanciate, reset, resetGen); } else if (n.GetType() == typeof(ANNOutputNode)) { ret = AddOutputNode((ANNOutputNode)n, instanciate, reset, resetGen); } else if (n.GetType() == typeof(ANNHiddenNode)) { ret = AddHiddenNode((ANNHiddenNode)n, instanciate); } if (savenet) { Parent().Serialize(); } return(ret); }
/// Creation /// // Nodes --- ANNGraphicNode CreateGNode(ANNNode n) { if (n == null) { return(null); } GUIStyle style = new GUIStyle(); if (n.GetLayerOrder() == (int)ANNLayerOrders.Input) { style = inStyle; } else if (n.GetLayerOrder() == (int)ANNLayerOrders.Output) { style = outStyle; } else { style = hdnStyle; } ANNGraphicNode gnode = new ANNGraphicNode(n, new Vector2(0, 0), nodeScale.x, nodeScale.y, style); gNodes.Add(gnode); return(gnode); }
public void LoadNodes(int first_index) { inputNodes.Clear(); outputNodes.Clear(); hiddenNodes.Clear(); string path = ANNSerialization.GLOBALS.NETS.Path + "/" + Parent().name + "/" + Parent().name; if (!File.Exists(path)) { Debug.LogWarning(this.name + " => Trying to LoadNodes() but there isn't any file serialized from this network."); return; } string file = File.ReadAllText(path); string str = ""; for (int i = first_index; i < file.Length; ++i) { if (file[i] == ANNSerialization.GLOBALS.NETS.NodeSeparator[0]) { ANNNode node = ANNSerialization.NODES.Load(str); AddNode(node, true, false, true, false); str = ""; // reset str } else { str += file[i]; } } UpdateNetwork(); }
// Serialization --- public static string ToStream(ANNNode node) { if (node == null) return ""; DATA_STRUCTS.NodeDataStruct data = new DATA_STRUCTS.NodeDataStruct(); data.Read(node); return JsonUtility.ToJson(data); }
// Load --- public static ANNNode Load(string stream) { DATA_STRUCTS.NodeDataStruct data = new DATA_STRUCTS.NodeDataStruct(); data = JsonUtility.FromJson<DATA_STRUCTS.NodeDataStruct>(stream); ANNNode node = null; switch((DATA_STRUCTS.NodeDataStruct.NodeType)data.type) { case DATA_STRUCTS.NodeDataStruct.NodeType.Input: node = LoadInputNode(data); break; case DATA_STRUCTS.NodeDataStruct.NodeType.Output: node = LoadOutputNode(data); break; case DATA_STRUCTS.NodeDataStruct.NodeType.Hidden: break; default: Debug.LogWarning("Trying to Load(stream) a node with unknown type."); return null; } if (node == null) return null; data.Write(ref node); return node; }
void ApplyActivationToLayer() { ANNNode node = (ANNNode)target; if (node == null || node.network == null) { return; } if (node.GetType() == typeof(ANNInputNode)) { List <ANNInputNode> list = node.network.InputNodes; for (int i = 0; i < list.Count; ++i) { list[i].ActivationMethodType = node.ActivationMethodType; } } else if (node.GetType() == typeof(ANNOutputNode)) { List <ANNOutputNode> list = node.network.OutputNodes; for (int i = 0; i < list.Count; ++i) { list[i].ActivationMethodType = node.ActivationMethodType; } } else if (node.GetType() == typeof(ANNHiddenNode)) { List <ANNHiddenNode> list = node.network.GetHiddenNodesFromLayerOrder(node.GetLayerOrder()); for (int i = 0; i < list.Count; ++i) { list[i].ActivationMethodType = node.ActivationMethodType; } } }
public override void OnInspectorGUI() { ANNNode node = (ANNNode)target; EditorGUILayout.ObjectField("Network:", node.network, typeof(ANNNetwork), false); node.Bias = EditorGUILayout.FloatField("Bias Value:", node.Bias); ANNActivationMethodsList auxActivationMethod = node.ActivationMethodType; node.ActivationMethodType = (ANNActivationMethodsList)EditorGUILayout.EnumPopup("Activation Method:", node.ActivationMethodType); if (auxActivationMethod != node.ActivationMethodType) { auxActivationMethod = node.ActivationMethodType; node.network.Serialize(); } EditorGUILayout.BeginHorizontal(); if (GUILayout.Button("Apply to Layer")) { ApplyActivationToLayer(); node.network.Serialize(); } if (GUILayout.Button("Apply to All Nodes")) { ApplyActivationToAllNodes(); node.network.Serialize(); } EditorGUILayout.EndHorizontal(); }
public ANNConnection(float weight, ANNNode inNode = null, ANNNode outNode = null, ConnectionType type = ConnectionType.FeedForward) { this.weight = weight; this.outNode = outNode; this.inNode = inNode; this.type = type; }
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; }
/// Utils /// public override void Copy(ANNNode target) { if (target.GetType() != typeof(ANNOutputNode)) { Debug.LogWarning(this.name + " => Trying to Copy(node) a node that is not an OutputNode"); return; } base.Copy(target); }
public void Write(ref ANNNode node) { if(node == null) { Debug.LogWarning("Trying to Write(node) a 'null' node."); return; } node.name = name; node.ActivationMethodType = (ANNActivationMethodsList)activationtype; }
/// UTILS /// public virtual ANNNode Instanciate() { ANNNode n = ScriptableObject.CreateInstance <ANNNode>(); n.Copy(this); n.OnEnable(); this.instances.Add(n); n.instanceParent = this.instanceParent == null ? this : this.instanceParent; return(n); }
private ANNGraphicNode GetGNodeByNeuralNode(ANNNode n) { foreach (ANNGraphicNode gn in gNodes) { if (gn.node == n) { return(gn); } } return(null); }
// Update Graphics Utils --- private bool IsInClosedNodes(ANNNode n, List <ANNNode> list) { for (int i = 0; i < list.Count; ++i) { if (list[i] == n) { return(true); } } return(false); }
void ApplyActivationToAllNodes() { ANNNode node = (ANNNode)target; if (node == null || node.network == null) { return; } for (int i = 0; i < node.network.nodeList.Count; ++i) { node.network.nodeList[i].ActivationMethodType = node.ActivationMethodType; } }
public void Read(ANNNode n) { if (n == null) { Debug.LogWarning("ANNodeThought => Trying to Read(node) a 'null' node"); return; } bias = n.Bias; for (int i = 0; i < n.inputConnections.Count; ++i) { weights.Add(n.inputConnections[i].weight); } }
/// Constructor /// public ANNGraphicNode(ANNNode n, Vector2 pos, float w, float h, GUIStyle s) { rect = new Rect(pos.x, pos.y, w, h); style = s; node = n; if (node.GetType() == typeof(ANNInputNode)) { title = node.name + " : " + ((ANNInputNode)node).GetInputValue(); } else if (node.GetType() == typeof(ANNHiddenNode)) { title = node.name; } else if (node.GetType() == typeof(ANNOutputNode)) { title = node.name + " : " + ((ANNOutputNode)node).output; } }
// Read & Write public void Read(ANNNode node) { if(node.GetType() == typeof(ANNInputNode)) { type = (int)NodeType.Input; } else if(node.GetType() == typeof(ANNOutputNode)) { type = (int)NodeType.Output; } else if(node.GetType() == typeof(ANNHiddenNode)) { type = (int)NodeType.Hidden; } else { Debug.LogWarning("Trying to serialize a node that is not an Input, Output or Hidden Node => Node: '" + node.name + "'."); return; } name = node.name; activationtype = (int)node.ActivationMethodType; AssetDatabase.TryGetGUIDAndLocalFileIdentifier(node.Parent(), out referenceuid, out localid); }
public ANNNodeThought(ANNNode node) { this.Read(node); }
// REMOVE NODES public void RemoveNode(ANNNode node) { if (node == null) { Debug.LogWarning(this.name + " -> Trying to remove a 'null' node."); return; } bool removed = false; if (node.GetType() == typeof(ANNInputNode)) { for (int i = 0; i < inputNodes.Count; ++i) { if (inputNodes[i] == node) { inputNodes.RemoveAt(i--); removed = true; } } } else if (node.GetType() == typeof(ANNHiddenNode)) { for (int i = 0; i < hiddenNodes.Count; ++i) { if (hiddenNodes[i] == node) { hiddenNodes.RemoveAt(i--); removed = true; } } } else if (node.GetType() == typeof(ANNOutputNode)) { for (int i = 0; i < outputNodes.Count; ++i) { if (outputNodes[i] == node) { outputNodes.RemoveAt(i--); removed = true; } } } if (removed == false) { Debug.LogWarning(this.name + " -> Tried to remove a node '" + node.name + "' that is not in this net."); } else { string path = "Assets/ANNProject/Serialization/Networks/" + Parent().name; if (Directory.Exists(path)) { AssetDatabase.DeleteAsset("Assets/ANNProject/Serialization/Networks/" + Parent().name + "/" + node.name + ".asset"); } if (Application.isEditor) { DestroyImmediate(node); } else { Destroy(node); } } SetupConnections(); this.Serialize(); }
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)); }