-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulate.py
329 lines (304 loc) · 14 KB
/
simulate.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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
from routines import *
class Processor:
def __init__(self, get_all_history=False):
# the order is, 5 lights, 3 doors, 2 tablelamp
# self.states = [1] * 10
# because the unity side door states are not accurate, so I have to keep a local copy
self.get_all_history = get_all_history
self.local_states_table = [1] * 11
self.excluded_list = [163, 172] # 163 is fridge, 172 is microwave
def initialize_graph(self, idx):
graph = None
if idx == 0:
self.local_states_table = [1] * 5 + [0, 1, 1] + [0] * 3
if not self.get_all_history:
comm.reset()
success, graph = comm.environment_graph()
tablelamps = find_nodes(graph, class_name='tablelamp')
for tablelamp in tablelamps:
tablelamp['states'] = ['OFF']
door = find_nodes(graph, class_name='door')[0]
door['states'] = ['CLOSED']
elif idx == 1:
self.local_states_table = [0, 0, 0, 0, 1] + [0, 0, 1] + [0, 0, 1]
if not self.get_all_history:
comm.reset()
success, graph = comm.environment_graph()
tablelamp = find_nodes(graph, class_name='tablelamp')[0]
tablelamp['states'] = ['OFF']
tablelamp = find_nodes(graph, class_name='tablelamp')[1]
tablelamp['states'] = ['OFF']
lights = find_nodes(graph, class_name='lightswitch')
for l in lights:
l['states'] = ['OFF']
door = find_nodes(graph, class_name='door')[0]
door['states'] = ['CLOSED']
door2 = find_nodes(graph, class_name='door')[1]
door2['states'] = ['CLOSED']
l5 = find_nodes(graph, class_name='lightswitch')[4]
l5['states'] = ['ON']
elif idx == 2:
self.local_states_table = [0] * 5 + [0, 0, 1] + [0, 0, 0]
if not self.get_all_history:
comm.reset()
success, graph = comm.environment_graph()
lights = find_nodes(graph, class_name='lightswitch')
for l in lights:
l['states'] = ['OFF']
tablelamps = find_nodes(graph, class_name='tablelamp')
for tablelamp in tablelamps:
tablelamp['states'] = ['OFF']
door = find_nodes(graph, class_name='door')[0]
door['states'] = ['CLOSED']
door2 = find_nodes(graph, class_name='door')[1]
door2['states'] = ['CLOSED']
elif idx == 3:
self.local_states_table = [0] * 5 + [1, 1, 1] + [0, 0, 0]
if not self.get_all_history:
comm.reset()
success, graph = comm.environment_graph()
lights = find_nodes(graph, class_name='lightswitch')
for l in lights:
l['states'] = ['OFF']
tablelamps = find_nodes(graph, class_name='tablelamp')
for tablelamp in tablelamps:
tablelamp['states'] = ['OFF']
elif idx == 4:
self.local_states_table = [0] * 5 + [1, 1, 1] + [0, 0, 0]
if not self.get_all_history:
comm.reset()
success, graph = comm.environment_graph()
lights = find_nodes(graph, class_name='lightswitch')
for l in lights:
l['states'] = ['OFF']
tablelamps = find_nodes(graph, class_name='tablelamp')
for tablelamp in tablelamps:
tablelamp['states'] = ['OFF']
else:
print("none")
return graph
def translate_from_state_to_action(self, msg):
if msg == "":
return None
local_state = int(msg.split()[0])
local_idx = int(msg.split()[1])
if local_idx <= 4: # lights
action = "switchon" if local_state else "switchoff"
obj = "light"
elif 4 < local_idx <= 7: # door
action = "open" if local_state else "close"
obj = "door"
else: # 7 < local_idx lamp
action = "switchon" if local_state else "switchoff"
obj = "tablelamp"
id = str(local_lookup_table[local_idx])
return "[{}] <{}> ({})".format(action, obj, id)
def process_programm(self, script, input):
# maybe we will keep using one trigger for now (since if we combine both programs,
# then their will be two actions, we'd rather let users write two programs)
## Input
if input is not None:
trigger = self.translate_from_state_to_action(input[0])
conditions = input[1]
and_or = input[2] # 0 is and, 1 is or
if_action = input[3]
else_action = input[4]
else:
# Empty
trigger = None
conditions = []
and_or = 0 # 0 is and, 1 is or
if_action = []
else_action = []
print(trigger)
print(conditions)
print(and_or)
print(if_action)
print(else_action)
# ## Task 0
# trigger = "[open] <door> (47)"
# # trigger = '[switchon] <tablelamp> (377)'
# conditions = ["1 9", "1 10"]
# and_or = 1 # 0 is and, 1 is or
# if_action = ["0 0", "0 1", "0 2", "0 3", "0 4"]
# else_action = []
## Task 1
# trigger = "[open] <door> (47)"
# conditions = []
# and_or = 0 # 0 is and, 1 is or
# if_action = ["0 4", "1 0", "0 10"]
# else_action = []
## Task 2
# need to update task 1 to the following (adding the else)
# trigger = "[open] <door> (47)"
# conditions = ["1 4"]
# and_or = 0 # 0 is and, 1 is or
# if_action = ["0 4", "1 0", "0 10"]
# else_action = ["1 0"]
for action in script:
if action.startswith('!'):
if action.startswith('!print'):
comm.experiment_log(action[7:])
else:
print("invalid action!!!")
continue
if trigger and trigger in action:
self.my_render_script(action)
# _, graph = get_current_states()
_, graph = comm.environment_graph()
results = []
is_satisfied = True
if conditions:
for condition in conditions:
local_state = int(condition.split()[0])
local_idx = int(condition.split()[1])
results.append(self.local_states_table[local_idx] == local_state)
if and_or == 0: # and
is_satisfied = all(results)
else: # or
is_satisfied = any(results)
if is_satisfied:
for local_action in if_action:
local_state = int(local_action.split()[0])
local_idx = int(local_action.split()[1])
self.local_states_table[local_idx] = local_state
else:
for local_action in else_action:
local_state = int(local_action.split()[0])
local_idx = int(local_action.split()[1])
self.local_states_table[local_idx] = local_state
# updated_script.append(action)
self.expand_current_states(self.local_states_table, graph)
else: # if the current action is not a trigger
self.my_render_script(action)
def my_render_script(self, action):
obj_id = int(action[action.find("(") + 1:action.find(")")].lower())
action_match = action[action.find("[") + 1:action.find("]")].lower()
# if it's not "walk" and those non-iot actions
if action_match in ['switchon', 'switchoff', 'close', 'open'] and obj_id not in self.excluded_list:
action_state = 1 if action_match in ['switchon', 'open'] else 0
local_index = local_lookup_table.index(obj_id)
if self.local_states_table[local_index] == action_state:
pass # will ignore an action if the states already satisfied
else:
self.local_states_table[local_index] = action_state
comm.render_script([action], find_solution=False)
else:
comm.render_script([action], find_solution=False)
def expand_current_states(self, states, graph):
# 0,1,2,3,4 are lights, 5,6,7 are doors, 8,9,10 are lamps
lights = find_nodes(graph, class_name='lightswitch')
doors = find_nodes(graph, class_name='door')
tablelamps = find_nodes(graph, class_name='tablelamp')
for x in range(len(lights)):
lights[x]['states'] = ['ON'] if states[x] else ['OFF']
for x in range(len(doors)):
doors[x]['states'] = ['OPEN'] if states[len(lights) + x] else ['CLOSED']
for x in range(len(tablelamps)):
tablelamps[x]['states'] = ['ON'] if states[len(lights) + len(doors) + x] else ['OFF']
_ = comm.expand_scene(graph)
def return_all_history(self, script):
all_history = []
all_history_changed_idx = []
for action in script:
if action.startswith('!'):
continue
if action == 'initial':
all_history.append(self.local_states_table.copy())
all_history_changed_idx.append('initial')
continue
obj_id = int(action[action.find("(") + 1:action.find(")")].lower())
action_match = action[action.find("[") + 1:action.find("]")].lower()
# if it's not "walk" and those non-iot actions
if action_match in ['switchon', 'switchoff', 'close', 'open'] and obj_id not in self.excluded_list:
action_state = 1 if action_match in ['switchon', 'open'] else 0
local_index = local_lookup_table.index(obj_id)
if self.local_states_table[local_index] == action_state:
pass # will ignore an action if the states already satisfied
else:
self.local_states_table[local_index] = action_state
all_history.append(self.local_states_table.copy())
all_history_changed_idx.append(str(action_state) + " " + str(local_index))
return all_history, all_history_changed_idx
def get_current_states(self):
light_states = []
door_states = []
tablelamp_states = []
_, graph = comm.environment_graph()
for node in graph['nodes']:
if node['class_name'] == 'lightswitch':
local_states = node["states"] # tracking binary for now
if "ON" in local_states or "OPEN" in local_states:
light_states.append(1)
elif "OFF" in local_states or "CLOSED" in local_states:
light_states.append(0)
elif node['class_name'] == 'door':
local_states = node["states"] # tracking binary for now
if "ON" in local_states or "OPEN" in local_states:
door_states.append(1)
elif "OFF" in local_states or "CLOSED" in local_states:
door_states.append(0)
elif node['class_name'] == 'tablelamp':
local_states = node["states"] # tracking binary for now
if "ON" in local_states or "OPEN" in local_states:
tablelamp_states.append(1)
elif "OFF" in local_states or "CLOSED" in local_states:
tablelamp_states.append(0)
return light_states + door_states + tablelamp_states, graph
def sim_in_unity(selected_task, input, get_all_history=False):
my_p = Processor(get_all_history)
# if selected_task == 0:
# script_my = enhanced_script_0(my_p, get_all_history)
# initial_room = "bedroom"
# elif selected_task == 1:
# script_my = script_1
# initial_room = "bedroom"
# elif selected_task == 2:
# script_my = script_2
# initial_room = "bedroom"
# #### Training
# elif selected_task == 3:
# script_my = script_3
# initial_room = "livingroom"
# elif selected_task == 4:
# script_my = script_4
# initial_room = "kitchen"
# #### Training
if selected_task == 0:
script_my = enhanced_script_0(my_p, get_all_history)
initial_room = "bedroom"
if get_all_history:
my_p.initialize_graph(0)
return my_p.return_all_history(script_my)
else:
graph = my_p.initialize_graph(0)
_ = comm.expand_scene(graph)
comm.add_character('Chars/Female1', initial_room=initial_room)
my_p.process_programm(script_my, input)
elif selected_task == 1:
script_my = enhanced_script_1_2(my_p, get_all_history)
initial_room = "bedroom"
if get_all_history:
my_p.initialize_graph(1)
return my_p.return_all_history(script_my)
else:
graph = my_p.initialize_graph(1)
_ = comm.expand_scene(graph)
comm.add_character('Chars/Female1', initial_room=initial_room)
my_p.process_programm(script_my, input)
elif selected_task == 2:
script_my = enhanced_script_3_4(my_p, get_all_history)
initial_room = "livingroom"
if get_all_history:
my_p.initialize_graph(3)
return my_p.return_all_history(script_my)
else:
graph = my_p.initialize_graph(3)
_ = comm.expand_scene(graph)
comm.add_character('Chars/Female1', initial_room=initial_room)
my_p.process_programm(script_my, input)
if __name__ == '__main__':
sim_in_unity(2, None)
# script_new = [
# '<char0> [open] <door> (47)'] # Add here your script
# comm.render_script(script_new, find_solution=False)