Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d8b6372

Browse files
Test lifecycle without crossover & mutation
1 parent 2f139d3 commit d8b6372

File tree

1 file changed

+70
-6
lines changed

1 file changed

+70
-6
lines changed

‎tests/test_lifecycle_callbacks_calls.py

Lines changed: 70 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
num_generations = 100
44

55
def number_lifecycle_callback_functions_calls(stop_criteria=None,
6-
on_generation_stop=None):
6+
on_generation_stop=None,
7+
crossover_type="single_point",
8+
mutation_type="random"):
79
actual_num_callbacks_calls = 0
810

911
def fitness_func(ga_instanse, solution, solution_idx):
@@ -46,6 +48,8 @@ def on_stop(ga_instance, last_population_fitness):
4648
fitness_func=fitness_func,
4749
sol_per_pop=10,
4850
num_genes=5,
51+
crossover_type=crossover_type,
52+
mutation_type=mutation_type,
4953
on_start=on_start,
5054
on_fitness=on_fitness,
5155
on_parents=on_parents,
@@ -71,12 +75,14 @@ def on_stop(ga_instance, last_population_fitness):
7175
# Use 'generations_completed' instead of 'num_generations' because the evolution may stops in the on_generation() callback.
7276
expected_num_callbacks_calls = 1 + ga_instance.generations_completed * 5 + 1
7377

74-
print("Expected number of callbacks calls is {expected_num_callbacks_calls}.".format(expected_num_callbacks_calls=expected_num_callbacks_calls))
75-
print("Actual number of callbacks calls is {actual_num_callbacks_calls}.".format(actual_num_callbacks_calls=actual_num_callbacks_calls))
78+
print("Expected {expected_num_callbacks_calls}.".format(expected_num_callbacks_calls=expected_num_callbacks_calls))
79+
print("Actual {actual_num_callbacks_calls}.".format(actual_num_callbacks_calls=actual_num_callbacks_calls))
7680
return actual_num_callbacks_calls, expected_num_callbacks_calls
7781

7882
def number_lifecycle_callback_methods_calls(stop_criteria=None,
79-
on_generation_stop=None):
83+
on_generation_stop=None,
84+
crossover_type="single_point",
85+
mutation_type="random"):
8086
actual_num_callbacks_calls = 0
8187

8288
class Callbacks:
@@ -121,6 +127,8 @@ def on_stop(self, ga_instance, last_population_fitness):
121127
fitness_func=Callbacks_obj.fitness_func,
122128
sol_per_pop=10,
123129
num_genes=5,
130+
crossover_type=crossover_type,
131+
mutation_type=mutation_type,
124132
on_start=Callbacks_obj.on_start,
125133
on_fitness=Callbacks_obj.on_fitness,
126134
on_parents=Callbacks_obj.on_parents,
@@ -146,8 +154,8 @@ def on_stop(self, ga_instance, last_population_fitness):
146154
# Use 'generations_completed' instead of 'num_generations' because the evolution may stops in the on_generation() callback.
147155
expected_num_callbacks_calls = 1 + ga_instance.generations_completed * 5 + 1
148156

149-
print("Expected number of callbacks calls is {expected_num_callbacks_calls}.".format(expected_num_callbacks_calls=expected_num_callbacks_calls))
150-
print("Actual number of callbacks calls is {actual_num_callbacks_calls}.".format(actual_num_callbacks_calls=actual_num_callbacks_calls))
157+
print("Expected {expected_num_callbacks_calls}.".format(expected_num_callbacks_calls=expected_num_callbacks_calls))
158+
print("Actual {actual_num_callbacks_calls}.".format(actual_num_callbacks_calls=actual_num_callbacks_calls))
151159
return actual_num_callbacks_calls, expected_num_callbacks_calls
152160

153161
def test_number_lifecycle_callback_functions_calls():
@@ -170,13 +178,69 @@ def test_number_lifecycle_callback_methods_calls_stop_criteria():
170178

171179
assert actual == expected
172180

181+
def test_number_lifecycle_callback_functions_calls_no_crossover():
182+
actual, expected = number_lifecycle_callback_functions_calls(crossover_type=None)
183+
184+
assert actual == expected
185+
186+
def test_number_lifecycle_callback_functions_calls_no_mutation():
187+
actual, expected = number_lifecycle_callback_functions_calls(mutation_type=None)
188+
189+
assert actual == expected
190+
191+
def test_number_lifecycle_callback_functions_calls_no_crossover_no_mutation():
192+
actual, expected = number_lifecycle_callback_functions_calls(crossover_type=None,
193+
mutation_type=None)
194+
195+
assert actual == expected
196+
197+
def test_number_lifecycle_callback_methods_calls_no_crossover():
198+
actual, expected = number_lifecycle_callback_methods_calls(crossover_type=None)
199+
200+
assert actual == expected
201+
202+
def test_number_lifecycle_callback_methods_calls_no_mutation():
203+
actual, expected = number_lifecycle_callback_methods_calls(mutation_type=None)
204+
205+
assert actual == expected
206+
207+
def test_number_lifecycle_callback_methods_calls_no_crossover_no_mutation():
208+
actual, expected = number_lifecycle_callback_methods_calls(crossover_type=None,
209+
mutation_type=None)
210+
211+
assert actual == expected
212+
173213
if __name__ == "__main__":
174214
print()
175215
test_number_lifecycle_callback_functions_calls()
176216
print()
217+
177218
test_number_lifecycle_callback_functions_calls_stop_criteria()
178219
print()
220+
179221
test_number_lifecycle_callback_methods_calls()
180222
print()
223+
181224
test_number_lifecycle_callback_methods_calls_stop_criteria()
182225
print()
226+
227+
test_number_lifecycle_callback_functions_calls_no_crossover()
228+
print()
229+
230+
test_number_lifecycle_callback_functions_calls_no_crossover()
231+
print()
232+
233+
test_number_lifecycle_callback_functions_calls_no_mutation()
234+
print()
235+
236+
test_number_lifecycle_callback_functions_calls_no_crossover_no_mutation()
237+
print()
238+
239+
test_number_lifecycle_callback_methods_calls_no_crossover()
240+
print()
241+
242+
test_number_lifecycle_callback_methods_calls_no_mutation()
243+
print()
244+
245+
test_number_lifecycle_callback_methods_calls_no_crossover_no_mutation()
246+
print()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /