1+
12# python-ne
23A neuroevolution library for python
34
4-
5- # how to install (github)
5+ # how to install
66
77```
88git clone https://github.com/MatheusZickuhr/python-ne.git
@@ -19,7 +19,13 @@ pip install python-ne
1919# example (xor)
2020
2121``` python
22+ from python_ne.core.ga.csv_logger import CsvLogger
23+ from python_ne.core.ga.console_logger import ConsoleLogger
24+ from python_ne.core.ga.matplotlib_logger import MatplotlibLogger
25+ from python_ne.core.ga.mutation_strategies import Mutation1
26+ from python_ne.core.model_adapters.default_model_adapter import DefaultModelAdapter
2227from python_ne.core.ga.genetic_algorithm import GeneticAlgorithm
28+ from python_ne.core.ga.crossover_strategies import Crossover1
2329import numpy as np
2430
2531
@@ -29,33 +35,42 @@ def calc_fitness(element):
2935
3036 fitness = 0
3137 for x, y in zip (X, Y):
32- prediction = element.get_output(x)
38+ prediction = np.argmax( element.get_output(x) )
3339 if prediction == y:
3440 fitness += 1
3541 return fitness
3642
3743
38- genetic_algorithm = GeneticAlgorithm(
39- population_size = 200 ,
40- input_shape = (2 ,),
41- output_size = 2 ,
42- selection_percentage = 0.5 ,
43- mutation_chance = 0.1 ,
44- fitness_threshold = 4 ,
45- ne_type = ' ne' , # ne or neat
46- backend_adapter = ' default' , # default or keras,
47- neural_network_config = [128 , 128 ] # two hidden layers with 128 neurons each
48- )
49-
50- genetic_algorithm.run(
51- number_of_generations = 1000 ,
52- calculate_fitness_callback = calc_fitness
53- )
54-
55- best_element = genetic_algorithm.get_best_element()
56-
57- print (f ' the fitness of the best element is { best_element.fitness} ' )
58-
59-
44+ if __name__ == ' __main__' :
45+ genetic_algorithm = GeneticAlgorithm(
46+ population_size = 200 ,
47+ selection_percentage = 0.9 ,
48+ mutation_chance = 0.01 ,
49+ fitness_threshold = 4 ,
50+ model_adapter = DefaultModelAdapter,
51+ crossover_strategy = Crossover1(),
52+ mutation_strategy = Mutation1(),
53+ neural_network_config = [((2 ,), 8 , ' tanh' ), (2 , ' tanh' )],
54+ )
55+
56+ csv_logger = CsvLogger()
57+ genetic_algorithm.add_observer(csv_logger)
58+ matplotlib_logger = MatplotlibLogger()
59+ genetic_algorithm.add_observer(matplotlib_logger)
60+ genetic_algorithm.add_observer(ConsoleLogger())
61+
62+ genetic_algorithm.run(
63+ number_of_generations = 1000 ,
64+ calculate_fitness_callback = calc_fitness
65+ )
66+
67+ csv_logger.save(' file.csv' )
68+ matplotlib_logger.save_fitness_chart(' fitness.png' )
69+ matplotlib_logger.save_time_chart(' time.png' )
70+ matplotlib_logger.save_std_chart(' std.png' )
71+
72+ best_element = genetic_algorithm.get_best_element()
73+
74+ print (f ' the fitness of the best element is { best_element.raw_fitness} ' )
6075
6176```
0 commit comments