Skip to content

Commit

Permalink
Update pub coordination and haggling scenarios.
Browse files Browse the repository at this point in the history
also:
- move most of substrate configuration into the sample_time_and_place
- fix minor bugs
PiperOrigin-RevId: 693430728
Change-Id: I7a53fae340f68fde6a372a4c297267ec20d84ddc
  • Loading branch information
vezhnick authored and copybara-github committed Nov 5, 2024
1 parent f7b3c3f commit b04c8a7
Show file tree
Hide file tree
Showing 10 changed files with 315 additions and 149 deletions.
24 changes: 8 additions & 16 deletions examples/modular/environment/haggling.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,11 @@ def configure_player(
f'Would {name} accept the offer?:': 'accept',
f'What price would {name} propose?:': '3 coins',
}
extras['specific_memories'] = [
f'{name} does not care about the price. {name} will accept any offer!'
' They are very vocal about it and will not haggle and will praise any'
' offer.'
]

return formative_memories.AgentConfig(
name=name,
Expand Down Expand Up @@ -549,10 +554,6 @@ def __init__(
resident_visitor_modules: Sequence[types.ModuleType] | None = None,
supporting_agent_module: types.ModuleType | None = None,
time_and_place_module: str | None = None,
num_supporting_player: int = 0,
only_match_with_support: bool = False,
num_games: int = 2,
num_main_players: int = 3,
seed: int | None = None,
):
"""Initialize the simulation object.
Expand All @@ -577,11 +578,7 @@ def __init__(
time_and_place_module: optionally, specify a module containing settings
that create a sense of setting in a specific time and place. If not
specified, a random module will be chosen from the default options.
num_supporting_player: the number of supporting players.
only_match_with_support: whether to only match main players with
supporting players.
num_games: the number of games to play.
num_main_players: the number of main players.
seed: the random seed to use.
"""
# Support for these parameters will be added in a future addition coming
Expand All @@ -595,11 +592,6 @@ def __init__(
seed=seed,
)
)
sampled_settings.num_supporting_players = num_supporting_player
sampled_settings.only_match_with_support = only_match_with_support
sampled_settings.num_main_players = num_main_players
sampled_settings.num_games = num_games

self._rng = random.Random(sampled_settings.random_seed)

