def __init__(self, agent): """Return a wrapper around the given agent.""" if hasattr(agent, 'benchmark') or hasattr(agent, 'agent'): raise ValueError("Wrapped agent must not define a benchmark or" + "an agent attribute.") self.agent = agent self.benchmark = BenchmarkDataSet() # For episodewide statistics self.__rewards = []
class BenchmarkingAgent(object): """Agent that is used as a middleware to record benchmarks into a BenchmarkDataSet. """ def __init__(self, agent): """Return a wrapper around the given agent.""" if hasattr(agent, 'benchmark') or hasattr(agent, 'agent'): raise ValueError("Wrapped agent must not define a benchmark or" + "an agent attribute.") self.agent = agent self.benchmark = BenchmarkDataSet() # For episodewide statistics self.__rewards = [] def giveReward(self, reward): self.agent.giveReward(reward) self.__rewards.append(reward) def newEpisode(self): episodeLength = len(self.__rewards) avgReward = sum(self.__rewards) / episodeLength self.benchmark.appendLinked(avgReward, episodeLength) self.__rewards = [] return self.agent.newEpisode() def __getattribute__(self, key): try: return super(BenchmarkingAgent, self).__getattribute__(key) except AttributeError: agent = super(BenchmarkingAgent, self).__getattribute__('agent') return getattr(agent, key) def __setattribute__(self, key, value): if hasattr(self, key): setattr(self, key, value) else: agent = super(BenchmarkingAgent, self).__getattribute__('agent') setattr(agent, key, value)