Skip to content

Commit 01fb750

Browse files
committed
Added Methods GetFitnesses, SetFitnesses and a overloaded SetFitness and implemented them
1 parent b6ec422 commit 01fb750

File tree

3 files changed

+117
-1
lines changed

3 files changed

+117
-1
lines changed

NeuralBotMasterFramework/NeuralBotMasterFramework/Interfaces/IGeneticAlgorithm.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,9 @@ public interface IGeneticAlgorithm
2626
void PropagateAllNetworks();
2727
void SortByFitness();
2828
void BreedBestNetworks();
29+
double[] GetFitnesses();
30+
void SetFitnesses(double[] fitnesses);
31+
void SetFitness(int networkIndex, double fitness);
32+
void SetFitness(IWeightedNetwork network, double fitness);
2933
}
3034
}

NeuralBotMasterFramework/NeuralBotMasterFramework/Logic/Algorithms/GeneticAlgorithm.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,5 +206,50 @@ private void MutateNetwork(IWeightedNetwork network)
206206
}
207207
}
208208
}
209+
210+
public double[] GetFitnesses()
211+
{
212+
return NetworksAndFitness.Values.ToArray();
213+
}
214+
215+
public void SetFitnesses(double[] fitnesses)
216+
{
217+
ThrowIfArgumentDoesNotHaveCorrectLength(fitnesses, TotalNetworks);
218+
219+
Dictionary<IWeightedNetwork, double> tempFitnessAndValues = new Dictionary<IWeightedNetwork, double>();
220+
for (int i = 0; i < fitnesses.Length; ++i)
221+
{
222+
IWeightedNetwork network = NetworksAndFitness.ElementAt(i).Key;
223+
tempFitnessAndValues.Add(network, fitnesses[i]);
224+
}
225+
NetworksAndFitness = tempFitnessAndValues;
226+
}
227+
228+
private void ThrowIfArgumentDoesNotHaveCorrectLength(double[] array, int expectedArrayLength)
229+
{
230+
if (array.Length != expectedArrayLength)
231+
{
232+
throw new ArgumentException($"Argument-Length not valid: Expected: {expectedArrayLength} Actual: {array.Length}");
233+
}
234+
}
235+
236+
public void SetFitness(int networkIndex, double fitness)
237+
{
238+
SetFitness(NetworksAndFitness.Keys.ElementAt(networkIndex), fitness);
239+
}
240+
241+
public void SetFitness(IWeightedNetwork network, double fitness)
242+
{
243+
ThrowIfNetworkNotInDictionary(network);
244+
NetworksAndFitness[network] = fitness;
245+
}
246+
247+
private void ThrowIfNetworkNotInDictionary(IWeightedNetwork network)
248+
{
249+
if (!NetworksAndFitness.Keys.Contains(network))
250+
{
251+
throw new ArgumentException($"Network not found inside {nameof(NetworksAndFitness)}");
252+
}
253+
}
209254
}
210255
}

NeuralBotMasterFramework/NeuralBotMasterFrameworkTests/Logic/Algorithms/GeneticAlgorithmTests.cs

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ public class GeneticAlgorithmTests
2020
private const int HIDDEN_LAYERS = 3;
2121
private const int OUTPUT_NODES = 3;
2222

23+
private readonly GeneticAlgorithm Algorithm = new GeneticAlgorithm(TOTAL_NETWORKS, INPUT_NODES, HIDDEN_NODES, HIDDEN_LAYERS, OUTPUT_NODES);
24+
2325
private double[][] Input = new double[][]
2426
{
2527
new double[]
@@ -52,7 +54,6 @@ public class GeneticAlgorithmTests
5254
},
5355
};
5456

55-
private readonly GeneticAlgorithm Algorithm = new GeneticAlgorithm(TOTAL_NETWORKS, INPUT_NODES, HIDDEN_NODES, HIDDEN_LAYERS, OUTPUT_NODES);
5657

5758
[TestMethod]
5859
public void ConstructorTest()
@@ -165,5 +166,71 @@ public void BreedBestNetworksTest()
165166
Assert.IsNotNull(network);
166167
}
167168
}
169+
170+
[TestMethod]
171+
public void SetAndGetFitnesses_CorrectFitnessAmount()
172+
{
173+
double[] fitnessValues = GetRandomDoubleArray(TOTAL_NETWORKS);
174+
Algorithm.SetFitnesses(fitnessValues);
175+
double[] resultFitnesses = Algorithm.GetFitnesses();
176+
177+
Assert.AreEqual(fitnessValues.Length, resultFitnesses.Length);
178+
for (int i = 0; i < fitnessValues.Length; i++)
179+
{
180+
Assert.AreEqual(fitnessValues[i], resultFitnesses[i]);
181+
}
182+
}
183+
184+
[TestMethod]
185+
public void SetFitnesses_MoreFitnessesThanNetworks_ShouldThrowException()
186+
{
187+
double[] fitnessValues = GetRandomDoubleArray(TOTAL_NETWORKS + 5);
188+
Assert.ThrowsException<ArgumentException>(() => Algorithm.SetFitnesses(fitnessValues), "No Exception when more Fitness-Values than Networks");
189+
}
190+
191+
[TestMethod]
192+
public void SetFitnesses_LessFitnessesThanNetworks_ShouldThrowException()
193+
{
194+
const int TOTAL_DOUBLE_VALUES = TOTAL_NETWORKS - 5;
195+
double[] fitnessValues = GetRandomDoubleArray(TOTAL_DOUBLE_VALUES >= 0 ? TOTAL_DOUBLE_VALUES : 0);
196+
Assert.ThrowsException<ArgumentException>(() => Algorithm.SetFitnesses(fitnessValues), "No Exception when more Fitness-Values than Networks");
197+
}
198+
199+
private double[] GetRandomDoubleArray(int totalValues)
200+
{
201+
double[] array = new double[totalValues];
202+
for (int i = 0; i < array.Length; ++i)
203+
{
204+
array[i] = RandomNumberGenerator.GetNextDouble();
205+
}
206+
return array;
207+
}
208+
209+
[TestMethod]
210+
public void SetFitness_IWeightedNetworkAsParameter()
211+
{
212+
double newValue = RandomNumberGenerator.GetNextDouble();
213+
IWeightedNetwork network = Algorithm.NetworksAndFitness.Keys.ElementAt(0);
214+
Algorithm.SetFitness(network, newValue);
215+
Assert.AreEqual(newValue, Algorithm.NetworksAndFitness.FirstOrDefault(x => x.Key == network).Value);
216+
}
217+
218+
[TestMethod]
219+
public void SetFitness_IWeightedNetworkAsParameter_NetworkNotFound_ShouldThrowArgumentException()
220+
{
221+
double newValue = RandomNumberGenerator.GetNextDouble();
222+
IWeightedNetwork network = new WeightedNetwork(INPUT_NODES, HIDDEN_LAYERS, HIDDEN_NODES, OUTPUT_NODES);
223+
Assert.ThrowsException<ArgumentException>(() => Algorithm.SetFitness(network, newValue), "No Exception when Network not found");
224+
}
225+
226+
[TestMethod]
227+
public void SetFitness_NetworkIndexAsParameter()
228+
{
229+
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.ElementAt(index);
233+
Assert.AreEqual(newFitnessValue, networkAndFitness.Value);
234+
}
168235
}
169236
}

0 commit comments

Comments
 (0)