Ejemplo n.º 1
0
def solve(num_arr, start, end):
	goal = vertex.Vertex(end, 0)
	# g is now cost
	g = {}
	g[utils.coord_id(start)] = 0
	parent = {}
	parent[utils.coord_id(start)] = start
	fr = fringe.Fringe()
	fr.insert(vertex.Vertex(start, 0))
	closed = set()
	
	while len(fr) != 0:
		s = fr.pop()
		s_c = [s.x, s.y]
		if s == goal:
			path = utils.goal_path(start, end, parent)
			return True, path, g, len(closed)
		closed.add(s)
		for s_p in utils.succ(num_arr,s, closed):
			sp_c = [s_p.x, s_p.y]
			sp_cid = utils.coord_id(sp_c)
			ch_cost = s.g_h + utils.cost(num_arr,s_c, sp_c)
			if s_p not in fr.to_list() and s_p not in closed:
				g[sp_cid] = ch_cost
				parent[sp_cid] = s_c
				fr.insert(vertex.Vertex(sp_c, ch_cost))
			elif s_p in fr.to_list() and ch_cost < s.g_h:
				fr.update(s_p, ch_cost)
				g[sp_cid] = ch_cost
				parent[sp_cid] = s_c
	return False, [], [], len(closed)
Ejemplo n.º 2
0
def solve(num_arr, start, end, w=1):
    goal = vertex.Vertex(end, 0)
    g = {}
    g[utils.coord_id(start)] = 0
    g_h_f = {}
    g_h_f[utils.coord_id(start)] = [0, heur(start, end), w * heur(start, end)]
    parent = {}
    parent[utils.coord_id(start)] = start
    fr = fringe.Fringe()
    fr.insert(vertex.Vertex(start,
                            g[utils.coord_id(start)] + heur(start, end)))
    closed = set()

    while len(fr) != 0:
        s = fr.pop()
        if s == goal:
            path = utils.goal_path(start, end, parent)
            return True, path, g_h_f, len(closed)
        closed.add(s)
        for s_p in utils.succ(num_arr, s, closed):
            if s_p not in fr.to_list():
                sp_cid = utils.coord_id([s_p.x, s_p.y])
                g[sp_cid] = math.inf
                parent[sp_cid] = None
            update_vertex(s, s_p, g, parent, fr, end, num_arr, w, g_h_f)
    return False, [], [], len(closed)
Ejemplo n.º 3
0
def solve(num_arr, start, end, w=[1, 1.25]):
    # open_i
    fr = []
    # closed_i
    closed = []
    #g_i
    g = []
    #bp_i
    parent = []
    # stuff to display
    g_h_f = []
    goal = vertex.Vertex(end, 0)
    for i in range(n):
        g.append({})
        g[i][utils.coord_id(start)] = 0
        g[i][utils.coord_id(end)] = math.inf
        parent.append({})
        g_h_f.append({})
        parent[i][utils.coord_id(start)] = start
        parent[i][utils.coord_id(end)] = None
        fr.append(fringe.Fringe(False))
        fr[i].insert(vertex.Vertex(start, key(start, i, g, end, w, g_h_f)))
        closed.append(set())

    goal_cid = utils.coord_id(end)
    while fr[0].minkey().g_h < math.inf:
        for i in range(n):
            if fr[i].minkey().g_h <= w[1] * fr[i].minkey().g_h:
                if g[i][goal_cid] <= fr[i].minkey().g_h:
                    if g[i][goal_cid] < math.inf:
                        avg_expanded = [len(y) for y in closed]
                        return True, utils.goal_path(
                            start, end,
                            parent[i]), g_h_f[i], np.mean(avg_expanded)
                else:
                    s = fr[i].top()
                    expand_state(s, i, fr, g, parent, num_arr, closed, end, w,
                                 g_h_f)
                    closed[i].add(s)
            else:
                if g[0][goal_cid] <= fr[0].minkey().g_h:
                    if g[0][goal_cid] < math.inf:
                        avg_expanded = [len(y) for y in closed]
                        return True, utils.goal_path(
                            start, end,
                            parent[i]), g_h_f[0], np.mean(avg_expanded)
                else:
                    s = fr[i].top()
                    expand_state(s, 0, fr, g, parent, num_arr, closed, end, w,
                                 g_h_f)
                    closed[0].add(s)
    return False, [], []
Ejemplo n.º 4
0
def update_vertex(s, s_p, g, parent, fr, end, num_arr, w, g_h_f):
    s_c = [s.x, s.y]
    sp_c = [s_p.x, s_p.y]
    s_cid = utils.coord_id(s_c)
    sp_cid = utils.coord_id(sp_c)
    next_cost = utils.cost(num_arr, s_c, sp_c)
    if g[s_cid] + next_cost < g[sp_cid]:
        g[sp_cid] = g[s_cid] + next_cost
        parent[sp_cid] = s_c
        if s_p in fr.to_list():
            fr.remove(sp_c)
        g_c = g[sp_cid]
        h_c = heur(sp_c, end)
        f_c = g_c + w * h_c
        g_h_f[utils.coord_id(sp_c)] = [g_c, h_c, f_c]
        fr.insert(vertex.Vertex(sp_c, f_c))
