Пример #1
0
def fitness_func(solution, sol_idx):
    global data_inputs, data_outputs, kerasGA, model

    predictions = predict(model=model, solution=solution, data=data_inputs)

    mae = MeanAbsoluteError()
    abs_error = mae(y_true=data_outputs, y_pred=predictions).numpy() + 0.00000001
    solution_fitness = 1.0 / abs_error

    return solution_fitness
Пример #2
0
                       parent_selection_type='rank',
                       fitness_func=fitness_func, # initial_population=kerasGA.population_weights,
                       sol_per_pop=9, num_genes=9, init_range_low=0.01, init_range_high=10.00,
                       crossover_type='single_point', mutation_type='random',
                       mutation_num_genes=2, save_best_solutions=True, save_solutions=True,
                       allow_duplicate_genes=True, stop_criteria='saturate_10',
                       on_generation=callback_generation)

# Run the Genetic algorithm
ga_instance.run()

# Plot the fitness value
ga_instance.plot_fitness(title='Iteration vs. Fitness', xlabel='Generation', ylabel='Fitness',
                         linewidth=4)

# To get the details about the best solution found by PyGAD
solution, solution_fitness, solution_idx = ga_instance.best_solution()
print(f'Fitness value of the best solution = '
      f'{solution_fitness}'.format(solution_fitness=solution_fitness))
print(f'Index of the best solution: {solution_idx}'.format(solution_idx=solution_idx))

# Make prediction based on the trained model's best solution.
predictions = predict(model=model, solution=solution, data=data_inputs)

print('Predictions: \n', predictions)

# Measure the trained model error.
mae = MeanAbsoluteError()
abs_error = mae(y_true=data_outputs, y_pred=predictions).numpy()
print('Absolute Error:', abs_error)
Пример #3
0
# Run the Genetic algorithm
ga_instance.run()

# Plot the fitness value
ga_instance.plot_fitness(title='Iteration vs. Fitness',
                         xlabel='Generation',
                         ylabel='Fitness',
                         linewidth=4)

# To get the details about the best solution found by PyGAD
solution, solution_fitness, solution_idx = ga_instance.best_solution()
print('Weights of the best solution: {solution}'.format(solution=solution))
print(f'Fitness value of the best solution = '
      f'{solution_fitness}'.format(solution_fitness=solution_fitness))
print(f'Index of the best solution: {solution_idx}'.format(
    solution_idx=solution_idx))

# Make prediction based on the trained model's best solution.
predictions = predict(model=model, solution=solution, data=data_inputs)

print('Predictions: \n', predictions)

# Measure the trained model error.
mae = MeanAbsoluteError()
abs_error = mae(y_true=data_outputs, y_pred=predictions).numpy()
print('Absolute Error:', abs_error)

data_inputs_homework = np.array([[0.8, 0.7], [0.8, 0.2], [0.2, 0.3]])
prediction = predict(model=model, solution=solution, data=data_inputs_homework)
print('Prediction: \n', prediction)