-
-
Notifications
You must be signed in to change notification settings - Fork 344
Added IAsyncFitness and IMutation.MinChromosomeLength #144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mikasoukhov
wants to merge
3
commits into
giacomelli:master
Choose a base branch
from
StockSharp:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/GeneticSharp.Domain.UnitTests/Mutations/MinChromosomeLengthTest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| using NUnit.Framework; | ||
|
|
||
| namespace GeneticSharp.Domain.UnitTests.Mutations | ||
| { | ||
| [TestFixture()] | ||
| [Category("Mutations")] | ||
| public class MinChromosomeLengthTest | ||
| { | ||
| [Test()] | ||
| public void MinChromosomeLength_SequenceMutations_Three() | ||
| { | ||
| Assert.AreEqual(3, new DisplacementMutation().MinChromosomeLength); | ||
| Assert.AreEqual(3, new InsertionMutation().MinChromosomeLength); | ||
| Assert.AreEqual(3, new PartialShuffleMutation().MinChromosomeLength); | ||
| Assert.AreEqual(3, new ReverseSequenceMutation().MinChromosomeLength); | ||
| } | ||
|
|
||
| [Test()] | ||
| public void MinChromosomeLength_NonSequenceMutations_Zero() | ||
| { | ||
| Assert.AreEqual(0, new TworsMutation().MinChromosomeLength); | ||
| Assert.AreEqual(0, new UniformMutation().MinChromosomeLength); | ||
| } | ||
| } | ||
| } |
41 changes: 41 additions & 0 deletions
41
src/GeneticSharp.Domain.UnitTests/Populations/AsyncFitnessStub.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| using System; | ||
| using System.Linq; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace GeneticSharp.Domain.UnitTests | ||
| { | ||
| public class AsyncFitnessStub : IFitness, IAsyncFitness | ||
| { | ||
| public AsyncFitnessStub() | ||
| { | ||
| ParallelSleep = 500; | ||
| } | ||
|
|
||
| public bool SupportsParallel { get; set; } | ||
| public int ParallelSleep { get; set; } | ||
|
|
||
| public double Evaluate(IChromosome chromosome) | ||
| { | ||
| throw new NotSupportedException("Use EvaluateAsync instead."); | ||
| } | ||
|
|
||
| public async Task<double> EvaluateAsync(IChromosome chromosome, CancellationToken cancellationToken) | ||
| { | ||
| if (SupportsParallel) | ||
| { | ||
| await Task.Delay(ParallelSleep, cancellationToken); | ||
| } | ||
|
|
||
| var genes = chromosome.GetGenes(); | ||
| double f = genes.Sum(g => (int)g.Value) / 20f; | ||
|
|
||
| if (f > 1) | ||
| { | ||
| f = 0; | ||
| } | ||
|
|
||
| return f; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| using System; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace GeneticSharp | ||
| { | ||
| /// <summary> | ||
| /// An IAsyncFitness implementation that defers the fitness evaluation to an async Func. | ||
| /// </summary> | ||
| public class AsyncFuncFitness : IFitness, IAsyncFitness | ||
| { | ||
| private readonly Func<IChromosome, CancellationToken, Task<double>> m_func; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="AsyncFuncFitness"/> class. | ||
| /// </summary> | ||
| /// <param name="func">The async fitness evaluation Func.</param> | ||
| public AsyncFuncFitness(Func<IChromosome, CancellationToken, Task<double>> func) | ||
| { | ||
| ExceptionHelper.ThrowIfNull("func", func); | ||
| m_func = func; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Evaluate the specified chromosome. | ||
| /// </summary> | ||
| /// <param name="chromosome">Chromosome.</param> | ||
| public double Evaluate(IChromosome chromosome) | ||
| { | ||
| throw new NotSupportedException("Use EvaluateAsync instead."); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Evaluate the specified chromosome asynchronously. | ||
| /// </summary> | ||
| /// <param name="chromosome">Chromosome.</param> | ||
| /// <param name="cancellationToken">The cancellation token.</param> | ||
| public Task<double> EvaluateAsync(IChromosome chromosome, CancellationToken cancellationToken) | ||
| { | ||
| return m_func(chromosome, cancellationToken); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace GeneticSharp | ||
| { | ||
| /// <summary> | ||
| /// Defines an interface for asynchronous fitness function. | ||
| /// </summary> | ||
| public interface IAsyncFitness | ||
| { | ||
| /// <summary> | ||
| /// Performs the evaluation against the specified chromosome asynchronously. | ||
| /// </summary> | ||
| /// <param name="chromosome">The chromosome to be evaluated.</param> | ||
| /// <param name="cancellationToken">The cancellation token.</param> | ||
| /// <returns>The fitness of the chromosome.</returns> | ||
| Task<double> EvaluateAsync(IChromosome chromosome, CancellationToken cancellationToken); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 7 additions & 5 deletions
12
src/GeneticSharp.Infrastructure.Framework.UnitTests/Threading/StubTaskExecutor.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,20 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace GeneticSharp.Infrastructure.Framework.UnitTests.Threading | ||
| { | ||
| public class StubTaskExecutor : TaskExecutorBase | ||
| public class StubTaskExecutor : TaskExecutorBase | ||
| { | ||
| public IList<Action> GetTasks() | ||
| { | ||
| public IList<Func<CancellationToken, ValueTask>> GetTasks() | ||
| { | ||
| return Tasks; | ||
| } | ||
|
|
||
| public bool GetStopRequested() | ||
| { | ||
| { | ||
| return StopRequested; | ||
| } | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Disposable 'CancellationTokenSource' is created but not disposed.