Skip to content
Browse files

Added Brainf-ck compiler.

AI now compiles solution to .exe file.
Refactored project to decouple dependencies.
  • Loading branch information...
1 parent da15351 commit a163b515f9ac7ace78dd29b522eb7a80f83ed5ab @primaryobjects committed
Showing with 1,811 additions and 1,394 deletions.
  1. +67 −0 AIProgrammer.Compiler/AIProgrammer.Compiler.csproj
  2. +90 −0 AIProgrammer.Compiler/Brainfuck.cs
  3. +36 −0 AIProgrammer.Compiler/Properties/AssemblyInfo.cs
  4. +79 −0 AIProgrammer.Fitness/AIProgrammer.Fitness.csproj
  5. +88 −88 {AIProgrammer/Fitness → AIProgrammer.Fitness}/Base/FitnessBase.cs
  6. +163 −164 {AIProgrammer/Fitness → AIProgrammer.Fitness}/Concrete/AddCleanFitness.cs
  7. +159 −160 {AIProgrammer/Fitness → AIProgrammer.Fitness}/Concrete/AddFitness.cs
  8. +166 −167 {AIProgrammer/Fitness → AIProgrammer.Fitness}/Concrete/AddToCharFitness.cs
  9. +187 −188 {AIProgrammer/Fitness → AIProgrammer.Fitness}/Concrete/HelloUserFitness.cs
  10. +158 −159 {AIProgrammer/Fitness → AIProgrammer.Fitness}/Concrete/MultiplyFitness.cs
  11. +73 −73 {AIProgrammer/Fitness → AIProgrammer.Fitness}/Concrete/StringFitness.cs
  12. +77 −77 {AIProgrammer/Fitness → AIProgrammer.Fitness}/Concrete/StringOptimizedFitness.cs
  13. +158 −159 {AIProgrammer/Fitness → AIProgrammer.Fitness}/Concrete/SubtractFitness.cs
  14. +36 −0 AIProgrammer.Fitness/Properties/AssemblyInfo.cs
  15. +1 −1 AIProgrammer.GA/GA.cs
  16. +64 −0 AIProgrammer.Managers/AIProgrammer.Managers.csproj
  17. +35 −35 {AIProgrammer/Managers → AIProgrammer.Managers}/CommonManager.cs
  18. +54 −55 {AIProgrammer/Managers → AIProgrammer.Managers}/GAManager.cs
  19. +36 −0 AIProgrammer.Managers/Properties/AssemblyInfo.cs
  20. +1 −1 AIProgrammer.Repository/Concrete/GARepository.cs
  21. +1 −0 AIProgrammer.Types/AIProgrammer.Types.csproj
  22. +44 −51 {AIProgrammer/Fitness → AIProgrammer.Types}/Interface/IFitness.cs
  23. +18 −0 AIProgrammer.sln
  24. +12 −12 AIProgrammer/AIProgrammer.csproj
  25. +8 −4 AIProgrammer/Program.cs
