You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The documentation of PyGAD is available at [Read The Docs](https://pygad.readthedocs.io/)https://pygad.readthedocs.io.
45
46
@@ -57,6 +58,136 @@ If you built a project that uses PyGAD, then please drop an e-mail to ahmed.f.ga
57
58
58
59
Please check the **Contact Us** section for more contact details.
59
60
61
+
# Example
62
+
63
+
Check the [PyGAD's documentation](https://pygad.readthedocs.io/en/latest/README_pygad_gann_ReadTheDocs.html) for information about the implementation of this example.
# Holds the fitness value of the previous generation.
96
+
last_fitness =0
97
+
98
+
# Preparing the NumPy array of the inputs.
99
+
data_inputs = numpy.array([[1, 1],
100
+
[1, 0],
101
+
[0, 1],
102
+
[0, 0]])
103
+
104
+
# Preparing the NumPy array of the outputs.
105
+
data_outputs = numpy.array([0,
106
+
1,
107
+
1,
108
+
0])
109
+
110
+
# The length of the input vector for each sample (i.e. number of neurons in the input layer).
111
+
num_inputs = data_inputs.shape[1]
112
+
# The number of neurons in the output layer (i.e. number of classes).
113
+
num_classes =2
114
+
115
+
# Creating an initial population of neural networks. The return of the initial_population() function holds references to the networks, not their weights. Using such references, the weights of all networks can be fetched.
116
+
num_solutions =6# A solution or a network can be used interchangeably.
# population does not hold the numerical weights of the network instead it holds a list of references to each last layer of each network (i.e. solution) in the population. A solution or a network can be used interchangeably.
125
+
# If there is a population with 3 solutions (i.e. networks), then the population is a list with 3 elements. Each element is a reference to the last layer of each network. Using such a reference, all details of the network can be accessed.
# To prepare the initial population, there are 2 ways:
129
+
# 1) Prepare it yourself and pass it to the initial_population parameter. This way is useful when the user wants to start the genetic algorithm with a custom initial population.
130
+
# 2) Assign valid integer values to the sol_per_pop and num_genes parameters. If the initial_population parameter exists, then the sol_per_pop and num_genes parameters are useless.
131
+
initial_population = population_vectors.copy()
132
+
133
+
num_parents_mating =4# Number of solutions to be selected as parents in the mating pool.
134
+
135
+
num_generations =500# Number of generations.
136
+
137
+
mutation_percent_genes =5# Percentage of genes to mutate. This parameter has no action if the parameter mutation_num_genes exists.
138
+
139
+
parent_selection_type ="sss"# Type of parent selection.
140
+
141
+
crossover_type ="single_point"# Type of the crossover operator.
142
+
143
+
mutation_type ="random"# Type of the mutation operator.
144
+
145
+
keep_parents =1# Number of parents to keep in the next population. -1 means keep all parents and 0 means keep nothing.
print("Parameters of the best solution : {solution}".format(solution=solution))
171
+
print("Fitness value of the best solution = {solution_fitness}".format(solution_fitness=solution_fitness))
172
+
print("Index of the best solution : {solution_idx}".format(solution_idx=solution_idx))
173
+
174
+
if ga_instance.best_solution_generation !=-1:
175
+
print("Best fitness value reached after {best_solution_generation} generations.".format(best_solution_generation=ga_instance.best_solution_generation))
176
+
177
+
# Predicting the outputs of the data using the best solution.
0 commit comments