Skip to content

Commit 0201895

Browse files
Refactored function code in main program to be easier to update with new functions.
1 parent a2d3590 commit 0201895

File tree

15 files changed

+396
-146
lines changed

15 files changed

+396
-146
lines changed

Diff for: AIProgrammer.Fitness/Base/FitnessBase.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ protected bool IsFitnessAchieved()
4949
{
5050
// We're done! Stop the GA algorithm.
5151
// Note, you can alternatively use the _ga.GAParams.TargetFitness to set a specific fitness to achieve.
52-
// 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.
53-
_ga.Stop = true;
52+
// 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.
53+
_ga.Stop = true;
5454

5555
// Set this genome as the solution.
5656
_fitness = Double.MaxValue;

Diff for: AIProgrammer.Functions/AIProgrammer.Functions.csproj

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{D1CE7F4D-5142-4FC9-A35B-89F0EDAA2936}</ProjectGuid>
8+
<OutputType>Library</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>AIProgrammer.Functions</RootNamespace>
11+
<AssemblyName>AIProgrammer.Functions</AssemblyName>
12+
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
</PropertyGroup>
15+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
16+
<DebugSymbols>true</DebugSymbols>
17+
<DebugType>full</DebugType>
18+
<Optimize>false</Optimize>
19+
<OutputPath>bin\Debug\</OutputPath>
20+
<DefineConstants>DEBUG;TRACE</DefineConstants>
21+
<ErrorReport>prompt</ErrorReport>
22+
<WarningLevel>4</WarningLevel>
23+
</PropertyGroup>
24+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
25+
<DebugType>pdbonly</DebugType>
26+
<Optimize>true</Optimize>
27+
<OutputPath>bin\Release\</OutputPath>
28+
<DefineConstants>TRACE</DefineConstants>
29+
<ErrorReport>prompt</ErrorReport>
30+
<WarningLevel>4</WarningLevel>
31+
</PropertyGroup>
32+
<ItemGroup>
33+
<Reference Include="System" />
34+
<Reference Include="System.Core" />
35+
<Reference Include="System.Xml.Linq" />
36+
<Reference Include="System.Data.DataSetExtensions" />
37+
<Reference Include="Microsoft.CSharp" />
38+
<Reference Include="System.Data" />
39+
<Reference Include="System.Xml" />
40+
</ItemGroup>
41+
<ItemGroup>
42+
<Compile Include="Concrete\StringFunction.cs" />
43+
<Compile Include="Properties\AssemblyInfo.cs" />
44+
</ItemGroup>
45+
<ItemGroup>
46+
<ProjectReference Include="..\AIProgrammer.Fitness\AIProgrammer.Fitness.csproj">
47+
<Project>{2fa2525f-4faa-45a6-8268-4d561f7c46f9}</Project>
48+
<Name>AIProgrammer.Fitness</Name>
49+
</ProjectReference>
50+
<ProjectReference Include="..\AIProgrammer.GA\AIProgrammer.GeneticAlgorithm.csproj">
51+
<Project>{8ec97de8-ddfd-437a-b216-91db8d1a3487}</Project>
52+
<Name>AIProgrammer.GeneticAlgorithm</Name>
53+
</ProjectReference>
54+
<ProjectReference Include="..\AIProgrammer.Managers\AIProgrammer.Managers.csproj">
55+
<Project>{06d57f9c-a784-433f-8ff3-0728fecf6f5c}</Project>
56+
<Name>AIProgrammer.Managers</Name>
57+
</ProjectReference>
58+
<ProjectReference Include="..\AIProgrammer.Types\AIProgrammer.Types.csproj">
59+
<Project>{7bcc43b1-33a8-44c8-8f4d-752d89672180}</Project>
60+
<Name>AIProgrammer.Types</Name>
61+
</ProjectReference>
62+
</ItemGroup>
63+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
64+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
65+
Other similar extension points exist, see Microsoft.Common.targets.
66+
<Target Name="BeforeBuild">
67+
</Target>
68+
<Target Name="AfterBuild">
69+
</Target>
70+
-->
71+
</Project>

Diff for: AIProgrammer.Functions/Concrete/StringFunction.cs

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
using AIProgrammer.Fitness.Concrete;
2+
using AIProgrammer.GeneticAlgorithm;
3+
using AIProgrammer.Managers;
4+
using AIProgrammer.Types;
5+
using AIProgrammer.Types.Interface;
6+
using System;
7+
using System.Collections.Generic;
8+
using System.Linq;
9+
using System.Text;
10+
using System.Threading.Tasks;
11+
12+
namespace AIProgrammer.Functions.Concrete
13+
{
14+
public class StringFunction : IFunction
15+
{
16+
private Func<IFitness> _getFitnessFunc;
17+
private GAStatus _bestStatus;
18+
private double _crossoverRate;
19+
private double _mutationRate;
20+
private int _genomeSize;
21+
private GAFunction _fitnessFunc;
22+
private OnGeneration _generationFunc;
23+
private TargetParams _targetParams;
24+
25+
public StringFunction(Func<IFitness> getFitnessMethod, GAStatus bestStatus, GAFunction fitnessFunc, OnGeneration generationFunc, double crossoverRate, double mutationRate, int genomeSize, TargetParams targetParams)
26+
{
27+
_getFitnessFunc = getFitnessMethod;
28+
_bestStatus = bestStatus;
29+
_crossoverRate = crossoverRate;
30+
_mutationRate = mutationRate;
31+
_genomeSize = genomeSize;
32+
_fitnessFunc = fitnessFunc;
33+
_generationFunc = generationFunc;
34+
_targetParams = targetParams;
35+
}
36+
37+
#region IFunction Members
38+
39+
public string Generate(IGeneticAlgorithm ga)
40+
{
41+
IFitness myFitness;
42+
string originalTargetString = _targetParams.TargetString;
43+
string program;
44+
string appendCode = "!";
45+
46+
// Generate functions.
47+
string[] parts = _targetParams.TargetString.Split(new char[] { ' ' });
48+
for (int i = 0; i < parts.Length - 1; i++)
49+
{
50+
_targetParams.TargetString = parts[i];
51+
if (i < parts.Length - 1)
52+
{
53+
_targetParams.TargetString = _targetParams.TargetString + " ";
54+
}
55+
56+
// Get the target fitness for this method.
57+
myFitness = _getFitnessFunc();
58+
59+
_targetParams.TargetFitness = myFitness.TargetFitness;
60+
61+
// Run the genetic algorithm and get the best brain.
62+
program = GAManager.Run(ga, _fitnessFunc, _generationFunc);
63+
64+
// For functions, replace ! with % return command.
65+
appendCode += "&" + program.Replace('!', '%') + "%";
66+
67+
// Reset the target fitness.
68+
((StringStrictFitness)myFitness).ResetTargetFitness();
69+
_bestStatus.Fitness = 0;
70+
_bestStatus.TrueFitness = 0;
71+
_bestStatus.Output = "";
72+
_bestStatus.LastChangeDate = DateTime.Now;
73+
_bestStatus.Program = "";
74+
_bestStatus.Ticks = 0;
75+
}
76+
77+
// Restore target string.
78+
_targetParams.TargetString = originalTargetString;
79+
80+
return appendCode;
81+
}
82+
83+
#endregion
84+
}
85+
}

Diff for: AIProgrammer.Functions/Properties/AssemblyInfo.cs

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("AIProgrammer.Functions")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("AIProgrammer.Functions")]
13+
[assembly: AssemblyCopyright("Copyright © 2013")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("a42d0aed-3cc1-42dd-b232-4bb1c3d45cd2")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]

