Skip to content

Commit 1253d45

Browse files
committed
Propagation of neural networks now happens with Tasks
1 parent 099925f commit 1253d45

File tree

2 files changed

+51
-11
lines changed

2 files changed

+51
-11
lines changed

NeuralBotMasterFramework/NeuralBotMasterFramework/Logic/Algorithms/GeneticAlgorithm.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,18 +66,29 @@ public void PropagateAllNetworks()
6666
ResetAllFitnesses();
6767
for (int inputIndex = 0; inputIndex < CurrentInput.Length; inputIndex++)
6868
{
69+
Task[] tasks = new Task[NetworksAndFitness.Count];
70+
int listIndex = 0;
6971
foreach (KeyValuePair<IWeightedNetwork, double> networkAndFitness in NetworksAndFitness)
7072
{
71-
networkAndFitness.Key.SetInput(CurrentInput[inputIndex]);
72-
networkAndFitness.Key.Propagate();
73+
tasks[listIndex] = Task.Factory.StartNew(()
74+
=> PropagateNetworkTask(networkAndFitness, inputIndex));
75+
76+
++listIndex;
7377
}
78+
Task.WaitAll(tasks);
7479
if (CurrentExpectedIsSet())
7580
{
7681
CalculateFitnesses(CurrentExpected[inputIndex]);
7782
}
7883
}
7984
}
8085

86+
private void PropagateNetworkTask(KeyValuePair<IWeightedNetwork, double> networkAndFitness, int inputIndex)
87+
{
88+
networkAndFitness.Key.SetInput(CurrentInput[inputIndex]);
89+
networkAndFitness.Key.Propagate();
90+
}
91+
8192
private bool CurrentExpectedIsSet()
8293
{
8394
return CurrentExpected?.Length >= 0;

NeuralBotMasterFramework/TestConsole/Program.cs

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,46 +14,75 @@ class Program
1414
private static double[][] inputData;
1515
private static double[][] expectedData;
1616

17-
const int TOTAL_BITS = 2;
17+
private static int dataSetsPerPropagation = 1000;
18+
private static double[][] currentInputData;
19+
private static double[][] currentExpectedData;
20+
21+
const int TOTAL_BITS = 12;
1822
private static int totalNumberLength = Math.Pow(2, TOTAL_BITS).ToString().Length;
1923

2024
static void Main(string[] args)
2125
{
22-
int totalNetworks = 100;
26+
int totalNetworks = 1000;
2327
int inputNodes = TOTAL_BITS;
2428
int hiddenNodes = 10;
2529
int hiddenLayers = 2;
2630
int outputNodes = totalNumberLength;
2731

2832
int networksToKeep = 10;
2933
int totalRandomNetworks = 10;
30-
double mutationRate = 0.1;
31-
double mutationChance = 0.0011;
34+
double mutationRate = 0.01;
35+
double mutationChance = 0.01;
3236

3337
SetupBinaryData();
3438

3539
GeneticAlgorithm algorithm = new GeneticAlgorithm(totalNetworks, inputNodes, hiddenNodes, hiddenLayers, outputNodes)
3640
{
37-
PoolGenerator = new IndexBasedPoolGenerator()
41+
PoolGenerator = new FitnessBasedPoolGenerator()
3842
};
3943

40-
algorithm.SetupTest(inputData, expectedData);
41-
4244
algorithm.NetworksToKeep = networksToKeep;
4345
algorithm.MutationRate = mutationRate;
4446
algorithm.MutationChance = mutationChance;
4547
algorithm.RandomNetworkAmount = totalRandomNetworks;
4648

4749
Console.WriteLine("Initialization complete");
4850

51+
int generation = 0;
4952
while (true)
5053
{
54+
Console.WriteLine($"Generation {generation}");
55+
SetCurrentDataSets();
56+
algorithm.SetupTest(currentInputData, currentExpectedData);
5157
Console.WriteLine("Propagating");
5258
algorithm.PropagateAllNetworks();
5359
Console.WriteLine("Breeding");
5460
algorithm.BreedBestNetworks();
55-
PrintData(algorithm);
56-
Console.ReadLine();
61+
if (generation % 20 == 0)
62+
{
63+
PrintData(algorithm);
64+
}
65+
++generation;
66+
}
67+
}
68+
69+
private static void SetCurrentDataSets()
70+
{
71+
int totalDataSets = inputData.Length < dataSetsPerPropagation ? inputData.Length : dataSetsPerPropagation;
72+
currentInputData = new double[totalDataSets][];
73+
currentExpectedData = new double[totalDataSets][];
74+
75+
List<int> indexedDataSets = new List<int>();
76+
for (int i = 0; i < totalDataSets;)
77+
{
78+
int index = NeuralBotMasterFramework.Helper.RandomNumberGenerator.GetNextNumber(0, inputData.Length - 1);
79+
if (!indexedDataSets.Contains(index))
80+
{
81+
currentInputData[i] = inputData[index];
82+
currentExpectedData[i] = expectedData[index];
83+
indexedDataSets.Add(index);
84+
++i;
85+
}
5786
}
5887
}
5988

0 commit comments

Comments
 (0)