Esempio n. 1
0
def CreateNfg(dim):
    """
    Create a new normal form game with the given dimensions
    and assign outcomes at all contingencies
    """
    nfg = gambit.NewTable(dim)
    profile = gambit.PureStrategyProfile(nfg)

    for cont in CrossProduct([
            range(1,
                  nfg.GetPlayer(pl).NumStrategies() + 1)
            for pl in range(1,
                            nfg.NumPlayers() + 1)
    ]):
        map(
            lambda pl: profile.SetStrategy(
                nfg.GetPlayer(pl).GetStrategy(cont[pl - 1])),
            range(1,
                  nfg.NumPlayers() + 1))
        profile.SetOutcome(nfg.NewOutcome())
    return nfg
Esempio n. 2
0
    def AsNfg(self):
        game = gambit.NewTable([len(self.contribs) for i in xrange(self.N)])

        # Label each strategy by its contribution
        for (pl, player) in enumerate(game.Players()):
            for (st, strategy) in enumerate(player.Strategies()):
                strategy.SetLabel(str(self.contribs[st]))

        for cont in gambit.StrategyIterator(gambit.StrategySupport(game)):
            # Create and attach an outcome to this contingency
            outcome = game.NewOutcome()
            cont.SetOutcome(outcome)
            # This is the vector of choices made in this contingency
            choices = [
                int(cont.GetStrategy(pl + 1).GetLabel())
                for pl in xrange(self.N)
            ]
            for pl in xrange(self.N):
                outcome.SetPayoff(
                    pl + 1, self.Payoff(choices[pl],
                                        sum(choices) - choices[pl]))

        return game
Esempio n. 3
0
# Function mapping own choice and choices of others into payoffs
def Payoff(alpha, omega, own, others):
    # Cost share depends only on lower quantities and one's own
    lower = [x for x in others if x <= own]
    lower.sort()
    lower.append(own)

    payoff = gambit.Rational(omega) + gambit.Rational(alpha * own)

    for k in xrange(1, len(lower) + 1):
        payoff -= gambit.Rational(
            Cost(Qsuper(lower, k)) - Cost(Qsuper(lower, k - 1)), N + 1 - k)
    return payoff


game = gambit.NewTable([len(strats) for i in xrange(N)])

# Label each strategy
for pl in xrange(N):
    player = game.GetPlayer(pl + 1)
    for st in xrange(player.NumStrategies()):
        strategy = player.GetStrategy(st + 1)
        strategy.SetLabel(str(strats[st]))

for (i,
     cont) in enumerate(gambit.StrategyIterator(gambit.StrategySupport(game))):
    if i % 1000 == 0: sys.stderr.write("%d\n" % i)
    # Create and attach an outcome to this contingency
    outcome = game.NewOutcome()
    cont.SetOutcome(outcome)
    # This is the vector of choices made in this contingency
Esempio n. 4
0
#
# A Python script to test the Gambit Python wrapper by building
# the minimum-unique-integer game
#

import gambit, sys

K = int(sys.argv[2])
N = int(sys.argv[1])
nfg = gambit.NewTable([K for i in range(N)])

# Pre-create outcomes.  Outcome 'i' is the outcome where player 'i' wins.

for pl in xrange(1, nfg.NumPlayers() + 1):
    outcome = nfg.NewOutcome()
    outcome.SetLabel('%d wins' % pl)
    outcome.SetPayoff(pl, "1")

for profile in gambit.StrategyIterator(gambit.StrategySupport(nfg)):
    choices = [
        profile.GetStrategy(pl).GetNumber()
        for pl in range(1,
                        nfg.NumPlayers() + 1)
    ]
    for ch in range(1, K + 1):
        if len([x for x in choices if x == ch]) == 1:
            # We have a winner
            profile.SetOutcome(nfg.GetOutcome(choices.index(ch) + 1))
            break
    # If we don't have a winner, leave outcome null, payoffs zero