Ejemplo n.º 1
0
	def __init__(self):
		Astar.__init__(self)
		Bfs.__init__(self)

		pygame.init()
		self.display_width = 1020+2+200
		self.display_height = 570+2+300-210
		self.screen = pygame.display.set_mode((self.display_width,self.display_height))
		self.clock = pygame.time.Clock()
		pygame.display.set_caption(u'Path Finding Visualizer')
		self.width = 34
		self.height =22

		self.walls = list()
		self.maze = [[]]

		self.white = (255,255,255)
		self.red = (255,0,0)
		self.less_red = (150,0,0)
		self.blue = (0,255,0)
		self.less_blue = (0,150,0)
		self.green = (0,0,255)
		self.less_green = (0,0,150)
		self.black = (0,0,0)
		self.grid_color = (0,150,255)
Ejemplo n.º 2
0
def use_bfs(initial_state, order, solution_filename, additional_filename):
    bfs = Bfs(initial_state, order)
    result = bfs.run_search()
    
    print_result(result)

    save_final_data(result, solution_filename, additional_filename)
Ejemplo n.º 3
0
    def transformaVetor(self, vetor, capital):

        lista = Lista(len(vetor))
        for i in range(len(vetor)):
            if vetor[i] != i:
                lista.addAresta(vetor[i], i)

        print("\n Lista de Adjacencia Gerada A partir do Vetor")
        lista.mostraLista()

        distancias = Bfs(lista.getLista(), capital)

        return distancias.get_distancias()
Ejemplo n.º 4
0
def main():
    G = Graph()
    G.buildGraph(argv[2])
    
    if argv[1] == 'bfs':
        s = G.getInitVertex()
        bfs = Bfs()
        bfs.execute(G, s)
        
    elif argv[1] == 'scc':
        scc = Scc()
        scc.execute(G)
        
    elif argv[1] == 'bf':
        s = G.getInitVertex()
        bf = Bf()
        bf.execute(G, s)
    
    elif argv[1] == 'bfall':
        bf = Bf()
        for key, s in G.getVertexes().iteritems():
            bf.execute(G, s)
    
    elif argv[1] == 'dk':
        s = G.getInitVertex()
        dk = Dk()
        dk.execute(G, s)
    
    elif argv[1] == 'dkall':
        dk = Dk()
        for key, s in G.getVertexes().iteritems():
            dk.execute(G, s)
    
    elif argv[1] == 'fw':
        W = G.getAdjMatrix()
        fw = Fw()
        fw.execute(G, W)
Ejemplo n.º 5
0
	def bfs(self):
		a,path = Bfs.search(self,self.maze,self.start_node,self.end_node)
		for a in a:
			
			pygame.draw.rect(self.screen,self.less_red,(a[0]*30,a[1]*30,29,29))
			pygame.display.update()
			self.clock.tick(60)
			
		draw = []		
		for i,j in enumerate(path):
			for k,l in enumerate(j):
				if l is not -1:
					draw.append((i,k))

		for i in draw[::-1]:
			
			pygame.draw.rect(self.screen,self.red,(i[0]*30,i[1]*30,29,29))
			self.clock.tick(200)
			pygame.display.update()
Ejemplo n.º 6
0
from vertex import Vertex
from program import WorkWithFile
import time
import sys

if __name__ == '__main__':
    algorithm = sys.argv[1]
    option = sys.argv[2]
    input_file = sys.argv[3]
    solution_file = sys.argv[4]
    statistic_file = sys.argv[5]
    work_with_file = WorkWithFile()
    init_vertex = Vertex(work_with_file.read_board(input_file))

    start_time = time.process_time()

    switch = {
        'bfs': Bfs(option).steps(init_vertex)
    }

    end_time = time.process_time()

    solution = switch.get(algorithm.lower())

    to_solution_file = work_with_file.prepare_solution(solution)
    to_stats_file = work_with_file.prepare_stats(solution, start_time, end_time)

    work_with_file.write_to_file(solution_file, to_solution_file)
    work_with_file.write_to_file(statistic_file, to_stats_file)

Ejemplo n.º 7
0
from Dfs import Dfs
from bfs import Bfs

