Skip to content

Commit 45e5969

Browse files
Added TimesTwoFitness (input * 2). Solved in 20 seconds.
Added results.
1 parent ae68901 commit 45e5969

15 files changed

+3465
-19
lines changed

Diff for: .gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
*.cache
22
*.dll
33
*.pdb
4-
*.txt
54
*.suo
65
*.exe
76
*.exe.config

Diff for: AIProgrammer.Fitness/AIProgrammer.Fitness.csproj

+2-1
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,11 @@
4848
<Compile Include="Concrete\Research\AddToCharFitness.cs" />
4949
<Compile Include="Concrete\IfThenFitness.cs" />
5050
<Compile Include="Concrete\FibonacciFitness.cs" />
51+
<Compile Include="Concrete\TimesTwoFitness.cs" />
5152
<Compile Include="Concrete\Research\NumberGuessFitness.cs" />
5253
<Compile Include="Concrete\ReverseStringFitness.cs" />
5354
<Compile Include="Concrete\HelloUserFitness.cs" />
54-
<Compile Include="Concrete\Research\MultiplyFitness.cs" />
55+
<Compile Include="Concrete\MultiplyFitness.cs" />
5556
<Compile Include="Concrete\StringFitness.cs" />
5657
<Compile Include="Concrete\StringOptimizedFitness.cs" />
5758
<Compile Include="Concrete\StringStrictFitness.cs" />

Diff for: AIProgrammer.Fitness/Concrete/FibonacciFitness.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class FibonacciFitness : FitnessBase
2424
/// </summary>
2525
public static string FibonacciFunctions = ",>,-[-<+>]<+.$@";
2626

