Skip to content

Commit 96b3da9

Browse files
authored
Merge pull request #208 from ahmedfgad/github-actions
GitHub actions
2 parents d4b98a3 + b4bfb9a commit 96b3da9

File tree

4 files changed

+284
-12
lines changed

4 files changed

+284
-12
lines changed

docs/source/README_pygad_ReadTheDocs.rst

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -913,7 +913,7 @@ It returns the following:
913913
- ``best_match_idx``: Index of the best solution in the current
914914
population.
915915

916-
.. _plotfitness-1:
916+
.. _plotfitness:
917917

918918
``plot_fitness()``
919919
------------------
@@ -946,7 +946,7 @@ and higher, this method accepts the following parameters:
946946

947947
8. ``save_dir``: Directory to save the figure.
948948

949-
.. _plotnewsolutionrate-1:
949+
.. _plotnewsolutionrate:
950950

951951
``plot_new_solution_rate()``
952952
----------------------------
@@ -979,7 +979,7 @@ This method accepts the following parameters:
979979

980980
8. ``save_dir``: Directory to save the figure.
981981

982-
.. _plotgenes-1:
982+
.. _plotgenes:
983983

984984
``plot_genes()``
985985
----------------
@@ -1229,7 +1229,7 @@ The next step is to import PyGAD as follows:
12291229
The ``pygad.GA`` class holds the implementation of all methods for
12301230
running the genetic algorithm.
12311231

1232-
.. _create-an-instance-of-the-pygadga-class-1:
1232+
.. _create-an-instance-of-the-pygadga-class:
12331233

12341234
Create an Instance of the ``pygad.GA`` Class
12351235
--------------------------------------------
@@ -2403,6 +2403,8 @@ The function should return 2 outputs:
24032403
2. The indices of the selected parents inside the population. It is a 1D
24042404
list with length equal to the number of selected parents.
24052405

2406+
The outputs must be of type ``numpy.ndarray``.
2407+
24062408
Here is a template for building a custom parent selection function.
24072409

24082410
.. code:: python
@@ -2427,7 +2429,7 @@ parents are selected. The number of parents is equal to the value in the
24272429
for parent_num in range(num_parents):
24282430
parents[parent_num, :] = ga_instance.population[fitness_sorted[parent_num], :].copy()
24292431
2430-
return parents, fitness_sorted[:num_parents]
2432+
return parents, numpy.array(fitness_sorted[:num_parents])
24312433
24322434
Finally, the defined function is assigned to the
24332435
``parent_selection_type`` parameter as in the next code.
@@ -2474,7 +2476,7 @@ previous 3 user-defined functions instead of the built-in functions.
24742476
for parent_num in range(num_parents):
24752477
parents[parent_num, :] = ga_instance.population[fitness_sorted[parent_num], :].copy()
24762478
2477-
return parents, fitness_sorted[:num_parents]
2479+
return parents, numpy.array(fitness_sorted[:num_parents])
24782480
24792481
def crossover_func(parents, offspring_size, ga_instance):
24802482
@@ -3004,7 +3006,7 @@ methods.
30043006
The ``plot_fitness()`` method shows the fitness value for each
30053007
generation.
30063008

3007-
.. _plottypeplot-1:
3009+
.. _plottypeplot:
30083010

30093011
``plot_type="plot"``
30103012
~~~~~~~~~~~~~~~~~~~~
@@ -3021,7 +3023,7 @@ line connecting the fitness values across all generations:
30213023
.. figure:: https://user-images.githubusercontent.com/16560492/122472609-d02f5280-cf8e-11eb-88a7-f9366ff6e7c6.png
30223024
:alt:
30233025

3024-
.. _plottypescatter-1:
3026+
.. _plottypescatter:
30253027

30263028
``plot_type="scatter"``
30273029
~~~~~~~~~~~~~~~~~~~~~~~
@@ -3037,7 +3039,7 @@ these dots can be changed using the ``linewidth`` parameter.
30373039
.. figure:: https://user-images.githubusercontent.com/16560492/122473159-75e2c180-cf8f-11eb-942d-31279b286dbd.png
30383040
:alt:
30393041

3040-
.. _plottypebar-1:
3042+
.. _plottypebar:
30413043

