# -*- coding: utf-8 -*-
"""
Implements PGT intelligence for iterSemiNFG objects
Part of: PyNFG - a Python package for modeling and solving Network Form Games
Created on Wed Jan 2 16:33:36 2013
Copyright (C) 2013 James Bono (jwbono@gmail.com)
GNU Affero General Public License
"""
from __future__ import division
import copy
import numpy as np
from pynfg import DecisionNode
from pynfg import iterSemiNFG
import scipy.stats.distributions as randvars
from pynfg.utilities.utilities import mh_decision
[docs]def uncoordinated_MC(G, S, noise, X, M, innoise, delta=1, integrand=None, \
mix=False, satisfice=None):
"""Run MC outer loop on random policy sequences for iterSemiNFG IQ calcs
:arg G: the game to be evaluated
:type G: iterSemiNFG or SemiNFG
:arg S: number of policy sequences to sample
:type S: int
:arg X: number of samples of each policy profile
:type X: int
:arg M: number of random alt policies to compare
:type M: int
:arg delta: the discount factor
:type delta: float
:arg integrand: a user-supplied function of G that is evaluated for each s
in S
:type integrand: func
.. note::
This is the uncoordinated approach because intelligence is assigned to
a DN instead of being assigned to a player.
"""
dnlist = [d.name for d in G.nodes if isinstance(d, DecisionNode)]
intel = {} #keys are MC iterations s, values are iq dicts
iq = dict(zip(dnlist, np.zeros(len(dnlist)))) #keys are node names, vals are iqs
funcout = {} #keys are s in S, vals are eval of integrand of G(s)
w = {}
weight = {}
for s in xrange(1, S+1): #sampling S sequences of policy profiles
print s
GG = copy.deepcopy(G)
for dn in dnlist: #drawing current policy
w[dn] = GG.node_dict[dn].perturbCPT(noise, mixed=mix, \
returnweight=True)
for dn in dnlist: #find the iq of each player's policy in turn
iq[dn] = uncoordinated_calciq(dn, GG, X, M, mix, delta, innoise, \
satisfice)
if integrand is not None:
funcout[s] = integrand(GG) #eval integrand GG(s), assign to funcout
intel[s] = copy.deepcopy(iq)
weight[s] = copy.deepcopy(w)
return intel, funcout, weight
[docs]def uncoordinated_MH(G, S, density, noise, X, M, innoise=1, delta=1, \
integrand=None, mix=False, satisfice=None):
"""Run MH for iterSemiNFG IQ calcs
:arg G: the iterated semiNFG to be evaluated
:type G: iterSemiNFG
:arg S: number of MH iterations
:type S: int
:arg noise: the degree of independence of the proposal distribution on the
current value.
:type noise: float
:arg density: the function that assigns weights to iq
:type density: func
:arg X: number of samples of each policy profile
:type X: int
:arg M: number of random alt policies to compare
:type M: int
:arg delta: the discount factor
:type delta: float
:arg integrand: a user-supplied function of G that is evaluated for each s
in S
:type integrand: func
.. warning::
This will throw an error if there is a decision node in G.starttime that
is not repeated throughout the net.
.. note::
This is the agent-approach because intelligence is assigned to a DN
instead of being assigned to a player.
"""
dnlist = [d.name for d in G.nodes if isinstance(d, DecisionNode)]
intel = {} #keys are s in S, vals are iq dict (dict of dicts)
iq = {} #keys are base names, iq timestep series
funcout = {} #keys are s in S, vals are eval of integrand of G(s)
dens = np.zeros(S+1) #storing densities for return
for s in xrange(1, S+1): #sampling S sequences of policy profiles
print s
GG = copy.deepcopy(G)
for dn in dnlist:
GG.node_dict[dn].perturbCPT(noise, mixed=mix, setCPT=False)
for dn in dnlist:#getting iq
iq[dn] = uncoordinated_calciq(dn, GG, X, M, mix, delta, innoise, \
satisfice)
# The MH decision
current_dens = density(iq)
verdict = mh_decision(current_dens, dens[s-1])
if verdict: #accepting new CPT
intel[s] = copy.deepcopy(iq)
G = copy.deepcopy(GG)
dens[s] = current_dens
else:
intel[s] = intel[s-1]
dens[s] = dens[s-1]
if integrand is not None:
funcout[s] = integrand(GG) #eval integrand G(s), assign to funcout
return intel, funcout, dens[1::]
[docs]def uncoordinated_calciq(dn, G, X, M, mix, delta, innoise, satisfice=None):
"""Calc IQ of CPT at dn, holding all other dns constant
:arg dn: the name of the decision node where intelligence is being
evaluated.
:type dn: str
:arg G: the semiNFG to be evaluated
:type G: SemiNFG or iterSemiNFG
:arg X: number of samples of each policy profile
:type X: int
:arg M: number of random alt policies with which to compare
:type M: int
:arg mix: if true, proposal distribution is over mixed CPTs. Default is
False.
:type mix: bool
:arg delta: the discount factor (ignored if SemiNFG)
:type delta: float
:arg innoise: the perturbation noise for the inner loop to draw alt CPTs
:type innoise: float
:returns: the fraction of alternative CPTs that have a lower utility than
the current CPT.
"""
util = 0
altutil = [0]*M
weight = np.ones(M)
tick = 0
p = G.node_dict[dn].player
oldCPT = copy.copy(G.node_dict[dn].CPT)
GG = copy.deepcopy(G)
if isinstance(G, iterSemiNFG):
ufoo = G.npv_reward
uargs = [p, G.starttime, delta]
else:
ufoo = G.utility
uargs = [p]
for x in xrange(1,X+1):
G.sample()
util = (ufoo(*uargs)+(x-1)*util)/x
if satisfice: #using the satisficing distribution for drawing alternatives
G = satisfice
for m in range(M): #Sample M alt CPTs for the player at the DN
if innoise == 1 or satisfice:
GG.node_dict[dn].perturbCPT(innoise, mixed=mix)
denw=1
else:
denw = GG.node_dict[dn].perturbCPT(innoise, mixed=mix, \
returnweight=True)
if not tick:
numw = denw #scaling constant num to ~ magnitude of den
weight[m] *= (numw/denw)
tick += 1
GG.sample() #sample altpolicy prof. to end of net
if isinstance(GG, iterSemiNFG):
altutil[m] = GG.npv_reward(p, GG.starttime, delta)
else:
altutil[m] = GG.utility(p)
GG.node_dict[dn].CPT = oldCPT #resetting the CPT for the next draw
#weight of alts worse than G
worse = [weight[m] for m in range(M) if altutil[m]<util]
return np.sum(worse)/np.sum(weight) #fraction of alts worse than G is IQ