@@ -20,6 +20,8 @@ public class GeneticAlgorithmTests
20
20
private const int HIDDEN_LAYERS = 3 ;
21
21
private const int OUTPUT_NODES = 3 ;
22
22
23
+ private readonly GeneticAlgorithm Algorithm = new GeneticAlgorithm ( TOTAL_NETWORKS , INPUT_NODES , HIDDEN_NODES , HIDDEN_LAYERS , OUTPUT_NODES ) ;
24
+
23
25
private double [ ] [ ] Input = new double [ ] [ ]
24
26
{
25
27
new double [ ]
@@ -52,7 +54,6 @@ public class GeneticAlgorithmTests
52
54
} ,
53
55
} ;
54
56
55
- private readonly GeneticAlgorithm Algorithm = new GeneticAlgorithm ( TOTAL_NETWORKS , INPUT_NODES , HIDDEN_NODES , HIDDEN_LAYERS , OUTPUT_NODES ) ;
56
57
57
58
[ TestMethod ]
58
59
public void ConstructorTest ( )
@@ -165,5 +166,71 @@ public void BreedBestNetworksTest()
165
166
Assert . IsNotNull ( network ) ;
166
167
}
167
168
}
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
+ }
168
235
}
169
236
}
0 commit comments