def test_iddfs(): print("--- Solving NQueens with IDDFS Tree") nqueens = NQueens(5, pruning=True) iddfs_tree = ID_DFS(verbose=False, tree=True) solution = iddfs_tree.solve(nqueens) iddfs_tree.print_solution() print("") print("--- Solving Towers with IDDFS Graph") towers = TowersOfHanoi(tower_size=4) iddfs_graph = ID_DFS(verbose=False, tree=False) solution = iddfs_graph.solve(towers) iddfs_graph.print_solution()
def test_iddfs(): print("-----------------------------------------------------------") print("--- Solving NQueens with BFS Tree") print("-----------------------------------------------------------") x = 4 start = timeit.default_timer() while ((timeit.default_timer() - start) < 120): print("--- Solving NQueens with BFS Tree size", x) start = timeit.default_timer() nqueens = NQueens(x, pruning=True) iddfs_tree = BFS_Tree(verbose=False) solution = iddfs_tree.solve(nqueens) #iddfs_tree.print_solution() iddfs_tree.print_stats() stop = timeit.default_timer() print('Time: ', stop - start) print("") x = x + 1 '''print("--- Solving NQueens with IDDFS Tree") nqueens = NQueens(5,pruning=True) iddfs_tree = ID_DFS(verbose=False, tree=True) solution = iddfs_tree.solve(nqueens) iddfs_tree.print_solution() iddfs_tree.print_stats()''' '''print("") print("--- Solving Towers with IDDFS Graph") towers = TowersOfHanoi(tower_size=9) #iddfs_graph = ID_DFS(verbose=False, tree=False, duplication="advanced_list") iddfs_graph = BFS_Graph(verbose=False, duplication="simple_list") solution = iddfs_graph.solve(towers) iddfs_graph.print_solution() iddfs_graph.print_stats()''' '''print("") print("--- Solving Towers with BFS Graph") towers = TowersOfHanoi(tower_size=4) bfs_graph = BFS_Graph(verbose=False) solution = bfs_graph.solve(towers) bfs_graph.print_solution() bfs_graph.print_stats() print("") print("--- Solving Towers with DFS Graph") towers = TowersOfHanoi(tower_size=4) dfs_graph = DFS_Graph(verbose=False) solution = dfs_graph.solve(towers) dfs_graph.print_solution() dfs_graph.print_stats()''' '''print("-----------------------------------------------------------")
from nqueens import NQueens import sys a = NQueens() n = int(input("A PORRA DO NUMERO DAS RAINHAS: ")) a.new_n_queens(n, 10000 * n)
import optparse from nqueens import NQueens if __name__ == '__main__': parser = optparse.OptionParser() parser.add_option('-d', dest='dimension', type='int', metavar="Dimension") parser.add_option('-p', dest='population_count', type='int', metavar="Population Count") parser.add_option('-m', dest='mutation_factor', type='float', metavar="Mutation Factor") parser.add_option('-i', dest='iteration', type='int', metavar="Iteration") (options, args) = parser.parse_args() nqueens = NQueens(5, 5, .5, 100) nqueens.solve()
__author__ = 'Alireza Mirzaeiyan' import optparse from nqueens import NQueens if __name__ == '__main__': parser = optparse.OptionParser() parser.add_option('-d', dest='dimension', type='int', metavar="Dimension") parser.add_option('-p', dest='population_count', type='int', metavar="Population Count") parser.add_option('-m', dest='mutation_factor', type='float', metavar="Mutation Factor") parser.add_option('-i', dest='iteration', type='int', metavar="Iteration") (options, args) = parser.parse_args() nqueens = NQueens(options.dimension, options.population_count, options.mutation_factor, options.iteration) nqueens.solve()
def test_n_nine(self): x = NQueens(9) assert x.solutions == 352
def test_n_eight(self): x = NQueens(8) assert x.solutions == 92
def test_n_seven(self): x = NQueens(7) assert x.solutions == 40
stop = timeit.default_timer() print('Time: ', stop - start) print("") x = x + 1 print("-----------------------------------------------------------") print("-----------------------------------------------------------") print("-----------------------------------------------------------") print("--- Solving Nqueens with BFS Tree") print("-----------------------------------------------------------") x = 5 start = timeit.default_timer() while((timeit.default_timer()-start)<120): print("--- Nqueens BFS Tree size: ", x) start = timeit.default_timer() nqueens = NQueens(x,pruning=False) search = BFS_Tree() search.solve(nqueens, all_solutions=False) search.print_solution() search.print_stats() stop = timeit.default_timer() print('Time: ', stop - start) x = x + 1 print("-----------------------------------------------------------") print("--- Solving Nqueens with DFS Tree") print("-----------------------------------------------------------") x = 5 start = timeit.default_timer() while((timeit.default_timer()-start)<120): print("--- Nqueens DFS Tree size: ", x)
def test_n_five(self): x = NQueens(5) assert x.solutions == 10
def test_n_four(self): x = NQueens(4) assert x.solutions == 2
def test_n_three(self): x = NQueens(3) assert x.solutions == 0
def test_n_two(self): x = NQueens(2) assert x.solutions == 0
def test_n_one(self): x = NQueens(1) assert x.solutions == 1
print("") x = x + 1''' print("-----------------------------------------------------------") print("--- Solving NQUEENS swap permute") print("-----------------------------------------------------------") z = 15 x = 1 y = 6 start = timeit.default_timer() while (x < y): print("--- Nqueens swap permute size: ", z) start = timeit.default_timer() problem = NQueens(z, neighbor_selection="swap", objective_fn="all", start_fn="permute", start_state=[6, 6, 6, 0, 7, 0, 8, 6, 9, 7]) algorithm = SimulatedAnnealing(verbose=False, alpha=.98, start=10000, end=.25, iterations=500) solution = algorithm.solve(problem) algorithm.print_solution() algorithm.print_stats() stop = timeit.default_timer() print('Time: ', stop - start) print("")
def test_n_six(self): x = NQueens(6) assert x.solutions == 4
import sys sys.path.append('problems') sys.path.append('algorithms') from towers import TowersOfHanoi from nqueens import NQueens from search import BFS_Tree, BFS_Graph from search import DFS_Tree, DFS_Graph # You can try different algorithms with different problems # path=True indicates that the path is part of the solution # FYI: tree searches for Towers take a loooooong time. if __name__ == "__main__": towers = TowersOfHanoi(tower_size=4) nqueens = NQueens(5, pruning=False) #search = BFS_Tree(verbose=False) #search = DFS_Tree(verbose=False) search = DFS_Graph() #search = BFS_Graph() #search.solve(towers, path=True) search.solve(towers, path=True, all_solutions=False) search.print_solution() search.print_stats()
import sys sys.path.append('problems') sys.path.append('algorithms') from nqueens import NQueens from bfs import BFS if __name__ == "__main__": #added just for easy testing, change to True/False if you want/do not want pruning. do_prune = True problem = NQueens(9, do_prune) bfs = BFS() #added just for easy testing, change to True/False if you want/do not want all solutions. find_all_solutions = True bfs.solve(problem, find_all_solutions) #added so duplicate solutions will not be printed if find_all_solutions == False: bfs.print_solution() bfs.print_stats()