Example #1
0
def bestresponse_node(Game, dn, N, delta=1, tol=30, verbose=False):
    """Compute level-k best response at the DN given Game

    :arg Game: the Network Form Game of interest
    :type Game: SemiNFG or iterSemiNFG
    :arg dn: the name of the decision node where MCEUs are estimated
    :type dn: str
    :arg N: the max number of iterations for the estimation
    :type N: int
    :arg tol: the minimum number of samples per parent value
    :type tol: int

    """
    G = copy.deepcopy(Game)
    EUtable = mceu(G, dn, N, tol, delta, verbose)
    G.node_dict[dn].CPT = convert_2_pureCPT(EUtable)
    return G
Example #2
0
    def train_node(self, nodename, level, logit=False, setCPT=False, verbose=False):
        """Compute level-k best response at the DN given Game

        :arg nodename: the name of the decision node where MCEUs are estimated
        :type nodename: str
        :arg level: The level at which to train that player
        :type level: int
        :arg setCPT: If the trained CPT should be set as the current CPT.
            Otherwise, it can be accessed through node.LevelCPT.  Default is
            False
        :type setCPT: bool

        Notes
        -----

        If training a player at level k, the other players' CPT will be accessed
        through self.Game.node_dict[other_player].LevelCPT[k-1]

        """
        print 'Training ' + nodename + ' at level ' + str(level)
        Game = copy.deepcopy(self.Game)  # copy in order to maintain original CPT
        ps = self.specs
        for node in Game.node_dict.values():  # Game changes, self.Game doesn't
            if type(node) is pynfg.DecisionNode:
                try:
                    node.CPT = node.LevelCPT[level - 1]
                except KeyError:
                    raise KeyError('Need to train other players at level %s'
                                   % str(level-1))
        EUtable = mceu(Game, nodename, Game.node_dict[nodename].N,
                       Game.node_dict[nodename].tol, Game.node_dict[nodename].delta,
                       verbose=verbose)
        if not logit:
            self.Game.node_dict[nodename].LevelCPT[level] = \
                  convert_2_pureCPT(EUtable)
            if setCPT:
                self.Game.node_dict[nodename].CPT = convert_2_pureCPT(EUtable)
        else:
            weight = np.exp(Game.node_dict[nodename].beta*EUtable)
            norm = np.sum(weight, axis=-1)
            self.Game.node_dict[nodename].LevelCPT[level] = \
            weight/norm[..., np.newaxis]
            if setCPT:
                self.Game.node_dict[nodename].CPT = weight/norm[..., np.newaxis]
Example #3
0
def logitresponse_node(Game, dn, N, delta=1, beta=1, tol=30, verbose=False):
    """Compute level-k logit response at the DN given Game

    :arg Game: the Network Form Game of interest
    :type Game: SemiNFG or iterSemiNFG
    :arg dn: the name of the decision node where MCEUs are estimated
    :type dn: str
    :arg N: the max number of iterations for the estimation
    :type N: int
    :arg tol: the minimum number of samples per parent value
    :type tol: int

    """
    G = copy.deepcopy(Game)
    EUtable = mceu(G, dn, N, tol, delta, verbose)
    weight = np.exp(beta*EUtable)
    norm = np.sum(weight, axis=-1)
    G.node_dict[dn].CPT = weight/norm[...,np.newaxis]
    return G