-
-
Notifications
You must be signed in to change notification settings - Fork 264
/
Copy pathHelloUserFitness.cs
192 lines (165 loc) · 5.86 KB
/
HelloUserFitness.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
using AIProgrammer.Fitness.Base;
using AIProgrammer.GeneticAlgorithm;
using AIProgrammer.Managers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AIProgrammer.Fitness.Concrete
{
/// <summary>
/// Prints "Hello [Name]". Prompts the user for input, one letter at a time, terminated by a zero. Then prints the text.
/// </summary>
public class HelloUserFitness : FitnessBase
{
private string _targetString;
private int _trainingCount;
public HelloUserFitness(GA ga, int maxIterationCount, string targetString, int maxTrainingCount = 4)
: base(ga, maxIterationCount)
{
_targetString = targetString;
_trainingCount = maxTrainingCount;
if (_targetFitness == 0)
{
for (int i=0; i<_trainingCount; i++)
{
_targetFitness += (_targetString.Length + (i + 1)) * 256;
}
}
}
#region FitnessBase Members
protected override double GetFitnessMethod(string program)
{
string name = "";
string targetStringName = "";
int state = 0;
double countBonus = 0;
double penalty = 0;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < _trainingCount; i++)
{
switch (i)
{
case 0: name = "s"; break;
case 1: name = "me"; break;
case 2: name = "jay"; break;
case 3: name = "kory"; break;
case 4: name = "jamie"; break;
};
sb.Clear();
sb.Append(_targetString);
sb.Append(name);
targetStringName = sb.ToString();
try
{
state = 0;
_console.Clear();
// Run the program.
_bf = new Interpreter(program, () =>
{
if (state > 0 && state < name.Length + 2)
{
if (state < name.Length + 1)
{
// We've output 2 bytes, we're ready to send input.
return (byte)name[state++ - 1];
}
else
{
// Send terminator character.
return 0;
}
}
else
{
// Not ready for input.
penalty++;
return 255;
}
},
(b) =>
{
_console.Append((char)b);
// Output the first half of the phrase before looking for input.
if (state == 0 && _console.Length >= _targetString.Length)
{
// We've output two bytes, let input come through.
state = 1;
}
});
_bf.Run(_maxIterationCount);
}
catch
{
}
_output.Append(_console.ToString());
_output.Append(",");
// Order bonus.
for (int j = 0; j < targetStringName.Length; j++)
{
if (_console.Length > j)
{
Fitness += 256 - Math.Abs(_console[j] - targetStringName[j]);
}
}
// Make the AI wait until a solution is found without the penalty (too many input characters).
Fitness -= penalty;
// Check for solution.
IsFitnessAchieved();
// Bonus for less operations to optimize the code.
countBonus += ((_maxIterationCount - _bf.m_Ticks) / 20.0);
Ticks += _bf.m_Ticks;
}
if (_fitness != Double.MaxValue)
{
_fitness = Fitness + countBonus;
}
return _fitness;
}
protected override void RunProgramMethod(string program)
{
for (int i = 0; i < 99; i++)
{
// Get input from the user.
Console.WriteLine();
Console.Write(">: ");
string line = Console.ReadLine();
int index = 0;
_console.Clear();
try
{
// Run the program.
Interpreter bf = new Interpreter(program, () =>
{
byte b;
// Send the next character.
if (index < line.Length)
{
b = (byte)line[index++];
}
else
{
b = 0;
}
return b;
},
(b) =>
{
_console.Append((char)b);
});
bf.Run(_maxIterationCount);
}
catch
{
}
Console.WriteLine(_console.ToString());
}
}
public override string GetConstructorParameters()
{
return _maxIterationCount + ", \"" + _targetString + "\", " + _trainingCount;
}
#endregion
}
}