Diff for: AIProgrammer.GA/GA.cs

+7-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
using System.Threading;
3333
using System.Threading.Tasks;
3434
using System.Timers;
35+
using AIProgrammer.Types.Interface;
3536
#endregion
3637

3738
namespace AIProgrammer.GeneticAlgorithm
@@ -42,7 +43,7 @@ namespace AIProgrammer.GeneticAlgorithm
4243
/// <summary>
4344
/// Genetic Algorithm class
4445
/// </summary>
45-
public class GA
46+
public class GA : IGeneticAlgorithm
4647
{
4748
public GAParams GAParams { get; set; }
4849
public bool Stop { get; set; }
@@ -110,6 +111,11 @@ public void Go(bool resume = false)
110111
GAParams.FitnessTable = new List<double>();
111112
GAParams.ThisGeneration = new List<Genome>(GAParams.Generations);
112113
GAParams.NextGeneration = new List<Genome>(GAParams.Generations);
114+
GAParams.TotalFitness = 0;
115+
GAParams.TargetFitness = 0;
116+
GAParams.TargetFitnessCount = 0;
117+
GAParams.CurrentGeneration = 0;
118+
Stop = false;
113119

114120
CreateGenomes();
115121
RankPopulation();

Diff for: AIProgrammer.Managers/AIProgrammer.Managers.csproj

+59-58
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,66 @@
1-
<?xml version="1.0" encoding="utf-8"?>
2-
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3-
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4-
<PropertyGroup>
5-
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6-
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7-
<ProjectGuid>{06D57F9C-A784-433F-8FF3-0728FECF6F5C}</ProjectGuid>
8-
<OutputType>Library</OutputType>
9-
<AppDesignerFolder>Properties</AppDesignerFolder>
10-
<RootNamespace>AIProgrammer.Managers</RootNamespace>
11-
<AssemblyName>AIProgrammer.Managers</AssemblyName>
12-
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
13-
<FileAlignment>512</FileAlignment>
14-
</PropertyGroup>
15-
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
16-
<DebugSymbols>true</DebugSymbols>
17-
<DebugType>full</DebugType>
18-
<Optimize>false</Optimize>
19-
<OutputPath>bin\Debug\</OutputPath>
20-
<DefineConstants>DEBUG;TRACE</DefineConstants>
21-
<ErrorReport>prompt</ErrorReport>
22-
<WarningLevel>4</WarningLevel>
23-
</PropertyGroup>
24-
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
25-
<DebugType>pdbonly</DebugType>
26-
<Optimize>true</Optimize>
27-
<OutputPath>bin\Release\</OutputPath>
28-
<DefineConstants>TRACE</DefineConstants>
29-
<ErrorReport>prompt</ErrorReport>
30-
<WarningLevel>4</WarningLevel>
31-
</PropertyGroup>
32-
<ItemGroup>
33-
<Reference Include="System" />
34-
<Reference Include="System.Configuration" />
35-
<Reference Include="System.Core" />
36-
<Reference Include="System.Xml.Linq" />
37-
<Reference Include="System.Data.DataSetExtensions" />
38-
<Reference Include="Microsoft.CSharp" />
39-
<Reference Include="System.Data" />
40-
<Reference Include="System.Xml" />
41-
</ItemGroup>
42-
<ItemGroup>
43-
<Compile Include="CommonManager.cs" />
44-
<Compile Include="GAManager.cs" />
45-
<Compile Include="Properties\AssemblyInfo.cs" />
46-
</ItemGroup>
47-
<ItemGroup>
48-
<ProjectReference Include="..\AIProgrammer.GA\AIProgrammer.GeneticAlgorithm.csproj">
49-
<Project>{8ec97de8-ddfd-437a-b216-91db8d1a3487}</Project>
50-
<Name>AIProgrammer.GeneticAlgorithm</Name>
51-
</ProjectReference>
52-
<ProjectReference Include="..\AIProgrammer.Types\AIProgrammer.Types.csproj">
53-
<Project>{7bcc43b1-33a8-44c8-8f4d-752d89672180}</Project>
54-
<Name>AIProgrammer.Types</Name>
55-
</ProjectReference>
56-
</ItemGroup>
57-
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{06D57F9C-A784-433F-8FF3-0728FECF6F5C}</ProjectGuid>
8+
<OutputType>Library</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>AIProgrammer.Managers</RootNamespace>
11+
<AssemblyName>AIProgrammer.Managers</AssemblyName>
12+
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
</PropertyGroup>
15+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
16+
<DebugSymbols>true</DebugSymbols>
17+
<DebugType>full</DebugType>
18+
<Optimize>false</Optimize>
19+
<OutputPath>bin\Debug\</OutputPath>
20+
<DefineConstants>DEBUG;TRACE</DefineConstants>
21+
<ErrorReport>prompt</ErrorReport>
22+
<WarningLevel>4</WarningLevel>
23+
</PropertyGroup>
24+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
25+
<DebugType>pdbonly</DebugType>
26+
<Optimize>true</Optimize>
27+
<OutputPath>bin\Release\</OutputPath>
28+
<DefineConstants>TRACE</DefineConstants>
29+
<ErrorReport>prompt</ErrorReport>
30+
<WarningLevel>4</WarningLevel>
31+
</PropertyGroup>
32+
<ItemGroup>
33+
<Reference Include="System" />
34+
<Reference Include="System.Configuration" />
35+
<Reference Include="System.Core" />
36+
<Reference Include="System.Xml.Linq" />
37+
<Reference Include="System.Data.DataSetExtensions" />
38+
<Reference Include="Microsoft.CSharp" />
39+
<Reference Include="System.Data" />
40+
<Reference Include="System.Xml" />
41+
</ItemGroup>
42+
<ItemGroup>
43+
<Compile Include="CommonManager.cs" />
44+
<Compile Include="GAManager.cs" />
45+
<Compile Include="Properties\AssemblyInfo.cs" />
46+
</ItemGroup>
47+
<ItemGroup>
48+
<ProjectReference Include="..\AIProgrammer.GA\AIProgrammer.GeneticAlgorithm.csproj">
49+
<Project>{8ec97de8-ddfd-437a-b216-91db8d1a3487}</Project>
50+
<Name>AIProgrammer.GeneticAlgorithm</Name>
51+
</ProjectReference>
52+
<ProjectReference Include="..\AIProgrammer.Types\AIProgrammer.Types.csproj">
53+
<Project>{7bcc43b1-33a8-44c8-8f4d-752d89672180}</Project>
54+
<Name>AIProgrammer.Types</Name>
55+
</ProjectReference>
56+
</ItemGroup>
57+
<ItemGroup />
58+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
5859
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
5960
Other similar extension points exist, see Microsoft.Common.targets.
6061
<Target Name="BeforeBuild">
6162
</Target>
6263
<Target Name="AfterBuild">
6364
</Target>
64-
-->
65+
-->
6566
</Project>

Diff for: AIProgrammer.Managers/GAManager.cs

+12-9
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
using AIProgrammer.GeneticAlgorithm;
2-
using System;
3-
using System.Collections.Generic;
4-
using System.IO;
5-
using System.Linq;
6-
using System.Text;
1+
using AIProgrammer.GeneticAlgorithm;
2+
using AIProgrammer.Types.Interface;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.IO;
6+
using System.Linq;
7+
using System.Text;
78
using System.Threading.Tasks;
89

910
namespace AIProgrammer.Managers
@@ -13,9 +14,11 @@ public static class GAManager
1314
/// <summary>
1415
/// Setup the genetic algorithm and run it.
1516
/// </summary>
16-
/// <returns>Best brain's output source code</returns>
17-
public static string Run(GA ga, GAFunction fitnessFunc, OnGeneration generationFunc, Action setupFunc = null, bool resume = false)
18-
{
17+
/// <returns>Best brain's output source code</returns>
18+
public static string Run(IGeneticAlgorithm iga, GAFunction fitnessFunc, OnGeneration generationFunc, Action setupFunc = null, bool resume = false)
19+
{
20+
GA ga = (GA)iga;
21+
1922
if (!resume)
2023
{
2124
if (setupFunc != null)

0 commit comments

Comments
 (0)