From 08507428e7c4360a1d4a01e4716c1464c2e6f6d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edgar=20Du=C3=A9=C3=B1ez-Guzm=C3=A1n?= Date: Mon, 4 Dec 2023 08:51:13 -0800 Subject: [PATCH] Make the single agent opinion function not a closure so it can be used when debugging. PiperOrigin-RevId: 587735488 Change-Id: I67c12bc68a9d822cbfe2b48fa69febfd9acba215 --- concordia/metrics/opinion_of_others.py | 81 +++++++++++++------------- 1 file changed, 41 insertions(+), 40 deletions(-) diff --git a/concordia/metrics/opinion_of_others.py b/concordia/metrics/opinion_of_others.py index b9332133..2f30b8a7 100644 --- a/concordia/metrics/opinion_of_others.py +++ b/concordia/metrics/opinion_of_others.py @@ -107,52 +107,53 @@ def name( """Returns the name of the measurement.""" return self._name - def update(self) -> None: - """See base class.""" - def get_opinion(of_player: str) -> None: - if of_player == self._player_name: - return # No self opinions. - - prompt = interactive_document.InteractiveDocument(self._model) - parent_state = self._context_fn() - prompt.statement(parent_state) - - question = self._question.format( - opining_player=self._player_name, - of_player=of_player, + def _get_opinion(self, of_player: str) -> None: + if of_player == self._player_name: + return # No self opinions. + + prompt = interactive_document.InteractiveDocument(self._model) + parent_state = self._context_fn() + prompt.statement(parent_state) + + question = self._question.format( + opining_player=self._player_name, + of_player=of_player, + ) + + answer = prompt.multiple_choice_question( + question=question, answers=self._scale, + ) + answer_str = self._scale[answer] + + answer_float = float(answer) / float(len(self._scale) - 1) + datum = { + 'time_str': self._clock.now().strftime('%H:%M:%S'), + 'clock_step': self._clock.get_step(), + 'timestep': self._timestep, + 'value_float': answer_float, + 'value_str': answer_str, + 'opining_player': self._player_name, + 'of_player': of_player, + } + if self._measurements: + self._measurements.publish_datum(self._channel, datum) + + datum['time'] = self._clock.now() + if self._verbose: + print( + f'{self._name} of {of_player} as viewed by ' + f'{self._player_name}: {answer_str}' ) - answer = prompt.multiple_choice_question( - question=question, answers=self._scale, - ) - answer_str = self._scale[answer] - - answer_float = float(answer) / float(len(self._scale) - 1) - datum = { - 'time_str': self._clock.now().strftime('%H:%M:%S'), - 'clock_step': self._clock.get_step(), - 'timestep': self._timestep, - 'value_float': answer_float, - 'value_str': answer_str, - 'opining_player': self._player_name, - 'of_player': of_player, - } - if self._measurements: - self._measurements.publish_datum(self._channel, datum) - - datum['time'] = self._clock.now() - if self._verbose: - print( - f'{self._name} of {of_player} as viewed by ' - f'{self._player_name}: {answer_str}' - ) - - return + return + + def update(self) -> None: + """See base class.""" with concurrent.futures.ThreadPoolExecutor( max_workers=len(self._player_names) ) as executor: - executor.map(get_opinion, self._player_names) + executor.map(self._get_opinion, self._player_names) self._timestep += 1 def state(