-
-
Notifications
You must be signed in to change notification settings - Fork 329
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3843bd5
commit ac425ca
Showing
13 changed files
with
160 additions
and
143 deletions.
There are no files selected for viewing
This file contains 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 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
147 changes: 74 additions & 73 deletions
147
src/GeneticSharp.Extensions/Drawing/BitmapEqualityFitness.cs
This file contains 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,93 +1,94 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.Generic; | ||
using System.Drawing; | ||
using GeneticSharp.Domain.Chromosomes; | ||
using GeneticSharp.Domain.Fitnesses; | ||
|
||
|
||
namespace GeneticSharp.Extensions.Drawing | ||
{ | ||
/// <summary> | ||
/// Bitmap equality fitness. | ||
/// </summary> | ||
public class BitmapEqualityFitness : IFitness | ||
{ | ||
{ | ||
/// <summary> | ||
/// Bitmap equality fitness. | ||
/// </summary> | ||
public class BitmapEqualityFitness : IFitness | ||
{ | ||
#region Fields | ||
private IList<Color> m_targetBitmapPixels; | ||
private int m_pixelsCount; | ||
private int m_pixelsCount; | ||
#endregion | ||
|
||
|
||
#region Constructors | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="BitmapEqualityFitness"/> class. | ||
/// </summary> | ||
/// <param name="targetBitmap">The target bitmap.</param> | ||
public BitmapEqualityFitness(Bitmap targetBitmap) | ||
{ | ||
Initialize(targetBitmap); | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="BitmapEqualityFitness"/> class. | ||
/// </summary> | ||
public BitmapEqualityFitness() | ||
{ | ||
} | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="BitmapEqualityFitness"/> class. | ||
/// </summary> | ||
/// <param name="targetBitmap">The target bitmap.</param> | ||
public BitmapEqualityFitness(Bitmap targetBitmap) | ||
: this() | ||
{ | ||
Initialize(targetBitmap); | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="BitmapEqualityFitness"/> class. | ||
/// </summary> | ||
public BitmapEqualityFitness() | ||
{ | ||
} | ||
#endregion | ||
|
||
|
||
#region Properties | ||
/// <summary> | ||
/// Gets the width of the bitmap. | ||
/// </summary> | ||
/// <value> | ||
/// The width of the bitmap. | ||
/// </value> | ||
public int BitmapWidth { get; private set; } | ||
|
||
/// <summary> | ||
/// Gets the height of the bitmap. | ||
/// </summary> | ||
/// <value> | ||
/// The height of the bitmap. | ||
/// </value> | ||
public int BitmapHeight { get; private set; } | ||
/// <summary> | ||
/// Gets the width of the bitmap. | ||
/// </summary> | ||
/// <value> | ||
/// The width of the bitmap. | ||
/// </value> | ||
public int BitmapWidth { get; private set; } | ||
|
||
/// <summary> | ||
/// Gets the height of the bitmap. | ||
/// </summary> | ||
/// <value> | ||
/// The height of the bitmap. | ||
/// </value> | ||
public int BitmapHeight { get; private set; } | ||
#endregion | ||
|
||
|
||
#region Methods | ||
/// <summary> | ||
/// Initializes the specified target bitmap. | ||
/// </summary> | ||
/// <param name="targetBitmap">The target bitmap.</param> | ||
public void Initialize(Bitmap targetBitmap) | ||
{ | ||
BitmapWidth = targetBitmap.Width; | ||
/// <summary> | ||
/// Initializes the specified target bitmap. | ||
/// </summary> | ||
/// <param name="targetBitmap">The target bitmap.</param> | ||
public void Initialize(Bitmap targetBitmap) | ||
{ | ||
BitmapWidth = targetBitmap.Width; | ||
BitmapHeight = targetBitmap.Height; | ||
|
||
m_targetBitmapPixels = BitmapChromosome.GetPixels(targetBitmap); | ||
m_pixelsCount = m_targetBitmapPixels.Count; | ||
m_targetBitmapPixels = BitmapChromosome.GetPixels(targetBitmap); | ||
m_pixelsCount = m_targetBitmapPixels.Count; | ||
} | ||
|
||
/// <summary> | ||
/// Evaluates the specified chromosome. | ||
/// </summary> | ||
/// <param name="chromosome">The chromosome.</param> | ||
/// <returns>The chromosome fitness.</returns> | ||
public double Evaluate(IChromosome chromosome) | ||
{ | ||
double fitness = 0; | ||
|
||
for (int i = 0; i < m_pixelsCount; i++) | ||
{ | ||
var targetPixel = m_targetBitmapPixels[i]; | ||
var chromosomePixel = (Color)chromosome.GetGene(i).Value; | ||
|
||
fitness -= Math.Abs(targetPixel.R - chromosomePixel.R); | ||
fitness -= Math.Abs(targetPixel.G - chromosomePixel.G); | ||
fitness -= Math.Abs(targetPixel.B - chromosomePixel.B); | ||
} | ||
|
||
return fitness; | ||
} | ||
|
||
/// <summary> | ||
/// Evaluates the specified chromosome. | ||
/// </summary> | ||
/// <param name="chromosome">The chromosome.</param> | ||
/// <returns>The chromosome fitness.</returns> | ||
public double Evaluate(IChromosome chromosome) | ||
{ | ||
double fitness = 0; | ||
|
||
for (int i = 0; i < m_pixelsCount; i++) | ||
{ | ||
var targetPixel = m_targetBitmapPixels[i]; | ||
var chromosomePixel = (Color)chromosome.GetGene(i).Value; | ||
|
||
fitness -= Math.Abs(targetPixel.R - chromosomePixel.R); | ||
fitness -= Math.Abs(targetPixel.G - chromosomePixel.G); | ||
fitness -= Math.Abs(targetPixel.B - chromosomePixel.B); | ||
} | ||
|
||
return fitness; | ||
} | ||
|
||
#endregion | ||
} | ||
} |
This file contains 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
134 changes: 67 additions & 67 deletions
134
src/GeneticSharp.Infrastructure.Framework.UnitTests/Threading/LinearTaskExecutorTest.cs
This file contains 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,67 +1,67 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using GeneticSharp.Infrastructure.Framework.Threading; | ||
using NUnit.Framework; | ||
|
||
namespace GeneticSharp.Infrastructure.Framework.UnitTests.Threading | ||
{ | ||
[TestFixture()] | ||
[Category("Infrastructure")] | ||
public class LinearTaskExecutorTest | ||
{ | ||
[Test()] | ||
public void Start_Task_TaskRan() | ||
{ | ||
var pipeline = ""; | ||
var target = new LinearTaskExecutor(); | ||
target.Add(() => pipeline += "1"); | ||
target.Add(() => pipeline += "2"); | ||
target.Add(() => pipeline += "3"); | ||
|
||
Assert.IsTrue(target.Start()); | ||
Assert.AreEqual("123", pipeline); | ||
} | ||
|
||
[Test()] | ||
public void Start_TakeMoreThanTimeout_False() | ||
{ | ||
var pipeline = ""; | ||
var target = new LinearTaskExecutor(); | ||
target.Add(() => pipeline += "1"); | ||
target.Add(() => | ||
{ | ||
pipeline += "2"; | ||
Thread.Sleep(100); | ||
}); | ||
target.Add(() => pipeline += "3"); | ||
|
||
target.Timeout = TimeSpan.FromMilliseconds(50); | ||
Assert.IsFalse(target.Start()); | ||
Assert.AreEqual("12", pipeline); | ||
} | ||
|
||
[Test()] | ||
public void Stop_ManyTasks_True() | ||
{ | ||
var pipeline = ""; | ||
var target = new LinearTaskExecutor(); | ||
target.Add(() => pipeline += "1"); | ||
target.Add(() => | ||
{ | ||
pipeline += "2"; | ||
Thread.Sleep(1000); | ||
}); | ||
target.Add(() => pipeline += "3"); | ||
|
||
Parallel.Invoke( | ||
() => Assert.IsTrue(target.Start()), | ||
() => | ||
{ | ||
Thread.Sleep(5); | ||
target.Stop(); | ||
}); | ||
} | ||
} | ||
} | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using GeneticSharp.Infrastructure.Framework.Threading; | ||
using NUnit.Framework; | ||
|
||
namespace GeneticSharp.Infrastructure.Framework.UnitTests.Threading | ||
{ | ||
[TestFixture()] | ||
[Category("Infrastructure")] | ||
public class LinearTaskExecutorTest | ||
{ | ||
[Test()] | ||
public void Start_Task_TaskRan() | ||
{ | ||
var pipeline = ""; | ||
var target = new LinearTaskExecutor(); | ||
target.Add(() => pipeline += "1"); | ||
target.Add(() => pipeline += "2"); | ||
target.Add(() => pipeline += "3"); | ||
|
||
Assert.IsTrue(target.Start()); | ||
Assert.AreEqual("123", pipeline); | ||
} | ||
|
||
[Test()] | ||
public void Start_TakeMoreThanTimeout_False() | ||
{ | ||
var pipeline = ""; | ||
var target = new LinearTaskExecutor(); | ||
target.Add(() => pipeline += "1"); | ||
target.Add(() => | ||
{ | ||
pipeline += "2"; | ||
Thread.Sleep(100); | ||
}); | ||
target.Add(() => pipeline += "3"); | ||
|
||
target.Timeout = TimeSpan.FromMilliseconds(100); | ||
Assert.IsFalse(target.Start()); | ||
Assert.AreEqual("12", pipeline); | ||
} | ||
|
||
[Test()] | ||
public void Stop_ManyTasks_True() | ||
{ | ||
var pipeline = ""; | ||
var target = new LinearTaskExecutor(); | ||
target.Add(() => pipeline += "1"); | ||
target.Add(() => | ||
{ | ||
pipeline += "2"; | ||
Thread.Sleep(1000); | ||
}); | ||
target.Add(() => pipeline += "3"); | ||
|
||
Parallel.Invoke( | ||
() => Assert.IsTrue(target.Start()), | ||
() => | ||
{ | ||
Thread.Sleep(5); | ||
target.Stop(); | ||
}); | ||
} | ||
} | ||
} | ||
|
Binary file modified
BIN
+0 Bytes
(100%)
src/Samples/Unity/Checkers/Assets/_Checkers/Libraries/GeneticSharp.Domain.dll
Binary file not shown.
Binary file modified
BIN
+0 Bytes
(100%)
src/Samples/Unity/Checkers/Assets/_Checkers/Libraries/GeneticSharp.Domain.dll.mdb
Binary file not shown.
Binary file modified
BIN
+0 Bytes
(100%)
src/Samples/Unity/Checkers/Assets/_Checkers/Libraries/GeneticSharp.Extensions.dll
Binary file not shown.
Binary file modified
BIN
+0 Bytes
(100%)
src/Samples/Unity/Checkers/Assets/_Checkers/Libraries/GeneticSharp.Extensions.dll.mdb
Binary file not shown.
Binary file modified
BIN
+0 Bytes
(100%)
...mples/Unity/Checkers/Assets/_Checkers/Libraries/GeneticSharp.Infrastructure.Framework.dll
Binary file not shown.
Binary file modified
BIN
+0 Bytes
(100%)
...s/Unity/Checkers/Assets/_Checkers/Libraries/GeneticSharp.Infrastructure.Framework.dll.mdb
Binary file not shown.
Binary file modified
BIN
+0 Bytes
(100%)
...mples/Unity/Checkers/Assets/_Checkers/Libraries/GeneticSharp.Infrastructure.Threading.dll
Binary file not shown.
Binary file modified
BIN
+0 Bytes
(100%)
...s/Unity/Checkers/Assets/_Checkers/Libraries/GeneticSharp.Infrastructure.Threading.dll.mdb
Binary file not shown.