Skip to content

Commit

Permalink
a* - será que está certo?
Browse files Browse the repository at this point in the history
  • Loading branch information
yaansz committed Nov 14, 2021
1 parent 3085eb3 commit 357be87
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 35 deletions.
8 changes: 5 additions & 3 deletions blind_search/algorithms/heuristic.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import show

def heuristic_manhattan(state):
res = 0
goal = state.get_goal()
Expand All @@ -6,9 +8,9 @@ def heuristic_manhattan(state):
x, y = state.table(i)
x_t, y_t = state.table( goal.index( state.state[i] ) )
res += abs(x - x_t) + abs(y - y_t)
return res

return state.depth + res


def heuristic_default(state):
return state.cost + 1
return 0 if state.parent is None else state.cost + 1
26 changes: 16 additions & 10 deletions blind_search/algorithms/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ def dfs(initial_state, limit=-1):

# While the stack is not empty
while stack:

# Remove the first state from the stack
current_state = stack.pop()
explored.add(current_state.key)

# If the current state is the goal state
if current_state.goal():
return {"state": current_state, "max_depth": max_depth, "max_frontier": max_frontier, "final_frontier": len(stack), "scanned": len(explored)}
Expand Down Expand Up @@ -116,7 +116,7 @@ def astar(initial_state):
max_frontier = 0

# Create a stack for the states to be explored
stack = deque() # Stack is LIFO
stack = [] # Stack is LIFO
explored = set()

# Add the initial state to the stack
Expand All @@ -125,21 +125,27 @@ def astar(initial_state):
while stack:

# Remove the first state from the stack
current_state = stack.popleft()
current_state = stack.pop(0)
explored.add(current_state.key)

# Current State ??
if current_state.goal():
return {"state": current_state, "max_depth": max_depth, "max_frontier": max_frontier, "final_frontier": len(stack), "scanned": len(explored)}


# Generating Children
children = current_state.get_neighbors()
children = current_state.get_neighbors(False)
# Calculating the cost to the specific child
for child in children:
child.cost = child.h(child)

distances = [child.h(child) for child in children]
# Filtering the children
children = [child for child in children if child.key not in explored]
# Most Valuable Child
stack.extend(children)

print(distances)

return {"state": current_state, "max_depth": max_depth, "max_frontier": max_frontier, "final_frontier": len(stack), "scanned": len(explored)}
stack = sorted(stack, key=lambda x: x.cost)
return {"state": None, "max_depth": max_depth, "max_frontier": max_frontier, "final_frontier": len(stack), "scanned": len(explored)}



Expand Down
2 changes: 0 additions & 2 deletions blind_search/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ def main():
f.write(f"RAM_Usage : {resource.getrusage(resource.RUSAGE_SELF).ru_maxrss/1000.0} kb\n")
f.close()

if(method == "bfs"):
traceback_board(current_state)
return

METHODS = {'bfs': bfs, 'dfs': dfs, 'idfs': idfs, "astar": astar}
Expand Down
14 changes: 7 additions & 7 deletions blind_search/res/astar.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
Method: astar
Final State Found: True
Path :
Cost : 16
Depth : 0
Path : left, up, right, down, left, left, up, up, right, down, right, up, left, down, down, left, up, up, right, down, down, right, up, left, down, left, up, right, right, down
Cost : 30
Depth : 30
Max_Depth : 0
Final_Frontier : 0
Final_Frontier : 9797
Max_Frontier : 0
Scanned : 0
Elapsed_Time : 0.0005576610565185547 s
RAM_Usage : 8.044 kb
Scanned : 17126
Elapsed_Time : 18.833747625350952 s
RAM_Usage : 24.996 kb
4 changes: 2 additions & 2 deletions blind_search/res/bfs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ Max_Depth : 31
Final_Frontier : 32
Max_Frontier : 24053
Scanned : 181440
Elapsed_Time : 2.547363519668579 s
RAM_Usage : 62.168 kb
Elapsed_Time : 4.625905752182007 s
RAM_Usage : 82.156 kb
4 changes: 2 additions & 2 deletions blind_search/res/dfs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ Max_Depth : 65982
Final_Frontier : 42564
Max_Frontier : 42826
Scanned : 146379
Elapsed_Time : 1.4203965663909912 s
RAM_Usage : 64.78 kb
Elapsed_Time : 2.3584494590759277 s
RAM_Usage : 87.284 kb
4 changes: 2 additions & 2 deletions blind_search/res/idfs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ Max_Depth : 31
Final_Frontier : 0
Max_Frontier : 26
Scanned : 312296
Elapsed_Time : 3.9806690216064453 s
RAM_Usage : 13.844 kb
Elapsed_Time : 5.968386173248291 s
RAM_Usage : 13.876 kb
18 changes: 11 additions & 7 deletions blind_search/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ def traceback_board(state):
"""
class State:

def __init__(self, state, goal, parent=None, cost=0, depth=0, action="NoOp", h=hrc.heuristic_default):
def __init__(self, state, goal, parent=None, cost=None, depth=0, action="NoOp", h=hrc.heuristic_default):
self.state = state
self.parent = parent
self.depth = depth

self.size = int(len(state)**(1/2))
self.key = "".join(str(x) for x in state)
Expand All @@ -45,10 +46,13 @@ def __init__(self, state, goal, parent=None, cost=0, depth=0, action="NoOp", h=h
self.__goal = goal.copy()

self.h = h

self.cost = 0
self.cost = self.h(self)
self.depth = depth

if not cost:
self.cost = 0
self.cost = self.h(self)
else:
self.cost = cost


def __eq__(self, other):
return self.key == other.key
Expand All @@ -69,7 +73,7 @@ def table(self, index):
return i, index - i * self.size


def get_neighbors(self):
def get_neighbors(self, cost = True):
neighbors = []

index = self.state.index(0)
Expand All @@ -83,7 +87,7 @@ def get_neighbors(self):
if 0 <= i_new < self.size and 0 <= j_new < self.size:
new_state = self.state[:]
new_state[index], new_state[i_new * self.size + j_new] = new_state[i_new * self.size + j_new], new_state[index]
neighbors.append(State(new_state, self.__goal, self, self.h(self), self.depth + 1, key, self.h))
neighbors.append(State(new_state, self.__goal, self, cost = None if not cost else self.h(self), depth = self.depth + 1, action = key, h = self.h))

return neighbors

Expand Down

0 comments on commit 357be87

Please sign in to comment.