Example #1
0
    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)))
Example #2
0
 def stochasticTriageDur(self):
     vic_colors = [
         color for color in self.color_names
         if color not in {WHITE_STR, RED_STR}
     ]
     for color in vic_colors:
         stochTree = {
             'distribution':
             [(incrementMatrix(self.world.time, c), p)
              for c, p in self.color_reqd_times[color].items()]
         }
         for actions in self.triageActs.values():
             triageActColor = actions[color]
             self.world.setDynamics(self.world.time, triageActColor,
                                    makeTree(stochTree))
Example #3
0
    def makeSearchAction(self, agent):
        action = agent.addAction({'verb': 'search'})

        # default: FOV is none
        fov_key = stateKey(agent.name, FOV_FEATURE)
        self.world.setDynamics(fov_key, True,
                               makeTree(setToConstantMatrix(fov_key, 'none')))

        # A victim can randomly appear in FOV
        fov_tree = self.makeRandomFOVDistr(agent)
        self.world.setDynamics(fov_key, action, makeTree(fov_tree))

        # increment time
        self.world.setDynamics(
            self.world.time, action,
            makeTree(incrementMatrix(self.world.time, SEARCH_TIME_INC)))

        self.searchActs[agent.name] = action
Example #4
0
    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
Example #5
0
    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
    world.addAgent(agent)

    # set parameters
    agent.setAttribute('discount', DISCOUNT)
    agent.setHorizon(HORIZON)

    # add position variable
    pos = world.defineState(agent.name, 'position', int, lo=-100, hi=100)
    world.setFeature(pos, 0)

    # define agents' actions (stay 0, left -1 and right +1)
    action = agent.addAction({'verb': 'move', 'action': 'nowhere'})
    tree = makeTree(setToFeatureMatrix(pos, pos))
    world.setDynamics(pos, action, tree)
    action = agent.addAction({'verb': 'move', 'action': 'left'})
    tree = makeTree(incrementMatrix(pos, -1))
    world.setDynamics(pos, action, tree)
    action = agent.addAction({'verb': 'move', 'action': 'right'})
    tree = makeTree(incrementMatrix(pos, 1))
    world.setDynamics(pos, action, tree)

    # define rewards (maximize position, i.e., always go right)
    agent.setReward(maximizeFeature(pos, agent.name), 1)

    # set order
    world.setOrder([agent.name])

    # agent has initial beliefs about its position, which will be updated after executing actions
    agent.omega = {actionKey(agent.name)}  # todo should not need this
    agent.setBelief(pos, Distribution({10: 0.5, 12: 0.5}))
    # agent.setBelief(pos, 10, get_true_model_name(agent))