Exemple #1
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
        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, [], []
Exemple #2
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:
                        return True, utils.goal_path(start, end,
                                                     parent[i]), g_h_f[i]
                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:
                        return True, utils.goal_path(start, end,
                                                     parent[i]), g_h_f[0]
                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, [], []
Exemple #3
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))
Exemple #4
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
Exemple #5
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)))
Exemple #6
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
Exemple #7
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)
Exemple #8
0
	def get_vert(self, s):
		cid = utils.coord_id(s)
		for obj in self.fringe[:]:
			if cid == obj.cid:
				return obj