// Next Gen --- public void GenerateNetFromGen(List <ANNThoughtProcess> lastGen) { List <ANNThoughtProcess> parentTPs = GetTPPair(lastGen); ANNThoughtProcess childTP = GeneticAlgorithmDelegate(parentTPs[0], parentTPs[1], mutationRate); this.AdjustValuesFromTP(childTP); }
/// METHODS /// public static ANNThoughtProcess SliceCrossover(ANNThoughtProcess TPa, ANNThoughtProcess TPb, float mutationRate) { ANNThoughtProcess newTP = new ANNThoughtProcess(); newTP = TPa; int nNeurons = TPa.thoughts.Count; int numberToCut = (int)(nNeurons * PercentageToSwap); int cutPosition = Random.Range(0, nNeurons); for (int i = 0; i < numberToCut; ++i) { newTP.thoughts[cutPosition] = TPb.thoughts[cutPosition]; cutPosition++; if (cutPosition >= nNeurons) { cutPosition = 0; } } // Mutations --- MutateTP(newTP, mutationRate); return(newTP); }
public static void Serialize(ANNThoughtProcess tp) { string path = GLOBALS.TP.Path(tp.net.name) + "/" + GLOBALS.TP.GenFolderName + tp.net.generation; string file = ""; file += ANNSerialization.GLOBALS.TP.TPMarker + ANNSerialization.TP.ToStream(tp) + GLOBALS.TP.TPSeparator; // thoughts file += ANNSerialization.GLOBALS.TP.ThoughtMarker; foreach(ANNNodeThought nt in tp.thoughts) { file += THOUGHTS.ToStream(nt) + GLOBALS.TP.ThoughtSeparator; } if(!Directory.Exists(GLOBALS.TP.Path())) { Directory.CreateDirectory(GLOBALS.TP.Path()); } if(!Directory.Exists(GLOBALS.TP.Path(tp.net.name))) { Directory.CreateDirectory(GLOBALS.TP.Path(tp.net.name)); } if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } string filename = GLOBALS.TP.FileName(path); //Debug.Log(path + " // " + filename); File.WriteAllText(path + "/" + filename, file); }
// Utils --- public static void MutateTP(ANNThoughtProcess tp, float rate) { for (int ti = 0; ti < tp.thoughts.Count; ++ti) { MutateBias(tp.thoughts[ti], rate); MutateWeights(tp.thoughts[ti], rate); } }
/// Serialization /// /// Thought Processes /// public void StoreThoughts(float fitness) { ANNThoughtProcess tp = new ANNThoughtProcess(); tp.Read(this, fitness); tp.Serialize(); Parent().thisGenTPs.Add(tp); }
public static ANNThoughtProcess Load(string stream) { DATA_STRUCTS.TPDataStruct data = new DATA_STRUCTS.TPDataStruct(); data = JsonUtility.FromJson<DATA_STRUCTS.TPDataStruct>(stream); ANNThoughtProcess tp = new ANNThoughtProcess(); data.Write(ref tp); return tp; }
public static ANNThoughtProcess RandomCrossover(ANNThoughtProcess TPa, ANNThoughtProcess TPb, float mutationRate) { ANNThoughtProcess newTP = new ANNThoughtProcess(); int nNeurons = TPa.thoughts.Count; for (int i = 0; i < nNeurons; ++i) { newTP.thoughts.Add(Random.Range(0f, 1f) > 0.5f ? TPa.thoughts[i] : TPb.thoughts[i]); } // Mutations --- MutateTP(newTP, mutationRate); return(newTP); }
public void AdjustValuesFromTP(ANNThoughtProcess tp) { if (tp == null) { Debug.LogWarning(this.name + " => Trying to AdjustValuesFromTP(thoughtprocess) with a 'null' thoughtprocess"); return; } int auxMaxIt = 0; int auxMinIt = 0; for (int ti = 0; ti < tp.thoughts.Count; ++ti) { // input nodes -> not needed to adjust // hidden nodes auxMaxIt = this.hiddenNodes.Count; auxMinIt = 0; if (ti >= auxMinIt && ti < auxMaxIt) { int ni = ti - auxMinIt; // iterate on HN hiddenNodes[ni].Bias = tp.thoughts[ti].bias; for (int wi = 0; wi < tp.thoughts[ti].weights.Count; ++wi) { hiddenNodes[ni].inputConnections[wi].weight = tp.thoughts[ti].weights[wi]; } } // output nodes auxMaxIt = tp.thoughts.Count; auxMinIt = this.hiddenNodes.Count; if (ti >= auxMinIt && ti < auxMaxIt) { int ni = ti - auxMinIt; // iterate on ON outputNodes[ni].Bias = tp.thoughts[ti].bias; for (int wi = 0; wi < tp.thoughts[ti].weights.Count; ++wi) { outputNodes[ni].inputConnections[wi].weight = tp.thoughts[ti].weights[wi]; } } } }
public List <ANNThoughtProcess> AdjustThoughtsProbability(List <ANNThoughtProcess> listOfThoughts) { // Compute the new total fitness float newTotal = 0; for (int i = 0; i < listOfThoughts.Count; ++i) { for (int j = 0; j < listOfThoughts.Count; ++j) { if (listOfThoughts[i].fitness > listOfThoughts[j].fitness) { ANNThoughtProcess aux = listOfThoughts[i]; listOfThoughts[i] = listOfThoughts[j]; listOfThoughts[j] = aux; } } } int nThoughts = (int)(listOfThoughts.Count * percentageOfTopFitness); List <float> auxfit = new List <float>(); for (int i = 0; i < nThoughts; ++i) { auxfit.Add(listOfThoughts[i].fitness * listOfThoughts[i].fitness); newTotal += auxfit[i]; } // Readjust thoughts probs for (int i = 0; i < listOfThoughts.Count; ++i) { if (i < nThoughts) { listOfThoughts[i].probability = (auxfit[i] / newTotal) * 100f; // '* 100' to be between 0 - 100, not 0 - 1 } else { listOfThoughts[i].probability = 0; } } return(listOfThoughts); }
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); }
public void Write(ref ANNThoughtProcess tp) { tp.fitness = fitness; }
public void Read(ANNThoughtProcess tp) { fitness = tp.fitness; }
public static string ToStream(ANNThoughtProcess tp) { DATA_STRUCTS.TPDataStruct data = new DATA_STRUCTS.TPDataStruct(); data.Read(tp); return JsonUtility.ToJson(data); }