27-
public FibonacciFitness(GA ga, int maxIterationCount, int maxDigits = 3, int maxTrainingCount = 3, string appendFunctions = null)
27+
public FibonacciFitness(GA ga, int maxIterationCount, int maxDigits = 4, int maxTrainingCount = 3, string appendFunctions = null)
2828
: base(ga, maxIterationCount, appendFunctions)
2929
{
3030
_maxDigits = maxDigits;

Diff for: AIProgrammer.Fitness/Concrete/Research/MultiplyFitness.cs renamed to AIProgrammer.Fitness/Concrete/MultiplyFitness.cs

+53-16
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
using System.Text;
88
using System.Threading.Tasks;
99

10-
namespace AIProgrammer.Fitness.Concrete.Research
10+
namespace AIProgrammer.Fitness.Concrete
1111
{
1212
/// <summary>
1313
/// Calculates the product of various input integers and outputs the result as byte values (ie., 3 => 3, you would need to do a ToString() to display it on the console).
@@ -16,14 +16,22 @@ namespace AIProgrammer.Fitness.Concrete.Research
1616
public class MultiplyFitness : FitnessBase
1717
{
1818
private int _trainingCount;
19+
private static int _functionCount; // number of functions in the appeneded code.
1920

20-
public MultiplyFitness(GA ga, int maxIterationCount, int maxTrainingCount = 4)
21-
: base(ga, maxIterationCount)
21+
/// <summary>
22+
/// Previously generated BrainPlus function for addition. Generated using AddFitness.
23+
/// To use, set _appendCode = MultiplyFitness.AddFunction in main program.
24+
/// </summary>
25+
public static string AddFunction = ",>,-[-<+>]<+.$@";
26+
27+
public MultiplyFitness(GA ga, int maxIterationCount, int maxTrainingCount = 3, string appendFunctions = null)
28+
: base(ga, maxIterationCount, appendFunctions)
2229
{
2330
_trainingCount = maxTrainingCount;
2431
if (_targetFitness == 0)
2532
{
2633
_targetFitness = _trainingCount * 256;
34+
_functionCount = CommonManager.GetFunctionCount(appendFunctions);
2735
}
2836
}
2937

@@ -34,22 +42,25 @@ protected override double GetFitnessMethod(string program)
3442
byte input1 = 0, input2 = 0;
3543
int state = 0;
3644
double countBonus = 0;
45+
double penalty = 0;
46+
byte result = 0;
3747

3848
for (int i = 0; i < _trainingCount; i++)
3949
{
4050
switch (i)
4151
{
42-
case 0: input1 = 2; input2 = 1; break;
43-
case 1: input1 = 4; input2 = 2; break;
44-
case 2: input1 = 5; input2 = 3; break;
45-
case 3: input1 = 6; input2 = 3; break;
46-
case 4: input1 = 7; input2 = 2; break;
52+
//case 0: input1 = 2; input2 = 1; break;
53+
case 0: input1 = 4; input2 = 2; break;
54+
case 1: input1 = 5; input2 = 3; break;
55+
case 2: input1 = 6; input2 = 4; break;
56+
//case 4: input1 = 7; input2 = 2; break;
4757
};
4858

4959
try
5060
{
5161
state = 0;
5262
_console.Clear();
63+
result = 0;
5364

5465
// Run the program.
5566
_bf = new Interpreter(program, () =>
@@ -66,12 +77,27 @@ protected override double GetFitnessMethod(string program)
6677
}
6778
else
6879
{
80+
// Not ready for input.
81+
penalty++;
82+
6983
return 0;
7084
}
7185
},
7286
(b) =>
7387
{
74-
_console.Append(b.ToString());
88+
if (state < 2)
89+
{
90+
// Not ready for output.
91+
penalty++;
92+
}
93+
else if (state == 2)
94+
{
95+
_console.Append(b);
96+
_console.Append(",");
97+
98+
result = b;
99+
state++;
100+
}
75101
});
76102
_bf.Run(_maxIterationCount);
77103
}
@@ -83,21 +109,32 @@ protected override double GetFitnessMethod(string program)
83109
if (_console.Length > 0)
84110
{
85111
_output.Append(_console.ToString());
86-
_output.Append(",");
112+
_output.Append("|");
87113

88-
int value;
89-
if (Int32.TryParse(_console.ToString(), out value))
90-
{
91-
Fitness += 256 - Math.Abs(value - (input1 * input2));
92-
}
114+
Fitness += 256 - Math.Abs(result - (input1 * input2));
93115
}
94116

117+
// Make the AI wait until a solution is found without the penalty (too many input characters).
118+
Fitness -= penalty;
119+
95120
// Check for solution.
96121
IsFitnessAchieved();
97122

98123
// Bonus for less operations to optimize the code.
99124
countBonus += ((_maxIterationCount - _bf.m_Ticks) / 1000.0);
100125

126+
// Bonus for using functions.
127+
if (_functionCount > 0)
128+
{
129+
for (char functionName = 'a'; functionName < 'a' + _functionCount; functionName++)
130+
{
131+
if (program.Contains(functionName))
132+
{
133+
countBonus += 25;
134+
}
135+
}
136+
}
137+
101138
Ticks += _bf.m_Ticks;
102139
}
103140

@@ -143,7 +180,7 @@ protected override void RunProgramMethod(string program)
143180
},
144181
(b) =>
145182
{
146-
Console.Write(b.ToString());
183+
Console.Write(b);
147184
});
148185

149186
bf.Run(_maxIterationCount);

Diff for: AIProgrammer.Fitness/Concrete/TimesTwoFitness.cs

