Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: CI

on:
push:
branches: [master, main]
pull_request:

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-dotnet@v4
with:
dotnet-version: 8.0.x
- run: dotnet build --configuration Release
- run: dotnet test --configuration Release --no-build
- run: dotnet pack GeneCodeCS/GeneCodeCS.csproj --configuration Release --no-build --output artifacts
- uses: actions/upload-artifact@v4
with:
name: package
path: artifacts/*.nupkg
10 changes: 4 additions & 6 deletions GeneCodeCS.Test/BotGeneratorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,11 @@ namespace GeneCodeCS.Test
public class BotGeneratorTest
{
[TestMethod]
[DeploymentItem("GeneCodeCS.dll")]
public void BotGeneratorCreateBotNameCreatesExpectedNameGivenParameters1() {
BotGeneratorCreateBotNameCreatesExpectedNameGivenParameters1Helper<BaseBot>();
}

[TestMethod]
[DeploymentItem("GeneCodeCS.dll")]
public void BotGeneratorCreateBotNameCreatesExpectedNameGivenParameters2() {
BotGeneratorCreateBotNameCreatesExpectedNameGivenParameters2Helper<BaseBot>();
}
Expand All @@ -42,19 +40,19 @@ public void BotGeneratorCreateRandomBotReturnsNonNullBotTree() {
BotGeneratorCreateRandomBotReturnsNonNullBotTreeHelper<TestBot>();
}

private static void BotGeneratorCreateBotNameCreatesExpectedNameGivenParameters1Helper<TBot>() {
private static void BotGeneratorCreateBotNameCreatesExpectedNameGivenParameters1Helper<TBot>() where TBot : BaseBot {
const int generation = 0;
const int populus = 0;
const string expected = "BaseBot_Gen0_Bot0";
var actual = Reproduction_Accessor<TBot>.CreateBotName(generation, populus);
var actual = Reproduction<TBot>.CreateBotName(generation, populus);
Assert.AreEqual(expected, actual);
}

private static void BotGeneratorCreateBotNameCreatesExpectedNameGivenParameters2Helper<TBot>() {
private static void BotGeneratorCreateBotNameCreatesExpectedNameGivenParameters2Helper<TBot>() where TBot : BaseBot {
const int generation = 4;
const int populus = 5;
const string expected = "BaseBot_Gen4_Bot5";
var actual = Reproduction_Accessor<TBot>.CreateBotName(generation, populus);
var actual = Reproduction<TBot>.CreateBotName(generation, populus);
Assert.AreEqual(expected, actual);
}

Expand Down
89 changes: 14 additions & 75 deletions GeneCodeCS.Test/GeneCodeCS.Test.csproj
Original file line number Diff line number Diff line change
@@ -1,82 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>
</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{70CC27C4-4073-4C9F-8822-A7B3313038BC}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>GeneCodeCS.Test</RootNamespace>
<AssemblyName>GeneCodeCS.Test</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<LangVersion>latest</LangVersion>
<IsPackable>false</IsPackable>
<NoWarn>$(NoWarn);CS8981</NoWarn>
</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="Common.Logging">
<HintPath>..\packages\Common.Logging.2.1.2\lib\net40\Common.Logging.dll</HintPath>
</Reference>
<Reference Include="Microsoft.CSharp" />
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
<Reference Include="System" />
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
</ItemGroup>
<ItemGroup>
<CodeAnalysisDependentAssemblyPaths Condition=" '$(VS100COMNTOOLS)' != '' " Include="$(VS100COMNTOOLS)..\IDE\PrivateAssemblies">
<Visible>False</Visible>
</CodeAnalysisDependentAssemblyPaths>
</ItemGroup>
<ItemGroup>
<Compile Include="BotGeneratorTest.cs" />
<Compile Include="BranchExpressionTests.cs" />
<Compile Include="BranchGeneTests.cs" />
<Compile Include="ChromosomeTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SequenceExpressionTests.cs" />
<Compile Include="SequenceGeneTests.cs" />
<Compile Include="TerminalExpressionTests.cs" />
<Compile Include="Helpers\TestBot.cs" />
<Compile Include="Helpers\TestParameter.cs" />
<Compile Include="TerminalGeneTests.cs" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\GeneCodeCS\GeneCodeCS.csproj">
<Project>{0A7CCAFD-4FB8-4739-A260-7B384E093469}</Project>
<Name>GeneCodeCS</Name>
</ProjectReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="MSTest.TestAdapter" Version="3.6.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.6.1" />
</ItemGroup>

<ItemGroup>
<None Include="packages.config" />
<Shadow Include="Test References\GeneCodeCS.accessor" />
<ProjectReference Include="..\GeneCodeCS\GeneCodeCS.csproj" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\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>

</Project>
36 changes: 36 additions & 0 deletions GeneCodeCS.Test/Helpers/CounterBot.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;

namespace GeneCodeCS.Test.Helpers
{
/// <summary>
/// A tiny bot for end-to-end tests: it has to reach a target number by choosing between increments and decrements.
/// </summary>
public class CounterBot : BaseBot
{
private const int Target = 10;
private int _steps;

public int Counter { get; private set; }

public void Increment(StepParameter step) { Counter += step.Value; }

public void Decrement(StepParameter step) { Counter -= step.Value; }

public bool BelowTarget() { return Counter < Target; }

protected override bool NotFinished() { return ++_steps <= 20; }

protected override void Evaluate() { Fitness = 1000 - Math.Abs(Target - Counter); }

public class StepParameter : Genetics.IParameter<int>
{
private readonly int _value;

public StepParameter(Random random) { _value = random.Next(1, 4); }

public StepParameter(int value) { _value = value; }

public int Value { get { return _value; } }
}
}
}
42 changes: 42 additions & 0 deletions GeneCodeCS.Test/PopulationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Linq;
using GeneCodeCS.Test.Helpers;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace GeneCodeCS.Test
{
[TestClass]
public class PopulationTests
{
[TestMethod]
public void SimulateGenerations_compiles_runs_and_returns_best_bots_without_writing_files() {
var before = System.IO.Directory.GetFiles(System.IO.Directory.GetCurrentDirectory(), "*.cs");
var population = new Population<CounterBot>();

var best = population.SimulateGenerations<object>(null, generationLimit: 5, botsPerGeneration: 20, maxTreeDepth: 3);

Assert.IsTrue(best.Count > 0);
Assert.IsTrue(best.All(b => b.Bot != null));
Assert.IsTrue(best.All(b => b.Bot.Fitness > int.MinValue), "Bots should run without throwing.");
CollectionAssert.AreEquivalent(before, System.IO.Directory.GetFiles(System.IO.Directory.GetCurrentDirectory(), "*.cs"));
}

[TestMethod]
public void SimulateGenerations_evolves_a_good_solution_to_the_counter_problem() {
var population = new Population<CounterBot>();

var best = population.SimulateGenerations<object>(null, generationLimit: 30, botsPerGeneration: 60, maxTreeDepth: 3,
fitnessThreshold: 999);

Assert.AreEqual(1000, best.First().Bot.Fitness);
}

[TestMethod]
public void SimulateIndividuals_returns_a_bot_exceeding_the_threshold() {
var population = new Population<CounterBot>();

var bot = population.SimulateIndividuals<object>(null, maxTreeDepth: 3, fitnessThreshold: 995);

Assert.IsTrue(bot.Bot.Fitness > 995);
}
}
}
35 changes: 0 additions & 35 deletions GeneCodeCS.Test/Properties/AssemblyInfo.cs

This file was deleted.

40 changes: 40 additions & 0 deletions GeneCodeCS.Test/ReproductionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using GeneCodeCS.Test.Helpers;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace GeneCodeCS.Test
{
[TestClass]
public class ReproductionTests
{
[TestMethod]
public void Rates_accept_values_from_0_to_100() {
var target = new Reproduction<CounterBot> { CrossoverRate = 0, MutationRate = 100, RandomBotPercentage = 50 };

Assert.AreEqual(0, target.CrossoverRate);
Assert.AreEqual(100, target.MutationRate);
Assert.AreEqual(50, target.RandomBotPercentage);
}

[TestMethod]
public void CrossoverRate_rejects_out_of_range_values() {
var target = new Reproduction<CounterBot>();
Assert.ThrowsException<ArgumentOutOfRangeException>(() => target.CrossoverRate = 101);
Assert.ThrowsException<ArgumentOutOfRangeException>(() => target.CrossoverRate = -1);
}

[TestMethod]
public void MutationRate_rejects_out_of_range_values() {
var target = new Reproduction<CounterBot>();
Assert.ThrowsException<ArgumentOutOfRangeException>(() => target.MutationRate = 101);
Assert.ThrowsException<ArgumentOutOfRangeException>(() => target.MutationRate = -1);
}

[TestMethod]
public void RandomBotPercentage_rejects_out_of_range_values() {
var target = new Reproduction<CounterBot>();
Assert.ThrowsException<ArgumentOutOfRangeException>(() => target.RandomBotPercentage = 101);
Assert.ThrowsException<ArgumentOutOfRangeException>(() => target.RandomBotPercentage = -1);
}
}
}
2 changes: 0 additions & 2 deletions GeneCodeCS.Test/Test References/GeneCodeCS.accessor

This file was deleted.

4 changes: 0 additions & 4 deletions GeneCodeCS.Test/packages.config

This file was deleted.

66 changes: 28 additions & 38 deletions GeneCodeCS.sln
Original file line number Diff line number Diff line change
@@ -1,38 +1,28 @@

Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GeneCodeCS", "GeneCodeCS\GeneCodeCS.csproj", "{0A7CCAFD-4FB8-4739-A260-7B384E093469}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GeneCodeCS.Test", "GeneCodeCS.Test\GeneCodeCS.Test.csproj", "{70CC27C4-4073-4C9F-8822-A7B3313038BC}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{DA875293-158A-47BB-AA48-1D64F41B6DAA}"
ProjectSection(SolutionItems) = preProject
GeneCodeCS.vsmdi = GeneCodeCS.vsmdi
LICENSE = LICENSE
Local.testsettings = Local.testsettings
README.md = README.md
TraceAndTestImpact.testsettings = TraceAndTestImpact.testsettings
EndProjectSection
EndProject
Global
GlobalSection(TestCaseManagementSettings) = postSolution
CategoryFile = GeneCodeCS.vsmdi
EndGlobalSection
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{0A7CCAFD-4FB8-4739-A260-7B384E093469}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0A7CCAFD-4FB8-4739-A260-7B384E093469}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0A7CCAFD-4FB8-4739-A260-7B384E093469}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0A7CCAFD-4FB8-4739-A260-7B384E093469}.Release|Any CPU.Build.0 = Release|Any CPU
{70CC27C4-4073-4C9F-8822-A7B3313038BC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{70CC27C4-4073-4C9F-8822-A7B3313038BC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{70CC27C4-4073-4C9F-8822-A7B3313038BC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{70CC27C4-4073-4C9F-8822-A7B3313038BC}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GeneCodeCS", "GeneCodeCS\GeneCodeCS.csproj", "{C5674E3C-5E3B-47E2-98A0-79F40D6E3C53}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GeneCodeCS.Test", "GeneCodeCS.Test\GeneCodeCS.Test.csproj", "{9A4746C8-5664-418A-B09F-356D38BEA082}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{C5674E3C-5E3B-47E2-98A0-79F40D6E3C53}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C5674E3C-5E3B-47E2-98A0-79F40D6E3C53}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C5674E3C-5E3B-47E2-98A0-79F40D6E3C53}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C5674E3C-5E3B-47E2-98A0-79F40D6E3C53}.Release|Any CPU.Build.0 = Release|Any CPU
{9A4746C8-5664-418A-B09F-356D38BEA082}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9A4746C8-5664-418A-B09F-356D38BEA082}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9A4746C8-5664-418A-B09F-356D38BEA082}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9A4746C8-5664-418A-B09F-356D38BEA082}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
Loading
Loading