Source code for ding.league.shared_payoff
import copy
from collections import defaultdict
from typing import Tuple, Optional
from easydict import EasyDict
from tabulate import tabulate
import numpy as np
from ding.utils import LockContext, LockContextType
from .player import Player
[docs]class BattleRecordDict(dict):
"""
Overview:
A dict which is used to record battle game result.
Initialized four fixed keys: `wins`, `draws`, `losses`, `games`; Each with value 0.
Interfaces:
__mul__
"""
data_keys = ['wins', 'draws', 'losses', 'games']
def __init__(self) -> None:
"""
Overview:
Initialize four fixed keys ['wins', 'draws', 'losses', 'games'] and set value to 0
"""
super(BattleRecordDict, self).__init__()
for k in self.data_keys:
self[k] = 0
[docs] def __mul__(self, decay: float) -> dict:
"""
Overview:
Multiply each key's value with the input multiplier ``decay``
Arguments:
- decay (:obj:`float`): The multiplier.
Returns:
- obj (:obj:`dict`): A deepcopied RecordDict after multiplication decay.
"""
obj = copy.deepcopy(self)
for k in obj.keys():
obj[k] *= decay
return obj
def create_payoff(cfg: EasyDict) -> Optional[BattleSharedPayoff]:
"""
Overview:
Given the key (payoff type), now supports keys ['solo', 'battle'],
create a new payoff instance if in payoff_mapping's values, or raise an KeyError.
Arguments:
- cfg (:obj:`EasyDict`): payoff config containing at least one key 'type'
Returns:
- payoff (:obj:`BattleSharedPayoff` or :obj:`SoloSharedPayoff`): the created new payoff, \
should be an instance of one of payoff_mapping's values
"""
payoff_mapping = {'battle': BattleSharedPayoff}
payoff_type = cfg.type
if payoff_type not in payoff_mapping.keys():
raise KeyError("not support payoff type: {}".format(payoff_type))
else:
return payoff_mapping[payoff_type](cfg)