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 b028c2f

Browse files
Merge pull request #187 from ahmedfgad/github-actions
GitHub Actions Workflows and new additions
2 parents 7dac6f0 + d8b6372 commit b028c2f

39 files changed

+32865
-26604
lines changed

‎.github/workflows/main.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: Testing PyGAD using PyTest
2+
3+
on:
4+
push:
5+
branches:
6+
- github-actions
7+
# - master
8+
9+
jobs:
10+
job_id_1:
11+
runs-on: ubuntu-latest
12+
name: PyTest Workflow Job
13+
14+
steps:
15+
- name: Checkout Pre-Built Action
16+
uses: actions/checkout@v3
17+
18+
- name: Setup Python 3.10
19+
uses: actions/setup-python@v4
20+
with:
21+
python-version: '3.10'
22+
23+
- name: Build PyGAD from the Repository
24+
run: |
25+
python3 -m pip install --upgrade build
26+
python3 -m build
27+
28+
- name: Install PyGAD after Building the .whl File
29+
run: |
30+
find ./dist/*.whl | xargs pip install
31+
32+
- name: Install PyTest
33+
run: pip install pytest
34+
35+
- name: Run the Tests by Calling PyTest
36+
run: |
37+
pytest
File renamed without changes.

‎example_clustering_2.py renamed to ‎examples/example_clustering_2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,4 @@ def fitness_func(ga_instance, solution, solution_idx):
119119
matplotlib.pyplot.scatter(cluster_x, cluster_y)
120120
matplotlib.pyplot.scatter(cluster_centers[cluster_idx, 0], cluster_centers[cluster_idx, 1], linewidths=5)
121121
matplotlib.pyplot.title("Clustering using PyGAD")
122-
matplotlib.pyplot.show()
122+
matplotlib.pyplot.show()
File renamed without changes.

‎example_custom_operators.py renamed to ‎examples/example_custom_operators.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,4 @@ def mutation_func(offspring, ga_instance):
7171
mutation_type=mutation_func)
7272

7373
ga_instance.run()
74-
ga_instance.plot_fitness()
74+
ga_instance.plot_fitness()

‎examples/example_fitness_wrapper.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import pygad
2+
import numpy
3+
4+
"""
5+
All the callback functions/methods in PyGAD have limits in the number of arguments passed.
6+
For example, the fitness function accepts only 3 arguments:
7+
1. The pygad.GA instance.
8+
2. The solution(s).
9+
3. The index (indices) of the passed solution(s).
10+
If it is necessary to pass extra arguments to the fitness function, for example, then follow these steps:
11+
1. Create a wrapper function that accepts only the number of arguments meeded by PyGAD.
12+
2. Define the extra arguments in the body of the wrapper function.
13+
3. Create an inner fitness function inside the wrapper function with whatever extra arguments needed.
14+
4. Call the inner fitness function from the wrapper function while passing the extra arguments.
15+
16+
This is an example that passes a list ([10, 20, 30]) to the inner fitness function. The list has 3 numbers.
17+
A number is randomly selected from the list and added to the calculated fitness.
18+
"""
19+
20+
function_inputs = [4,-2,3.5,5,-11,-4.7]
21+
desired_output = 44
22+
23+
def fitness_func_wrapper(ga_instanse, solution, solution_idx):
24+
def fitness_func(ga_instanse, solution, solution_idx, *args):
25+
output = numpy.sum(solution*function_inputs)
26+
output += numpy.random.choice(args)
27+
fitness = 1.0 / (numpy.abs(output - desired_output) + 0.000001)
28+
return fitness
29+
args = [10, 20, 30]
30+
fitness = fitness_func(ga_instanse, solution, solution_idx, *args)
31+
return fitness
32+
33+
ga_instance = pygad.GA(num_generations=3,
34+
num_parents_mating=5,
35+
fitness_func=fitness_func_wrapper,
36+
sol_per_pop=10,
37+
num_genes=len(function_inputs),
38+
suppress_warnings=True)
39+
40+
ga_instance.run()
41+
ga_instance.plot_fitness()
File renamed without changes.

‎example_travelling_salesman.ipynb renamed to ‎examples/example_travelling_salesman.ipynb

Lines changed: 26535 additions & 26535 deletions
Large diffs are not rendered by default.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
(0)

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