Пример #1
0
def Equations(support, player):
    payoffs = [ [] for strategy in support.Strategies(player) ]

    players = [ (pl, oplayer)
                for (pl, oplayer) in enumerate(support.GetGame().Players())
                if player != oplayer ]
    strategies = [ (st, strategy) 
                   for (st, strategy) in enumerate(support.Strategies(player)) ]
    for profile in gambit.StrategyIterator(support,
                                           support.GetStrategy(player.GetNumber(),
                                                               1)):
        contingency = "*".join([playerletters[pl] +
                                "%d" %
                                (profile.GetStrategy(oplayer).GetNumber()-1)
                                for (pl, oplayer) in players])
        for (st, strategy) in strategies:
            payoffs[st].append("(%s*%s)" %
                               (str(float(profile.GetStrategyValue(strategy))),
                                contingency))

    equations = [ "(%s)-(%s);\n" % ("+".join(payoffs[0]), "+".join(s2))
                  for s2 in payoffs[1:] ]
        
    # Substitute in sum-to-one constraints
    for (pl, oplayer) in enumerate(support.GetGame().Players()):
        if player == oplayer: continue
        playerchar = playerletters[pl]
        if support.NumStrategies(pl+1) == 1:
            for (i, equation) in enumerate(equations):
                equations[i] = equation.replace("*%s%d" % (playerchar,
                                                           support.GetStrategy(pl+1, 1).GetNumber()-1),
                                                "")
        else:
            subvar = "1-" + "-".join( playerchar+"%d" % (strategy.GetNumber() - 1)
                                      for (st, strategy) in enumerate(support.Strategies(oplayer))
                                      if st != 0 )
            for (i, equation) in enumerate(equations):
                equations[i] = equation.replace("%s%d" % (playerchar,
                                                          support.GetStrategy(pl+1, 1).GetNumber()-1),
                                                "("+subvar+")")

    return equations
Пример #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
Пример #3
0
    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
    choices = [int(cont.GetStrategy(pl + 1).GetLabel()) for pl in xrange(N)]
    for pl in xrange(N):
        pay = Payoff(alphas[pl], omegas[pl], choices[pl],
                     choices[:pl] + choices[pl + 1:])
        #print pay
        outcome.SetPayoff(pl + 1, pay)

print game.AsNfgFile()
Пример #4
0
# 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

print nfg.AsNfgFile()
Пример #5
0
    if own == 0: return omega

    return (gambit.Rational(omega) + gambit.Rational(alpha * own) -
            gambit.Rational(own, total) * gambit.Rational(Cost(total)))


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.StrategySupportProfile(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
    choices = [int(cont.GetStrategy(pl + 1).GetLabel()) for pl in xrange(N)]
    for pl in xrange(N):
        pay = Payoff(alphas[pl], omegas[pl], choices[pl],
                     choices[:pl] + choices[pl + 1:])
        #print pay
        outcome.SetPayoff(pl + 1, pay)

print game.AsNfgFile()
Пример #6
0
        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.StrategySupportProfile(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
    choices = [ int(cont.GetStrategy(pl+1).GetLabel())
                for pl in xrange(N) ]
    for pl in xrange(N):
        pay = Payoff(alphas[pl], omegas[pl], choices[pl],
                     choices[:pl] + choices[pl+1:])
        #print pay
        outcome.SetPayoff(pl+1, pay)

print game.AsNfgFile()