View
67 AIProgrammer.Compiler/AIProgrammer.Compiler.csproj
@@ -0,0 +1,67 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+ <ProjectGuid>{5F99B6A4-5CB2-432B-8018-E2EDFBB957F7}</ProjectGuid>
+ <OutputType>Library</OutputType>
+ <AppDesignerFolder>Properties</AppDesignerFolder>
+ <RootNamespace>AIProgrammer.Compiler</RootNamespace>
+ <AssemblyName>AIProgrammer.Compiler</AssemblyName>
+ <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
+ <FileAlignment>512</FileAlignment>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+ <DebugSymbols>true</DebugSymbols>
+ <DebugType>full</DebugType>
+ <Optimize>false</Optimize>
+ <OutputPath>bin\Debug\</OutputPath>
+ <DefineConstants>DEBUG;TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+ <DebugType>pdbonly</DebugType>
+ <Optimize>true</Optimize>
+ <OutputPath>bin\Release\</OutputPath>
+ <DefineConstants>TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <ItemGroup>
+ <Reference Include="System" />
+ <Reference Include="System.Core" />
+ <Reference Include="System.Xml.Linq" />
+ <Reference Include="System.Data.DataSetExtensions" />
+ <Reference Include="Microsoft.CSharp" />
+ <Reference Include="System.Data" />
+ <Reference Include="System.Xml" />
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="Brainfuck.cs" />
+ <Compile Include="Properties\AssemblyInfo.cs" />
+ </ItemGroup>
+ <ItemGroup>
+ <ProjectReference Include="..\AIProgrammer.Fitness\AIProgrammer.Fitness.csproj">
+ <Project>{2fa2525f-4faa-45a6-8268-4d561f7c46f9}</Project>
+ <Name>AIProgrammer.Fitness</Name>
+ </ProjectReference>
+ <ProjectReference Include="..\AIProgrammer.GA\AIProgrammer.GeneticAlgorithm.csproj">
+ <Project>{8ec97de8-ddfd-437a-b216-91db8d1a3487}</Project>
+ <Name>AIProgrammer.GeneticAlgorithm</Name>
+ </ProjectReference>
+ <ProjectReference Include="..\AIProgrammer.Types\AIProgrammer.Types.csproj">
+ <Project>{7bcc43b1-33a8-44c8-8f4d-752d89672180}</Project>
+ <Name>AIProgrammer.Types</Name>
+ </ProjectReference>
+ </ItemGroup>
+ <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+ <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
+ Other similar extension points exist, see Microsoft.Common.targets.
+ <Target Name="BeforeBuild">
+ </Target>
+ <Target Name="AfterBuild">
+ </Target>
+ -->
+</Project>
View
90 AIProgrammer.Compiler/Brainfuck.cs
@@ -0,0 +1,90 @@
+using AIProgrammer.Types.Interface;
+using AIProgrammer.Fitness;
+using Microsoft.CSharp;
+using System;
+using System.CodeDom.Compiler;
+using System.Collections.Generic;
+using System.Linq;
+using System.Reflection;
+
+namespace AIProgrammer.Compiler
+{
+ /// <summary>
+ /// Usage: Compiler.Compile("++++.", "StringFitness", 2000);
+ /// Output: exe file thats runs the brainfuck code.
+ /// </summary>
+ public static class Brainfuck
+ {
+ /// <summary>
+ /// Compiles brainfuck code into an executable.
+ /// </summary>
+ /// <param name="program">Brainfuck source code</param>
+ /// <param name="pathName">Executable file path</param>
+ /// <param name="fitness">IFitness</param>
+ /// <param name="maxIterations">Max number of instructions that interpreter will execute</param>
+ public static void Compile(string program, string pathName, IFitness fitness, int maxIterations)
+ {
+ Compile(program, pathName, fitness.GetType().Name, maxIterations);
+ }
+
+ /// <summary>
+ /// Compiles brainfuck code into an executable.
+ /// </summary>
+ /// <param name="program">Brainfuck source code</param>
+ /// <param name="pathName">Executable file path</param>
+ /// <param name="fitnessMethod">string (HelloUserFitness, StringOptimizedFitness, etc)</param>
+ /// <param name="maxIterations">Max number of instructions that interpreter will execute</param>
+ /// <param name="includeHeader">True to display the header (Brainfuck .NET Compiler 1.0, Created by ...).</param>
+ public static void Compile(string program, string pathName, string fitnessMethod, int maxIterations, bool includeHeader = true)
+ {
+ string sourceCode = @"
+using System;
+using AIProgrammer.Fitness.Concrete;
+using AIProgrammer.Types.Interface;
+
+class Program {
+ public static void Main(string[] args)
+ {
+ string program = ""[SOURCE]"";
+ IFitness fitness = new [FITNESSMETHOD](null, [MAXITERATIONS], """");
+
+ [HEADER]
+
+ Console.WriteLine(fitness.RunProgram(program));
+
+ Console.WriteLine();
+ Console.Write(""Press any key to continue.."");
+ Console.ReadKey();
+ }
+}";
+
+ CSharpCodeProvider provider = new CSharpCodeProvider(new Dictionary<string, string>() { { "CompilerVersion", "v4.0" } });
+ CompilerParameters parameters = new CompilerParameters(new [] { "mscorlib.dll", "System.Core.dll" }, pathName, true);
+ parameters.GenerateExecutable = true;
+ parameters.ReferencedAssemblies.Add(Assembly.GetAssembly(typeof(AIProgrammer.Fitness.Concrete.StringFitness)).Location);
+ parameters.ReferencedAssemblies.Add(Assembly.GetAssembly(typeof(AIProgrammer.Types.Interface.IFitness)).Location);
+ parameters.ReferencedAssemblies.Add(Assembly.GetAssembly(typeof(AIProgrammer.GeneticAlgorithm.GA)).Location);
+
+ if (includeHeader)
+ {
+ sourceCode = sourceCode.Replace("[HEADER]", @"
+ Console.WriteLine(""Brainfuck .NET Compiler 1.0"");
+ Console.WriteLine(""Created by Kory Becker"");
+ Console.WriteLine(""http://www.primaryobjects.com/kory-becker.aspx"");
+ Console.WriteLine();");
+ }
+ else
+ {
+ sourceCode = sourceCode.Replace("[HEADER]", "");
+ }
+
+ sourceCode = sourceCode.Replace("[SOURCE]", program);
+ sourceCode = sourceCode.Replace("[FITNESSMETHOD]", fitnessMethod);
+ sourceCode = sourceCode.Replace("[MAXITERATIONS]", maxIterations.ToString());
+
+ CompilerResults results = provider.CompileAssemblyFromSource(parameters, sourceCode);
+
+ results.Errors.Cast<CompilerError>().ToList().ForEach(error => Console.WriteLine(error.ErrorText));
+ }
+ }
+}
View
36 AIProgrammer.Compiler/Properties/AssemblyInfo.cs
@@ -0,0 +1,36 @@
+using System.Reflection;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+
+// General Information about an assembly is controlled through the following
+// set of attributes. Change these attribute values to modify the information
+// associated with an assembly.
+[assembly: AssemblyTitle("AIProgrammer.Compiler")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("")]
+[assembly: AssemblyProduct("AIProgrammer.Compiler")]
+[assembly: AssemblyCopyright("Copyright © 2013")]
+[assembly: AssemblyTrademark("")]
+[assembly: AssemblyCulture("")]
+
+// Setting ComVisible to false makes the types in this assembly not visible
+// to COM components. If you need to access a type in this assembly from
+// COM, set the ComVisible attribute to true on that type.
+[assembly: ComVisible(false)]
+
+// The following GUID is for the ID of the typelib if this project is exposed to COM
+[assembly: Guid("75493eac-55db-45d4-b813-f71bce58e5a5")]
+
+// Version information for an assembly consists of the following four values:
+//
+// Major Version
+// Minor Version
+// Build Number
+// Revision
+//
+// You can specify all the values or you can default the Build and Revision Numbers
+// by using the '*' as shown below:
+// [assembly: AssemblyVersion("1.0.*")]
+[assembly: AssemblyVersion("1.0.0.0")]
+[assembly: AssemblyFileVersion("1.0.0.0")]
View
79 AIProgrammer.Fitness/AIProgrammer.Fitness.csproj
@@ -0,0 +1,79 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+ <ProjectGuid>{2FA2525F-4FAA-45A6-8268-4D561F7C46F9}</ProjectGuid>
+ <OutputType>Library</OutputType>
+ <AppDesignerFolder>Properties</AppDesignerFolder>
+ <RootNamespace>AIProgrammer.Fitness</RootNamespace>
+ <AssemblyName>AIProgrammer.Fitness</AssemblyName>
+ <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
+ <FileAlignment>512</FileAlignment>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+ <DebugSymbols>true</DebugSymbols>
+ <DebugType>full</DebugType>
+ <Optimize>false</Optimize>
+ <OutputPath>bin\Debug\</OutputPath>
+ <DefineConstants>DEBUG;TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+ <DebugType>pdbonly</DebugType>
+ <Optimize>true</Optimize>
+ <OutputPath>bin\Release\</OutputPath>
+ <DefineConstants>TRACE</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <ItemGroup>
+ <Reference Include="System" />
+ <Reference Include="System.Core" />
+ <Reference Include="System.Xml.Linq" />
+ <Reference Include="System.Data.DataSetExtensions" />
+ <Reference Include="Microsoft.CSharp" />
+ <Reference Include="System.Data" />
+ <Reference Include="System.Xml" />
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="Base\FitnessBase.cs" />
+ <Compile Include="Concrete\AddCleanFitness.cs" />
+ <Compile Include="Concrete\AddFitness.cs" />
+ <Compile Include="Concrete\AddToCharFitness.cs" />
+ <Compile Include="Concrete\HelloUserFitness.cs" />
+ <Compile Include="Concrete\MultiplyFitness.cs" />
+ <Compile Include="Concrete\StringFitness.cs" />
+ <Compile Include="Concrete\StringOptimizedFitness.cs" />
+ <Compile Include="Concrete\SubtractFitness.cs" />
+ <Compile Include="Properties\AssemblyInfo.cs" />
+ </ItemGroup>
+ <ItemGroup>
+ <ProjectReference Include="..\AIProgrammer.GA\AIProgrammer.GeneticAlgorithm.csproj">
+ <Project>{8ec97de8-ddfd-437a-b216-91db8d1a3487}</Project>
+ <Name>AIProgrammer.GeneticAlgorithm</Name>
+ </ProjectReference>
+ <ProjectReference Include="..\AIProgrammer.Interpreter\AIProgrammer.Interpreter.csproj">
+ <Project>{4f4f1800-be33-4d38-ad95-6835644c076e}</Project>
+ <Name>AIProgrammer.Interpreter</Name>
+ </ProjectReference>
+ <ProjectReference Include="..\AIProgrammer.Managers\AIProgrammer.Managers.csproj">
+ <Project>{06d57f9c-a784-433f-8ff3-0728fecf6f5c}</Project>
+ <Name>AIProgrammer.Managers</Name>
+ </ProjectReference>
+ <ProjectReference Include="..\AIProgrammer.Types\AIProgrammer.Types.csproj">
+ <Project>{7bcc43b1-33a8-44c8-8f4d-752d89672180}</Project>
+ <Name>AIProgrammer.Types</Name>
+ </ProjectReference>
+ </ItemGroup>
+ <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+ <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
+ Other similar extension points exist, see Microsoft.Common.targets.
+ <Target Name="BeforeBuild">
+ </Target>
+ <Target Name="AfterBuild">
+ </Target>
+ -->
+</Project>
View
176 AIProgrammer/Fitness/Base/FitnessBase.cs → AIProgrammer.Fitness/Base/FitnessBase.cs
@@ -1,88 +1,88 @@
-using AIProgrammer.Fitness.Interface;
-using AIProgrammer.GeneticAlgorithm;
-using AIProgrammer.Utilities;
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-
-namespace AIProgrammer.Fitness.Base
-{
- public abstract class FitnessBase : IFitness
- {
- public string Program { get; set; } // Brainfuck source code.
- public string Output { get; set; } // Program execution output.
- public double Fitness { get; set; } // Fitness used for determining solution fitness (ie., true fitness).
- public double TargetFitness { get { return _targetFitness; } } // Target fitness to achieve solution.
- public int Ticks { get; set; } // Number of instructions executed by the best program.
-
- protected double _fitness = 0; // Total fitness to return to genetic algorithm (may be variable, solution is not based upon this value, just the rank).
- protected static double _targetFitness = 0; // Target fitness to achieve. Static so we only evaluate this once across instantiations of the fitness class.
- protected int _maxIterationCount = 2000; // Max iterations a program may run before being killed (prevents infinite loops).
- protected StringBuilder _console = new StringBuilder(); // Used by classes to collect console output.
- protected StringBuilder _output = new StringBuilder(); // Used by classes to collect and concat output for assigning to Output.
- protected Interpreter _bf = null; // Brainfuck interpreter instance
- protected GA _ga; // Shared genetic algorithm instance
-
- public FitnessBase(GA ga, int maxIterationCount)
- {
- _ga = ga;
- _maxIterationCount = maxIterationCount;
- Output = "";
- Program = "";
- }
-
- protected bool IsFitnessAchieved()
- {
- bool result = false;
-
- // Did we find a perfect fitness?
- if (Fitness >= TargetFitness)
- {
- // We're done! Stop the GA algorithm.
- // Note, you can alternatively use the _ga.GAParams.TargetFitness to set a specific fitness to achieve.
- // 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.
- _ga.Stop = true;
-
- // Set this genome as the solution.
- _fitness = Double.MaxValue;
-
- result = true;
- }
-
- return result;
- }
-
- #region IFitness Members
-
- public double GetFitness(double[] weights)
- {
- // Get the resulting Brainfuck program.
- Program = CommonManager.ConvertDoubleArrayToBF(weights);
-
- // Get the fitness.
- double fitness = GetFitnessMethod(Program);
-
- // Get the output.
- if (_output.Length > 0)
- {
- Output = _output.ToString().TrimEnd(',');
- }
-
- return fitness;
- }
-
- public string RunProgram(string program)
- {
- RunProgramMethod(program);
-
- return _console.ToString();
- }
-
- #endregion
-
- public abstract double GetFitnessMethod(string program);
- public abstract void RunProgramMethod(string program);
- }
-}
+using AIProgrammer.Types.Interface;
+using AIProgrammer.GeneticAlgorithm;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using AIProgrammer.Managers;
+
+namespace AIProgrammer.Fitness.Base
+{
+ public abstract class FitnessBase : IFitness
+ {
+ public string Program { get; set; } // Brainfuck source code.
+ public string Output { get; set; } // Program execution output.
+ public double Fitness { get; set; } // Fitness used for determining solution fitness (ie., true fitness).
+ public double TargetFitness { get { return _targetFitness; } } // Target fitness to achieve solution.
+ public int Ticks { get; set; } // Number of instructions executed by the best program.
+
+ protected double _fitness = 0; // Total fitness to return to genetic algorithm (may be variable, solution is not based upon this value, just the rank).
+ protected static double _targetFitness = 0; // Target fitness to achieve. Static so we only evaluate this once across instantiations of the fitness class.
+ protected int _maxIterationCount = 2000; // Max iterations a program may run before being killed (prevents infinite loops).
+ protected StringBuilder _console = new StringBuilder(); // Used by classes to collect console output.
+ protected StringBuilder _output = new StringBuilder(); // Used by classes to collect and concat output for assigning to Output.
+ protected Interpreter _bf = null; // Brainfuck interpreter instance
+ protected GA _ga; // Shared genetic algorithm instance
+
+ public FitnessBase(GA ga, int maxIterationCount)
+ {
+ _ga = ga;
+ _maxIterationCount = maxIterationCount;
+ Output = "";
+ Program = "";
+ }
+
+ protected bool IsFitnessAchieved()
+ {
+ bool result = false;
+
+ // Did we find a perfect fitness?
+ if (Fitness >= TargetFitness)
+ {
+ // We're done! Stop the GA algorithm.
+ // Note, you can alternatively use the _ga.GAParams.TargetFitness to set a specific fitness to achieve.
+ // 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.
+ _ga.Stop = true;
+
+ // Set this genome as the solution.
+ _fitness = Double.MaxValue;
+
+ result = true;
+ }
+
+ return result;
+ }
+
+ #region IFitness Members
+
+ public double GetFitness(double[] weights)
+ {
+ // Get the resulting Brainfuck program.
+ Program = CommonManager.ConvertDoubleArrayToBF(weights);
+
+ // Get the fitness.
+ double fitness = GetFitnessMethod(Program);
+
+ // Get the output.
+ if (_output.Length > 0)
+ {
+ Output = _output.ToString().TrimEnd(',');
+ }
+
+ return fitness;
+ }
+
+ public string RunProgram(string program)
+ {
+ RunProgramMethod(program);
+
+ return _console.ToString();
+ }
+
+ #endregion
+
+ public abstract double GetFitnessMethod(string program);
+ public abstract void RunProgramMethod(string program);
+ }
+}
View
327 ...ammer/Fitness/Concrete/AddCleanFitness.cs → ...ammer.Fitness/Concrete/AddCleanFitness.cs
@@ -1,164 +1,163 @@
-using AIProgrammer.Fitness.Base;
-using AIProgrammer.GeneticAlgorithm;
-using AIProgrammer.Managers;
-using AIProgrammer.Utilities;
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-
-namespace AIProgrammer.Fitness.Concrete
-{
- /// <summary>
- /// Calculates the sum 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).
- /// Only checks the output after the two inputs have been provided, and only considers one output. Ignores the rest.
- /// Note, input is taken in byte value (not ASCII character) so you will probably get different results if you run results on web-based interpreters, as those usually translate inputs into ASCII values.
- /// </summary>
- public class AddCleanFitness : FitnessBase
- {
- private int _trainingCount = 5;
-
- public AddCleanFitness(GA ga, int maxIterationCount, int maxTrainingCount)
- : base(ga, maxIterationCount)
- {
- _trainingCount = maxTrainingCount;
- if (_targetFitness == 0)
- {
- _targetFitness = _trainingCount * 256;
- }
- }
-
- #region FitnessBase Members
-
- public override double GetFitnessMethod(string program)
- {
- byte input1 = 0, input2 = 0;
- int state = 0;
- double countBonus = 0;
-
- for (int i = 0; i < _trainingCount; i++)
- {
- switch (i)
- {
- case 0: input1 = 1; input2 = 2; break;
- case 1: input1 = 3; input2 = 4; break;
- case 2: input1 = 5; input2 = 1; break;
- case 3: input1 = 6; input2 = 2; break;
- case 4: input1 = 3; input2 = 6; break;
- };
-
- try
- {
- state = 0;
- _console.Clear();
-
- // Run the program.
- _bf = new Interpreter(program, () =>
- {
- if (state == 0)
- {
- state++;
- return input1;
- }
- else if (state == 1)
- {
- state++;
- return input2;
- }
- else
- {
- return 0;
- }
- },
- (b) =>
- {
- if (state == 2 && _console.Length == 0)
- {
- _console.Append(b.ToString());
- }
- });
- _bf.Run(_maxIterationCount);
- }
- catch
- {
- }
-
- // Order bonus.
- if (_console.Length > 0)
- {
- _output.Append(_console.ToString());
- _output.Append(", ");
-
- Byte value = Byte.Parse(_console.ToString());
- Fitness += 256 - Math.Abs(value - (input1 + input2));
- }
-
- // Check for solution.
- IsFitnessAchieved();
-
- // Bonus for less operations to optimize the code.
- countBonus += ((_maxIterationCount - _bf.m_Ticks) / 1000.0);
-
- Ticks += _bf.m_Ticks;
- }
-
- if (_fitness != Double.MaxValue)
- {
- _fitness = Fitness + countBonus;
- }
-
- return _fitness;
- }
-
- public override void RunProgramMethod(string program)
- {
- for (int i = 0; i < 99; i++)
- {
- try
- {
- int state = 0;
- bool alreadyDisplayed = false;
-
- // Run the program.
- Interpreter bf = new Interpreter(program, () =>
- {
- if (state == 0)
- {
- state++;
- Console.Write(">: ");
- byte b = Byte.Parse(Console.ReadLine());
- return b;
- }
- else if (state == 1)
- {
- state++;
- Console.Write(">: ");
- byte b = Byte.Parse(Console.ReadLine());
- return b;
- }
- else
- {
- return 0;
- }
- },
- (b) =>
- {
- if (state == 2 && !alreadyDisplayed)
- {
- alreadyDisplayed = true;
- Console.Write(b.ToString());
- }
- });
-
- bf.Run(_maxIterationCount);
- }
- catch
- {
- }
- }
- }
-
- #endregion
- }
-}
+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>
+ /// Calculates the sum 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).
+ /// Only checks the output after the two inputs have been provided, and only considers one output. Ignores the rest.
+ /// Note, input is taken in byte value (not ASCII character) so you will probably get different results if you run results on web-based interpreters, as those usually translate inputs into ASCII values.
+ /// </summary>
+ public class AddCleanFitness : FitnessBase
+ {
+ private int _trainingCount = 5;
+
+ public AddCleanFitness(GA ga, int maxIterationCount, int maxTrainingCount)
+ : base(ga, maxIterationCount)
+ {
+ _trainingCount = maxTrainingCount;
+ if (_targetFitness == 0)
+ {
+ _targetFitness = _trainingCount * 256;
+ }
+ }
+
+ #region FitnessBase Members
+
+ public override double GetFitnessMethod(string program)
+ {
+ byte input1 = 0, input2 = 0;
+ int state = 0;
+ double countBonus = 0;
+
+ for (int i = 0; i < _trainingCount; i++)
+ {
+ switch (i)
+ {
+ case 0: input1 = 1; input2 = 2; break;
+ case 1: input1 = 3; input2 = 4; break;
+ case 2: input1 = 5; input2 = 1; break;
+ case 3: input1 = 6; input2 = 2; break;
+ case 4: input1 = 3; input2 = 6; break;
+ };
+
+ try
+ {
+ state = 0;
+ _console.Clear();
+
+ // Run the program.
+ _bf = new Interpreter(program, () =>
+ {
+ if (state == 0)
+ {
+ state++;
+ return input1;
+ }
+ else if (state == 1)
+ {
+ state++;
+ return input2;
+ }
+ else
+ {
+ return 0;
+ }
+ },
+ (b) =>
+ {
+ if (state == 2 && _console.Length == 0)
+ {
+ _console.Append(b.ToString());
+ }
+ });
+ _bf.Run(_maxIterationCount);
+ }
+ catch
+ {
+ }
+
+ // Order bonus.
+ if (_console.Length > 0)
+ {
+ _output.Append(_console.ToString());
+ _output.Append(", ");
+
+ Byte value = Byte.Parse(_console.ToString());
+ Fitness += 256 - Math.Abs(value - (input1 + input2));
+ }
+
+ // Check for solution.
+ IsFitnessAchieved();
+
+ // Bonus for less operations to optimize the code.
+ countBonus += ((_maxIterationCount - _bf.m_Ticks) / 1000.0);
+
+ Ticks += _bf.m_Ticks;
+ }
+
+ if (_fitness != Double.MaxValue)
+ {
+ _fitness = Fitness + countBonus;
+ }
+
+ return _fitness;
+ }
+
+ public override void RunProgramMethod(string program)
+ {
+ for (int i = 0; i < 99; i++)
+ {
+ try
+ {
+ int state = 0;
+ bool alreadyDisplayed = false;
+
+ // Run the program.
+ Interpreter bf = new Interpreter(program, () =>
+ {
+ if (state == 0)
+ {
+ state++;
+ Console.Write(">: ");
+ byte b = Byte.Parse(Console.ReadLine());
+ return b;
+ }
+ else if (state == 1)
+ {
+ state++;
+ Console.Write(">: ");
+ byte b = Byte.Parse(Console.ReadLine());
+ return b;
+ }
+ else
+ {
+ return 0;
+ }
+ },
+ (b) =>
+ {
+ if (state == 2 && !alreadyDisplayed)
+ {
+ alreadyDisplayed = true;
+ Console.Write(b.ToString());
+ }
+ });
+
+ bf.Run(_maxIterationCount);
+ }
+ catch
+ {
+ }
+ }
+ }
+
+ #endregion
+ }
+}
View
319 AIProgrammer/Fitness/Concrete/AddFitness.cs → AIProgrammer.Fitness/Concrete/AddFitness.cs
@@ -1,160 +1,159 @@
-using AIProgrammer.Fitness.Base;
-using AIProgrammer.GeneticAlgorithm;
-using AIProgrammer.Managers;
-using AIProgrammer.Utilities;
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-
-namespace AIProgrammer.Fitness.Concrete
-{
- /// <summary>
- /// Calculates the sum 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).
- /// Note, input is taken in byte value (not ASCII character) so you will probably get different results if you run results on web-based interpreters, as those usually translate inputs into ASCII values.
- /// </summary>
- public class AddFitness : FitnessBase
- {
- private int _trainingCount = 5;
-
- public AddFitness(GA ga, int maxIterationCount, int maxTrainingCount)
- : base(ga, maxIterationCount)
- {
- _trainingCount = maxTrainingCount;
- if (_targetFitness == 0)
- {
- _targetFitness = _trainingCount * 256;
- }
- }
-
- #region FitnessBase Members
-
- public override double GetFitnessMethod(string program)
- {
- byte input1 = 0, input2 = 0;
- int state = 0;
- double countBonus = 0;
-
- for (int i = 0; i < _trainingCount; i++)
- {
- switch (i)
- {
- case 0: input1 = 1; input2 = 2; break;
- case 1: input1 = 3; input2 = 4; break;
- case 2: input1 = 5; input2 = 1; break;
- case 3: input1 = 6; input2 = 2; break;
- case 4: input1 = 3; input2 = 6; break;
- };
-
- try
- {
- state = 0;
- _console.Clear();
-
- // Run the program.
- _bf = new Interpreter(program, () =>
- {
- if (state == 0)
- {
- state++;
- return input1;
- }
- else if (state == 1)
- {
- state++;
- return input2;
- }
- else
- {
- return 0;
- }
- },
- (b) =>
- {
- _console.Append(b.ToString());
- });
- _bf.Run(_maxIterationCount);
- }
- catch
- {
- }
-
- // Order bonus.
- if (_console.Length > 0)
- {
- _output.Append(_console.ToString());
- _output.Append(",");
-
- int value;
- if (Int32.TryParse(_console.ToString(), out value))
- {
- Fitness += 256 - Math.Abs(value - (input1 + input2));
- }
- }
-
- // Check for solution.
- IsFitnessAchieved();
-
- // Bonus for less operations to optimize the code.
- countBonus += ((_maxIterationCount - _bf.m_Ticks) / 1000.0);
-
- Ticks += _bf.m_Ticks;
- }
-
- if (_fitness != Double.MaxValue)
- {
- _fitness = Fitness + countBonus;
- }
-
- return _fitness;
- }
-
- public override void RunProgramMethod(string program)
- {
- for (int i = 0; i < 99; i++)
- {
- try
- {
- int state = 0;
-
- // Run the program.
- Interpreter bf = new Interpreter(program, () =>
- {
- if (state == 0)
- {
- state++;
- Console.WriteLine();
- Console.Write(">: ");
- byte b = Byte.Parse(Console.ReadLine());
- return b;
- }
- else if (state == 1)
- {
- state++;
- Console.WriteLine();
- Console.Write(">: ");
- byte b = Byte.Parse(Console.ReadLine());
- return b;
- }
- else
- {
- return 0;
- }
- },
- (b) =>
- {
- Console.Write(b.ToString());
- });
-
- bf.Run(_maxIterationCount);
- }
- catch
- {
- }
- }
- }
-
- #endregion
- }
-}
+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>
+ /// Calculates the sum 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).
+ /// Note, input is taken in byte value (not ASCII character) so you will probably get different results if you run results on web-based interpreters, as those usually translate inputs into ASCII values.
+ /// </summary>
+ public class AddFitness : FitnessBase
+ {
+ private int _trainingCount = 5;
+
+ public AddFitness(GA ga, int maxIterationCount, int maxTrainingCount)
+ : base(ga, maxIterationCount)
+ {
+ _trainingCount = maxTrainingCount;
+ if (_targetFitness == 0)
+ {
+ _targetFitness = _trainingCount * 256;
+ }
+ }
+
+ #region FitnessBase Members
+
+ public override double GetFitnessMethod(string program)
+ {
+ byte input1 = 0, input2 = 0;
+ int state = 0;
+ double countBonus = 0;
+
+ for (int i = 0; i < _trainingCount; i++)
+ {
+ switch (i)
+ {
+ case 0: input1 = 1; input2 = 2; break;
+ case 1: input1 = 3; input2 = 4; break;
+ case 2: input1 = 5; input2 = 1; break;
+ case 3: input1 = 6; input2 = 2; break;
+ case 4: input1 = 3; input2 = 6; break;
+ };
+
+ try
+ {
+ state = 0;
+ _console.Clear();
+
+ // Run the program.
+ _bf = new Interpreter(program, () =>
+ {
+ if (state == 0)
+ {
+ state++;
+ return input1;
+ }
+ else if (state == 1)
+ {
+ state++;
+ return input2;
+ }
+ else
+ {
+ return 0;
+ }
+ },
+ (b) =>
+ {
+ _console.Append(b.ToString());
+ });
+ _bf.Run(_maxIterationCount);
+ }
+ catch
+ {
+ }
+
+ // Order bonus.
+ if (_console.Length > 0)
+ {
+ _output.Append(_console.ToString());
+ _output.Append(",");
+
+ int value;
+ if (Int32.TryParse(_console.ToString(), out value))
+ {
+ Fitness += 256 - Math.Abs(value - (input1 + input2));
+ }
+ }
+
+ // Check for solution.
+ IsFitnessAchieved();
+
+ // Bonus for less operations to optimize the code.
+ countBonus += ((_maxIterationCount - _bf.m_Ticks) / 1000.0);
+
+ Ticks += _bf.m_Ticks;
+ }
+
+ if (_fitness != Double.MaxValue)
+ {
+ _fitness = Fitness + countBonus;
+ }
+
+ return _fitness;
+ }
+
+ public override void RunProgramMethod(string program)
+ {
+ for (int i = 0; i < 99; i++)
+ {
+ try
+ {
+ int state = 0;
+
+ // Run the program.
+ Interpreter bf = new Interpreter(program, () =>
+ {
+ if (state == 0)
+ {
+ state++;
+ Console.WriteLine();
+ Console.Write(">: ");
+ byte b = Byte.Parse(Console.ReadLine());
+ return b;
+ }
+ else if (state == 1)
+ {
+ state++;
+ Console.WriteLine();
+ Console.Write(">: ");
+ byte b = Byte.Parse(Console.ReadLine());
+ return b;
+ }
+ else
+ {
+ return 0;
+ }
+ },
+ (b) =>
+ {
+ Console.Write(b.ToString());
+ });
+
+ bf.Run(_maxIterationCount);
+ }
+ catch
+ {
+ }
+ }
+ }
+
+ #endregion
+ }
+}
View
333 ...mmer/Fitness/Concrete/AddToCharFitness.cs → ...mmer.Fitness/Concrete/AddToCharFitness.cs
@@ -1,167 +1,166 @@
-using AIProgrammer.Fitness.Base;
-using AIProgrammer.GeneticAlgorithm;
-using AIProgrammer.Managers;
-using AIProgrammer.Utilities;
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-
-namespace AIProgrammer.Fitness.Concrete
-{
- /// <summary>
- /// Calculates the sum of input integers, assuming input and output are ASCII values (ie., 1 => 49, 2 => 50, 3 => 51).
- /// Note, this currently does not work. It has something to do with the input being ascii values and then trying to do addition on them, to return the result as an ascii-range value (where addition should be done on the byte value, not the ascii value, ie: 49 + 51 = 4 wont work, whereas 1 + 3 = 4 works).
- /// </summary>
- public class AddToCharFitness : FitnessBase
- {
- private int _trainingCount = 5;
-
- public AddToCharFitness(GA ga, int maxIterationCount, int maxTrainingCount)
- : base(ga, maxIterationCount)
- {
- _trainingCount = maxTrainingCount;
- if (_targetFitness == 0)
- {
- _targetFitness = _trainingCount * 256;
- }
- }
-
- #region FitnessBase Members
-
- public override double GetFitnessMethod(string program)
- {
- char input1 = '\0', input2 = '\0';
- int state = 0;
- double countBonus = 0;
-
- for (int i = 0; i < _trainingCount; i++)
- {
- switch (i)
- {
- case 0: input1 = '1'; input2 = '2'; break;
- case 1: input1 = '3'; input2 = '4'; break;
- case 2: input1 = '5'; input2 = '1'; break;
- case 3: input1 = '6'; input2 = '2'; break;
- case 4: input1 = '3'; input2 = '6'; break;
- };
-
- try
- {
- state = 0;
- _console.Clear();
-
- // Run the program.
- _bf = new Interpreter(program, () =>
- {
- if (state == 0)
- {
- state++;
- return (byte)input1;
- }
- else if (state == 1)
- {
- state++;
- return (byte)input2;
- }
- else
- {
- return 0;
- }
- },
- (b) =>
- {
- // b = 2 (char)b => 2 ' ' b.ToString()[0] => 50 '2'
- // When input is char, we're already in the ascii range, so just append the (char)b.
- // When input is byte, we need the ascii version of the value, b.ToString() does that.
- _console.Append((char)b);
- });
- _bf.Run(_maxIterationCount);
- }
- catch
- {
- }
-
- // Order bonus.
- if (_console.Length > 0)
- {
- _output.Append((byte)_console[0]);
- _output.Append(" '");
- _output.Append(_console[0]);
- _output.Append("',");
-
- string _targetString = (Convert.ToInt32(input1.ToString()) + Convert.ToInt32(input2.ToString())).ToString();
- for (int j = 0; j < _targetString.Length; j++)
- {
- if (_console.Length > j)
- {
- // Fitness will add up wrong for targetString values with more than 1 digit (eg., 12 will check the 1 and 2 causing 256 * 2 higher of a fitness score).
- Fitness += 256 - Math.Abs(_console[j] - _targetString[j]);
- }
- }
- }
-
- // Check for solution.
- IsFitnessAchieved();
-
- // Bonus for less operations to optimize the code.
- countBonus += ((_maxIterationCount - _bf.m_Ticks) / 1000.0);
-
- Ticks += _bf.m_Ticks;
- }
-
- if (_fitness != Double.MaxValue)
- {
- _fitness = Fitness + countBonus;
- }
-
- return _fitness;
- }
-
- public override void RunProgramMethod(string program)
- {
- for (int i = 0; i < 99; i++)
- {
- try
- {
- int state = 0;
-
- // Run the program.
- Interpreter bf = new Interpreter(program, () =>
- {
- if (state == 0)
- {
- state++;
- Console.Write(">: ");
- byte b = (byte)Char.Parse(Console.ReadLine());
- return b;
- }
- else if (state == 1)
- {
- state++;
- Console.Write(">: ");
- byte b = (byte)Char.Parse(Console.ReadLine());
- return b;
- }
- else
- {
- return 0;
- }
- },
- (b) =>
- {
- Console.Write((char)b);
- });
-
- bf.Run(_maxIterationCount);
- }
- catch
- {
- }
- }
- }
-
- #endregion
- }
-}
+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>
+ /// Calculates the sum of input integers, assuming input and output are ASCII values (ie., 1 => 49, 2 => 50, 3 => 51).
+ /// Note, this currently does not work. It has something to do with the input being ascii values and then trying to do addition on them, to return the result as an ascii-range value (where addition should be done on the byte value, not the ascii value, ie: 49 + 51 = 4 wont work, whereas 1 + 3 = 4 works).
+ /// </summary>
+ public class AddToCharFitness : FitnessBase
+ {
+ private int _trainingCount = 5;
+
+ public AddToCharFitness(GA ga, int maxIterationCount, int maxTrainingCount)
+ : base(ga, maxIterationCount)
+ {
+ _trainingCount = maxTrainingCount;
+ if (_targetFitness == 0)
+ {
+ _targetFitness = _trainingCount * 256;
+ }
+ }
+
+ #region FitnessBase Members
+
+ public override double GetFitnessMethod(string program)
+ {
+ char input1 = '\0', input2 = '\0';
+ int state = 0;
+ double countBonus = 0;
+
+ for (int i = 0; i < _trainingCount; i++)
+ {
+ switch (i)
+ {
+ case 0: input1 = '1'; input2 = '2'; break;
+ case 1: input1 = '3'; input2 = '4'; break;
+ case 2: input1 = '5'; input2 = '1'; break;
+ case 3: input1 = '6'; input2 = '2'; break;
+ case 4: input1 = '3'; input2 = '6'; break;
+ };
+
+ try
+ {
+ state = 0;
+ _console.Clear();
+
+ // Run the program.
+ _bf = new Interpreter(program, () =>
+ {
+ if (state == 0)
+ {
+ state++;
+ return (byte)input1;
+ }
+ else if (state == 1)
+ {
+ state++;
+ return (byte)input2;
+ }
+ else
+ {
+ return 0;
+ }
+ },
+ (b) =>
+ {
+ // b = 2 (char)b => 2 ' ' b.ToString()[0] => 50 '2'
+ // When input is char, we're already in the ascii range, so just append the (char)b.
+ // When input is byte, we need the ascii version of the value, b.ToString() does that.
+ _console.Append((char)b);
+ });
+ _bf.Run(_maxIterationCount);
+ }
+ catch
+ {
+ }
+
+ // Order bonus.
+ if (_console.Length > 0)
+ {
+ _output.Append((byte)_console[0]);
+ _output.Append(" '");
+ _output.Append(_console[0]);
+ _output.Append("',");
+
+ string _targetString = (Convert.ToInt32(input1.ToString()) + Convert.ToInt32(input2.ToString())).ToString();
+ for (int j = 0; j < _targetString.Length; j++)
+ {
+ if (_console.Length > j)
+ {
+ // Fitness will add up wrong for targetString values with more than 1 digit (eg., 12 will check the 1 and 2 causing 256 * 2 higher of a fitness score).
+ Fitness += 256 - Math.Abs(_console[j] - _targetString[j]);
+ }
+ }
+ }
+
+ // Check for solution.
+ IsFitnessAchieved();
+
+ // Bonus for less operations to optimize the code.
+ countBonus += ((_maxIterationCount - _bf.m_Ticks) / 1000.0);
+
+ Ticks += _bf.m_Ticks;
+ }
+
+ if (_fitness != Double.MaxValue)
+ {
+ _fitness = Fitness + countBonus;
+ }
+
+ return _fitness;
+ }
+
+ public override void RunProgramMethod(string program)
+ {
+ for (int i = 0; i < 99; i++)
+ {
+ try
+ {
+ int state = 0;
+
+ // Run the program.
+ Interpreter bf = new Interpreter(program, () =>
+ {
+ if (state == 0)
+ {
+ state++;
+ Console.Write(">: ");
+ byte b = (byte)Char.Parse(Console.ReadLine());
+ return b;
+ }
+ else if (state == 1)
+ {
+ state++;
+ Console.Write(">: ");
+ byte b = (byte)Char.Parse(Console.ReadLine());
+ return b;
+ }
+ else
+ {
+ return 0;
+ }
+ },
+ (b) =>
+ {
+ Console.Write((char)b);
+ });
+
+ bf.Run(_maxIterationCount);
+ }
+ catch
+ {
+ }
+ }
+ }
+
+ #endregion
+ }
+}
View
375 ...mmer/Fitness/Concrete/HelloUserFitness.cs → ...mmer.Fitness/Concrete/HelloUserFitness.cs
@@ -1,188 +1,187 @@
-using AIProgrammer.Fitness.Base;
-using AIProgrammer.GeneticAlgorithm;
-using AIProgrammer.Managers;
-using AIProgrammer.Utilities;
-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 = 4;
-
- public HelloUserFitness(GA ga, int maxIterationCount, string targetString, int maxTrainingCount = 4)
- : base(ga, maxIterationCount)
- {
- _targetString = targetString;
- _trainingCount = maxTrainingCount;
- if (_targetFitness == 0)
- {
- //_targetFitness = ((_targetString.Length + 1) * 256) + ((_targetString.Length + 2) * 256) + ((_targetString.Length + 3) * 256) + ((_targetString.Length + 4) * 256);
- for (int i=0; i<_trainingCount; i++)
- {
- _targetFitness += (_targetString.Length + (i + 1)) * 256;
- }
- }
- }
-
- #region FitnessBase Members
-
- public 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;
- };
-
- 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;
- }
-
- public 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());
- }
- }
-
- #endregion
- }
-}
+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 = 4;
+
+ public HelloUserFitness(GA ga, int maxIterationCount, string targetString, int maxTrainingCount = 4)
+ : base(ga, maxIterationCount)
+ {
+ _targetString = targetString;
+ _trainingCount = maxTrainingCount;
+ if (_targetFitness == 0)
+ {
+ //_targetFitness = ((_targetString.Length + 1) * 256) + ((_targetString.Length + 2) * 256) + ((_targetString.Length + 3) * 256) + ((_targetString.Length + 4) * 256);
+ for (int i=0; i<_trainingCount; i++)
+ {
+ _targetFitness += (_targetString.Length + (i + 1)) * 256;
+ }
+ }
+ }
+
+ #region FitnessBase Members
+
+ public 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;
+ };
+
+ 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;
+ }
+
+ public 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());
+ }
+ }
+
+ #endregion
+ }
+}
View
317 ...ammer/Fitness/Concrete/MultiplyFitness.cs → ...ammer.Fitness/Concrete/MultiplyFitness.cs
@@ -1,159 +1,158 @@
-using AIProgrammer.Fitness.Base;
-using AIProgrammer.GeneticAlgorithm;
-using AIProgrammer.Managers;
-using AIProgrammer.Utilities;
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-
-namespace AIProgrammer.Fitness.Concrete
-{
- /// <summary>
- /// 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).
- /// </summary>
- public class MultiplyFitness : FitnessBase
- {
- private int _trainingCount = 5;
-
- public MultiplyFitness(GA ga, int maxIterationCount, int maxTrainingCount)
- : base(ga, maxIterationCount)
- {
- _trainingCount = maxTrainingCount;
- if (_targetFitness == 0)
- {
- _targetFitness = _trainingCount * 256;
- }
- }
-
- #region FitnessBase Members
-
- public override double GetFitnessMethod(string program)
- {
- byte input1 = 0, input2 = 0;
- int state = 0;
- double countBonus = 0;
-
- for (int i = 0; i < _trainingCount; i++)
- {
- switch (i)
- {
- case 0: input1 = 2; input2 = 1; break;
- case 1: input1 = 4; input2 = 2; break;
- case 2: input1 = 5; input2 = 3; break;
- case 3: input1 = 6; input2 = 3; break;
- case 4: input1 = 7; input2 = 2; break;
- };
-
- try
- {
- state = 0;
- _console.Clear();
-
- // Run the program.
- _bf = new Interpreter(program, () =>
- {
- if (state == 0)
- {
- state++;
- return input1;
- }
- else if (state == 1)
- {
- state++;
- return input2;
- }
- else
- {
- return 0;
- }
- },
- (b) =>
- {
- _console.Append(b.ToString());
- });
- _bf.Run(_maxIterationCount);
- }
- catch
- {
- }
-
- // Order bonus.
- if (_console.Length > 0)
- {
- _output.Append(_console.ToString());
- _output.Append(",");
-
- int value;
- if (Int32.TryParse(_console.ToString(), out value))
- {
- Fitness += 256 - Math.Abs(value - (input1 * input2));
- }
- }
-
- // Check for solution.
- IsFitnessAchieved();
-
- // Bonus for less operations to optimize the code.
- countBonus += ((_maxIterationCount - _bf.m_Ticks) / 1000.0);
-
- Ticks += _bf.m_Ticks;
- }
-
- if (_fitness != Double.MaxValue)
- {
- _fitness = Fitness + countBonus;
- }
-
- return _fitness;
- }
-
- public override void RunProgramMethod(string program)
- {
- for (int i = 0; i < 99; i++)
- {
- try
- {
- int state = 0;
-
- // Run the program.
- Interpreter bf = new Interpreter(program, () =>
- {
- if (state == 0)
- {
- state++;
- Console.WriteLine();
- Console.Write(">: ");
- byte b = Byte.Parse(Console.ReadLine());
- return b;
- }
- else if (state == 1)
- {
- state++;
- Console.WriteLine();
- Console.Write(">: ");
- byte b = Byte.Parse(Console.ReadLine());
- return b;
- }
- else
- {
- return 0;
- }
- },
- (b) =>
- {
- Console.Write(b.ToString());
- });
-
- bf.Run(_maxIterationCount);
- }
- catch
- {
- }
- }
- }
-
- #endregion
- }
-}
+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>
+ /// 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).
+ /// </summary>
+ public class MultiplyFitness : FitnessBase
+ {
+ private int _trainingCount = 5;
+
+ public MultiplyFitness(GA ga, int maxIterationCount, int maxTrainingCount)
+ : base(ga, maxIterationCount)
+ {
+ _trainingCount = maxTrainingCount;
+ if (_targetFitness == 0)
+ {
+ _targetFitness = _trainingCount * 256;
+ }
+ }
+
+ #region FitnessBase Members
+
+ public override double GetFitnessMethod(string program)
+ {
+ byte input1 = 0, input2 = 0;
+ int state = 0;
+ double countBonus = 0;
+
+ for (int i = 0; i < _trainingCount; i++)
+ {
+ switch (i)
+ {
+ case 0: input1 = 2; input2 = 1; break;
+ case 1: input1 = 4; input2 = 2; break;
+ case 2: input1 = 5; input2 = 3; break;
+ case 3: input1 = 6; input2 = 3; break;
+ case 4: input1 = 7; input2 = 2; break;
+ };
+
+ try
+ {
+ state = 0;
+ _console.Clear();
+
+ // Run the program.
+ _bf = new Interpreter(program, () =>
+ {
+ if (state == 0)
+ {
+ state++;
+ return input1;
+ }
+ else if (state == 1)
+ {
+ state++;
+ return input2;
+ }
+ else
+ {
+ return 0;
+ }
+ },
+ (b) =>
+ {
+ _console.Append(b.ToString());
+ });
+ _bf.Run(_maxIterationCount);
+ }
+ catch
+ {
+ }
+
+ // Order bonus.
+ if (_console.Length > 0)
+ {
+ _output.Append(_console.ToString());
+ _output.Append(",");
+
+ int value;
+ if (Int32.TryParse(_console.ToString(), out value))
+ {
+ Fitness += 256 - Math.Abs(value - (input1 * input2));
+ }
+ }
+
+ // Check for solution.
+ IsFitnessAchieved();
+
+ // Bonus for less operations to optimize the code.
+ countBonus += ((_maxIterationCount - _bf.m_Ticks) / 1000.0);
+
+ Ticks += _bf.m_Ticks;
+ }
+
+ if (_fitness != Double.MaxValue)
+ {
+ _fitness = Fitness + countBonus;
+ }
+
+ return _fitness;
+ }
+
+ public override void RunProgramMethod(string program)
+ {
+ for (int i = 0; i < 99; i++)
+ {
+ try
+ {
+ int state = 0;
+
+ // Run the program.
+ Interpreter bf = new Interpreter(program, () =>
+ {
+ if (state == 0)
+ {
+ state++;
+ Console.WriteLine();
+ Console.Write(">: ");
+ byte b = Byte.Parse(Console.ReadLine());
+ return b;
+ }
+ else if (state == 1)
+ {
+ state++;
+ Console.WriteLine();
+ Console.Write(">: ");
+ byte b = Byte.Parse(Console.ReadLine());
+ return b;
+ }
+ else
+ {
+ return 0;
+ }
+ },
+ (b) =>
+ {
+ Console.Write(b.ToString());
+ });
+
+ bf.Run(_maxIterationCount);
+ }
+ catch
+ {
+ }
+ }
+ }
+
+ #endregion
+ }
+}
View
146 ...grammer/Fitness/Concrete/StringFitness.cs → ...grammer.Fitness/Concrete/StringFitness.cs
@@ -1,73 +1,73 @@
-using AIProgrammer.Fitness.Base;
-using AIProgrammer.GeneticAlgorithm;
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-
-namespace AIProgrammer.Fitness.Concrete
-{
- /// <summary>
- /// Displays a string in the console.
- /// </summary>
- public class StringFitness : FitnessBase
- {
- private string _targetString;
-
- public StringFitness(GA ga, int maxIterationCount, string targetString)
- : base(ga, maxIterationCount)
- {
- _targetString = targetString;
- if (_targetFitness == 0)
- {
- _targetFitness = _targetString.Length * 256;
- }
- }
-
- #region FitnessBase Members
-
- public override double GetFitnessMethod(string program)
- {
- // Run the source code.
- Output = RunProgram(program);
-
- // Order bonus.
- for (int i = 0; i < _targetString.Length; i++)
- {
- if (_console.Length > i)
- {
- Fitness += 256 - Math.Abs(_console[i] - _targetString[i]);
- }
- }
-
- _fitness += Fitness;
-
- // Check for solution.
- IsFitnessAchieved();
-
- Ticks = _bf.m_Ticks;
-
- return _fitness;
- }
-
- public override void RunProgramMethod(string program)
- {
- try
- {
- // Run the program.
- _bf = new Interpreter(program, null, (b) =>
- {
- _console.Append((char)b);
- });
-
- _bf.Run(_maxIterationCount);
- }
- catch
- {
- }
- }
-
- #endregion
- }
-}
+using AIProgrammer.Fitness.Base;
+using AIProgrammer.GeneticAlgorithm;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace AIProgrammer.Fitness.Concrete
+{
+ /// <summary>
+ /// Displays a string in the console.
+ /// </summary>
+ public class StringFitness : FitnessBase
+ {
+ private string _targetString;
+
+ public StringFitness(GA ga, int maxIterationCount, string targetString)
+ : base(ga, maxIterationCount)
+ {
+ _targetString = targetString;
+ if (_targetFitness == 0)
+ {
+ _targetFitness = _targetString.Length * 256;
+ }
+ }
+
+ #region FitnessBase Members
+
+ public override double GetFitnessMethod(string program)
+ {
+ // Run the source code.
+ Output = RunProgram(program);
+
+ // Order bonus.
+ for (int i = 0; i < _targetString.Length; i++)
+ {
+ if (_console.Length > i)
+ {
+ Fitness += 256 - Math.Abs(_console[i] - _targetString[i]);
+ }
+ }
+
+ _fitness += Fitness;
+
+ // Check for solution.
+ IsFitnessAchieved();
+
+ Ticks = _bf.m_Ticks;
+
+ return _fitness;
+ }
+
+ public override void RunProgramMethod(string program)
+ {
+ try
+ {
+ // Run the program.
+ _bf = new Interpreter(program, null, (b) =>
+ {
+ _console.Append((char)b);
+ });
+
+ _bf.Run(_maxIterationCount);
+ }
+ catch
+ {
+ }
+ }
+
+ #endregion
+ }
+}
View
154 ...itness/Concrete/StringOptimizedFitness.cs → ...itness/Concrete/StringOptimizedFitness.cs
@@ -1,77 +1,77 @@
-using AIProgrammer.Fitness.Base;
-using AIProgrammer.GeneticAlgorithm;
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-
-namespace AIProgrammer.Fitness.Concrete
-{
- /// <summary>
- /// Displays a string in the console, with minimum number of instructions (optimized code).
- /// </summary>
- public class StringOptimizedFitness : FitnessBase
- {
- private string _targetString;
-
- public StringOptimizedFitness(GA ga, int maxIterationCount, string targetString)
- : base(ga, maxIterationCount)
- {
- _targetString = targetString;
- if (_targetFitness == 0)
- {
- _targetFitness = _targetString.Length * 256;
- }
- }
-
- #region FitnessBase Members
-
- public override double GetFitnessMethod(string program)
- {
- // Run the source code.
- Output = RunProgram(program);
-
- // Order bonus.
- for (int i = 0; i < _targetString.Length; i++)
- {
- if (_console.Length > i)
- {
- Fitness += 256 - Math.Abs(_console[i] - _targetString[i]);
- }
- }
-
- _fitness += Fitness;
-
- // Check for solution.
- if (!IsFitnessAchieved())
- {
- // Bonus for less operations to optimize the code.
- _fitness += ((_maxIterationCount - _bf.m_Ticks) / 20.0);
- }
-
- Ticks = _bf.m_Ticks;
-
- return _fitness;
- }
-
- public override void RunProgramMethod(string program)
- {
- try
- {
- // Run the program.
- _bf = new Interpreter(program, null, (b) =>
- {
- _console.Append((char)b);
- });
-
- _bf.Run(_maxIterationCount);
- }
- catch
- {
- }
- }
-
- #endregion
- }
-}
+using AIProgrammer.Fitness.Base;
+using AIProgrammer.GeneticAlgorithm;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace AIProgrammer.Fitness.Concrete
+{
+ /// <summary>
+ /// Displays a string in the console, with minimum number of instructions (optimized code).
+ /// </summary>
+ public class StringOptimizedFitness : FitnessBase
+ {
+ private string _targetString;
+
+ public StringOptimizedFitness(GA ga, int maxIterationCount, string targetString)
+ : base(ga, maxIterationCount)
+ {
+ _targetString = targetString;
+ if (_targetFitness == 0)
+ {
+ _targetFitness = _targetString.Length * 256;
+ }
+ }
+
+ #region FitnessBase Members
+
+ public override double GetFitnessMethod(string program)
+ {
+ // Run the source code.
+ Output = RunProgram(program);
+
+ // Order bonus.
+ for (int i = 0; i < _targetString.Length; i++)
+ {
+ if (_console.Length > i)
+ {
+ Fitness += 256 - Math.Abs(_console[i] - _targetString[i]);
+ }
+ }
+
+ _fitness += Fitness;
+
+ // Check for solution.
+ if (!IsFitnessAchieved())
+ {
+ // Bonus for less operations to optimize the code.
+ _fitness += ((_maxIterationCount - _bf.m_Ticks) / 20.0);
+ }
+
+ Ticks = _bf.m_Ticks;
+
+ return _fitness;
+ }
+
+ public override void RunProgramMethod(string program)
+ {
+ try
+ {
+ // Run the program.
+ _bf = new Interpreter(program, null, (b) =>
+ {
+ _console.Append((char)b);
+ });
+
+ _bf.Run(_maxIterationCount);
+ }
+ catch
+ {
+ }
+ }
+
+ #endregion
+ }
+}
View
317 ...ammer/Fitness/Concrete/SubtractFitness.cs → ...ammer.Fitness/Concrete/SubtractFitness.cs
@@ -1,159 +1,158 @@
-using AIProgrammer.Fitness.Base;
-using AIProgrammer.GeneticAlgorithm;
-using AIProgrammer.Managers;
-using AIProgrammer.Utilities;
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-
-namespace AIProgrammer.Fitness.Concrete
-{
- /// <summary>
- /// Calculates the subtraction 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).
- /// </summary>
- public class SubtractFitness : FitnessBase
- {
- private int _trainingCount = 5;
-
- public SubtractFitness(GA ga, int maxIterationCount, int maxTrainingCount)
- : base(ga, maxIterationCount)
- {
- _trainingCount = maxTrainingCount;
- if (_targetFitness == 0)
- {
- _targetFitness = _trainingCount * 256;
- }
- }
-
- #region FitnessBase Members
-
- public override double GetFitnessMethod(string program)
- {
- byte input1 = 0, input2 = 0;
- int state = 0;
- double countBonus = 0;
-
- for (int i = 0; i < _trainingCount; i++)
- {
- switch (i)
- {
- case 0: input1 = 9; input2 = 8; break;
- case 1: input1 = 6; input2 = 3; break;
- case 2: input1 = 4; input2 = 2; break;
- case 3: input1 = 5; input2 = 1; break;
- case 4: input1 = 7; input2 = 2; break;
- };
-
- try
- {
- state = 0;
- _console.Clear();
-
- // Run the program.
- _bf = new Interpreter(program, () =>
- {
- if (state == 0)
- {
- state++;
- return input1;
- }