Skip to content

Commit aab09bd

Browse files
committed
Run tests
1 parent 21f4788 commit aab09bd

File tree

5 files changed

+78
-7
lines changed

5 files changed

+78
-7
lines changed

examples/example.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@ def on_generation(ga_instance):
4444

4545
# Returning the details of the best solution.
4646
solution, solution_fitness, solution_idx = ga_instance.best_solution(ga_instance.last_generation_fitness)
47-
print("Parameters of the best solution : {solution}".format(solution=solution))
48-
print("Fitness value of the best solution = {solution_fitness}".format(solution_fitness=solution_fitness))
49-
print("Index of the best solution : {solution_idx}".format(solution_idx=solution_idx))
47+
print(f"Parameters of the best solution : {solution}")
48+
print(f"Fitness value of the best solution = {solution_fitness}")
49+
print(f"Index of the best solution : {solution_idx}")
5050

5151
prediction = numpy.sum(numpy.array(function_inputs)*solution)
52-
print("Predicted output based on the best solution : {prediction}".format(prediction=prediction))
52+
print(f"Predicted output based on the best solution : {prediction}")
5353

5454
if ga_instance.best_solution_generation != -1:
55-
print("Best fitness value reached after {best_solution_generation} generations.".format(best_solution_generation=ga_instance.best_solution_generation))
55+
print(f"Best fitness value reached after {ga_instance.best_solution_generation} generations.")
5656

5757
# Saving the GA instance.
5858
filename = 'genetic' # The filename to which the instance is saved. The name is without extension.

examples/example_multi_objective.py

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import pygad
2+
import numpy
3+
4+
"""
5+
Given these 2 functions:
6+
y1 = f(w1:w6) = w1x1 + w2x2 + w3x3 + w4x4 + w5x5 + 6wx6
7+
y2 = f(w1:w6) = w1x7 + w2x8 + w3x9 + w4x10 + w5x11 + 6wx12
8+
where (x1,x2,x3,x4,x5,x6)=(4,-2,3.5,5,-11,-4.7) and y=50
9+
and (x7,x8,x9,x10,x11,x12)=(-2,0.7,-9,1.4,3,5) and y=30
10+
What are the best values for the 6 weights (w1 to w6)? We are going to use the genetic algorithm to optimize these 2 functions.
11+
This is a multi-objective optimization problem.
12+
13+
PyGAD considers the problem as multi-objective if the fitness function returns:
14+
1) List.
15+
2) Or tuple.
16+
3) Or numpy.ndarray.
17+
"""
18+
19+
function_inputs1 = [4,-2,3.5,5,-11,-4.7] # Function 1 inputs.
20+
function_inputs2 = [-2,0.7,-9,1.4,3,5] # Function 2 inputs.
21+
desired_output1 = 50 # Function 1 output.
22+
desired_output2 = 30 # Function 2 output.
23+
24+
def fitness_func(ga_instance, solution, solution_idx):
25+
output1 = numpy.sum(solution*function_inputs1)
26+
output2 = numpy.sum(solution*function_inputs2)
27+
fitness1 = 1.0 / (numpy.abs(output1 - desired_output1) + 0.000001)
28+
fitness2 = 1.0 / (numpy.abs(output2 - desired_output2) + 0.000001)
29+
return [fitness1, fitness2]
30+
31+
num_generations = 100 # Number of generations.
32+
num_parents_mating = 10 # Number of solutions to be selected as parents in the mating pool.
33+
34+
sol_per_pop = 20 # Number of solutions in the population.
35+
num_genes = len(function_inputs1)
36+
37+
last_fitness = 0
38+
def on_generation(ga_instance):
39+
global last_fitness
40+
print("Generation = {generation}".format(generation=ga_instance.generations_completed))
41+
print("Fitness = {fitness}".format(fitness=ga_instance.best_solution(pop_fitness=ga_instance.last_generation_fitness)[1]))
42+
print("Change = {change}".format(change=ga_instance.best_solution(pop_fitness=ga_instance.last_generation_fitness)[1] - last_fitness))
43+
last_fitness = ga_instance.best_solution(pop_fitness=ga_instance.last_generation_fitness)[1]
44+
45+
ga_instance = pygad.GA(num_generations=num_generations,
46+
num_parents_mating=num_parents_mating,
47+
sol_per_pop=sol_per_pop,
48+
num_genes=num_genes,
49+
fitness_func=fitness_func,
50+
parent_selection_type='nsga2',
51+
on_generation=on_generation)
52+
53+
# Running the GA to optimize the parameters of the function.
54+
ga_instance.run()
55+
56+
ga_instance.plot_fitness()
57+
58+
# Returning the details of the best solution.
59+
solution, solution_fitness, solution_idx = ga_instance.best_solution(ga_instance.last_generation_fitness)
60+
print("Parameters of the best solution : {solution}".format(solution=solution))
61+
print("Fitness value of the best solution = {solution_fitness}".format(solution_fitness=solution_fitness))
62+
print("Index of the best solution : {solution_idx}".format(solution_idx=solution_idx))
63+
64+
prediction = numpy.sum(numpy.array(function_inputs1)*solution)
65+
print("Predicted output 1 based on the best solution : {prediction}".format(prediction=prediction))
66+
prediction = numpy.sum(numpy.array(function_inputs2)*solution)
67+
print("Predicted output 2 based on the best solution : {prediction}".format(prediction=prediction))
68+
69+
if ga_instance.best_solution_generation != -1:
70+
print("Best fitness value reached after {best_solution_generation} generations.".format(best_solution_generation=ga_instance.best_solution_generation))
71+

examples/genetic.pkl

17.8 KB
Binary file not shown.

pygad/utils/mutation.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ def adaptive_mutation_randomly(self, offspring):
704704
# Compare the fitness of each offspring to the average fitness of each objective function.
705705
fitness_comparison = offspring_fitness[offspring_idx] < average_fitness
706706
# Check if the problem is single or multi-objective optimization.
707-
if type(fitness_comparison) is bool:
707+
if type(fitness_comparison) in [bool, numpy.bool_]:
708708
# Single-objective optimization problem.
709709
if fitness_comparison:
710710
adaptive_mutation_num_genes = self.mutation_num_genes[0]

pygad/visualize/plot.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def plot_fitness(self,
4444
raise RuntimeError("The plot_fitness() (i.e. plot_result()) method can only be called after completing at least 1 generation but ({self.generations_completed}) is completed.")
4545

4646
fig = matplotlib.pyplot.figure()
47-
if len(self.best_solutions_fitness[0]) > 1:
47+
if type(self.best_solutions_fitness[0]) in [list, tuple, numpy.ndarray] and len(self.best_solutions_fitness[0]) > 1:
4848
# Multi-objective optimization problem.
4949
if type(linewidth) in pygad.GA.supported_int_float_types:
5050
linewidth = [linewidth]

0 commit comments

Comments
 (0)