+193
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
using AIProgrammer.Fitness.Base;
2+
using AIProgrammer.GeneticAlgorithm;
3+
using AIProgrammer.Managers;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
10+
namespace AIProgrammer.Fitness.Concrete
11+
{
12+
/// <summary>
13+
/// Calculates input * 2. Example: 3 => 6, 5 => 10, etc. Input and output is a byte value (not ASCII character).
14+
/// </summary>
15+
public class TimesTwoFitness : FitnessBase
16+
{
17+
private int _trainingCount;
18+
private static int _functionCount; // number of functions in the appeneded code.
19+
20+
/// <summary>
21+
/// Previously generated BrainPlus function for addition. Generated using AddFitness.
22+
/// To use, set _appendCode = DoubleFitness.AddFunction in main program.
23+
/// </summary>
24+
public static string AddFunction = ",>,-[-<+>]<+.$@";
25+
26+
public TimesTwoFitness(GA ga, int maxIterationCount, int maxTrainingCount = 2, string appendFunctions = null)
27+
: base(ga, maxIterationCount, appendFunctions)
28+
{
29+
_trainingCount = maxTrainingCount;
30+
if (_targetFitness == 0)
31+
{
32+
_targetFitness = _trainingCount * 256;
33+
_functionCount = CommonManager.GetFunctionCount(appendFunctions);
34+
}
35+
}
36+
37+
#region FitnessBase Members
38+
39+
protected override double GetFitnessMethod(string program)
40+
{
41+
byte input1 = 0;
42+
int state = 0;
43+
double countBonus = 0;
44+
double penalty = 0;
45+
byte result = 0;
46+
47+
for (int i = 0; i < _trainingCount; i++)
48+
{
49+
switch (i)
50+
{
51+
case 0: input1 = 4; break;
52+
case 1: input1 = 5; break;
53+
case 2: input1 = 8; break;
54+
};
55+
56+
try
57+
{
58+
state = 0;
59+
_console.Clear();
60+
result = 0;
61+
62+
// Run the program.
63+
_bf = new Interpreter(program, () =>
64+
{
65+
if (state == 0)
66+
{
67+
state++;
68+
return input1;
69+
}
70+
else
71+
{
72+
// Not ready for input.
73+
penalty++;
74+
75+
return 0;
76+
}
77+
},
78+
(b) =>
79+
{
80+
if (state == 1)
81+
{
82+
_console.Append(b);
83+
_console.Append(",");
84+
85+
result = b;
86+
state++;
87+
}
88+
else
89+
{
90+
// Not ready for output.
91+
//penalty = 1;
92+
}
93+
});
94+
_bf.Run(_maxIterationCount);
95+
}
96+
catch
97+
{
98+
}
99+
100+
// Order bonus.
101+
if (_console.Length > 0)
102+
{
103+
_output.Append(_console.ToString());
104+
_output.Append("|");
105+
106+
Fitness += 256 - Math.Abs(result - (input1 + input1));
107+
}
108+
109+
// Make the AI wait until a solution is found without the penalty (too many input characters).
110+
Fitness -= penalty;
111+
112+
// Check for solution.
113+
IsFitnessAchieved();
114+
115+
// Bonus for less operations to optimize the code.
116+
countBonus += ((_maxIterationCount - _bf.m_Ticks) / 1000.0);
117+
118+
// Bonus for using functions.
119+
if (_functionCount > 0)
120+
{
121+
for (char functionName = 'a'; functionName < 'a' + _functionCount; functionName++)
122+
{
123+
if (program.Contains(functionName))
124+
{
125+
countBonus += 25;
126+
}
127+
}
128+
}
129+
130+
Ticks += _bf.m_Ticks;
131+
}
132+
133+
if (_fitness != Double.MaxValue)
134+
{
135+
_fitness = Fitness + countBonus;
136+
}
137+
138+
return _fitness;
139+
}
140+
141+
protected override void RunProgramMethod(string program)
142+
{
143+
for (int i = 0; i < 99; i++)
144+
{
145+
try
146+
{
147+
int state = 0;
148+
149+
// Run the program.
150+
Interpreter bf = new Interpreter(program, () =>
151+
{
152+
if (state == 0)
153+
{
154+
state++;
155+
Console.WriteLine();
156+
Console.Write(">: ");
157+
byte b = Byte.Parse(Console.ReadLine());
158+
return b;
159+
}
160+
else if (state == 1)
161+
{
162+
state++;
163+
Console.WriteLine();
164+
Console.Write(">: ");
165+
byte b = Byte.Parse(Console.ReadLine());
166+
return b;
167+
}
168+
else
169+
{
170+
return 0;
171+
}
172+
},
173+
(b) =>
174+
{
175+
Console.Write(b);
176+
});
177+
178+
bf.Run(_maxIterationCount);
179+
}
180+
catch
181+
{
182+
}
183+
}
184+
}
185+
186+
public override string GetConstructorParameters()
187+
{
188+
return _maxIterationCount + ", " + _trainingCount;
189+
}
190+
191+
#endregion
192+
}
193+
}

0 commit comments

Comments
 (0)