Skip to content

Commit

Permalink
Make the single agent opinion function not a closure so it can be use…
Browse files Browse the repository at this point in the history
…d when debugging.

PiperOrigin-RevId: 587735488
Change-Id: I67c12bc68a9d822cbfe2b48fa69febfd9acba215
  • Loading branch information
duenez authored and copybara-github committed Dec 4, 2023
1 parent 3f48d0f commit 0850742
Showing 1 changed file with 41 additions and 40 deletions.
81 changes: 41 additions & 40 deletions concordia/metrics/opinion_of_others.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 0850742

Please sign in to comment.