Skip to content

Commit c722463

Browse files
committed
Added FitnessBased PoolGenerator and started using it in Program.cs
1 parent 5fdaaaa commit c722463

File tree

5 files changed

+98
-2
lines changed

5 files changed

+98
-2
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using NeuralBotMasterFramework.Helper;
7+
using NeuralBotMasterFramework.Interfaces;
8+
9+
namespace NeuralBotMasterFramework.Logic.PoolGenerators
10+
{
11+
public class FitnessBasedPoolGenerator : IBreedingPoolGenerator
12+
{
13+
public List<IWeightedNetwork> GenerateBreedingPool(Dictionary<IWeightedNetwork, double> networksAndFitness)
14+
{
15+
List<IWeightedNetwork> poolList = new List<IWeightedNetwork>();
16+
double[] fitnesses = MultiplyUntilBiggerThanOne(networksAndFitness.Values.ToArray());
17+
int totalFitness = (int)Math.Round(fitnesses.Sum(), 0, MidpointRounding.AwayFromZero);
18+
for (int networkIndex = 0; networkIndex < networksAndFitness.Count; ++networkIndex)
19+
{
20+
int totalNetworksToAdd = (int)Math.Round(fitnesses[networkIndex] / totalFitness * networksAndFitness.Count * 100);
21+
KeyValuePair<IWeightedNetwork, double> currentNetworkAndFitness = networksAndFitness.ElementAt(networkIndex);
22+
for (int fitness = 0; fitness < totalNetworksToAdd; ++fitness)
23+
{
24+
poolList.Add(currentNetworkAndFitness.Key);
25+
}
26+
}
27+
return poolList;
28+
}
29+
30+
private double[] MultiplyUntilBiggerThanOne(double[] values)
31+
{
32+
double[] returnValues = (double[])values.Clone();
33+
while (returnValues.Min() < 1)
34+
{
35+
for (int i = 0; i < returnValues.Length; ++i)
36+
{
37+
returnValues[i] *= 10;
38+
}
39+
}
40+
return returnValues;
41+
}
42+
}
43+
}

NeuralBotMasterFramework/NeuralBotMasterFramework/NeuralBotMasterFramework.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
<Compile Include="Interfaces\INode.cs" />
5454
<Compile Include="Logic\Algorithms\GeneticAlgorithm.cs" />
5555
<Compile Include="Logic\Networks\WeightedNetwork.cs" />
56+
<Compile Include="Logic\PoolGenerators\FitnessBasedPoolGenerator.cs" />
5657
<Compile Include="Logic\PoolGenerators\IndexBasedPoolGenerator.cs" />
5758
<Compile Include="Models\Layer.cs" />
5859
<Compile Include="Models\Node.cs" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using NeuralBotMasterFramework.Logic.PoolGenerators;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
using NeuralBotMasterFramework.Logic.Algorithms;
9+
using NeuralBotMasterFramework.Helper;
10+
using NeuralBotMasterFramework.Interfaces;
11+
12+
namespace NeuralBotMasterFramework.Logic.PoolGenerators.Tests
13+
{
14+
[TestClass]
15+
public class FitnessBasedPoolGeneratorTests
16+
{
17+
[TestMethod]
18+
public void GenerateBreedingPoolTest()
19+
{
20+
const int TOTAL_NETWORKS = 15;
21+
const int INPUT_NODES = 5;
22+
const int HIDDEN_NODES = 5;
23+
const int HIDDEN_LAYERS = 5;
24+
const int OUTPUT_NODES = 1;
25+
const int NETWORKS_TO_KEEP = 10;
26+
GeneticAlgorithm algorithm = new GeneticAlgorithm(TOTAL_NETWORKS, INPUT_NODES, HIDDEN_NODES, HIDDEN_LAYERS, OUTPUT_NODES);
27+
double[][] inputData = new double[][] { GetRandomDoubleArray(INPUT_NODES) };
28+
double[][] expectedData = new double[][] { GetRandomDoubleArray(OUTPUT_NODES) };
29+
algorithm.SetupTest(inputData, expectedData);
30+
algorithm.PropagateAllNetworks();
31+
algorithm.SortByFitness();
32+
FitnessBasedPoolGenerator generator = new FitnessBasedPoolGenerator();
33+
List<IWeightedNetwork> networkList = generator.GenerateBreedingPool(algorithm.NetworksAndFitness.Take(NETWORKS_TO_KEEP).ToDictionary(x => x.Key, x => x.Value));
34+
Assert.IsTrue(networkList.Count > 0);
35+
}
36+
37+
private double[] GetRandomDoubleArray(int totalValues)
38+
{
39+
double[] array = new double[totalValues];
40+
for (int i = 0; i < array.Length; ++i)
41+
{
42+
array[i] = RandomNumberGenerator.GetNextDouble();
43+
}
44+
return array;
45+
}
46+
}
47+
}

NeuralBotMasterFramework/NeuralBotMasterFrameworkTests/NeuralBotMasterFrameworkTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
<ItemGroup>
5959
<Compile Include="Helper\RandomNumberGeneratorTests.cs" />
6060
<Compile Include="Logic\Algorithms\GeneticAlgorithmTests.cs" />
61+
<Compile Include="Logic\PoolGenerators\FitnessBasedPoolGeneratorTests.cs" />
6162
<Compile Include="Logic\WeightedNetworkTests.cs" />
6263
<Compile Include="Models\LayerTests.cs" />
6364
<Compile Include="Models\NodeTests.cs" />

NeuralBotMasterFramework/TestConsole/Program.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Text;
55
using System.Threading.Tasks;
66
using NeuralBotMasterFramework.Logic.Algorithms;
7+
using NeuralBotMasterFramework.Logic.PoolGenerators;
78

89
namespace TestConsole
910
{
@@ -23,13 +24,16 @@ static void Main(string[] args)
2324
int hiddenLayers = 3;
2425
int outputNodes = totalNumberLength;
2526

26-
int networksToKeep = 10;
27+
int networksToKeep = 100;
2728
double mutationRate = 0.2;
2829
double mutationChance = 0.4;
2930

3031
SetupBinaryData();
3132

32-
GeneticAlgorithm algorithm = new GeneticAlgorithm(totalNetworks, inputNodes, hiddenNodes, hiddenLayers, outputNodes);
33+
GeneticAlgorithm algorithm = new GeneticAlgorithm(totalNetworks, inputNodes, hiddenNodes, hiddenLayers, outputNodes)
34+
{
35+
PoolGenerator = new IndexBasedPoolGenerator()
36+
};
3337

3438
algorithm.SetupTest(inputData, expectedData);
3539

0 commit comments

Comments
 (0)