|
| 1 | +using AIProgrammer.Fitness.Base; |
| 2 | +using AIProgrammer.GeneticAlgorithm; |
| 3 | +using AIProgrammer.Managers; |
| 4 | +using AIProgrammer.Types; |
| 5 | +using Newtonsoft.Json; |
| 6 | +using System; |
| 7 | +using System.Collections.Generic; |
| 8 | +using System.Linq; |
| 9 | +using System.Text; |
| 10 | +using System.Text.RegularExpressions; |
| 11 | +using System.Threading.Tasks; |
| 12 | +using System.Xml; |
| 13 | + |
| 14 | +namespace AIProgrammer.Fitness.Concrete |
| 15 | +{ |
| 16 | + /// <summary> |
| 17 | + /// Returns the length of a string. |
| 18 | + /// </summary> |
| 19 | + public class StringLengthFitness : FitnessBase |
| 20 | + { |
| 21 | + private static string[] _trainingExamples = { "cori@domain.com", "mt@po.box", "test", "johnandjanesdfgjnsdkfjgjnrtkhreuitgure", "unknown-string-goes-here" }; |
| 22 | + private static int[] _trainingResults; |
| 23 | + |
| 24 | + #region Settings |
| 25 | + |
| 26 | + public override int? GenomeSize |
| 27 | + { |
| 28 | + get |
| 29 | + { |
| 30 | + return 10; |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + public override int? MaxGenomeSize |
| 35 | + { |
| 36 | + get |
| 37 | + { |
| 38 | + return 150; |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + public override int? ExpandAmount |
| 43 | + { |
| 44 | + get |
| 45 | + { |
| 46 | + return 2; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + public override int? ExpandRate |
| 51 | + { |
| 52 | + get |
| 53 | + { |
| 54 | + return 2000; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + #endregion |
| 59 | + |
| 60 | + public StringLengthFitness(GA ga, int maxIterationCount, string appendFunctions = null) |
| 61 | + : base(ga, maxIterationCount, appendFunctions) |
| 62 | + { |
| 63 | + if (_targetFitness == 0) |
| 64 | + { |
| 65 | + // Extract expected results from training examples. |
| 66 | + _trainingResults = _trainingExamples.ToList().ConvertAll(e => { return e.Length; }).ToArray(); |
| 67 | + |
| 68 | + // Assign target fitness. |
| 69 | + _targetFitness = _trainingExamples.Length * 256; |
| 70 | + //_targetFitness += _trainingExamples.Length * 10; // bonus for matching exact length of output |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + #region FitnessBase Members |
| 75 | + |
| 76 | + protected override double GetFitnessMethod(string program) |
| 77 | + { |
| 78 | + double countBonus = 0; |
| 79 | + double penalty = 0; |
| 80 | + |
| 81 | + for (int i = 0; i < _trainingExamples.Length; i++) |
| 82 | + { |
| 83 | + try |
| 84 | + { |
| 85 | + int state = 0; |
| 86 | + _console.Clear(); |
| 87 | + |
| 88 | + // Run the program. |
| 89 | + _bf = new Interpreter(program, () => |
| 90 | + { |
| 91 | + if (state < _trainingExamples[i].Length) |
| 92 | + { |
| 93 | + // Send input. |
| 94 | + return (byte)_trainingExamples[i][state++]; |
| 95 | + } |
| 96 | + else |
| 97 | + { |
| 98 | + // Not ready for input. |
| 99 | + return 0; |
| 100 | + } |
| 101 | + }, |
| 102 | + (b) => |
| 103 | + { |
| 104 | + if (state < _trainingExamples[i].Length) |
| 105 | + { |
| 106 | + // Apply a penalty for outputting before reading the input. |
| 107 | + penalty += 10; |
| 108 | + } |
| 109 | + |
| 110 | + _console.Append(b.ToString()); |
| 111 | + }); |
| 112 | + _bf.Run(_maxIterationCount); |
| 113 | + } |
| 114 | + catch |
| 115 | + { |
| 116 | + } |
| 117 | + |
| 118 | + if (_console.Length > 0) |
| 119 | + { |
| 120 | + string console = _console.ToString(); |
| 121 | + _output.Append(console); |
| 122 | + _output.Append(","); |
| 123 | + |
| 124 | + if (Int32.TryParse(console, out int value)) |
| 125 | + { |
| 126 | + Fitness += 256 - Math.Abs(value - _trainingResults[i]); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + // Make the AI wait until a solution is found without the penalty. |
| 131 | + Fitness -= penalty; |
| 132 | + |
| 133 | + // Check for solution. |
| 134 | + IsFitnessAchieved(); |
| 135 | + |
| 136 | + // Length bonus (percentage of 100). |
| 137 | + //Fitness += 10;// * ((1 - Math.Abs(_console.Length - 1)) / 1); |
| 138 | + |
| 139 | + // Bonus for less operations to optimize the code. |
| 140 | + countBonus += ((_maxIterationCount - _bf.m_Ticks) / 1000.0); |
| 141 | + |
| 142 | + Ticks += _bf.m_Ticks; |
| 143 | + TotalTicks += _bf.m_TotalTicks; |
| 144 | + } |
| 145 | + |
| 146 | + if (_fitness != Double.MaxValue) |
| 147 | + { |
| 148 | + _fitness = Fitness + countBonus; |
| 149 | + } |
| 150 | + |
| 151 | + return _fitness; |
| 152 | + } |
| 153 | + |
| 154 | + protected override void RunProgramMethod(string program) |
| 155 | + { |
| 156 | + for (int i = 0; i < 99; i++) |
| 157 | + { |
| 158 | + // Get input from the user. |
| 159 | + Console.WriteLine(); |
| 160 | + Console.Write(">: "); |
| 161 | + string line = Console.ReadLine(); |
| 162 | + int index = 0; |
| 163 | + |
| 164 | + _console.Clear(); |
| 165 | + |
| 166 | + try |
| 167 | + { |
| 168 | + // Run the program. |
| 169 | + Interpreter bf = new Interpreter(program, () => |
| 170 | + { |
| 171 | + byte b; |
| 172 | + |
| 173 | + // Send the next character. |
| 174 | + if (index < line.Length) |
| 175 | + { |
| 176 | + b = (byte)line[index++]; |
| 177 | + } |
| 178 | + else |
| 179 | + { |
| 180 | + b = 0; |
| 181 | + } |
| 182 | + |
| 183 | + return b; |
| 184 | + }, |
| 185 | + (b) => |
| 186 | + { |
| 187 | + _console.Append(b.ToString()); |
| 188 | + }); |
| 189 | + |
| 190 | + bf.Run(_maxIterationCount); |
| 191 | + } |
| 192 | + catch |
| 193 | + { |
| 194 | + } |
| 195 | + |
| 196 | + Console.WriteLine(_console.ToString()); |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + public override string GetConstructorParameters() |
| 201 | + { |
| 202 | + return _maxIterationCount.ToString(); |
| 203 | + } |
| 204 | + |
| 205 | + #endregion |
| 206 | + } |
| 207 | +} |
0 commit comments