Skip to content

Commit 3d25559

Browse files
Added callback event for when a function solves a step. Allows for client code to output the program code generated so far.
Reset genomeSize when a function completes a step. Display minutes and seconds in status.
1 parent 56ee953 commit 3d25559

File tree

4 files changed

+43
-7
lines changed

4 files changed

+43
-7
lines changed

AIProgrammer.Functions/Concrete/StringFunction.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ public class StringFunction : IFunction
1919
private double _mutationRate;
2020
private int _genomeSize;
2121
private GAFunction _fitnessFunc;
22+
private Action<string, object> _onStepComplete;
2223
private OnGeneration _generationFunc;
2324
private TargetParams _targetParams;
2425

25-
public StringFunction(Func<IFitness> getFitnessMethod, GAStatus bestStatus, GAFunction fitnessFunc, OnGeneration generationFunc, double crossoverRate, double mutationRate, int genomeSize, TargetParams targetParams)
26+
public StringFunction(Func<IFitness> getFitnessMethod, Action<string, object> onStepComplete, GAStatus bestStatus, GAFunction fitnessFunc, OnGeneration generationFunc, double crossoverRate, double mutationRate, int genomeSize, TargetParams targetParams)
2627
{
2728
_getFitnessFunc = getFitnessMethod;
29+
_onStepComplete = onStepComplete;
2830
_bestStatus = bestStatus;
2931
_crossoverRate = crossoverRate;
3032
_mutationRate = mutationRate;
@@ -79,6 +81,9 @@ public string Generate(IGeneticAlgorithm ga)
7981
_bestStatus.LastChangeDate = DateTime.Now;
8082
_bestStatus.Program = "";
8183
_bestStatus.Ticks = 0;
84+
85+
// Notify parent of progress.
86+
_onStepComplete(appendCode, term);
8287
}
8388

8489
// Restore target string.

AIProgrammer.Functions/Concrete/StringFunctionChunk.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,15 @@ public class StringFunctionChunk : IFunction
3131
private double _mutationRate;
3232
private int _genomeSize;
3333
private GAFunction _fitnessFunc;
34+
private Action<string, object> _onStepComplete;
3435
private OnGeneration _generationFunc;
3536
private TargetParams _targetParams;
3637
private int _chunkSize;
3738

38-
public StringFunctionChunk(Func<IFitness> getFitnessMethod, GAStatus bestStatus, GAFunction fitnessFunc, OnGeneration generationFunc, double crossoverRate, double mutationRate, int genomeSize, TargetParams targetParams, int chunkSize = 4)
39+
public StringFunctionChunk(Func<IFitness> getFitnessMethod, Action<string, object> onStepComplete, GAStatus bestStatus, GAFunction fitnessFunc, OnGeneration generationFunc, double crossoverRate, double mutationRate, int genomeSize, TargetParams targetParams, int chunkSize = 4)
3940
{
4041
_getFitnessFunc = getFitnessMethod;
42+
_onStepComplete = onStepComplete;
4143
_bestStatus = bestStatus;
4244
_crossoverRate = crossoverRate;
4345
_mutationRate = mutationRate;
@@ -111,6 +113,9 @@ public string Generate(IGeneticAlgorithm ga)
111113
_bestStatus.LastChangeDate = DateTime.Now;
112114
_bestStatus.Program = "";
113115
_bestStatus.Ticks = 0;
116+
117+
// Notify parent of progress.
118+
_onStepComplete(appendCode, term);
114119
}
115120

116121
// Restore target string.