peta1 = {'K': set(['M']),
         'M': set(['T', 'S','K','N']),
         'N': set(['M']),
         'S': set(['M', 'T', 'R']),
         'T': set(['M', 'S', 'U']),
         'R': set(['S', 'U']),
         'U': set(['T', 'R']),}


app = Flask(__name__)

dfs = Dfs('M', 'U', peta1)
bfs = Bfs('M', 'U', peta1)

@app.route('/dfs')
def dfs_searching():
    hasil = dfs.proses_searching()
    return render_template('index.html',Hasil =hasil )


@app.route('/bfs')
def bfs_searching():
    hasil = bfs.proses_searching()
    return render_template('index.html', Hasil=hasil)


# Debug = True -> Agar auto relod server
if __name__ == '__main__':
import scraper_utils
import logging
from bfs import Bfs

logging.basicConfig(level=logging.INFO)
ORIGIN = 'Gandhi'
NUM_ITERATION = 10
NUM_THREADS = 5

bfs = Bfs()
scraper_utils.setup(bfs, ORIGIN)


thread_list = scraper_utils.do_search_threads(bfs, NUM_ITERATION / NUM_THREADS, NUM_THREADS)
for thread in thread_list:
    thread.start()

for thread in thread_list:
    thread.join()

bfs.print_dictionary()
Ejemplo n.º 9
0
def main():
    # all puzzle arrays will be the same size, 4x4
    correct_puzzle = generate_correct_state()
    Puzzle.correct_state, Puzzle.puzzle_height, Puzzle.puzzle_width = correct_puzzle, 4, 4

    # get all filenames of puzzle files
    list_of_filenames = [[
        filename for filename in os.listdir("to_draw/")
        if "4x4_0" + str(i) in filename
    ] for i in range(1, 8)]

    # generate list of all puzzles
    list_of_puzzles = [[
        load_initial_puzzle(filename) for filename in list_of_filenames[i]
    ] for i in range(7)]

    # generate list of all first state objects
    list_for_bfs = [[Puzzle(single) for single in single_list]
                    for single_list in list_of_puzzles]
    list_for_dfs = [[Puzzle(single) for single in single_list]
                    for single_list in list_of_puzzles]
    list_for_astr_manh = [[Puzzle(single) for single in single_list]
                          for single_list in list_of_puzzles]

    # generate list of all results for given methods

    # for bfs

    start_time = time.perf_counter()
    res_bfs = [[(Bfs(single_state, order).run_search(), order)
                for order in direction_orders for single_state in single_list]
               for single_list in list_for_bfs]
    end_time = time.perf_counter()
    print("BFS generating time: ", round(end_time - start_time, 3), "s")

    # for dfs

    start_time = time.perf_counter()
    executors_list_dfs = []
    with ProcessPoolExecutor(max_workers=3) as executor:
        for i in range(len(list_for_dfs)):
            executors_list_dfs.append(
                executor.submit(dfs_worker, list_for_dfs[i]))
    end_time = time.perf_counter()
    res_dfs = [i.result() for i in executors_list_dfs]

    print("DFS generating time: ", round(end_time - start_time, 3), "s",
          round((end_time - start_time) / 3600, 3), "h")

    # for astar, hamm with manh

    start_time = time.perf_counter()
    res_astr = [[(Astr(single_state, heuristic).run_search(), heuristic)
                 for heuristic in heuristics for single_state in single_list]
                for single_list in list_for_astr_manh]
    end_time = time.perf_counter()
    print("Astr generating time: ", round(end_time - start_time, 3), "s")

    # Section for plots generating process
    """
    Solution length / Visited states / Processed states / Max depth / execution, together 20 graphs
    First graph: Means for BFS, DFS, Astar together
    Second graph: Means for BFS orderly separated
    Third graph: Means for DFS orderly separated
    Fourth graph: Means for Astar heuristics separated
    """

    # Draw means plots for results together
    for i in range(5):
        draw_means_together(res_bfs, res_dfs, res_astr, i)
        draw_separated(res_bfs, "bfs", i)
        draw_separated(res_dfs, "dfs", i)
        draw_separated(res_astr, "astr", i)
Ejemplo n.º 10
0
def bfs_worker(single_list):
    res_bfs = [(Bfs(single_state, order).run_search(), order)
               for order in direction_orders for single_state in single_list]
    return res_bfs