Skip to content

Commit 6aca9e5

Browse files
Added feature to save fitness history as csv, for plotting on a graph.
1 parent 28f3982 commit 6aca9e5

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

AIProgrammer.GA/GA.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ namespace AIProgrammer.GeneticAlgorithm
4242
public class GA
4343
{
4444
public GAParams GAParams { get; set; }
45+
private string _historyPath = Directory.GetCurrentDirectory() + "\\history.txt";
4546

4647
/// <summary>
4748
/// Default constructor sets mutation rate to 5%, crossover to 80%, population to 100,
@@ -118,7 +119,10 @@ public void Go(bool resume = false)
118119

119120
if (GAParams.CurrentGeneration % 100 == 0)
120121
{
121-
Console.WriteLine("Generation " + GAParams.CurrentGeneration + ", Best Fitness: " + fitness + ", Best Age: " + GAParams.m_thisGeneration[GAParams.m_thisGeneration.Count - 1].age);
122+
Console.WriteLine("Generation " + GAParams.CurrentGeneration + ", Best Fitness: " + fitness);
123+
124+
// Record history timeline.
125+
File.AppendAllText(_historyPath, DateTime.Now.ToString() + "," + fitness + "," + GAParams.targetFitness + "," + GAParams.CurrentGeneration + "\r\n");
122126
}
123127

124128
if (GAParams.targetFitness > 0 && fitness >= GAParams.targetFitness)

AIProgrammer/Program.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
using System.Text;
55
using System.Threading.Tasks;
66
using AIProgrammer.GeneticAlgorithm;
7+
using AIProgrammer.Repository.Interface;
8+
using RSSAutoGen.Repository.Concrete;
9+
using AIProgrammer.Types;
710

811
namespace AIProgrammer
912
{
@@ -23,7 +26,7 @@ class Program
2326
private static int bestiteration = 0; // Current iteration (generation) count.
2427
private static bool bestnoerrors = false; // Indicator if the program had errors or not.
2528
private static DateTime bestlastchangedate = DateTime.Now; // Time of last improved evolution.
26-
private static int maxIterationCount = 5000; // Max iterations a program may run before being killed (prevents infinite loops).
29+
private static int maxIterationCount = 2500; // Max iterations a program may run before being killed (prevents infinite loops).
2730
private static string targetString = "reddit"; // Target string to generate a program to print.
2831

2932
/// <summary>
@@ -34,7 +37,7 @@ private static void OnGeneration(GA ga)
3437
if (bestiteration++ > 1000)
3538
{
3639
bestiteration = 0;
37-
Console.WriteLine("Best Fitness: " + bestfitness + ", No Errors?: " + bestnoerrors + ", Best Output: " + bestoutput + ", Changed: " + bestlastchangedate.ToString() + ", Program: " + bestprogram);
40+
Console.WriteLine("Best Fitness: " + bestfitness + "/" + ga.GAParams.targetFitness + " " + Math.Round(bestfitness / ga.GAParams.targetFitness * 100) + "%, Best Output: " + bestoutput + ", Changed: " + bestlastchangedate.ToString() + ", Program: " + bestprogram);
3841

3942
ga.Save("my-genetic-algorithm.dat");
4043
}

0 commit comments

Comments
 (0)