Skip to content

Commit 8c1d6ff

Browse files
authored
Merge pull request #345 from ahmedfgad/github-actions
Refactor the pygad.py script
2 parents 9ac7527 + 9d4d1ab commit 8c1d6ff

File tree

15 files changed

+2905
-2366
lines changed

15 files changed

+2905
-2366
lines changed

.github/workflows/scorecard.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
name: Scorecard supply-chain security
66
on:
7+
# This allows you to run the workflow manually from the Actions tab
8+
workflow_dispatch:
9+
710
# For Branch-Protection check. Only the default branch is supported. See
811
# https://github.com/ossf/scorecard/blob/main/docs/checks.md#branch-protection
912
branch_protection_rule:
@@ -61,7 +64,7 @@ jobs:
6164
- name: "Upload artifact"
6265
uses: actions/upload-artifact@97a0fba1372883ab732affbe8f94b823f91727db # v3.pre.node20
6366
with:
64-
name: SARIF file
67+
name: SARIF-file
6568
path: results.sarif
6669
retention-days: 5
6770

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import pygad
2+
import numpy
3+
4+
"""
5+
Use a method to build the lifecycle.
6+
"""
7+
8+
class Fitness:
9+
def __call__(self, ga_instance, solution, solution_idx):
10+
fitness = numpy.sum(solution)
11+
return fitness
12+
13+
class Crossover:
14+
def __call__(self, parents, offspring_size, ga_instance):
15+
return numpy.random.rand(offspring_size[0], offspring_size[1])
16+
17+
class Mutation:
18+
def __call__(self, offspring, ga_instance):
19+
return offspring
20+
21+
class OnStart:
22+
def __call__(self, ga_instance):
23+
print("on_start")
24+
25+
class OnFitness:
26+
def __call__(self, ga_instance, fitness):
27+
print("on_fitness")
28+
29+
class OnCrossover:
30+
def __call__(self, ga_instance, offspring):
31+
print("on_crossover")
32+
33+
class OnMutation:
34+
def __call__(self, ga_instance, offspring):
35+
print("on_mutation")
36+
37+
class OnParents:
38+
def __call__(self, ga_instance, parents):
39+
print("on_parents")
40+
41+
class OnGeneration:
42+
def __call__(self, ga_instance):
43+
print("on_generation")
44+
45+
class OnStop:
46+
def __call__(self, ga_instance, fitness):
47+
print("on_stop")
48+
49+
num_generations = 10 # Number of generations.
50+
num_parents_mating = 5 # Number of solutions to be selected as parents in the mating pool.
51+
52+
sol_per_pop = 10 # Number of solutions in the population.
53+
num_genes = 5
54+
55+
ga_instance = pygad.GA(num_generations=num_generations,
56+
num_parents_mating=num_parents_mating,
57+
sol_per_pop=sol_per_pop,
58+
num_genes=num_genes,
59+
60+
fitness_func=Fitness(),
61+
62+
crossover_type=Crossover(),
63+
mutation_type=Mutation(),
64+
65+
on_start=OnStart(),
66+
on_fitness=OnFitness(),
67+
on_crossover=OnCrossover(),
68+
on_mutation=OnMutation(),
69+
on_parents=OnParents(),
70+
on_generation=OnGeneration(),
71+
on_stop=OnStop(),
72+
73+
suppress_warnings=True)
74+
75+
# Running the GA to optimize the parameters of the function.
76+
ga_instance.run()
77+
78+
ga_instance.plot_fitness()
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import pygad
2+
import numpy
3+
4+
"""
5+
Use a method to build the lifecycle.
6+
"""
7+
8+
class GAOperations:
9+
def fitness_func(self, ga_instance, solution, solution_idx):
10+
fitness = numpy.sum(solution)
11+
return fitness
12+
13+
def crossover(self, parents, offspring_size, ga_instance):
14+
return numpy.random.rand(offspring_size[0], offspring_size[1])
15+
16+
def mutation(self, offspring, ga_instance):
17+
return offspring
18+
19+
class Lifecycle:
20+
def on_start(self, ga_instance):
21+
print("on_start")
22+
23+
def on_fitness(self, ga_instance, fitness):
24+
print("on_fitness")
25+
26+
def on_crossover(self, ga_instance, offspring):
27+
print("on_crossover")
28+
29+
def on_mutation(self, ga_instance, offspring):
30+
print("on_mutation")
31+
32+
def on_parents(self, ga_instance, parents):
33+
print("on_parents")
34+
35+
def on_generation(self, ga_instance):
36+
print("on_generation")
37+
38+
def on_stop(self, ga_instance, fitness):
39+
print("on_stop")
40+
41+
ga_obj = GAOperations()
42+
lifecycle_obj = Lifecycle()
43+
44+
num_generations = 10 # Number of generations.
45+
num_parents_mating = 5 # Number of solutions to be selected as parents in the mating pool.
46+
47+
sol_per_pop = 10 # Number of solutions in the population.
48+
num_genes = 5
49+
50+
ga_instance = pygad.GA(num_generations=num_generations,
51+
num_parents_mating=num_parents_mating,
52+
sol_per_pop=sol_per_pop,
53+
num_genes=num_genes,
54+
55+
fitness_func=ga_obj.fitness_func,
56+
57+
crossover_type=ga_obj.crossover,
58+
mutation_type=ga_obj.mutation,
59+
60+
on_start=lifecycle_obj.on_start,
61+
on_fitness=lifecycle_obj.on_fitness,
62+
on_crossover=lifecycle_obj.on_crossover,
63+
on_mutation=lifecycle_obj.on_mutation,
64+
on_parents=lifecycle_obj.on_parents,
65+
on_generation=lifecycle_obj.on_generation,
66+
on_stop=lifecycle_obj.on_stop,
67+
68+
suppress_warnings=True)
69+
70+
# Running the GA to optimize the parameters of the function.
71+
ga_instance.run()
72+
73+
ga_instance.plot_fitness()

