public static void MutateWeights(ANNNodeThought thought, float rate) { for (int i = 0; i < thought.weights.Count; ++i) { thought.weights[i] = MutateFloat(thought.weights[i], rate); } }
// Load --- public static ANNNodeThought Load(string stream) { DATA_STRUCTS.ThoughtDataStruct data = new DATA_STRUCTS.ThoughtDataStruct(); data = JsonUtility.FromJson<DATA_STRUCTS.ThoughtDataStruct>(stream); ANNNodeThought nt = new ANNNodeThought(); data.Write(ref nt); return nt; }
// Read & Write public void Read(ANNNodeThought nt) { bias = nt.bias; if(weights == null) { weights = new List<float>(); } for (int i = 0; i < nt.weights.Count; ++i) { weights.Add(nt.weights[i]); } }
public void Write(ref ANNNodeThought nt) { if (nt == null) { Debug.LogWarning("Trying to Write(nodethought) a 'null' thought."); return; } nt.bias = bias; for(int i = 0; i < this.weights.Count; ++i) { nt.weights.Add(this.weights[i]); } }
public static void MutateBias(ANNNodeThought thought, float rate) { thought.bias = MutateFloat(thought.bias, rate); }
public void LoadGenerationIntoNet(int gen) { if (gen == 0) { Parent().Generation = 0; Parent().UpdateNetwork(); } string path = ANNSerialization.GLOBALS.TP.Path(Parent().name) + "/" + ANNSerialization.GLOBALS.TP.GenFolderName + gen; if (!Directory.Exists(path)) { Debug.LogWarning(this.name + " => Trying to LoadGeneration(gen) but there isn't any file serialized from this network."); Debug.Log(path); return; } thisGenTPs.Clear(); lastGenTPs.Clear(); for (int index = 0; File.Exists(index.ToString()); ++index) { ANNThoughtProcess tp = null; string file = File.ReadAllText(path + "/" + index.ToString()); string str = ""; char marker = ' '; for (int i = 0; i < file.Length; ++i) { if (file[i] == ANNSerialization.GLOBALS.TP.TPMarker[0]) { marker = file[i]; } else if (file[i] == ANNSerialization.GLOBALS.TP.ThoughtMarker[0]) { marker = file[i]; } else { if (marker == ANNSerialization.GLOBALS.TP.TPMarker[0]) { if (file[i] == ANNSerialization.GLOBALS.TP.TPSeparator[0]) { tp = ANNSerialization.TP.Load(str); } else { str += file[i]; } } else if (marker == ANNSerialization.GLOBALS.TP.ThoughtMarker[0]) { if (file[i] == ANNSerialization.GLOBALS.TP.ThoughtSeparator[0]) { ANNNodeThought nt = ANNSerialization.THOUGHTS.Load(str); tp.thoughts.Add(nt); str = ""; } else { str += file[i]; } } } } if (tp != null) { thisGenTPs.Add(tp); } } StepToNextGeneration(); UpdateNetwork(); }
public List <ANNThoughtProcess> LoadGeneration(int gen) { List <ANNThoughtProcess> ret = new List <ANNThoughtProcess>(); if (gen <= 0) { Debug.LogWarning(this.name + " => Trying to LoadGeneration(int gen) with an invalid gen number (Note: gen <= 0 is invalid). Gen -> " + gen); return(ret); } string path = ANNSerialization.GLOBALS.TP.Path(Parent().name) + "/" + ANNSerialization.GLOBALS.TP.GenFolderName + gen; if (!Directory.Exists(path)) { Debug.LogWarning(this.name + " => Trying to LoadGeneration(int gen) but there isn't any file serialized from this network."); return(ret); } for (int index = 0; File.Exists(path + "/" + index.ToString()); ++index) { ANNThoughtProcess tp = new ANNThoughtProcess(); string file = File.ReadAllText(path + "/" + index.ToString()); string str = ""; char marker = ' '; for (int i = 0; i < file.Length; ++i) { if (marker == ' ' && file[i] == ANNSerialization.GLOBALS.TP.TPMarker[0]) { marker = ANNSerialization.GLOBALS.TP.TPMarker[0]; } else if (marker == ANNSerialization.GLOBALS.TP.TPMarker[0] && file[i] == ANNSerialization.GLOBALS.TP.ThoughtMarker[0]) { marker = ANNSerialization.GLOBALS.TP.ThoughtMarker[0]; } else { if (marker == ANNSerialization.GLOBALS.TP.TPMarker[0]) { if (file[i] == ANNSerialization.GLOBALS.TP.TPSeparator[0]) { ANNSerialization.DATA_STRUCTS.TPDataStruct tp_data = ANNSerialization.TP.LoadData(str); tp_data.Write(ref tp); str = ""; } else { str += file[i]; } } else if (marker == ANNSerialization.GLOBALS.TP.ThoughtMarker[0]) { if (file[i] == ANNSerialization.GLOBALS.TP.ThoughtSeparator[0]) { if (tp != null) { ANNNodeThought nt = ANNSerialization.THOUGHTS.Load(str); tp.thoughts.Add(nt); str = ""; } } else { str += file[i]; } } } } if (tp != null) { ret.Add(tp); } } return(ret); }
// Serialization --- public static string ToStream(ANNNodeThought thought) { DATA_STRUCTS.ThoughtDataStruct data = new DATA_STRUCTS.ThoughtDataStruct(); data.Read(thought); return JsonUtility.ToJson(data); }