@@ -21,23 +21,26 @@ class Program
21
21
{
22
22
private static GA _ga = null ; // Our genetic algorithm instance.
23
23
private static double _bestFitness = 0 ; // Best fitness so far.
24
+ private static double _bestTrueFitness = 0 ; // Best fitness so far, without optimization bonus.
24
25
private static string _bestProgram = "" ; // Best program so far.
25
26
private static string _bestOutput = "" ; // Best program output so far.
26
- private static int _besIiteration = 0 ; // Current iteration (generation) count.
27
+ private static int _bestIteration = 0 ; // Current iteration (generation) count.
28
+ private static int _bestTotalInstructions = 0 ; // Number of instructions executed by the best program.
27
29
private static bool _bestNoErrors = false ; // Indicator if the program had errors or not.
28
30
private static DateTime _bestLastChangeDate = DateTime . Now ; // Time of last improved evolution.
29
31
private static int _maxIterationCount = 2000 ; // Max iterations a program may run before being killed (prevents infinite loops).
30
- private static string _targetString = "hello" ; // Target string to generate a program to print.
32
+ private static string _targetString = "reddit" ; // Target string to generate a program to print.
33
+ private static int _targetFitness = _targetString . Length * 256 ;
31
34
32
35
/// <summary>
33
36
/// Event handler that is called upon each generation. We use this opportunity to display some status info and save the current genetic algorithm in case of crashes etc.
34
37
/// </summary>
35
38
private static void OnGeneration ( GA ga )
36
39
{
37
- if ( _besIiteration ++ > 1000 )
40
+ if ( _bestIteration ++ > 1000 )
38
41
{
39
- _besIiteration = 0 ;
40
- Console . WriteLine ( "Best Fitness: " + _bestFitness + "/" + ga . GAParams . TargetFitness + " " + Math . Round ( _bestFitness / ga . GAParams . TargetFitness * 100 ) + "%, Best Output: " + _bestOutput + ", Changed: " + _bestLastChangeDate . ToString ( ) + ", Program: " + _bestProgram ) ;
42
+ _bestIteration = 0 ;
43
+ Console . WriteLine ( "Best Fitness: " + _bestTrueFitness + "/" + _targetFitness + " " + Math . Round ( _bestTrueFitness / _targetFitness * 100 ) + "%, Ticks: " + _bestTotalInstructions + ", Output: " + _bestOutput + ", Changed: " + _bestLastChangeDate . ToString ( ) + ", Program: " + _bestProgram ) ;
41
44
42
45
ga . Save ( "my-genetic-algorithm.dat" ) ;
43
46
}
@@ -50,7 +53,9 @@ private static void OnGeneration(GA ga)
50
53
/// <returns>double, indicating the score</returns>
51
54
private static double fitnessFunction ( double [ ] weights )
52
55
{
56
+ Interpreter bf = null ;
53
57
double fitness = 0 ;
58
+ double trueFitness = 0 ; // fitness without secondary bonuses (ie., optimization, etc)
54
59
string console = "" ;
55
60
bool noErrors = false ;
56
61
@@ -60,7 +65,7 @@ private static double fitnessFunction(double[] weights)
60
65
try
61
66
{
62
67
// Run the program.
63
- Interpreter bf = new Interpreter ( program , null , ( b ) =>
68
+ bf = new Interpreter ( program , null , ( b ) =>
64
69
{
65
70
console += ( char ) b ;
66
71
} ) ;
@@ -78,18 +83,34 @@ private static double fitnessFunction(double[] weights)
78
83
{
79
84
if ( console . Length > i )
80
85
{
81
- fitness += 256 - Math . Abs ( console [ i ] - _targetString [ i ] ) ;
86
+ trueFitness += 256 - Math . Abs ( console [ i ] - _targetString [ i ] ) ;
82
87
}
83
88
}
84
89
90
+ fitness += trueFitness ;
91
+
92
+ // Did we find a perfect fitness?
93
+ if ( trueFitness >= _targetFitness )
94
+ {
95
+ // We're done! Stop the GA algorithm.
96
+ // Note, you can alternatively use the _ga.GAParams.TargetFitness to set a specific fitness to achieve.
97
+ // In our case, the number of ticks (instructions executed) is a variable part of the fitness, so we don't know the exact perfect fitness value once this part is added.
98
+ _ga . Stop = true ;
99
+ }
100
+
101
+ // Bonus for less operations to optimize the code.
102
+ fitness += ( ( _maxIterationCount - bf . m_Ticks ) / 10 ) ;
103
+
85
104
// Is this a new best fitness?
86
105
if ( fitness > _bestFitness )
87
106
{
88
107
_bestFitness = fitness ;
108
+ _bestTrueFitness = trueFitness ;
89
109
_bestOutput = console ;
90
110
_bestNoErrors = noErrors ;
91
111
_bestLastChangeDate = DateTime . Now ;
92
112
_bestProgram = program ;
113
+ _bestTotalInstructions = bf . m_Ticks ;
93
114
}
94
115
95
116
return fitness ;
@@ -130,7 +151,7 @@ private static double[] Setup()
130
151
131
152
// Start a new genetic algorithm.
132
153
_ga . GAParams . Elitism = true ;
133
- _ga . GAParams . TargetFitness = _targetString . Length * 256 ;
154
+ // _ga.GAParams.TargetFitness = _targetFitness ;
134
155
_ga . GAParams . HistoryPath = System . IO . Directory . GetCurrentDirectory ( ) + "\\ history.txt" ;
135
156
_ga . FitnessFunction = new GAFunction ( fitnessFunction ) ;
136
157
_ga . OnGenerationFunction = new OnGeneration ( OnGeneration ) ;
0 commit comments