examples/example_summary.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import pygad
2+
import numpy
3+
4+
function_inputs = [4,-2,3.5,5,-11,-4.7] # Function inputs.
5+
desired_output = 44 # Function output.
6+
7+
def fitness_func(ga_instance, solution, solution_idx):
8+
output = numpy.sum(solution*function_inputs)
9+
fitness = 1.0 / (numpy.abs(output - desired_output) + 0.000001)
10+
return fitness
11+
12+
num_generations = 100 # Number of generations.
13+
num_parents_mating = 10 # Number of solutions to be selected as parents in the mating pool.
14+
15+
sol_per_pop = 20 # Number of solutions in the population.
16+
num_genes = len(function_inputs)
17+
18+
ga_instance = pygad.GA(num_generations=num_generations,
19+
num_parents_mating=num_parents_mating,
20+
sol_per_pop=sol_per_pop,
21+
num_genes=num_genes,
22+
fitness_func=fitness_func)
23+
24+
# Running the GA to optimize the parameters of the function.
25+
ga_instance.run()
26+
27+
ga_instance.plot_fitness()
28+
29+
ga_instance.summary()
30+

examples/pygad_lifecycle.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,15 @@ def on_stop(ga_instance, last_population_fitness):
3737
fitness_func=fitness_function,
3838
sol_per_pop=10,
3939
num_genes=len(function_inputs),
40+
4041
on_start=on_start,
4142
on_fitness=on_fitness,
4243
on_parents=on_parents,
4344
on_crossover=on_crossover,
4445
on_mutation=on_mutation,
4546
on_generation=on_generation,
46-
on_stop=on_stop)
47+
on_stop=on_stop,
48+
49+
suppress_warnings=True)
4750

4851
ga_instance.run()

pygad/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
from .pygad import * # Relative import.
22

3-
__version__ = "3.5.0"
3+
__version__ = "3.6.0"

pygad/helper/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
from pygad.helper import unique
22
from pygad.helper import misc
33

4-
__version__ = "1.2.0"
4+
__version__ = "1.3.0"

0 commit comments

Comments
 (0)