File size: 12,800 Bytes
acad479 |
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 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 |
import numpy as np
import random
from itertools import combinations
from itertools import permutations
class Action(object):
def __init__(self, templates):
self.templates = templates
def render_declarative(self):
assert 'declarative' in self.templates and \
len(self.templates['declarative']) > 0
return np.random.choice(self.templates['declarative'])
def render_interrogative(self):
assert 'interrogative' in self.templates and \
len(self.templates['interrogative']) > 0, str(self.templates)
return np.random.choice(self.templates['interrogative'])
class ExitAction(Action):
def __init__(self):
templates = {
'declarative': [
'%s exited the %s.',
'%s left the %s.',
'%s went out of the %s.',
],
}
super().__init__(templates)
#########################################
############### Questions ###############
#########################################
class ZeroQ(Action):
def __init__(self, oracle, obj):
fill = (obj, oracle.get_object_container(obj))
templates = {
'interrogative': [
'Question: Where is the %s really?\nAnswer: %s' % fill,
]
}
super().__init__(templates)
class FirstQ(Action):
def __init__(self, oracle, agent, obj):
fill = (agent, obj, oracle.get_first_belief(agent, obj))
templates = {
'interrogative': [
'Question: Where does %s really think the %s is?\nAnswer: %s' % fill,
]
}
super().__init__(templates)
class SecondQ(Action):
def __init__(self, oracle, a1, a2, obj):
fill = (a1, a2, obj, oracle.get_second_belief(a1, a2, obj))
templates = {
'interrogative': [
'Question: Where does %s think %s thinks the %s is?\nAnswer: %s' % fill,
]
}
super().__init__(templates)
class ThirdQ(Action):
def __init__(self, oracle, a1, a2, a3, obj):
fill = (a1, a2, a3, obj, oracle.get_third_belief(a1, a2, a3, obj))
templates = {
'interrogative': [
'Question: Where does %s think %s thinks %s thinks the %s is?\nAnswer: %s' % fill,
]
}
super().__init__(templates)
class FourthQ(Action):
def __init__(self, oracle, a1, a2, a3, a4, obj):
fill = (a1, a2, a3, a4, obj,
oracle.get_fourth_belief(a1, a2, a3, a4, obj))
templates = {
'interrogative': [
'Question: Where does %s think %s thinks %s thinks %s thinks the %s is?\nAnswer: %s' % fill,
]
}
super().__init__(templates)
# class MemoryAction(Action):
# def __init__(self, oracle_start_state, obj):
# fill = (obj, oracle_start_state[obj])
# templates = {
# 'interrogative': [
# 'Where was the %s at the beginning?\t%s' % fill,
# ]
# }
# super().__init__(templates)
# class LocationAction(Action):
# def __init__(self, oracle, args):
# """
# Creaters string with args and modifies
# oracle in accordance with action.
# """
# if len(args) == 2:
# statement = '%s is in the %s.' % args
# a1, loc = args
# # may be redundant
# oracle.set_location(a1, loc)
# else : # 2 people
# statement = '%s and %s are in the %s.' % args
# a1, a2, loc = args
# # may be redundant
# oracle.set_location(a1, loc)
# oracle.set_location(a2, loc)
# templates = {
# 'declarative': [
# statement,
# ]
# }
# super().__init__(templates)
class ObjectLocAction(Action):
def __init__(self, oracle, obj, observers):
container = oracle.get_object_container(obj)
templates = {
'declarative': [
'The %s is in the %s.' % (obj, container),
]
}
# set first beliefs
for observer in observers:
oracle.set_first_belief(observer, obj, container)
# set second beliefs
if len(observers) >= 2:
for observer1, observer2 in combinations(observers, 2):
oracle.set_second_belief(observer1, observer2, obj, container)
oracle.set_second_belief(observer2, observer1, obj, container)
# set third beliefs
if len(observers) >= 3:
for chosen_observers in combinations(observers, 3):
for observer1, observer2, observer3 in permutations(chosen_observers):
oracle.set_third_belief(
observer1, observer2, observer3, obj, container)
# set fourth beliefs
if len(observers) >= 4:
for chosen_observers in combinations(observers, 4):
for observer1, observer2, observer3, observer4 in permutations(chosen_observers):
oracle.set_fourth_belief(
observer1, observer2, observer3, observer4, obj, container)
super().__init__(templates)
class ExitedAction(Action):
def __init__(self, oracle, agent):
fill = (agent, oracle.get_location(agent))
templates = {
'declarative': [
'%s exited the %s.' % fill,
]
}
oracle.set_location(agent, None)
super().__init__(templates)
class MoveAction(Action):
def __init__(self, oracle, args, observers=None, move=True):
agent, obj, container = args
if not move:
location = oracle.get_container_location(container)
templates = {
'declarative': [
f'{args[0]} made no movements and stayed in the {location} for 1 minute.',
]
}
else:
templates = {
'declarative': [
'%s moved the %s to the %s.' % args,
]
}
oracle.set_object_container(obj, container)
if not observers:
observers = []
observers.append(agent)
# set first beliefs
for observer in observers:
oracle.set_first_belief(observer, obj, container)
# set second beliefs
if len(observers) >= 2:
for observer1, observer2 in combinations(observers, 2):
oracle.set_second_belief(
observer1, observer2, obj, container)
oracle.set_second_belief(
observer2, observer1, obj, container)
# set third beliefs
if len(observers) >= 3:
for chosen_observers in combinations(observers, 3):
for observer1, observer2, observer3 in permutations(chosen_observers):
oracle.set_third_belief(
observer1, observer2, observer3, obj, container)
# set fourth beliefs
if len(observers) >= 4:
for chosen_observers in combinations(observers, 4):
for observer1, observer2, observer3, observer4 in permutations(chosen_observers):
oracle.set_fourth_belief(
observer1, observer2, observer3, observer4, obj, container)
super().__init__(templates)
class PublicTellAction(Action):
def __init__(self, oracle, speaker, obj, container, listeners=None, believers=None):
templates = {
'declarative': [
'%s publicly claimed that %s is in the %s now.' % (
speaker, obj, container),
]
}
disbelievers = [
listener for listener in listeners if listener not in believers]
# All listeners would think others believe the claim
# for believer in believers:
# for disbeliever in disbelievers:
# oracle.set_second_belief(believer, disbeliever, obj, container)
# oracle.set_second_belief(disbeliever, believer, obj, container)
# A believer would think speaker also believes the obj is in container, speaker would think his words are trusted
for believer in believers:
oracle.set_first_belief(believer, obj, container)
oracle.set_second_belief(believer, speaker, obj, container)
oracle.set_second_belief(speaker, believer, obj, container)
for disbeliever in disbelievers:
oracle.set_second_belief(speaker, disbeliever, obj, container)
# for listener in listeners:
# # the speaker believes that all the listeners believe him
# oracle.set_second_belief(speaker, listener, obj, container)
# # all listeners know the believers based on the exiting order
# for believer in believers:
# oracle.set_second_belief(listener, believer, obj, container)
super().__init__(templates)
class PrivateTellAction(Action):
def __init__(self, oracle, speaker, listener, obj, container, trust=True):
templates = {
'declarative': [
'%s privately told %s that the %s is in the %s now.' % (
speaker, listener, obj, container),
]
}
# when the listener has less information (exit the room earlier), he'll trust the speaker
if trust:
oracle.set_first_belief(listener, obj, container)
oracle.set_second_belief(listener, speaker, obj, container)
oracle.set_second_belief(speaker, listener, obj, container)
super().__init__(templates)
class EnterAction(Action):
def __init__(self, oracle, args, observers=None, no_world_adjust=False):
templates = {
'declarative': [
', '.join(args[:-2]) + ' and ' + args[-2] +
' entered the ' + args[-1] + '.',
]
}
agents = args[:-1]
location = args[-1]
if location == 'waiting_room':
super().__init__(templates)
return
for agent in agents:
oracle.set_location(agent, location)
objs = oracle.get_objects_at_location(location)
observers = agents
# agent knows location of everything
if not no_world_adjust:
for obj in objs:
container = oracle.get_object_container(obj)
# oracle.set_first_belief(agent, obj, container)
# set first beliefs
if len(observers) >= 1:
for observer in observers:
oracle.set_first_belief(observer, obj, container)
# set second beliefs
if len(observers) >= 2:
for observer1, observer2 in combinations(observers, 2):
oracle.set_second_belief(
observer1, observer2, obj, container)
oracle.set_second_belief(
observer2, observer1, obj, container)
# set third beliefs
if len(observers) >= 3:
for chosen_observers in combinations(observers, 3):
for observer1, observer2, observer3 in permutations(chosen_observers):
oracle.set_third_belief(
observer1, observer2, observer3, obj, container)
# set fourth beliefs
if len(observers) >= 4:
for chosen_observers in combinations(observers, 4):
for observer1, observer2, observer3, observer4 in permutations(chosen_observers):
oracle.set_fourth_belief(
observer1, observer2, observer3, observer4, obj, container)
super().__init__(templates)
class NoiseAction(Action):
def __init__(self, agents, containers, objects):
animals = ['cat', 'dog', 'monkey', 'mouse']
personal_items = ['watch', 'gloves', 'phone']
distractors = [
f'{random.choice(agents)} saw a {random.choice(animals)}.',
f'{random.choice(agents)} lost his {random.choice(personal_items)}.',
f'{random.choice(agents)} likes the {random.choice(containers)}.',
f'{random.choice(agents)} dislikes the {random.choice(objects)}.',
]
templates = {
'declarative': [
random.choice(distractors)
]
}
super().__init__(templates)
|