Skip to content

Commit bcf9699

Browse files
authored
Merge pull request #1 from EddPorter/ccr-827a2223-jxfdy0
Modernise to .NET 8, compile bots in memory, add GitHub Pages site
2 parents afd643b + efe8313 commit bcf9699

27 files changed

Lines changed: 1105 additions & 753 deletions

‎.github/workflows/ci.yml‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [master, main]
6+
pull_request:
7+
8+
jobs:
9+
build:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v4
13+
- uses: actions/setup-dotnet@v4
14+
with:
15+
dotnet-version: 8.0.x
16+
- run: dotnet build --configuration Release
17+
- run: dotnet test --configuration Release --no-build
18+
- run: dotnet pack GeneCodeCS/GeneCodeCS.csproj --configuration Release --no-build --output artifacts
19+
- uses: actions/upload-artifact@v4
20+
with:
21+
name: package
22+
path: artifacts/*.nupkg

‎GeneCodeCS.Test/BotGeneratorTest.cs‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,11 @@ namespace GeneCodeCS.Test
2626
public class BotGeneratorTest
2727
{
2828
[TestMethod]
29-
[DeploymentItem("GeneCodeCS.dll")]
3029
public void BotGeneratorCreateBotNameCreatesExpectedNameGivenParameters1() {
3130
BotGeneratorCreateBotNameCreatesExpectedNameGivenParameters1Helper<BaseBot>();
3231
}
3332

3433
[TestMethod]
35-
[DeploymentItem("GeneCodeCS.dll")]
3634
public void BotGeneratorCreateBotNameCreatesExpectedNameGivenParameters2() {
3735
BotGeneratorCreateBotNameCreatesExpectedNameGivenParameters2Helper<BaseBot>();
3836
}
@@ -42,19 +40,19 @@ public void BotGeneratorCreateRandomBotReturnsNonNullBotTree() {
4240
BotGeneratorCreateRandomBotReturnsNonNullBotTreeHelper<TestBot>();
4341
}
4442

45-
private static void BotGeneratorCreateBotNameCreatesExpectedNameGivenParameters1Helper<TBot>() {
43+
private static void BotGeneratorCreateBotNameCreatesExpectedNameGivenParameters1Helper<TBot>() where TBot : BaseBot {
4644
const int generation = 0;
4745
const int populus = 0;
4846
const string expected = "BaseBot_Gen0_Bot0";
49-
var actual = Reproduction_Accessor<TBot>.CreateBotName(generation, populus);
47+
var actual = Reproduction<TBot>.CreateBotName(generation, populus);
5048
Assert.AreEqual(expected, actual);
5149
}
5250

53-
private static void BotGeneratorCreateBotNameCreatesExpectedNameGivenParameters2Helper<TBot>() {
51+
private static void BotGeneratorCreateBotNameCreatesExpectedNameGivenParameters2Helper<TBot>() where TBot : BaseBot {
5452
const int generation = 4;
5553
const int populus = 5;
5654
const string expected = "BaseBot_Gen4_Bot5";
57-
var actual = Reproduction_Accessor<TBot>.CreateBotName(generation, populus);
55+
var actual = Reproduction<TBot>.CreateBotName(generation, populus);
5856
Assert.AreEqual(expected, actual);
5957
}
6058

Lines changed: 14 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,21 @@
1-
<?xml version="1.0" encoding="utf-8"?>
2-
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
33
<PropertyGroup>
4-
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5-
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
6-
<ProductVersion>
7-
</ProductVersion>
8-
<SchemaVersion>2.0</SchemaVersion>
9-
<ProjectGuid>{70CC27C4-4073-4C9F-8822-A7B3313038BC}</ProjectGuid>
10-
<OutputType>Library</OutputType>
11-
<AppDesignerFolder>Properties</AppDesignerFolder>
4+
<TargetFramework>net8.0</TargetFramework>
125
<RootNamespace>GeneCodeCS.Test</RootNamespace>
13-
<AssemblyName>GeneCodeCS.Test</AssemblyName>
14-
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
15-
<FileAlignment>512</FileAlignment>
16-
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
6+
<LangVersion>latest</LangVersion>
7+
<IsPackable>false</IsPackable>
8+
<NoWarn>$(NoWarn);CS8981</NoWarn>
179
</PropertyGroup>
18-
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
19-
<DebugSymbols>true</DebugSymbols>
20-
<DebugType>full</DebugType>
21-
<Optimize>false</Optimize>
22-
<OutputPath>bin\Debug\</OutputPath>
23-
<DefineConstants>DEBUG;TRACE</DefineConstants>
24-
<ErrorReport>prompt</ErrorReport>
25-
<WarningLevel>4</WarningLevel>
26-
</PropertyGroup>
27-
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
28-
<DebugType>pdbonly</DebugType>
29-
<Optimize>true</Optimize>
30-
<OutputPath>bin\Release\</OutputPath>
31-
<DefineConstants>TRACE</DefineConstants>
32-
<ErrorReport>prompt</ErrorReport>
33-
<WarningLevel>4</WarningLevel>
34-
</PropertyGroup>
35-
<ItemGroup>
36-
<Reference Include="Common.Logging">
37-
<HintPath>..\packages\Common.Logging.2.1.2\lib\net40\Common.Logging.dll</HintPath>
38-
</Reference>
39-
<Reference Include="Microsoft.CSharp" />
40-
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
41-
<Reference Include="System" />
42-
<Reference Include="System.Core">
43-
<RequiredTargetFramework>3.5</RequiredTargetFramework>
44-
</Reference>
45-
</ItemGroup>
46-
<ItemGroup>
47-
<CodeAnalysisDependentAssemblyPaths Condition=" '$(VS100COMNTOOLS)' != '' " Include="$(VS100COMNTOOLS)..\IDE\PrivateAssemblies">
48-
<Visible>False</Visible>
49-
</CodeAnalysisDependentAssemblyPaths>
50-
</ItemGroup>
51-
<ItemGroup>
52-
<Compile Include="BotGeneratorTest.cs" />
53-
<Compile Include="BranchExpressionTests.cs" />
54-
<Compile Include="BranchGeneTests.cs" />
55-
<Compile Include="ChromosomeTests.cs" />
56-
<Compile Include="Properties\AssemblyInfo.cs" />
57-
<Compile Include="SequenceExpressionTests.cs" />
58-
<Compile Include="SequenceGeneTests.cs" />
59-
<Compile Include="TerminalExpressionTests.cs" />
60-
<Compile Include="Helpers\TestBot.cs" />
61-
<Compile Include="Helpers\TestParameter.cs" />
62-
<Compile Include="TerminalGeneTests.cs" />
63-
</ItemGroup>
10+
6411
<ItemGroup>
65-
<ProjectReference Include="..\GeneCodeCS\GeneCodeCS.csproj">
66-
<Project>{0A7CCAFD-4FB8-4739-A260-7B384E093469}</Project>
67-
<Name>GeneCodeCS</Name>
68-
</ProjectReference>
12+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
13+
<PackageReference Include="MSTest.TestAdapter" Version="3.6.1" />
14+
<PackageReference Include="MSTest.TestFramework" Version="3.6.1" />
6915
</ItemGroup>
16+
7017
<ItemGroup>
71-
<None Include="packages.config" />
72-
<Shadow Include="Test References\GeneCodeCS.accessor" />
18+
<ProjectReference Include="..\GeneCodeCS\GeneCodeCS.csproj" />
7319
</ItemGroup>
74-
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
75-
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
76-
Other similar extension points exist, see Microsoft.Common.targets.
77-
<Target Name="BeforeBuild">
78-
</Target>
79-
<Target Name="AfterBuild">
80-
</Target>
81-
-->
82-
</Project>
20+
21+
</Project>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
3+
namespace GeneCodeCS.Test.Helpers
4+
{
5+
/// <summary>
6+
/// A tiny bot for end-to-end tests: it has to reach a target number by choosing between increments and decrements.
7+
/// </summary>
8+
public class CounterBot : BaseBot
9+
{
10+
private const int Target = 10;
11+
private int _steps;
12+
13+
public int Counter { get; private set; }
14+
15+
public void Increment(StepParameter step) { Counter += step.Value; }
16+
17+
public void Decrement(StepParameter step) { Counter -= step.Value; }
18+
19+
public bool BelowTarget() { return Counter < Target; }
20+
21+
protected override bool NotFinished() { return ++_steps <= 20; }
22+
23+
protected override void Evaluate() { Fitness = 1000 - Math.Abs(Target - Counter); }
24+
25+
public class StepParameter : Genetics.IParameter<int>
26+
{
27+
private readonly int _value;
28+
29+
public StepParameter(Random random) { _value = random.Next(1, 4); }
30+
31+
public StepParameter(int value) { _value = value; }
32+
33+
public int Value { get { return _value; } }
34+
}
35+
}
36+
}

‎GeneCodeCS.Test/PopulationTests.cs‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System.Linq;
2+
using GeneCodeCS.Test.Helpers;
3+
using Microsoft.VisualStudio.TestTools.UnitTesting;
4+
5+
namespace GeneCodeCS.Test
6+
{
7+
[TestClass]
8+
public class PopulationTests
9+
{
10+
[TestMethod]
11+
public void SimulateGenerations_compiles_runs_and_returns_best_bots_without_writing_files() {
12+
var before = System.IO.Directory.GetFiles(System.IO.Directory.GetCurrentDirectory(), "*.cs");
13+
var population = new Population<CounterBot>();
14+
15+
var best = population.SimulateGenerations<object>(null, generationLimit: 5, botsPerGeneration: 20, maxTreeDepth: 3);
16+
17+
Assert.IsTrue(best.Count > 0);
18+
Assert.IsTrue(best.All(b => b.Bot != null));
19+
Assert.IsTrue(best.All(b => b.Bot.Fitness > int.MinValue), "Bots should run without throwing.");
20+
CollectionAssert.AreEquivalent(before, System.IO.Directory.GetFiles(System.IO.Directory.GetCurrentDirectory(), "*.cs"));
21+
}
22+
23+
[TestMethod]
24+
public void SimulateGenerations_evolves_a_good_solution_to_the_counter_problem() {
25+
var population = new Population<CounterBot>();
26+
27+
var best = population.SimulateGenerations<object>(null, generationLimit: 30, botsPerGeneration: 60, maxTreeDepth: 3,
28+
fitnessThreshold: 999);
29+
30+
Assert.AreEqual(1000, best.First().Bot.Fitness);
31+
}
32+
33+
[TestMethod]
34+
public void SimulateIndividuals_returns_a_bot_exceeding_the_threshold() {
35+
var population = new Population<CounterBot>();
36+
37+
var bot = population.SimulateIndividuals<object>(null, maxTreeDepth: 3, fitnessThreshold: 995);
38+
39+
Assert.IsTrue(bot.Bot.Fitness > 995);
40+
}
41+
}
42+
}

‎GeneCodeCS.Test/Properties/AssemblyInfo.cs‎

Lines changed: 0 additions & 35 deletions
This file was deleted.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using GeneCodeCS.Test.Helpers;
3+
using Microsoft.VisualStudio.TestTools.UnitTesting;
4+
5+
namespace GeneCodeCS.Test
6+
{
7+
[TestClass]
8+
public class ReproductionTests
9+
{
10+
[TestMethod]
11+
public void Rates_accept_values_from_0_to_100() {
12+
var target = new Reproduction<CounterBot> { CrossoverRate = 0, MutationRate = 100, RandomBotPercentage = 50 };
13+
14+
Assert.AreEqual(0, target.CrossoverRate);
15+
Assert.AreEqual(100, target.MutationRate);
16+
Assert.AreEqual(50, target.RandomBotPercentage);
17+
}
18+
19+
[TestMethod]
20+
public void CrossoverRate_rejects_out_of_range_values() {
21+
var target = new Reproduction<CounterBot>();
22+
Assert.ThrowsException<ArgumentOutOfRangeException>(() => target.CrossoverRate = 101);
23+
Assert.ThrowsException<ArgumentOutOfRangeException>(() => target.CrossoverRate = -1);
24+
}
25+
26+
[TestMethod]
27+
public void MutationRate_rejects_out_of_range_values() {
28+
var target = new Reproduction<CounterBot>();
29+
Assert.ThrowsException<ArgumentOutOfRangeException>(() => target.MutationRate = 101);
30+
Assert.ThrowsException<ArgumentOutOfRangeException>(() => target.MutationRate = -1);
31+
}
32+
33+
[TestMethod]
34+
public void RandomBotPercentage_rejects_out_of_range_values() {
35+
var target = new Reproduction<CounterBot>();
36+
Assert.ThrowsException<ArgumentOutOfRangeException>(() => target.RandomBotPercentage = 101);
37+
Assert.ThrowsException<ArgumentOutOfRangeException>(() => target.RandomBotPercentage = -1);
38+
}
39+
}
40+
}

‎GeneCodeCS.Test/Test References/GeneCodeCS.accessor‎

Lines changed: 0 additions & 2 deletions
This file was deleted.

‎GeneCodeCS.Test/packages.config‎

Lines changed: 0 additions & 4 deletions
This file was deleted.

‎GeneCodeCS.sln‎

Lines changed: 28 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,28 @@
1-
2-
Microsoft Visual Studio Solution File, Format Version 11.00
3-
# Visual Studio 2010
4-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GeneCodeCS", "GeneCodeCS\GeneCodeCS.csproj", "{0A7CCAFD-4FB8-4739-A260-7B384E093469}"
5-
EndProject
6-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GeneCodeCS.Test", "GeneCodeCS.Test\GeneCodeCS.Test.csproj", "{70CC27C4-4073-4C9F-8822-A7B3313038BC}"
7-
EndProject
8-
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{DA875293-158A-47BB-AA48-1D64F41B6DAA}"
9-
ProjectSection(SolutionItems) = preProject
10-
GeneCodeCS.vsmdi = GeneCodeCS.vsmdi
11-
LICENSE = LICENSE
12-
Local.testsettings = Local.testsettings
13-
README.md = README.md
14-
TraceAndTestImpact.testsettings = TraceAndTestImpact.testsettings
15-
EndProjectSection
16-
EndProject
17-
Global
18-
GlobalSection(TestCaseManagementSettings) = postSolution
19-
CategoryFile = GeneCodeCS.vsmdi
20-
EndGlobalSection
21-
GlobalSection(SolutionConfigurationPlatforms) = preSolution
22-
Debug|Any CPU = Debug|Any CPU
23-
Release|Any CPU = Release|Any CPU
24-
EndGlobalSection
25-
GlobalSection(ProjectConfigurationPlatforms) = postSolution
26-
{0A7CCAFD-4FB8-4739-A260-7B384E093469}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27-
{0A7CCAFD-4FB8-4739-A260-7B384E093469}.Debug|Any CPU.Build.0 = Debug|Any CPU
28-
{0A7CCAFD-4FB8-4739-A260-7B384E093469}.Release|Any CPU.ActiveCfg = Release|Any CPU
29-
{0A7CCAFD-4FB8-4739-A260-7B384E093469}.Release|Any CPU.Build.0 = Release|Any CPU
30-
{70CC27C4-4073-4C9F-8822-A7B3313038BC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
31-
{70CC27C4-4073-4C9F-8822-A7B3313038BC}.Debug|Any CPU.Build.0 = Debug|Any CPU
32-
{70CC27C4-4073-4C9F-8822-A7B3313038BC}.Release|Any CPU.ActiveCfg = Release|Any CPU
33-
{70CC27C4-4073-4C9F-8822-A7B3313038BC}.Release|Any CPU.Build.0 = Release|Any CPU
34-
EndGlobalSection
35-
GlobalSection(SolutionProperties) = preSolution
36-
HideSolutionNode = FALSE
37-
EndGlobalSection
38-
EndGlobal
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.0.31903.59
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GeneCodeCS", "GeneCodeCS\GeneCodeCS.csproj", "{C5674E3C-5E3B-47E2-98A0-79F40D6E3C53}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GeneCodeCS.Test", "GeneCodeCS.Test\GeneCodeCS.Test.csproj", "{9A4746C8-5664-418A-B09F-356D38BEA082}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Release|Any CPU = Release|Any CPU
14+
EndGlobalSection
15+
GlobalSection(SolutionProperties) = preSolution
16+
HideSolutionNode = FALSE
17+
EndGlobalSection
18+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
19+
{C5674E3C-5E3B-47E2-98A0-79F40D6E3C53}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
20+
{C5674E3C-5E3B-47E2-98A0-79F40D6E3C53}.Debug|Any CPU.Build.0 = Debug|Any CPU
21+
{C5674E3C-5E3B-47E2-98A0-79F40D6E3C53}.Release|Any CPU.ActiveCfg = Release|Any CPU
22+
{C5674E3C-5E3B-47E2-98A0-79F40D6E3C53}.Release|Any CPU.Build.0 = Release|Any CPU
23+
{9A4746C8-5664-418A-B09F-356D38BEA082}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
24+
{9A4746C8-5664-418A-B09F-356D38BEA082}.Debug|Any CPU.Build.0 = Debug|Any CPU
25+
{9A4746C8-5664-418A-B09F-356D38BEA082}.Release|Any CPU.ActiveCfg = Release|Any CPU
26+
{9A4746C8-5664-418A-B09F-356D38BEA082}.Release|Any CPU.Build.0 = Release|Any CPU
27+
EndGlobalSection
28+
EndGlobal

0 commit comments

Comments
 (0)