forked from giacomelli/GeneticSharp
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAsyncFuncFitness.cs
More file actions
43 lines (39 loc) · 1.46 KB
/
AsyncFuncFitness.cs
File metadata and controls
43 lines (39 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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);
}
}
}