示例#1
0
    def calc_util(self, next_pos_idxs=None):
        """Calculate the utility of moving in any given direction."""

        # set and use next pos idxs if they haven't been provided
        if next_pos_idxs is None:
            next_pos_idxs = self.get_next_pos_idxs()

        # calculate utility for all moves
        self.move_utils[:] = -np.inf

        for move_ctr, next_pos_idx in enumerate(next_pos_idxs):

            # leave at minimum if move idx is out of bounds
            if self.env.idx_out_of_bounds(next_pos_idx):
                continue

            # get change in entropy if source found
            deltaS_find_src = -self.S

            # calculate expected change in entropy if source not found

            # calculate probability of all possible odor sample values and
            # change in src pos entropy resulting from each odor
            odor_prob = np.zeros(self.odor_domain.shape, dtype=float)
            deltaS = np.zeros(self.odor_domain.shape, dtype=float)

            for odor_idx, odor in enumerate(self.odor_domain):
                # get src prior, src like & src-odor joint
                like = np.exp(self.get_loglike(odor, next_pos_idx))
                joint = like * self.prob

                # marginalize over src position to get odor prob
                odor_prob[odor_idx] = np.sum(joint)

                # calculate change in entropy of src pos distribution given
                # this odor
                next_prob = self.update_src_prob(odor,
                                                 next_pos_idx,
                                                 store=False,
                                                 log=False)
                deltaS[odor_idx] = entropy(next_prob) - self.S

            # calculate expected change in entropy if source not found
            # (averaged over odors)
            exptd_deltaS_no_src = np.dot(deltaS, odor_prob)

            ## calculate probability of finding source
            pfind_src = self.prob[tuple(next_pos_idx)]
            pno_src = 1 - pfind_src

            # calculate final expected change in entropy
            exptd_deltaS = exptd_deltaS_no_src * pno_src + deltaS_find_src * pfind_src

            if np.isnan(exptd_deltaS):
                print 'Error! Switching to debugging mode...'
                import pdb
                pdb.set_trace()

            self.move_utils[move_ctr] = -exptd_deltaS
示例#2
0
    def calc_util(self, next_pos_idxs=None):
        """Calculate the utility of moving in any given direction."""
        
        # set and use next pos idxs if they haven't been provided
        if next_pos_idxs is None:
            next_pos_idxs = self.get_next_pos_idxs()
        
        # calculate utility for all moves
        self.move_utils[:] = -np.inf
        
        for move_ctr, next_pos_idx in enumerate(next_pos_idxs):
            
            # leave at minimum if move idx is out of bounds
            if self.env.idx_out_of_bounds(next_pos_idx):
                continue

            # get change in entropy if source found
            deltaS_find_src = -self.S

            # calculate expected change in entropy if source not found

            # calculate probability of all possible odor sample values and
            # change in src pos entropy resulting from each odor
            odor_prob = np.zeros(self.odor_domain.shape, dtype=float)
            deltaS = np.zeros(self.odor_domain.shape, dtype=float)
            
            for odor_idx, odor in enumerate(self.odor_domain):
                # get src prior, src like & src-odor joint
                like = np.exp(self.get_loglike(odor, next_pos_idx))
                joint = like * self.prob
                
                # marginalize over src position to get odor prob
                odor_prob[odor_idx] = np.sum(joint)

                # calculate change in entropy of src pos distribution given
                # this odor
                next_prob = self.update_src_prob(odor, next_pos_idx,
                                                 store=False, log=False)
                deltaS[odor_idx] = entropy(next_prob) - self.S
                
            # calculate expected change in entropy if source not found
            # (averaged over odors)
            exptd_deltaS_no_src = np.dot(deltaS, odor_prob)
            
            ## calculate probability of finding source
            pfind_src = self.prob[tuple(next_pos_idx)]
            pno_src = 1 - pfind_src

            # calculate final expected change in entropy
            exptd_deltaS = exptd_deltaS_no_src*pno_src + deltaS_find_src*pfind_src
            
            if np.isnan(exptd_deltaS):
                print 'Error! Switching to debugging mode...'
                import pdb; pdb.set_trace()
            
            self.move_utils[move_ctr] = -exptd_deltaS
示例#3
0
    def update_src_prob(self, odor=None, pos_idx=None, store=True, log=True):
        """Update probability distribution over src pos.
        
        Args:
            store: set to True to store updated log source probability
            log: set to True to calculate unnormalized log probability. If 
                False, then normalized probability will be returned (not log)."""

        # if no odor or pos_idx provided, assume we're using the stored one
        if odor is None:
            odor = self.odor
        if pos_idx is None:
            pos_idx = self.pos_idx

        # create unnormalized log posterior
        logposterior = self.get_loglike(odor, pos_idx) + self.logprob

        # set probability to zero (log to -inf) at insect's position
        logposterior[tuple(pos_idx)] = -np.inf

        # if all values are -np.inf, set them all to zero, otherwise translate
        # so that max value is 0 to keep things within reasonable dyn. range
        if np.all(np.isinf(logposterior) * (logposterior < 0)):
            logposterior[:] = 0.
        else:
            logposterior -= logposterior.max()

        # either store or return log src probability
        if store:
            self.logprob = logposterior
            # store real probability and entropy
            # calculate normalized probability
            prob = np.exp(self.logprob)
            # normalize
            prob /= prob.sum()
            self.prob = prob
            self.S = entropy(prob)
        else:
            if log:
                return logposterior
            else:
                # calculate normalized probability
                prob = np.exp(logposterior)
                # normalize
                prob /= prob.sum()
                return prob
示例#4
0
    def update_src_prob(self, odor=None, pos_idx=None, store=True, log=True):
        """Update probability distribution over src pos.
        
        Args:
            store: set to True to store updated log source probability
            log: set to True to calculate unnormalized log probability. If 
                False, then normalized probability will be returned (not log)."""
        
        # if no odor or pos_idx provided, assume we're using the stored one
        if odor is None:
            odor = self.odor
        if pos_idx is None:
            pos_idx = self.pos_idx

        # create unnormalized log posterior
        logposterior = self.get_loglike(odor, pos_idx) + self.logprob

        # set probability to zero (log to -inf) at insect's position
        logposterior[tuple(pos_idx)] = -np.inf

        # if all values are -np.inf, set them all to zero, otherwise translate
        # so that max value is 0 to keep things within reasonable dyn. range
        if np.all(np.isinf(logposterior)*(logposterior < 0)):
            logposterior[:] = 0.
        else:
            logposterior -= logposterior.max()

        # either store or return log src probability
        if store:
            self.logprob = logposterior
            # store real probability and entropy
            # calculate normalized probability
            prob = np.exp(self.logprob)
            # normalize
            prob /= prob.sum()
            self.prob = prob
            self.S = entropy(prob)
        else:
            if log:
                return logposterior
            else:
                # calculate normalized probability
                prob = np.exp(logposterior)
                # normalize
                prob /= prob.sum()
                return prob