30423044
``plot_type="bar"``
30433045
~~~~~~~~~~~~~~~~~~~
@@ -3393,8 +3395,6 @@ parameter:
33933395
given the value 0, this means do not use parallel processing. This is
33943396
identical to ``parallel_processing=None``.
33953397

3396-
.. _examples-1:
3397-
33983398
Examples
33993399
--------
34003400

tests/test_adaptive_mutation.py

Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
import pygad
2+
import random
3+
import numpy
4+
5+
num_generations = 1
6+
7+
initial_population = [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
8+
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
9+
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
10+
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
11+
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
12+
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
13+
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
14+
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
15+
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
16+
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]
17+
18+
def output_adaptive_mutation(gene_space=None,
19+
gene_type=float,
20+
num_genes=10,
21+
mutation_by_replacement=False,
22+
random_mutation_min_val=-1,
23+
random_mutation_max_val=1,
24+
init_range_low=-4,
25+
init_range_high=4,
26+
initial_population=None,
27+
mutation_probability=[0.2, 0.1],
28+
fitness_batch_size=None,
29+
mutation_type="adaptive"):
30+
31+
def fitness_func_single(ga, solution, idx):
32+
return random.random()
33+
34+
def fitness_func_batch(ga, soluions, idxs):
35+
return numpy.random.uniform(size=len(soluions))
36+
37+
if fitness_batch_size in [1, None]:
38+
fitness_func = fitness_func_single
39+
else:
40+
fitness_func = fitness_func_batch
41+
42+
ga_instance = pygad.GA(num_generations=num_generations,
43+
num_parents_mating=5,
44+
fitness_func=fitness_func,
45+
sol_per_pop=10,
46+
num_genes=num_genes,
47+
gene_space=gene_space,
48+
gene_type=gene_type,
49+
initial_population=initial_population,
50+
init_range_low=init_range_low,
51+
init_range_high=init_range_high,
52+
random_mutation_min_val=random_mutation_min_val,
53+
random_mutation_max_val=random_mutation_max_val,
54+
allow_duplicate_genes=True,
55+
mutation_by_replacement=mutation_by_replacement,
56+
save_solutions=True,
57+
mutation_probability=mutation_probability,
58+
mutation_type=mutation_type,
59+
suppress_warnings=True,
60+
fitness_batch_size=fitness_batch_size,
61+
random_seed=1)
62+
63+
ga_instance.run()
64+
65+
return None, ga_instance
66+
67+
def test_adaptive_mutation():
68+
result, ga_instance = output_adaptive_mutation()
69+
70+
# assert result == True
71+
72+
def test_adaptive_mutation_gene_space():
73+
result, ga_instance = output_adaptive_mutation(gene_space=range(10))
74+
75+
# assert result == True
76+
77+
def test_adaptive_mutation_int_gene_type():
78+
result, ga_instance = output_adaptive_mutation(gene_type=int)
79+
80+
# assert result == True
81+
82+
def test_adaptive_mutation_gene_space_gene_type():
83+
result, ga_instance = output_adaptive_mutation(gene_space={"low": 0, "high": 10},
84+
gene_type=[float, 2])
85+
86+
# assert result == True
87+
88+
def test_adaptive_mutation_nested_gene_space():
89+
result, ga_instance = output_adaptive_mutation(gene_space=[[0, 1, 2, 3, 4],
90+
numpy.arange(5, 10),
91+
range(10, 15),
92+
{"low": 15, "high": 20},
93+
{"low": 20, "high": 30, "step": 2},
94+
None,
95+
numpy.arange(30, 35),
96+
numpy.arange(35, 40),
97+
numpy.arange(40, 45),
98+
[45, 46, 47, 48, 49]])
99+
# assert result == True
100+
101+
def test_adaptive_mutation_nested_gene_type():
102+
result, ga_instance = output_adaptive_mutation(gene_type=[int, float, numpy.float64, [float, 3], [float, 4], numpy.int16, [numpy.float32, 1], int, float, [float, 3]])
103+
104+
# assert result == True
105+
106+
def test_adaptive_mutation_nested_gene_space_nested_gene_type():
107+
result, ga_instance = output_adaptive_mutation(gene_space=[[0, 1, 2, 3, 4],
108+
numpy.arange(5, 10),
109+
range(10, 15),
110+
{"low": 15, "high": 20},
111+
{"low": 20, "high": 30, "step": 2},
112+
None,
113+
numpy.arange(30, 35),
114+
numpy.arange(35, 40),
115+
numpy.arange(40, 45),
116+
[45, 46, 47, 48, 49]],
117+
gene_type=[int, float, numpy.float64, [float, 3], [float, 4], numpy.int16, [numpy.float32, 1], int, float, [float, 3]])
118+
119+
# assert result == True
120+
121+
def test_adaptive_mutation_initial_population():
122+
global initial_population
123+
result, ga_instance = output_adaptive_mutation(initial_population=initial_population)
124+
125+
# assert result == True
126+
127+
def test_adaptive_mutation_initial_population_nested_gene_type():
128+
global initial_population
129+
result, ga_instance = output_adaptive_mutation(initial_population=initial_population,
130+
gene_type=[int, float, numpy.float64, [float, 3], [float, 4], numpy.int16, [numpy.float32, 1], int, float, [float, 3]])
131+
132+
# assert result == True
133+
134+
def test_adaptive_mutation_fitness_batch_size_1():
135+
result, ga_instance = output_adaptive_mutation(fitness_batch_size=1)
136+
137+
def test_adaptive_mutation_fitness_batch_size_2():
138+
result, ga_instance = output_adaptive_mutation(fitness_batch_size=2)
139+
140+
def test_adaptive_mutation_fitness_batch_size_3():
141+
result, ga_instance = output_adaptive_mutation(fitness_batch_size=3)
142+
143+
def test_adaptive_mutation_fitness_batch_size_4():
144+
result, ga_instance = output_adaptive_mutation(fitness_batch_size=4)
145+
146+
def test_adaptive_mutation_fitness_batch_size_5():
147+
result, ga_instance = output_adaptive_mutation(fitness_batch_size=5)
148+
149+
def test_adaptive_mutation_fitness_batch_size_6():
150+
result, ga_instance = output_adaptive_mutation(fitness_batch_size=6)
151+
152+
def test_adaptive_mutation_fitness_batch_size_7():
153+
result, ga_instance = output_adaptive_mutation(fitness_batch_size=7)
154+
155+
def test_adaptive_mutation_fitness_batch_size_8():
156+
result, ga_instance = output_adaptive_mutation(fitness_batch_size=8)
157+
158+
def test_adaptive_mutation_fitness_batch_size_9():
159+
result, ga_instance = output_adaptive_mutation(fitness_batch_size=9)
160+
161+
def test_adaptive_mutation_fitness_batch_size_10():
162+
result, ga_instance = output_adaptive_mutation(fitness_batch_size=10)
163+
164+
if __name__ == "__main__":
165+
print()
166+
test_adaptive_mutation()
167+
print()
168+
169+
test_adaptive_mutation_int_gene_type()
170+
print()
171+
172+
test_adaptive_mutation_gene_space()
173+
print()
174+
175+
test_adaptive_mutation_gene_space_gene_type()
176+
print()
177+
178+
test_adaptive_mutation_nested_gene_space()
179+
print()
180+
181+
test_adaptive_mutation_nested_gene_type()
182+
print()
183+
184+
test_adaptive_mutation_initial_population()
185+
print()
186+
187+
test_adaptive_mutation_initial_population_nested_gene_type()
188+
print()
189+
190+
test_adaptive_mutation_fitness_batch_size_1()
191+
print()
192+
193+
test_adaptive_mutation_fitness_batch_size_1()
194+
print()
195+
196+
test_adaptive_mutation_fitness_batch_size_2()
197+
print()
198+
199+
test_adaptive_mutation_fitness_batch_size_3()
200+
print()
201+
202+
test_adaptive_mutation_fitness_batch_size_4()
203+
print()
204+
205+
test_adaptive_mutation_fitness_batch_size_5()
206+
print()
207+
208+
test_adaptive_mutation_fitness_batch_size_6()
209+
print()
210+
211+
test_adaptive_mutation_fitness_batch_size_7()
212+
print()
213+
214+
test_adaptive_mutation_fitness_batch_size_8()
215+
print()
216+
217+
test_adaptive_mutation_fitness_batch_size_9()
218+
print()
219+
220+
test_adaptive_mutation_fitness_batch_size_10()
221+
print()
222+

0 commit comments

Comments
 (0)