Porting GeneticSharp to .NET Core

The GeneticSharp v2.0.0-rc1 is out.

In this version GeneticSharp starts to support .NET Standard 2.0 and .NET Framework 4.6.2.

Porting to

Introduction

Sometime ago I started to porting GeneticSharp to .NET Core and today, after more than 40 hours of work, I finally finish it.

Although GeneticSharp born as a multi-platform library that ran in any OS supported by .NET Framework and Mono, convert it to .NET Core was a desired thing, because .NET Core is where .NET ecosystem is targeting.

Release candidate

This 2.0.0-rc1 was already published to nuget.org, but it’s marked as a pre-realease package. I will kept it as a RC until get sure that there is no issue with the porting and until the Unity3d remove the “experimental” status to its support to .NET Standard 2.0.

If you are in .NET Core or in .NET Framework 4.6.2+ project, please try the the 2.0.0-rc1 version.

Only GeneticSharp:

install-package GeneticSharp -Version 2.0.0-rc1

GeneticSharp and extensions (TSP, AutoConfig, Bitmap equality, Equality equation, Equation solver, Function builder, etc):

install-package GeneticSharp.Extensions -Version 2.0.0-rc1

If you’re still in a .NET Framework version lower than 4.6.2 project use the 1.2.0 version.

install-package GeneticSharp -Version 1.2.0

Porting

Warmup

To start the porting I followed the good pratices describe on links below:

Based on those reads, I decided to support the .NET Standard 2.0 (netstandard2.0) and the .NET Framework 4.6.2 (net462). This meaning that GeneticSharp can be used in .NET Standard and .NET Framework projects, with no differences.

Another thing that I considered was the Unity3d support, so based on this post Unity 2018.1 - .NET Standard 2.0 and .NET 4.6 support Unity is already using a experimental support to netstandard2.0 and net462 libraries.

Domain

The domain part of the library, the GeneticSharp.Domain library was the easy part to port, if I cleary remember there was no change it all, besides the changes on .csproj.

Extensions

The extensions from GeneticSharp.Extensions project needed some more work, because the System.Drawing do not exists directly in .NET Standard 2.0. That was resolved using the System.Drawing.Common NuGet package.

The NCalc library used on FunctionBuilderFitness was updated to use the NCalc.NetCore version.

Threading

One of the most tricky porting was the GeneticSharp.Infrastructure.Threading, because it used the external library SmartThreadPool and that one was not supporting .NET Core at that time. I decided to implement the parallel task executor using the .NET built-in ThreadPool class, this implementation was done on ParallelTaskExecutor and its use can be tested on ParallelTaskExecutorTest.

GtkApp

GeneticSharp sample app (GeneticSharp.Runner.GtkApp) was built using Gtk# 2, but only version 3 was ported to .NET Core, and there are huge breaking changes between these two Gtk# versions, so for a while I’ll keeping the sample app still running only on .NET Framework/Mono.

I hope in a near future we can built a cross-platform sample app, maybe using Xamarin Forms for iOS, Android, macOS and UWP. This would be great, but perform this task during the to .NET Core supporting is far from the scope right now.

The links below are some readings that I did while looking for some GTK# alternatives:

Unit tests

Unit tests projects were using the Rhino Mocks as the mocking library, but Rhino Mocks was not supporting (maybe never) .NET Core, so it was replaced by the amazing NSubstitute.

Code Coverage

As the time I wrote this, there was no unanimity about what is the best cross-platform tool to collect code-coverage in .NET Core. There are some tools, but each one has some pros and cons:

  • VS 2017 Code Coverage Can be easily used on Windows platform directly on VS 2017 or in command line:
    dotnet test --collect:"Code Coverage"
    
  • altcover Instrumenting coverage tool for .net/.net core and Mono, emitting NCover or OpenCover format output.

  • MiniCover Minimalist Code Coverage Tool for .NET Core.

  • coverlet Cross platform code coverage library for .NET Core.

I guess the most promising tool is the coverlet, but I’ll waiting until the end of GeneticSharp 2.0.0 release candidate period to choose one.

Problems, readings, tricks and hacks

Of course that a porting always come with some problems that will you need to figure out by doing some research and solve them, most of time, using some trick and hacks. These one was some of I’ve done:

.NET SDK 2.0 projects targeting .NET 4.x on Mono

As GeneticSharp need to support .NET Framework 4.6.2, but the msbuild does not know where to looking for the framework assemblies in macOS and Linux, so I found thi .NET SDK issue comment issue comment:

When compiling .NET SDK 2.0 projects targeting .NET 4.x on Mono using ‘dotnet build’ you have to teach MSBuild where the Mono copy of the reference asssemblies is.

This msbuild file GeneticSharp.dotnet-core.targets was created and referenced in all .csproj.

dotnet test “Failed to initialize client proxy”

Sometimes dotnet test gives the error:

Starting test execution, please wait...
Failed to initialize client proxy: could not connect to test process.

Test Run Aborted

Delete the “obj” and “bin” folders from unit test project will “fix” the problem.

Breaking changes

  • The GeneticSharp NuGet package was split into two packages:
    • GeneticSharp: only GeneticSharp.Domain.dll and GeneticSharp.Infrastructure.Framework.dll
    • GeneticSharp.Extensions: depends on GenetiSharp.Package and add the GeneticSharp.Extensions.dll.
  • SmartThreadPoolTaskExecutor does not exists anymore. You should use ParallelTaskExecutor class (not disposable).
  • The projects are not signed anymore:
  • In the ConsoleApp the EquationSolverSampleController was removed until the Jace library suportt .NET Core.

Reads during the porting

Some readings I did during the whole process, maybe can be useful to someone porting a library to .NET Core too.

Now I’m planning to create a GeneticSharp’s sample using Unity3d’s new beta features, like the new ECS (Entity Component System), C# Job System and the Burst compiler.

Let’s evolve!

Icons made by Freepik from www.flaticon.com is licensed by CC 3.0 BY
Written on March 28, 2018