Skip to content

Commit

Permalink
Run tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmedfgad committed Sep 7, 2023
1 parent 21f4788 commit aab09bd
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 7 deletions.
10 changes: 5 additions & 5 deletions examples/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ def on_generation(ga_instance):

# Returning the details of the best solution.
solution, solution_fitness, solution_idx = ga_instance.best_solution(ga_instance.last_generation_fitness)
print("Parameters of the best solution : {solution}".format(solution=solution))
print("Fitness value of the best solution = {solution_fitness}".format(solution_fitness=solution_fitness))
print("Index of the best solution : {solution_idx}".format(solution_idx=solution_idx))
print(f"Parameters of the best solution : {solution}")
print(f"Fitness value of the best solution = {solution_fitness}")
print(f"Index of the best solution : {solution_idx}")

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

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

# Saving the GA instance.
filename = 'genetic' # The filename to which the instance is saved. The name is without extension.
Expand Down
71 changes: 71 additions & 0 deletions examples/example_multi_objective.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import pygad
import numpy

"""
Given these 2 functions:
y1 = f(w1:w6) = w1x1 + w2x2 + w3x3 + w4x4 + w5x5 + 6wx6
y2 = f(w1:w6) = w1x7 + w2x8 + w3x9 + w4x10 + w5x11 + 6wx12
where (x1,x2,x3,x4,x5,x6)=(4,-2,3.5,5,-11,-4.7) and y=50
and (x7,x8,x9,x10,x11,x12)=(-2,0.7,-9,1.4,3,5) and y=30
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.
This is a multi-objective optimization problem.
PyGAD considers the problem as multi-objective if the fitness function returns:
1) List.
2) Or tuple.
3) Or numpy.ndarray.
"""

function_inputs1 = [4,-2,3.5,5,-11,-4.7] # Function 1 inputs.
function_inputs2 = [-2,0.7,-9,1.4,3,5] # Function 2 inputs.
desired_output1 = 50 # Function 1 output.
desired_output2 = 30 # Function 2 output.

def fitness_func(ga_instance, solution, solution_idx):
output1 = numpy.sum(solution*function_inputs1)
output2 = numpy.sum(solution*function_inputs2)
fitness1 = 1.0 / (numpy.abs(output1 - desired_output1) + 0.000001)
fitness2 = 1.0 / (numpy.abs(output2 - desired_output2) + 0.000001)
return [fitness1, fitness2]

num_generations = 100 # Number of generations.
num_parents_mating = 10 # Number of solutions to be selected as parents in the mating pool.

sol_per_pop = 20 # Number of solutions in the population.
num_genes = len(function_inputs1)

last_fitness = 0
def on_generation(ga_instance):
global last_fitness
print("Generation = {generation}".format(generation=ga_instance.generations_completed))
print("Fitness = {fitness}".format(fitness=ga_instance.best_solution(pop_fitness=ga_instance.last_generation_fitness)[1]))
print("Change = {change}".format(change=ga_instance.best_solution(pop_fitness=ga_instance.last_generation_fitness)[1] - last_fitness))
last_fitness = ga_instance.best_solution(pop_fitness=ga_instance.last_generation_fitness)[1]

ga_instance = pygad.GA(num_generations=num_generations,
num_parents_mating=num_parents_mating,
sol_per_pop=sol_per_pop,
num_genes=num_genes,
fitness_func=fitness_func,
parent_selection_type='nsga2',
on_generation=on_generation)

# Running the GA to optimize the parameters of the function.
ga_instance.run()

ga_instance.plot_fitness()

# Returning the details of the best solution.
solution, solution_fitness, solution_idx = ga_instance.best_solution(ga_instance.last_generation_fitness)
print("Parameters of the best solution : {solution}".format(solution=solution))
print("Fitness value of the best solution = {solution_fitness}".format(solution_fitness=solution_fitness))
print("Index of the best solution : {solution_idx}".format(solution_idx=solution_idx))

prediction = numpy.sum(numpy.array(function_inputs1)*solution)
print("Predicted output 1 based on the best solution : {prediction}".format(prediction=prediction))
prediction = numpy.sum(numpy.array(function_inputs2)*solution)
print("Predicted output 2 based on the best solution : {prediction}".format(prediction=prediction))

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

Binary file added examples/genetic.pkl
Binary file not shown.
2 changes: 1 addition & 1 deletion pygad/utils/mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ def adaptive_mutation_randomly(self, offspring):
# Compare the fitness of each offspring to the average fitness of each objective function.
fitness_comparison = offspring_fitness[offspring_idx] < average_fitness
# Check if the problem is single or multi-objective optimization.
if type(fitness_comparison) is bool:
if type(fitness_comparison) in [bool, numpy.bool_]:
# Single-objective optimization problem.
if fitness_comparison:
adaptive_mutation_num_genes = self.mutation_num_genes[0]
Expand Down
2 changes: 1 addition & 1 deletion pygad/visualize/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def plot_fitness(self,
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.")

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

0 comments on commit aab09bd

Please sign in to comment.