def makeRandomFOVDistr(self, agent): fov_key = stateKey(agent.name, FOV_FEATURE) tree = { 'if': equalRow(stateKey(agent.name, 'loc'), self.world_map.all_locations), None: noChangeMatrix(fov_key) } for il, loc in enumerate(self.world_map.all_locations): if loc not in self.victimClrCounts.keys(): tree[il] = setToConstantMatrix(fov_key, 'none') continue sub_tree, leaves = self._make_fov_color_dist(loc, 0) for dist in leaves: prob_dist = Distribution(dist) prob_dist.normalize() dist.clear() weights = [(setToConstantMatrix(fov_key, c), p) for c, p in prob_dist.items() if p > 0] if len(weights) == 1: weights.append((noChangeMatrix(fov_key), 0)) dist['distribution'] = weights tree[il] = sub_tree return tree
def makeExpiryDynamics(self): vic_colors = [ color for color in self.color_names if color not in {WHITE_STR, RED_STR} ] # update victim loc counters for loc in self.world_map.all_locations: red_ctr = stateKey(WORLD, 'ctr_' + loc + '_' + RED_STR) for color in vic_colors: ctr = stateKey(WORLD, 'ctr_' + loc + '_' + color) expire = self.color_expiry[color] # RED: if death time is reached, copy amount of alive victims to counter deathTree = { 'if': thresholdRow(self.world.time, expire), True: addFeatureMatrix(red_ctr, ctr), False: noChangeMatrix(red_ctr) } self.world.setDynamics(red_ctr, True, makeTree(deathTree)) # GREEN and GOLD: if death time reached, zero-out alive victims of that color deathTree = { 'if': thresholdRow(self.world.time, expire), True: setToConstantMatrix(ctr, 0), False: noChangeMatrix(ctr) } self.world.setDynamics(ctr, True, makeTree(deathTree))
def _makeLightToggleAction(self, agent): """ Action to toggle the light switch in a loc that has one. Toggling a switch in a loc affects the light status in all rooms that share its light """ locKey = stateKey(agent.name, 'loc') locsWithLights = set(self.sharedLights.keys()) ## Legal if I'm in a room with a light switch legalityTree = makeTree({ 'if': equalRow(locKey, locsWithLights), True: True, False: False }) action = agent.addAction({'verb': 'toggleLight'}, makeTree(legalityTree)) ## Instead of iterating over locations, I'll iterate over those that have ## switches and create a tree for each affected room for switch, affected in self.sharedLights.items(): for aff in affected: affFlag = stateKey(WORLD, 'light' + str(aff)) txnTree = { 'if': equalRow(locKey, switch), True: { 'if': equalRow(affFlag, True), True: setToConstantMatrix(affFlag, False), False: setToConstantMatrix(affFlag, True) }, False: noChangeMatrix(affFlag) } self.world.setDynamics(affFlag, action, makeTree(txnTree)) self.lightActions[agent.name] = action
def set_reward(self, agent, weight, model=None): rwd_feat = rewardKey(agent.name) # compares agent's current location rwd_tree = {'if': equalRow(self.location_feat, self.all_locations), None: noChangeMatrix(rwd_feat)} # get visitation count according to location for i, loc in enumerate(self.all_locations): loc_freq_feat = get_num_visits_location_key(agent, loc) rwd_tree[i] = dynamicsMatrix(rwd_feat, {self.time_feat: 1., loc_freq_feat: -1.}) \ if self.inverse else setToFeatureMatrix(rwd_feat, loc_freq_feat) agent.setReward(makeTree(rwd_tree), weight * self.normalize_factor, model)
def set_reward(self, agent, weight, model=None): rwd_feat = rewardKey(agent.name) # compares agent's current location rwd_tree = {'if': equalRow(self.location_feat, self.all_locations), None: noChangeMatrix(rwd_feat)} # get binary value according to visitation of location for i, loc in enumerate(self.all_locations): loc_freq_feat = get_num_visits_location_key(agent, loc) rwd_tree[i] = {'if': thresholdRow(loc_freq_feat, 1), True: setToConstantMatrix(rwd_feat, 1), False: setToConstantMatrix(rwd_feat, 0)} agent.setReward(makeTree(rwd_tree), weight * self.normalize_factor, model)
def _makeMoveActions(self, agent): """ N/E/S/W actions Legality: if current location has a neighbor in the given direction Dynamics: 1) change human's location; 2) set the seen flag for new location to True 3) Set the observable victim variables to the first victim at the new location, if any 4) Reset the crosshair/approached vars to none """ self.moveActions[agent.name] = [] locKey = stateKey(agent.name, 'loc') for direction in Directions: # Legal if current location has a neighbor in the given direction locsWithNbrs = set(self.neighbors[direction.value].keys()) legalityTree = makeTree({ 'if': equalRow(locKey, locsWithNbrs), True: True, False: False }) action = agent.addAction({ 'verb': 'move', 'object': direction.name }, legalityTree) self.moveActions[agent.name].append(action) # Dynamics of this move action: change the agent's location to 'this' location lstlocsWithNbrs = list(locsWithNbrs) tree = {'if': equalRow(locKey, lstlocsWithNbrs)} for il, loc in enumerate(lstlocsWithNbrs): tree[il] = setToConstantMatrix( locKey, self.neighbors[direction.value][loc]) self.world.setDynamics(locKey, action, makeTree(tree)) # move increments the counter of the location we moved to for dest in self.all_locations: destKey = stateKey(agent.name, 'locvisits_' + str(dest)) tree = makeTree({ 'if': equalRow(makeFuture(locKey), dest), True: incrementMatrix(destKey, 1), False: noChangeMatrix(destKey) }) self.world.setDynamics(destKey, action, tree) # increment time self.world.setDynamics( self.world.time, action, makeTree(incrementMatrix(self.world.time, MOVE_TIME_INC)))
def _createTriageAction(self, agent, color): loc_key = stateKey(agent.name, 'loc') # legal only if any "active" victim of given color is in the same loc tree = { 'if': equalRow(loc_key, self.world_map.all_locations), None: False } for i, loc in enumerate(self.world_map.all_locations): vicsInLocOfClrKey = stateKey(WORLD, 'ctr_' + loc + '_' + color) tree[i] = { 'if': thresholdRow(vicsInLocOfClrKey, 0), True: True, False: False } action = agent.addAction({'verb': 'triage_' + color}, makeTree(tree)) # different triage time thresholds according to victim type threshold = 7 if color == GREEN_STR else 14 long_enough = differenceRow(makeFuture(self.world.time), self.world.time, threshold) # make triage dynamics for counters of each loc for loc in self.world_map.all_locations: # successful triage conditions conds = [equalRow(loc_key, loc), long_enough] # location-specific counter of vics of this color: if successful, decrement vicsInLocOfClrKey = stateKey(WORLD, 'ctr_' + loc + '_' + color) tree = makeTree( anding(conds, incrementMatrix(vicsInLocOfClrKey, -1), noChangeMatrix(vicsInLocOfClrKey))) self.world.setDynamics(vicsInLocOfClrKey, action, tree) # white: increment vicsInLocOfClrKey = stateKey(WORLD, 'ctr_' + loc + '_' + WHITE_STR) tree = makeTree( anding(conds, incrementMatrix(vicsInLocOfClrKey, 1), noChangeMatrix(vicsInLocOfClrKey))) self.world.setDynamics(vicsInLocOfClrKey, action, tree) # Color saved counter: increment saved_key = stateKey(agent.name, 'numsaved_' + color) tree = { 'if': long_enough, True: incrementMatrix(saved_key, 1), False: noChangeMatrix(saved_key) } self.world.setDynamics(saved_key, action, makeTree(tree)) # Color saved: according to difference diff = {makeFuture(saved_key): 1, saved_key: -1} saved_key = stateKey(agent.name, 'saved_' + color) self.world.setDynamics(saved_key, action, makeTree(dynamicsMatrix(saved_key, diff))) self.world.setDynamics( saved_key, True, makeTree(setFalseMatrix(saved_key))) # default: set to False # increment time self.world.setDynamics( self.world.time, action, makeTree(incrementMatrix(self.world.time, threshold))) self.triageActs[agent.name][color] = action
def _createTriageAction(self, agent, color): fov_key = stateKey(agent.name, FOV_FEATURE) loc_key = stateKey(agent.name, 'loc') legal = {'if': equalRow(fov_key, color), True: True, False: False} action = agent.addAction({'verb': 'triage_' + color}, makeTree(legal)) if color == GREEN_STR: threshold = 7 else: threshold = 14 longEnough = differenceRow(makeFuture(self.world.time), self.world.time, threshold) for loc in self.world_map.all_locations: # successful triage conditions conds = [ equalRow(fov_key, color), equalRow(loc_key, loc), longEnough ] # location-specific counter of vics of this color: if successful, decrement vicsInLocOfClrKey = stateKey(WORLD, 'ctr_' + loc + '_' + color) tree = makeTree( anding(conds, incrementMatrix(vicsInLocOfClrKey, -1), noChangeMatrix(vicsInLocOfClrKey))) self.world.setDynamics(vicsInLocOfClrKey, action, tree) # white: increment vicsInLocOfClrKey = stateKey(WORLD, 'ctr_' + loc + '_' + WHITE_STR) tree = makeTree( anding(conds, incrementMatrix(vicsInLocOfClrKey, 1), noChangeMatrix(vicsInLocOfClrKey))) self.world.setDynamics(vicsInLocOfClrKey, action, tree) # Fov update to white tree = { 'if': longEnough, True: setToConstantMatrix(fov_key, WHITE_STR), False: noChangeMatrix(fov_key) } self.world.setDynamics(fov_key, action, makeTree(tree)) # Color saved counter: increment saved_key = stateKey(agent.name, 'numsaved_' + color) tree = { 'if': longEnough, True: incrementMatrix(saved_key, 1), False: noChangeMatrix(saved_key) } self.world.setDynamics(saved_key, action, makeTree(tree)) # Color saved: according to difference diff = {makeFuture(saved_key): 1, saved_key: -1} saved_key = stateKey(agent.name, 'saved_' + color) self.world.setDynamics(saved_key, action, makeTree(dynamicsMatrix(saved_key, diff))) self.world.setDynamics( saved_key, True, makeTree(setFalseMatrix(saved_key))) # default: set to False # increment time self.world.setDynamics( self.world.time, action, makeTree(incrementMatrix(self.world.time, threshold))) self.triageActs[agent.name][color] = action