Ejemplo n.º 5
0
 def upd_vert(self, s, g_h):
     cid = utils.coord_id(s)
     for obj in self.fringe[:]:
         if cid == obj.cid:
             obj.g_h = g_h
             heapq.heapify(self.fringe)
             return True
     return False
Ejemplo n.º 6
0
def expand_state(s, i, fr, g, parent, num_arr, closed, end, w, g_h_f):
    s_c = [s.x, s.y]
    s_cid = utils.coord_id(s_c)
    fr[i].remove(s_c)
    for s_p in utils.succ(num_arr, s, closed[i]):
        sp_c = [s_p.x, s_p.y]
        sp_cid = utils.coord_id(sp_c)
        tmp_cost = utils.cost(num_arr, s_c, sp_c)
        # may be wrong
        if s_p not in fr[i].to_list():
            g[i][sp_cid] = math.inf
            parent[i][sp_cid] = None
        if g[i][sp_cid] > g[i][s_cid] + tmp_cost:
            g[i][sp_cid] = g[i][s_cid] + tmp_cost
            parent[i][sp_cid] = s_c
            if s_p not in closed[i]:
                fr[i].insert(
                    vertex.Vertex(sp_c, key(sp_c, i, g, end, w, g_h_f)))
Ejemplo n.º 7
0
def key(s, i, g, end, w, g_h_f):
    g_i = g[i][utils.coord_id(s)]
    # hueristic 1
    if i == 0:
        h_c = heur1(s, end)
        f_c = g_i + w[0] * h_c
        g_h_f[i][utils.coord_id(s)] = [g_i, h_c, f_c]
        return f_c
    # hueristic 2
    if i == 1:
        h_c = heur2(s, end)
        f_c = g_i + w[0] * h_c
        g_h_f[i][utils.coord_id(s)] = [g_i, h_c, f_c]
        return f_c
    # hueristic 3
    if i == 2:
        h_c = heur3(s, end)
        f_c = g_i + w[0] * h_c
        g_h_f[i][utils.coord_id(s)] = [g_i, h_c, f_c]
        return f_c
    # hueristic 4
    if i == 3:
        h_c = heur4(s, end)
        f_c = g_i + w[0] * h_c
        g_h_f[i][utils.coord_id(s)] = [g_i, h_c, f_c]
        return f_c
    # hueristic 5
    if i == 4:
        h_c = heur5(s, end)
        f_c = g_i + w[0] * h_c
        g_h_f[i][utils.coord_id(s)] = [g_i, h_c, f_c]
        return f_c
Ejemplo n.º 8
0
		filename = 'grid' + str(j + 1) + '_' + str(k + 1) + ".txt"
		grid, start, end, regions = utils.load_file(filename)
		opt_path = a_star.min_d(start,end)
		start1 = time.time()
		solved1, path1, g_h_f1, expanded1 = a_star_seq.solve(grid, start, end, [1.5, 1.5])
		end1 = time.time()
		start2 = time.time()
		solved2, path2, g_h_f2, expanded2 = a_star_seq.solve(grid, start, end, [2, 2])
		end2= time.time()
		start3 = time.time()
		solved3, path3, g_h_f3, expanded3 = a_star_seq.solve(grid, start, end, [3, 3])
		end3 = time.time()
		tot_runtime_a += end1 - start1
		tot_runtime_b += end2 - start2
		tot_runtime_c += end3 - start3
		avg_path_perf_a += g_h_f1[utils.coord_id(end)][0] / opt_path
		avg_path_perf_b += g_h_f2[utils.coord_id(end)][0] / opt_path
		avg_path_perf_c += g_h_f3[utils.coord_id(end)][0] / opt_path
		avg_nodes_a += expanded1
		avg_nodes_b += expanded2
		avg_nodes_c += expanded3
		print("STATS FOR weighted A* seq w=[1.5, 1.5]")
		print("File: " + filename)
		print("Runtime: " + str(tot_runtime_a))
		print("G-Value: " + str(g_h_f1[utils.coord_id(end)][0]))
		print("Nodes expanded: " + str(avg_nodes_a))
		print("--------------------------------")
		print("STATS FOR weighted A* seq w=[2, 2]")
		print("File: " + filename)
		print("Runtime: " + str(tot_runtime_b))
		print("G-Value: " + str(g_h_f2[utils.coord_id(end)][0]))
Ejemplo n.º 9
0
 def __init__(self, point, g_h):
     self.x = point[0]
     self.y = point[1]
     self.g_h = g_h
     self.cid = utils.coord_id(point)
Ejemplo n.º 10
0
 def get_vert(self, s):
     cid = utils.coord_id(s)
     for obj in self.fringe[:]:
         if cid == obj.cid:
             return obj