-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex2.py
More file actions
142 lines (119 loc) · 5 KB
/
ex2.py
File metadata and controls
142 lines (119 loc) · 5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
from searchPlus import *
from MedoTotal import *
from collections import deque
import time
def depth_first_tree_search_all_count(problem, optimal=False, verbose=False):
if not problem:
return None
stack = deque()
visited = list()
n1 = Node(problem.initial)
stack.append(n1)
if not optimal:
visited.append(n1)
max_mem = 0
estados_finais = []
beststate = None
while(stack):
node = stack.pop()
state = node.state
npc = node.path_cost
if optimal:
if verbose:
print("---------------------")
print()
print(problem.display(state))
print("Custo:", node.path_cost)
acts = problem.actions(state)
l = len(acts)
for i in range(len(acts)):
lmi = l - i - 1
action = acts[lmi]
res = problem.result(state, action)
if problem.goal_test(res):
a2 = acts[i]
res2 = problem.result(state, a2)
child = Node(res2, node, a2, problem.path_cost(npc, state, a2, res2))
if beststate is not None and child.path_cost >= beststate.path_cost:
continue
estados_finais.append(child)
if verbose:
print("---------------------")
print()
print(problem.display(res2))
if beststate is None or child.path_cost < beststate.path_cost:
beststate = child
if verbose:
print("GGGGooooooallllll --------- com o custo:" ,child.path_cost)
print("Di best goal até agora")
else:
if verbose:
if child in estados_finais:
print("GGGGooooooallllll --------- com o custo:" ,child.path_cost)
else:
print("Custo:", child.path_cost)
visited.append(child)
else:
child = Node(res, node, action, problem.path_cost(node.path_cost, state, action, res))
if beststate is None or child.path_cost < beststate.path_cost:
stack.append(child)
visited.append(node)
ls = len(stack)
if ls > max_mem:
max_mem = ls
continue
if verbose:
print("---------------------")
print()
print(problem.display(state))
print("Custo:", node.path_cost)
acts = problem.actions(state)
l = len(acts)
for i in range(l):
action = acts[l - i - 1]
res = problem.result(state, action)
if problem.goal_test(res):
a2 = acts[i]
res2 = problem.result(state, a2)
child = Node(res2, node, a2, problem.path_cost(npc, state, a2, res2))
estados_finais.append(child)
print("---------------------")
print()
print(problem.display(res2))
if beststate is None or child.path_cost < beststate.path_cost:
beststate = child
print("GGGGooooooallllll --------- com o custo:" ,child.path_cost)
print("Di best goal até agora")
else:
if child in estados_finais:
print("GGGGooooooallllll --------- com o custo:" ,child.path_cost)
else:
print("Custo:", child.path_cost)
else:
child = Node(res, node, action, problem.path_cost(npc, state, action, res))
stack.append(child)
visited.append(child)
ls = len(stack)
if ls > max_mem:
max_mem = ls
continue
acts = problem.actions(state)
l = len(acts)
for i in range(l):
lmi = l - i - 1
action = acts[lmi]
npc = node.path_cost
res = problem.result(state, action)
child = Node(res, node, action, problem.path_cost(npc, state, action, res))
if problem.goal_test(res):
estados_finais.append(child)
if beststate is None or child.path_cost < beststate.path_cost:
beststate = child
visited.append(child)
else:
visited.append(child)
stack.append(child)
ls = len(stack)
if ls > max_mem:
max_mem = ls
return beststate, max_mem, len(visited), len(estados_finais)