-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplanner_naive.py
97 lines (68 loc) · 2.76 KB
/
planner_naive.py
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
import queue
import numpy as np
import math
from main import mdp_2, mdp_1, mdp_0
from neighbourhood import Neighbourhood
from planner import Planner
from utils import construct_graph, modify_options, a_subset_b, matrix_to_list, start_as_key_value, a_intersects_b
class PlannerNaive():
def __init__(self, options_as_dict, options, graph):
self.options_dict = options_as_dict
self.options = options
self.graph = graph
def check(self, a_as_list, b_as_list):
return a_subset_b(a_as_list,b_as_list)
def extract_plan(self, name, parents): #backtrack using parents and return the plan
route = []
parent_name = name
while parents[parent_name][0] != None:
parent_name, option = parents[parent_name]
# TODO: option needs to be regular option
route.insert(0,self.options_dict[option.name])
return route
def bfs_plan(self, S, G):
"""
Implement breadth-first search.
Input:
problem - the problem on which the search is conducted, a SearchProblem
Output: a list of states representing the path of the solution
"""
s_as_list = matrix_to_list(S)
g_as_list = matrix_to_list(G)
# we are already at goal state
check , _= self.check(s_as_list,g_as_list)
if check:
print("start is alread at goal")
return []
frontier = queue.Queue()
s_key, s_value = start_as_key_value(s_as_list,self.options_dict,self.options, False)
#print(s_key,s_value)
reached = set()
reached.add(s_key)
self.graph[s_key] = s_value
parents = {s_key: [None, None]}
frontier.put(s_key)
while (not frontier.empty()):
key = frontier.get()
for value_option, _, _ in self.graph[key]:
if self.check(value_option.termination_as_list,g_as_list)[0]:
parents[value_option.name] = [key,value_option]
best_plan = self.extract_plan(value_option.name, parents)
return best_plan
elif not value_option.name in reached:
reached.add(value_option.name)
parents[value_option.name] = [key,value_option]
frontier.put(value_option.name)
return []
if __name__ == "__main__":
options_dict = {}
for o in mdp_0:
options_dict[o.name] = o
planner = PlannerNaive(options_dict, mdp_0, construct_graph(options_dict, mdp_0, False))
arr1 = np.zeros((8, 8))
arr1[1, 1] = 1
arr2 = np.zeros((8, 8))
arr2[7, 7] = 1
plan = planner.bfs_plan(arr1, arr2)
for o in plan:
print(o.name)