start_time = datetime.datetime(
Expand Down Expand Up @@ -699,10 +691,10 @@ def __init__(
),
)
explicit_preference = agent_components.constant.Constant(
pre_act_key='explicit preference',
pre_act_key='Explicit preference',
state=(
f'{player_config.name} will accept any offer! They are very vocal'
' about it.'
' about it and will not haggle and will praise any offer.'
),
)
player = self._build_supporting_agent(
Expand Down
39 changes: 0 additions & 39 deletions examples/modular/environment/haggling_gullible.py

This file was deleted.

28 changes: 12 additions & 16 deletions examples/modular/environment/haggling_multi_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class WorldConfig:
buyer_base_reward_min: int = 5
seller_base_reward_max: int = 2
num_games: int = 2
num_main_players: int = 3
num_main_players: int = 2
prices: Sequence[int] = (1, 2, 3, 4, 5, 6)
items_for_sale: Sequence[str] = ('apple', 'banana', 'pear')
random_seed: int = 42
Expand Down Expand Up @@ -169,6 +169,11 @@ def configure_player(
f'Would {name} accept the offer?:': 'accept',
f'What price would {name} propose?:': '3 coins',
}
extras['specific_memories'] = [
f'{name} does not care about the price. {name} will accept any offer!'
' They are very vocal about it and will not haggle and will praise any'
' offer.'
]

return formative_memories.AgentConfig(
name=name,
Expand Down Expand Up @@ -579,10 +584,6 @@ def __init__(
resident_visitor_modules: Sequence[types.ModuleType] | None = None,
supporting_agent_module: types.ModuleType | None = None,
time_and_place_module: str | None = None,
num_supporting_player: int = 0,
only_match_with_support: bool = False,
num_games: int = 2,
num_main_players: int = 3,
seed: int | None = None,
):
"""Initialize the simulation object.
Expand All @@ -607,11 +608,6 @@ def __init__(
time_and_place_module: optionally, specify a module containing settings
that create a sense of setting in a specific time and place. If not
specified, a random module will be chosen from the default options.
num_supporting_player: the number of supporting players.
only_match_with_support: whether to only match main players with
supporting players.
num_games: the number of games to play.
num_main_players: the number of main players.
seed: the random seed to use.
"""
# Support for these parameters will be added in a future addition coming
Expand All @@ -625,10 +621,10 @@ def __init__(
seed=seed,
)
)
sampled_settings.num_supporting_players = num_supporting_player
sampled_settings.only_match_with_support = only_match_with_support
sampled_settings.num_main_players = num_main_players
sampled_settings.num_games = num_games
# sampled_settings.num_supporting_players = num_supporting_player
# sampled_settings.only_match_with_support = only_match_with_support
# sampled_settings.num_main_players = num_main_players
# sampled_settings.num_games = num_games
self._rng = random.Random(sampled_settings.random_seed)

start_time = datetime.datetime(
Expand Down Expand Up @@ -728,10 +724,10 @@ def __init__(
),
)
explicit_preference = agent_components.constant.Constant(
pre_act_key='explicit preference',
pre_act_key='Explicit preference',
state=(
f'{player_config.name} will accept any offer! They are very vocal'
' about it.'
' about it and will not haggle and will praise any offer.'
),
)
player = self._build_supporting_agent(
Expand Down
137 changes: 137 additions & 0 deletions examples/modular/environment/modules/fruitville_haggling_gullible.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
# Copyright 2024 DeepMind Technologies Limited.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""World configuration for the Fruitville haggling scenario."""

import random
from examples.modular.environment import haggling

YEAR = 1895
MONTH = 9
DAY = 12

FEMALE_NAMES = [
"Anya Blossomwood",
"Brynn Orchardheart",
"Clara Applebrook",
"Della Plumstone",
"Elara Berryvine",
"Faye Honeydew",
"Gaia Willowbrook",
"Hazel Nutgrove",
"Ivy Pearblossom",
"Juniper Quincewood",
"Kira Citronbloom",
"Lila Figleaf",
"Maeve Mulberry",
"Nova Peachwood",
"Opal Apricot",
"Sasha Grapevine",
"Quinn Cherryblossom",
"Rowan Cranberry",
"Sage Honeycomb",
"Willow Blackberry",
]

MALE_NAMES = [
"Aiden Riversong",
"Blaise Windwillow",
"Cedric Meadowbrook",
"Dorian Sunstone",
"Elias Berryfield",
"Flynn Honeycomb",
"Garen Willowshade",
"Hunter Nutwood",
"Ivor Pearbrook",
"Jasper Quincehill",
"Kieran Citronbrook",
"Lennon Figtree",
"Magnus Mulberrylane",
"Nolan Peachgrove",
"Orion Apricotwood",
"Peregrine Grapehill",
"Quentin Cherrybrook",
"Rowan Cranberryfield",
"Silas Honeybrook",
"Tristan Blackberrylane",
]

SCENARIO_PREMISE = (
"In the realm of Ouroboros, there is a quiet village of"
" Fruitville, which is famous for its fruit market. Traders from"
" all over the realm come to Fruitville to buy and sell produce."
)

VISUAL_SCENE_OPENINGS = [
(
"The first rays of dawn painted the sky above Fruitville in hues of"
" orange and gold, casting a warm glow over the bustling market. Stalls"
" overflowed with vibrant fruits, their aromas mingling in the crisp"
" morning air."
),
(
"As the sun peeked over the horizon, the market of Fruitville stirred"
" to life. Merchants, their voices a cheerful symphony, arranged their"
" wares: glistening berries, plump melons, and exotic fruits from"
" distant lands."
),
(
"Dewdrops clung to the colorful fruits displayed in the market of"
" Fruitville, reflecting the soft morning light. The air buzzed with"
" anticipation as traders and customers alike gathered for the day's"
" trade."
),
(
"The cobblestone streets of Fruitville echoed with the clatter of"
" hooves and the rumble of carts as the market awoke. Underneath"
" colorful awnings, merchants proudly presented their bountiful"
" harvests, their voices a chorus of greetings and bartering."
),
(
"In the heart of Fruitville, the market square transformed into a"
" kaleidoscope of colors as the sun rose. Fruits of every imaginable"
" shape and size adorned the stalls, a feast for the eyes and a promise"
" of delightful flavors."
),
]


def sample_parameters(seed: int | None = None):
"""Samples a set of parameters for the world configuration."""
seed = seed if seed is not None else random.getrandbits(63)

config = haggling.WorldConfig(
year=YEAR,
location="Fruitville",
premise=SCENARIO_PREMISE,
scene_visuals=VISUAL_SCENE_OPENINGS,
random_seed=seed,
num_supporting_players=1,
num_main_players=1,
num_games=3,
buyer_base_reward_min=6,
seller_base_reward_max=1,
only_match_with_support=True,
)
all_names = list(MALE_NAMES) + list(FEMALE_NAMES)
rng = random.Random(config.random_seed)
rng.shuffle(all_names)
config.people = all_names

for _, name in enumerate(MALE_NAMES):
config.person_data[name] = {"gender": "male"}
for _, name in enumerate(FEMALE_NAMES):
config.person_data[name] = {"gender": "female"}

return config
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ def sample_parameters(seed: int | None = None):
premise=SCENARIO_PREMISE,
scene_visuals=VISUAL_SCENE_OPENINGS,
random_seed=seed,
num_supporting_players=1,
num_main_players=1,
num_games=3,
only_match_with_support=True,
)

all_names = list(MALE_NAMES) + list(FEMALE_NAMES)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Copyright 2024 DeepMind Technologies Limited.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""A set of pub names and reasons to like them."""

import random
from examples.modular.environment import pub_coordination
from examples.modular.environment.modules import pub_coordination_london

YEAR = pub_coordination_london.YEAR
MONTH = pub_coordination_london.MONTH
DAY = pub_coordination_london.DAY

NUM_PUBS = pub_coordination_london.NUM_PUBS


def sample_parameters(seed: int | None = None):
"""Samples a set of parameters for the world configuration."""
seed = seed if seed is not None else random.getrandbits(63)
rng = random.Random(seed)

pubs = rng.sample(
list(pub_coordination_london.PUB_PREFERENCES.keys()),
pub_coordination_london.NUM_PUBS,
)
pub_preferences = {
k: pub_coordination_london.PUB_PREFERENCES[k] for k in pubs
}

config = pub_coordination.WorldConfig(
year=pub_coordination_london.YEAR,
location="London",
event="European football cup",
game_countries=pub_coordination_london.EURO_CUP_COUNTRIES,
venues=pubs,
venue_preferences=pub_preferences,
social_context=pub_coordination_london.SOCIAL_CONTEXT,
random_seed=seed,
num_main_players=2,
num_supporting_players=4,
num_games=5,
)

all_names = list(pub_coordination_london.MALE_NAMES) + list(
pub_coordination_london.FEMALE_NAMES
)

rng.shuffle(all_names)
config.people = all_names

for _, name in enumerate(pub_coordination_london.MALE_NAMES):
config.person_data[name] = {"gender": "male"}
for _, name in enumerate(pub_coordination_london.FEMALE_NAMES):
config.person_data[name] = {"gender": "female"}

return config
Loading

0 comments on commit b04c8a7

Please sign in to comment.