Skip to content

Commit 014114b

Browse files
committed
Removed Massive Bottleneck when setting the Fitness manually due to calling IndexOF in the GeneticAlgorithm.
Also Note that Networks are now added to the end of the List when settings Fitness manually since its the fastest way. You may now need to do another SortToFitness-Call after setting the Fitness manually
1 parent 43b4376 commit 014114b

File tree

3 files changed

+7
-6
lines changed

3 files changed

+7
-6
lines changed

NeuralBotMasterFramework/NeuralBotMasterFramework/Logic/Algorithms/GeneticAlgorithm.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,8 @@ public void SetFitness(IWeightedNetwork network, double fitness)
247247
{
248248
ThrowIfNetworkNotInDictionary(network);
249249
KeyValuePair<IWeightedNetwork, double> temp = NetworksAndFitness.FirstOrDefault(x => x.Key == network);
250-
int index = NetworksAndFitness.IndexOf(temp);
251-
NetworksAndFitness[index] = new KeyValuePair<IWeightedNetwork, double>(network, fitness);
250+
NetworksAndFitness.Remove(temp);
251+
NetworksAndFitness.Add(new KeyValuePair<IWeightedNetwork, double>(network, fitness));
252252
}
253253

254254
private void ThrowIfNetworkNotInDictionary(IWeightedNetwork network)

NeuralBotMasterFramework/NeuralBotMasterFrameworkTests/Logic/Algorithms/GeneticAlgorithmTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -227,10 +227,10 @@ public void SetFitness_IWeightedNetworkAsParameter_NetworkNotFound_ShouldThrowAr
227227
public void SetFitness_NetworkIndexAsParameter()
228228
{
229229
int index = RandomNumberGenerator.GetNextNumber(0, Algorithm.NetworksAndFitness.Count);
230-
double newFitnessValue = RandomNumberGenerator.GetNextDouble();
231-
Algorithm.SetFitness(index, newFitnessValue);
232-
KeyValuePair<IWeightedNetwork, double> networkAndFitness = Algorithm.NetworksAndFitness[index];
233-
Assert.AreEqual(newFitnessValue, networkAndFitness.Value);
230+
double newValue = RandomNumberGenerator.GetNextDouble();
231+
IWeightedNetwork network = Algorithm.NetworksAndFitness.Select(x => x.Key).ElementAt(index);
232+
Algorithm.SetFitness(index, newValue);
233+
Assert.AreEqual(newValue, Algorithm.NetworksAndFitness.FirstOrDefault(x => x.Key == network).Value);
234234
}
235235
}
236236
}

NeuralBotMasterFramework/NeuralBotMasterFrameworkTests/Logic/PoolGenerators/FitnessBasedPoolGeneratorTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public void GenerateBreedingPoolTest()
3939
public void GenerateBreedingPool_ZeroFitnessScore_ShouldIgnoreZeroFitnesses()
4040
{
4141
algorithm.SetFitness(0, 100);
42+
algorithm.SortByFitness();
4243
IBreedingPoolGenerator generator = new FitnessBasedPoolGenerator();
4344
List<IWeightedNetwork> networkList = generator.GenerateBreedingPool(algorithm.NetworksAndFitness.Take(NETWORKS_TO_KEEP).ToList());
4445
Assert.IsTrue(networkList.Count > 0);

0 commit comments

Comments
 (0)