AIProgrammer.Managers/GAManager.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,17 @@ public static string Run(IGeneticAlgorithm iga, GAFunction fitnessFunc, OnGenera
2525
{
2626
// Perform any additional setup for this fitness.
2727
setupFunc();
28+
}
29+
30+
try
31+
{
32+
// Delete any existing dat file.
33+
File.Delete(Directory.GetCurrentDirectory() + "\\my-genetic-algorithm.dat");
34+
}
35+
catch (Exception excep)
36+
{
37+
Console.WriteLine("Unable to delete " + Directory.GetCurrentDirectory() + "\\my-genetic-algorithm.dat\n" + excep.Message);
2838
}
29-
30-
// Delete any existing dat file.
31-
File.Delete(Directory.GetCurrentDirectory() + "\\my-genetic-algorithm.dat");
3239

3340
// Start a new genetic algorithm.
3441
ga.GAParams.Elitism = true;

AIProgrammer/Program.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,11 @@ class Program
4444
private static int _maxIterationCount = 5000; // Max iterations a program may run before being killed (prevents infinite loops).
4545
private static int _expandAmount = 0; // The max genome size will expand by this amount, every _expandRate iterations (may help learning). Set to 0 to disable.
4646
private static int _expandRate = 5000; // The max genome size will expand by _expandAmount, at this interval of generations.
47+
private static int _originalGenomeSize = _genomeSize;
4748

4849
#endregion
4950

50-
private static IFunction _functionGenerator = null; //new StringFunction(() => GetFitnessMethod(), _bestStatus, fitnessFunction, OnGeneration, _crossoverRate, _mutationRate, _genomeSize, _targetParams); /* Functions require setting BrainfuckVersion=2 in App.config */
51+
private static IFunction _functionGenerator = null; //new StringFunction(() => GetFitnessMethod(), (program, param) => OnFunctionStepComplete(program, param), _bestStatus, fitnessFunction, OnGeneration, _crossoverRate, _mutationRate, _genomeSize, _targetParams); /* Functions require setting BrainfuckVersion=2 in App.config */
5152

5253
/// <summary>
5354
/// Selects the type of fitness algorithm to use (Hello World solutions, Calculation solutions, etc).
@@ -67,6 +68,24 @@ private static IFitness GetFitnessMethod()
6768
return new StringStrictFitness(_ga, _maxIterationCount, _targetParams.TargetString, _appendCode);
6869
}
6970

71+
/// <summary>
72+
/// Callback handler for each time a function completes solving a step of its process. For example, solving a word within a sentence, etc.
73+
/// </summary>
74+
/// <param name="program">Complete append code generated so far. This can be set as the value for _appendCode to generate programs.</param>
75+
/// <param name="param">Optional parameter supplied by Function to indicate what has been solved (i.e., the term, sentence, numeric value, etc).</param>
76+
private static void OnFunctionStepComplete(string program, object param)
77+
{
78+
// Reset genome size back to its original value for subsequent solving steps.
79+
_genomeSize = _originalGenomeSize;
80+
_ga.GAParams.GenomeSize = _genomeSize;
81+
82+
// Reset timer.
83+
_startTime = DateTime.Now;
84+
85+
// Display generated code so far.
86+
Console.WriteLine(param.ToString() + "\n" + program);
87+
}
88+
7089
#region Worker Methods
7190

7291
/// <summary>
@@ -77,7 +96,7 @@ private static void OnGeneration(GA ga)
7796
if (_bestStatus.Iteration++ > 1000)
7897
{
7998
_bestStatus.Iteration = 0;
80-
Console.WriteLine("Best Fitness: " + _bestStatus.TrueFitness + "/" + _targetParams.TargetFitness + " " + Math.Round(_bestStatus.TrueFitness / _targetParams.TargetFitness * 100, 2) + "%, Ticks: " + _bestStatus.Ticks + ", Total Ticks: " + _bestStatus.TotalTicks + ", Running: " + Math.Round((DateTime.Now - _startTime).TotalMinutes) + "m, Size: " + _genomeSize + ", Best Output: " + _bestStatus.Output + ", Changed: " + _bestStatus.LastChangeDate.ToString() + ", Program: " + _bestStatus.Program);
99+
Console.WriteLine("Best Fitness: " + _bestStatus.TrueFitness + "/" + _targetParams.TargetFitness + " " + Math.Round(_bestStatus.TrueFitness / _targetParams.TargetFitness * 100, 2) + "%, Ticks: " + _bestStatus.Ticks + ", Total Ticks: " + _bestStatus.TotalTicks + ", Running: " + Math.Floor((DateTime.Now - _startTime).TotalSeconds / 60) + "m " + Math.Round(((DateTime.Now - _startTime).TotalSeconds % 60)) + "s, Size: " + _genomeSize + ", Best Output: " + _bestStatus.Output + ", Changed: " + _bestStatus.LastChangeDate.ToString() + ", Program: " + _bestStatus.Program);
81100

82101
ga.Save("my-genetic-algorithm.dat");
83102
}

0 commit comments

Comments
 (0)