def selection_cross_prob(): count = 11 attempts_size = 30 min_values = [] success_count = [] median_values = [] for k in range(0, count, 1): results = [] for _ in range(0, attempts_size): solver = nq.Solver_8_queens(cross_prob=k / 10) best_fit, epoch_num, visualization = solver.solve(max_epochs=500) if best_fit == 1.0: results.append(epoch_num) if len(results) != 0: min_values.append(np.min(results)) success_count.append( int((attempts_size - len(results)) / attempts_size * 100)) median_values.append(np.median(results)) else: min_values.append(501) success_count.append( int((attempts_size - len(results)) / attempts_size * 100)) median_values.append(501) # print('size; ', size) # print('success: ', len(results)) # print('min: ', np.min(results)) # print('median: ', np.median(results)) # print('average: ', np.average(results)) # print('max: ', np.max(results)) # print('\n') pl.subplot(311) pl.plot(range(0, count, 1), min_values, 'r-', range(0, count, 1), min_values, 'ro') pl.subplot(312) pl.plot( range(0, count, 1), median_values, 'g-', range(0, count, 1), median_values, 'go', ) pl.subplot(313) pl.plot( range(0, count, 1), success_count, 'b-', range(0, count, 1), success_count, 'bo', ) pl.show()
def func (x, y): #best_fit= None for i in range(len(x)): for j in range(len(x[0])): solver= nq.Solver_8_queens(pop_size=100, cross_prob=x[i][j], mut_prob=y[i][j]) best_fit, epoch_num, visualization = solver.solve() z[i,j]= best_fit return z
def selection_poll_size(): count = 41 attempts_size = 30 min_values = [] success_count = [] median_values = [] for size in range(2, count, 2): results = [] for _ in range(0, attempts_size): solver = nq.Solver_8_queens(pop_size=size) best_fit, epoch_num, visualization = solver.solve(max_epochs=500) if best_fit == 1.0: results.append(epoch_num) min_values.append(np.min(results)) success_count.append( int((attempts_size - len(results)) / attempts_size * 100)) median_values.append(np.median(results)) # print('size; ', size) # print('success: ', len(results)) # print('min: ', np.min(results)) # print('median: ', np.median(results)) # print('average: ', np.average(results)) # print('max: ', np.max(results)) # print('\n') pl.subplot(311) pl.plot(range(2, count, 2), min_values, 'r-', range(2, count, 2), min_values, 'ro') pl.subplot(312) pl.plot( range(2, count, 2), median_values, 'g-', range(2, count, 2), median_values, 'go', ) pl.subplot(313) pl.plot( range(2, count, 2), success_count, 'b-', range(2, count, 2), success_count, 'bo', ) pl.show()
#!/usr/bin/env python3 # -*- coding: utf-8 -*_ """ This script uses nqueens.py module for solving 8-queens problem by using a generic algorithm """ import sys print('Python version:', sys.version) import nqueens as nq solver = nq.Solver_8_queens() best_fit, epoch_num, visualization = solver.solve() print("Best solution:") print("Fitness:", best_fit) print("Iterations:", epoch_num) print(visualization)
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ This script uses nqueens.py module for solving 8-queens problem by using a genetic algorithm """ import sys print('Python version:', sys.version) import nqueens as nq solver=nq.Solver_8_queens(0.3,0.4,160) best_sol,best_fit, epoch_num, visualization = solver.solve(0.9,100) print("Best solution:",best_sol) print("Fitness:", best_fit) print("Iterations:", epoch_num) print(visualization)