bool Reforce(TablePredictor original, TablePredictor factoryTester, Random rdm, int breath) { int variationCount = ((int)short.MaxValue) * 2 * 256; //All entries, times 2 (prediction,state) , times 256 possible states int startVariation = rdm.Next(variationCount); int count = 0; for (int i = startVariation; i < variationCount; i = (i + 1) % variationCount) { count++; if (count > breath) { return(false); } TablePredictor.variationOverride(factoryTester, i); factoryTester.ResetState(); factoryTester.ResetMetrics(); factoryTester.testPredict(test); if (original.accuracy < factoryTester.accuracy) { TablePredictor.variationOverride(original, i); original.predictionCount = factoryTester.predictionCount; original.errorCount = factoryTester.errorCount; return(true); //return both the original and the factory tester with the same variation } TablePredictor.variationUndo(factoryTester, i, original); } return(false); }
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()); }