void Iterate() { Random rdm = new Random(); //List<float> crossPerformance = new List<float>(); //List<float> mutationPerformance = new List<float>(); ; //List<float> randomPerformance = new List<float>(); ; //Create mutated children double crossRatio = 0.5; for (int i = permanence; i < permanence + children; i++) { if (rdm.NextDouble() < crossRatio) { //Cross int parent1 = rdm.Next(permanence); int parent2 = rdm.Next(permanence); TablePredictor.crossOverride(population[i], population[parent1], population[parent2], rdm); population[i].birthTime = cycle; population[i].testPredict(test); //crossPerformance.Add(population[i].accuracy); } else { //Mutation int parent = rdm.Next(permanence); TablePredictor.mutateOverride(population[i], population[parent], rdm, 0.3); population[i].birthTime = cycle; population[i].testPredict(test); //mutationPerformance.Add(population[i].accuracy); } } //Add randoms for (int i = permanence + children; i < population.Count; i++) { TablePredictor.randomOverride(population[i], rdm); population[i].testPredict(test); //randomPerformance.Add(population[i].accuracy); } //sort to find weaker individuals sortPopulation(); //Convert weaker same species individuals to randoms for (int i = 0; i < population.Count; i++) { for (int j = population.Count - 1; j > i; j--) { if (population[j].species == population[i].species) { TablePredictor.randomOverride(population[j], rdm); population[j].testPredict(test); //randomPerformance.Add(population[j].accuracy); } } } //sort again sortPopulation(); //mutation measures //float waterlineAccuracy = population[permanence-1].accuracy; //int pCount = 0; //for (int i = 0; i < mutationPerformance.Count; i++) // if (mutationPerformance[i] > waterlineAccuracy) // pCount++; //if(mutationPerformance.Count > 0) // Console.WriteLine("Mutation Performance: " + pCount / (float)mutationPerformance.Count); //pCount = 0; //for (int i = 0; i < crossPerformance.Count; i++) // if (crossPerformance[i] > waterlineAccuracy) // pCount++; //if (crossPerformance.Count > 0) // Console.WriteLine("Cross Performance: " + pCount / (float)crossPerformance.Count); //pCount = 0; //for (int i = 0; i < randomPerformance.Count; i++) // if (randomPerformance[i] > waterlineAccuracy) // pCount++; //if (randomPerformance.Count > 0) // Console.WriteLine("Random Performance: " + pCount / (float)randomPerformance.Count); cycle++; }
void VFLearner() { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); Random rdm = new Random(0); TablePredictor testTable = new TablePredictor(symbolCount, population[0].stateSize); TablePredictor temp; int printRun = 100000; double crossRatio = 0.0; double randomRatio = 0.1; double mutatedRatio = 1 - (crossRatio + randomRatio); double mutationIntensity = 0.5; int runs = int.MaxValue; double decision; int parent; for (int i = 0; i < runs; i++) { cycle++; if (i % printRun == 0) { population[0].SaveToFile("Run0-" + (i / printRun) + "v0.tp"); Console.WriteLine("m.i.: " + ("" + mutationIntensity).PadRight(10).Substring(0, 6)); printPopulation(1); } decision = rdm.NextDouble(); if (decision < mutatedRatio) { //Mutation parent = rdm.Next(populationCount); TablePredictor.mutateOverride(testTable, population[parent], rdm, mutationIntensity); testTable.birthTime = cycle; } else if (decision < mutatedRatio + crossRatio) { throw new Exception("NO CROSS ALLOWED"); //Cross int parent1 = rdm.Next(populationCount); int parent2 = rdm.Next(populationCount); TablePredictor.crossOverride(testTable, population[parent1], population[parent2], rdm); testTable.birthTime = cycle; if (testTable.species == population[parent1].species) { parent = parent1; } else { parent = parent2; } } else { TablePredictor.randomOverride(testTable, rdm); parent = -1; } testTable.testPredict(test); if (testTable.accuracy > population[populationCount - 1].accuracy) { if (parent != -1) { //Children analysis if (population[parent].accuracy < testTable.accuracy) { //Parent beat //Soft change for sucessful mutation intensity (x2 because of random average behaviour) mutationIntensity = 0.8 * mutationIntensity + 0.2 * (2 * testTable.mutationIntensity); if (mutationIntensity > 0.9) { mutationIntensity = 0.9; //maximum allowed mutation intensity } temp = population[parent]; population[parent] = testTable; testTable = temp; //Reorder for (int j = parent; j > 0; j--) { if (population[j - 1].accuracy < population[j].accuracy) { temp = population[j - 1]; population[j - 1] = population[j]; population[j] = temp; } } } } else { //Random win, order for (int j = 0; j < populationCount; j++) { if (testTable.accuracy > population[j].accuracy) { temp = population[j]; population[j] = testTable; testTable = temp; } } } } } stopwatch.Stop(); long elapsedTime = stopwatch.ElapsedMilliseconds; Console.WriteLine("Genetic Algorithms runtime at: \nMiliseconds: " + elapsedTime + "\nSeconds: " + elapsedTime / 1000f + "\nMinutes" + (elapsedTime / 1000f) / 60f); printPopulation(populationCount); TablePredictor best = population[0]; Console.WriteLine("Rank : 0 " + best.getStats()); best.SaveToFile("Potato.tp"); TablePredictor lel = TablePredictor.LoadFromFile("Potato.tp"); lel.testPredict(test); Console.WriteLine("Loaded: " + lel.getStats()); }