forked from MengGuo/P_MAS_TG
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest2.py
More file actions
180 lines (148 loc) · 5.72 KB
/
Copy pathtest2.py
File metadata and controls
180 lines (148 loc) · 5.72 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
from P_MAS_TG.ts import MotionFts, ActionModel, MotActModel
from P_MAS_TG.planner import ltl_planner
from P_MAS_TG.buchi import mission_to_buchi
from P_MAS_TG.product import ProdAut
from networkx_viewer import Viewer
# export PYTHONPATH=$PYTHONPATH:/to/your/P_MAS_TG
import matplotlib.pyplot as plt
import networkx as nx
import time
# ##############################
# # motion FTS
# aap = {'r1', 'r2', 'r3', 'r4', 'r5', 'r6', 'r', 'b'}
# # +-----+-----+-----+
# # | r4,r| r5,b| r6,b|
# # +-----+-----+-----+
# # | r1,r| r2,b| r3,r|
# # +-----+-----+-----+
# aregions = { (0, 0, 1): set(['r1', 'r']),
# (1, 0, 1): set(['r2', 'b']),
# (2, 0, 1): set(['r3', 'r']),
# (0, 1, 1): set(['r4', 'r']),
# (1, 1, 1): set(['r5', 'b']),
# (2, 1, 1): set(['r6', 'b'])
# }
# aedges = [((0, 0, 1), (1, 0, 1)),
# ((1, 0, 1), (2, 0, 1)),
# ((0, 1, 1), (1, 1, 1)),
# ((1, 1, 1), (2, 1, 1)),
# ((0, 0, 1), (0, 1, 1)),
# ((1, 0, 1), (1, 1, 1)),
# ((2, 0, 1), (2, 1, 1))
# ]
ap = {'r','b','basket1','yball','basket2','gball'}#,'pickrball','droprball','pickgball','dropgball'}
regions = {}
edges = []
N = 25
k = 0
asdf = ['r','b']
for i in range(0,N):
for j in range(0,N):
if j == 9 and i == 16:
regions[(j,i,1)] = set(['r'+str(k),'yball']) #, 'pick'])
elif j ==8 and i == 15:
regions[(j,i,1)] = set(['r'+str(k),'basket1'])#, 'drop'])
elif j == 12 and i == 6:
regions[(j,i,1)] = set(['r'+str(k),'gball'])#, 'pick'])
elif j == 18 and i == 7:
regions[(j,i,1)] = set(['r'+str(k),'basket2'])#, 'drop'])
else:
regions[(j,i,1)] = set(['r'+str(k),asdf[j%2]])
ap.add('r'+str(k))
if i>0 and ((i-1,j,1),(i,j,1)) not in edges and ((i,j,1),(i-1,j,1)) not in edges:
edges.append(((i-1,j,1),(i,j,1)))
if i<N-1 and ((i+1,j,1),(i,j,1)) not in edges and ((i,j,1),(i+1,j,1)) not in edges:
edges.append(((i+1,j,1),(i,j,1)))
if j>0 and ((i,j-1,1),(i,j,1)) not in edges and ((i,j,1),(i,j-1,1)) not in edges:
edges.append(((i,j-1,1),(i,j,1)))
if j<N-1 and ((i,j+1,1),(i,j,1)) not in edges and ((i,j,1),(i,j+1,1)) not in edges:
edges.append(((i,j+1,1),(i,j,1)))
k=k+1
print k
#robot_motion = MotionFts(aregions, aap, 'office' )
#robot_motion.set_initial((0, 0, 1))
#robot_motion.add_un_edges(aedges, unit_cost = 0.1)
robot_motion = MotionFts(regions, ap, 'asdf' )
robot_motion.set_initial((0, 0, 1))
robot_motion.add_un_edges(edges, unit_cost = 1)
##############################
# action FTS
############# no action model
action = dict()
############# with action
action = { 'pickrball': (10, 'yball', set(['pickrball'])),
'droprball': (10, 'basket1', set(['droprball'])),
'pickgball': (10, 'gball', set(['pickgball'])),
'dropgball': (10, 'basket2', set(['dropgball']))
}
robot_action = ActionModel(action)
##############################
# complete robot model
robot_model = MotActModel(robot_motion, robot_action)
##############################
# specify tasks
########## only hard
# hard_task = '<>(r1 && <> (r2 && <> r6)) && (<>[] r6)'
#hard_task = '(<>(pick && <> drop)) && ([]<> r3) && ([]<> r6)'
#soft_task = None
# +-----+-----+-----+
# | r4,r| r5,b| r6,b|
# +-----+-----+-----+
# | r1,r| r2,b| r3,r|
# +-----+-----+-----+
########## soft and hard
#hard_task = '[](<> r312 && <> r395 && <>r602)'
#hard_task = '<> r312 && <> r395 && <>r602 '
#hard_task = '<> (r312 && <>( r395 && <>r602)) '
#hard_task = '<> r1 && <> r600 || <>r7'
#hard_task = '!(r300 || r400 || r5) U r445'
#hard_task = '<>((rball && pick) && <> (basket && drop)) && <>[] r448'
#hard_task = '<>((pickrball && rball) && <> (droprball && basket1)) && <>((pickgball && gball) && <> (dropgball && basket2)) && [](pickrball -> X(!pickgball U droprball)) && [](pickgball -> X(!pickrball U dropgball))'#' && <>[] r422 '
hard_task = '<>(pickrball && <> droprball) && <>(pickgball && <> dropgball ) && [](pickrball -> X(!pickgball U droprball)) && [](pickgball -> X(!pickrball U dropgball)) && <>[] r422 '
soft_task = None#'([]! b)'
##############################
# set planner
robot_planner = ltl_planner(robot_model, hard_task, soft_task)
robot_planner.product.graph['ts'].build_full()
robot_planner.product.build_full()
#buchi = mission_to_buchi(hard_task, soft_task)
#networkx.draw(ProdAut(robot_model, buchi))
colP = []
i = 0
labels = {}
for node in robot_planner.product.node:
colP.append(robot_planner.product.node[node]['color'])
labels[node] = robot_planner.product.node[node]['dist']
#print robot_planner.product.node[node]
#print robot_planner.product.node
#for node in robot_planner.product.node:
# print node
#print robot_planner.product.edge[node]
#for node in robot_planner.product.graph['buchi']:
#print node
#print robot_planner.product.graph['buchi'].edge[node]
#a = robot_planner.product.graph['buchi'].edge[node].keys()[1]
#print type(robot_planner.product.graph['buchi'].edge[node][a]['guard'])
colB = []
l = {}
for node in robot_planner.product.graph['buchi'].node:
l[node] = robot_planner.product.graph['buchi'].node[node]['dist']
if node in robot_planner.product.graph['buchi'].graph['accept']:
print 'accept'
colB.append('r')
elif node in robot_planner.product.graph['buchi'].graph['initial']:
colB.append('b')
else:
colB.append('w')
#print 'len(colB)'
#print len(colB)
#nx.draw_networkx(robot_planner.product,node_color=colP,labels=labels)
#plt.show()
#nx.draw_networkx(robot_planner.product.graph['buchi'],node_color=colB,labels=l)
#plt.show()
#app = Viewer(robot_planner.product)
#app.mainloop()
# synthesis
start = time.time()
robot_planner.optimal(10,'static')
print 'full construction and synthesis done within %.2fs \n' %(time.time()-start)