Skip to content

Commit

Permalink
Merge pull request #272 from valory-xyz/fix/redeeming
Browse files Browse the repository at this point in the history
Update the utilized tools when adjusting the policy's tools
  • Loading branch information
dagacha authored Jun 10, 2024
2 parents 35e8515 + 671cb99 commit 0c584db
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 28 deletions.
10 changes: 5 additions & 5 deletions packages/packages.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
"contract/valory/transfer_nft_condition/0.1.0": "bafybeihnict3irtvnyxtkwyg6wphe44wz3dogijiha45xrkcrh5ktq2lsi",
"contract/valory/relayer/0.1.0": "bafybeiaabvxim4blp5fxb6qjlzjivtvkme3fk24h5jte7w6vr6rsx72j6u",
"skill/valory/market_manager_abci/0.1.0": "bafybeicbvxvjkoksbknujaid5hx7krjlgm6barcjcwo33tdccanrcp674a",
"skill/valory/decision_maker_abci/0.1.0": "bafybeiaw5kilhtdu7fcmk5553l3m6v366myaflekvipe4qa2vp4arbzlva",
"skill/valory/trader_abci/0.1.0": "bafybeibvlfhotlobiuemgra677qrmb5juumhpg4wz5uayi42sa6asxlona",
"skill/valory/tx_settlement_multiplexer_abci/0.1.0": "bafybeidkr6vthdixhjxe423bur7deeynzfwqmqc2gyylhxadfsbzo22faa",
"skill/valory/decision_maker_abci/0.1.0": "bafybeibb2qs73ug56hazdmuirwa2avbbdmenuhbmmwcp7zn4fdgztd6sjy",
"skill/valory/trader_abci/0.1.0": "bafybeietcp2q726jgo3wwoautzkxuwjoa4jdv2bllcx4o44plsh5gqt7fu",
"skill/valory/tx_settlement_multiplexer_abci/0.1.0": "bafybeiaybhcckjudnmelw3vc4mhw7fmydisyguanvlb2h6kdbhke3kjsvi",
"skill/valory/staking_abci/0.1.0": "bafybeigo7bicej5t2rbki37cmcwkzgwpcnopokn7ijhylmkihsbqw47xr4",
"skill/valory/check_stop_trading_abci/0.1.0": "bafybeickfeuqlpmryegnfvfu2duk2v4ycowwloohu3xxrafd5md6xl5swi",
"agent/valory/trader/0.1.0": "bafybeidt6cy3vo33dcue5ocyi7j2y6gxomzdotlgjq7hresembhdam533i",
"service/valory/trader/0.1.0": "bafybeid6wzptaugaeyf3mfgjop2buxjawnuignycbdod46dh2kazja6eim"
"agent/valory/trader/0.1.0": "bafybeiczhuenxymwnoankdarwogerydxmzkjeefe6anokksaok25dto6nu",
"service/valory/trader/0.1.0": "bafybeih3l6tcadclvaumuvgyle3jgt52d6o7s5rb4r34tfmzsic25bmwje"
},
"third_party": {
"protocol/open_aea/signing/1.0.0": "bafybeihv62fim3wl2bayavfcg3u5e5cxu3b7brtu4cn5xoxd6lqwachasi",
Expand Down
6 changes: 3 additions & 3 deletions packages/valory/agents/trader/aea-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ skills:
- valory/reset_pause_abci:0.1.0:bafybeiameewywqigpupy3u2iwnkfczeiiucue74x2l5lbge74rmw6bgaie
- valory/termination_abci:0.1.0:bafybeif2zim2de356eo3sipkmoev5emwadpqqzk3huwqarywh4tmqt3vzq
- valory/transaction_settlement_abci:0.1.0:bafybeic3tccdjypuge2lewtlgprwkbb53lhgsgn7oiwzyrcrrptrbeyote
- valory/tx_settlement_multiplexer_abci:0.1.0:bafybeidkr6vthdixhjxe423bur7deeynzfwqmqc2gyylhxadfsbzo22faa
- valory/tx_settlement_multiplexer_abci:0.1.0:bafybeiaybhcckjudnmelw3vc4mhw7fmydisyguanvlb2h6kdbhke3kjsvi
- valory/market_manager_abci:0.1.0:bafybeicbvxvjkoksbknujaid5hx7krjlgm6barcjcwo33tdccanrcp674a
- valory/decision_maker_abci:0.1.0:bafybeiaw5kilhtdu7fcmk5553l3m6v366myaflekvipe4qa2vp4arbzlva
- valory/trader_abci:0.1.0:bafybeibvlfhotlobiuemgra677qrmb5juumhpg4wz5uayi42sa6asxlona
- valory/decision_maker_abci:0.1.0:bafybeibb2qs73ug56hazdmuirwa2avbbdmenuhbmmwcp7zn4fdgztd6sjy
- valory/trader_abci:0.1.0:bafybeietcp2q726jgo3wwoautzkxuwjoa4jdv2bllcx4o44plsh5gqt7fu
- valory/staking_abci:0.1.0:bafybeigo7bicej5t2rbki37cmcwkzgwpcnopokn7ijhylmkihsbqw47xr4
- valory/check_stop_trading_abci:0.1.0:bafybeickfeuqlpmryegnfvfu2duk2v4ycowwloohu3xxrafd5md6xl5swi
- valory/mech_interact_abci:0.1.0:bafybeih2cck5xu6yaibomwtm5zbcp6llghr3ighdnk56fzwu3ihu5xx35e
Expand Down
2 changes: 1 addition & 1 deletion packages/valory/services/trader/service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ license: Apache-2.0
fingerprint:
README.md: bafybeigtuothskwyvrhfosps2bu6suauycolj67dpuxqvnicdrdu7yhtvq
fingerprint_ignore_patterns: []
agent: valory/trader:0.1.0:bafybeidt6cy3vo33dcue5ocyi7j2y6gxomzdotlgjq7hresembhdam533i
agent: valory/trader:0.1.0:bafybeiczhuenxymwnoankdarwogerydxmzkjeefe6anokksaok25dto6nu
number_of_agents: 4
deployment:
agent:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def __init__(self, **kwargs: Any) -> None:
self._mech_id: int = 0
self._mech_hash: str = ""
self._mech_tools: Optional[List[str]] = None
self._utilized_tools: Dict[str, int] = {}

@property
def mech_id(self) -> int:
Expand Down Expand Up @@ -89,18 +90,26 @@ def mech_tools(self, mech_tools: List[str]) -> None:
@property
def utilized_tools(self) -> Dict[str, int]:
"""Get the utilized tools."""
if self.is_first_period:
tools = self._try_recover_utilized_tools()
if tools is not None:
return tools
return {}
return self.synchronized_data.utilized_tools
return self._utilized_tools

@utilized_tools.setter
def utilized_tools(self, utilized_tools: Dict[str, int]) -> None:
"""Get the utilized tools."""
self._utilized_tools = utilized_tools

@property
def mech_tools_api(self) -> AgentToolsSpecs:
"""Get the mech agent api specs."""
return self.context.agent_tools

def setup(self) -> None:
"""Set the behaviour up."""
self.utilized_tools = (
self._try_recover_utilized_tools()
if self.is_first_period
else self.synchronized_data.utilized_tools
)

def set_mech_agent_specs(self) -> None:
"""Set the mech's agent specs."""
full_ipfs_hash = CID_PREFIX + self.mech_hash
Expand Down Expand Up @@ -201,19 +210,38 @@ def _get_tools(
):
yield from self.wait_for_condition_with_sleep(step)

def _update_utilized_tools(
self, indexes: List[int], remove_mode: bool = False
) -> None:
"""Update the utilized tools' indexes to match the fact that the given ones have been removed or added."""
updated_tools = {}
for idx in indexes:
for tx_hash, tool_idx in self.utilized_tools.items():
if tool_idx == idx:
continue

updated_idx = tool_idx
if tool_idx > idx:
updated_idx += -1 if remove_mode else 1

updated_tools[tx_hash] = updated_idx
self.utilized_tools = updated_tools

def _adjust_policy_tools(self, local: List[str]) -> None:
"""Add or remove tools from the policy to match the remote tools."""
# remove tools if they are not available anymore
# process the indices in reverse order to avoid index shifting when removing the unavailable tools later
reversed_idx = range(len(local) - 1, -1, -1)
removed_idx = [idx for idx in reversed_idx if local[idx] not in self.mech_tools]
self.policy.remove_tools(removed_idx)
self._update_utilized_tools(removed_idx, remove_mode=True)

# add tools if there are new ones available
# process the indices in reverse order to avoid index shifting when adding the new tools later
reversed_idx = range(len(self.mech_tools) - 1, -1, -1)
new_idx = [idx for idx in reversed_idx if self.mech_tools[idx] not in local]
self.policy.add_new_tools(new_idx)
self._update_utilized_tools(new_idx)

def _set_policy(self) -> None:
"""Set the E Greedy Policy."""
Expand Down Expand Up @@ -252,16 +280,15 @@ def _try_recover_policy(self) -> Optional[EGreedyPolicy]:
self.context.logger.warning(f"Could not recover the policy: {e}.")
return None

def _try_recover_utilized_tools(self) -> Optional[Dict[str, Any]]:
def _try_recover_utilized_tools(self) -> Dict[str, int]:
"""Try to recover the available tools from the tools store."""
try:
tools_path = self.params.store_path / self.UTILIZED_TOOLS_STORE
with open(tools_path, "r") as f:
tools = json.load(f)
return tools
except Exception as e:
self.context.logger.warning(f"Could not recover the tools: {e}.")
return None
tools_path = self.params.store_path / self.UTILIZED_TOOLS_STORE
with open(tools_path, "r") as tools_file:
try:
return json.load(tools_file)
except Exception as exc:
self.context.logger.warning(f"Could not recover the tools: {exc}.")
return {}

def _try_recover_mech_tools(self) -> Optional[List[str]]:
"""Try to recover the available tools from the tools store."""
Expand Down
2 changes: 1 addition & 1 deletion packages/valory/skills/decision_maker_abci/skill.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ fingerprint:
behaviours/reedem.py: bafybeieyqb2w7bvvl4wkgpqwryyyb4lex4wmwoykylehr2zljz3a7tkeja
behaviours/round_behaviour.py: bafybeibvhobpvzzd37ecleuyp2jrbed6nontcw7urtsilbbzvqsmmupx64
behaviours/sampling.py: bafybeibtkli72qsvotkrsepkgpiumtr5sershtkpb427oygnszs3dpgxry
behaviours/tool_selection.py: bafybeie7xhopohfoct7jggerk6nu3vkhlx7uhcowo3dfktoqmr7227j4ke
behaviours/tool_selection.py: bafybeicjfo5pqaupv4bpupvkccfffg65sygcgzlg4cieoiktjfilzf5l3a
dialogues.py: bafybeigpwuzku3we7axmxeamg7vn656maww6emuztau5pg3ebsoquyfdqm
fsm_specification.yaml: bafybeif2kas4eho4aielucf3ihjzjsog32icccbxrvje6kkw7aw4m4zg6q
handlers.py: bafybeielhwnfm2blt6clmwr557g2g3gddnt5bpb4cqul6p3wiwsfoksvlu
Expand Down
4 changes: 2 additions & 2 deletions packages/valory/skills/trader_abci/skill.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ skills:
- valory/transaction_settlement_abci:0.1.0:bafybeic3tccdjypuge2lewtlgprwkbb53lhgsgn7oiwzyrcrrptrbeyote
- valory/termination_abci:0.1.0:bafybeif2zim2de356eo3sipkmoev5emwadpqqzk3huwqarywh4tmqt3vzq
- valory/market_manager_abci:0.1.0:bafybeicbvxvjkoksbknujaid5hx7krjlgm6barcjcwo33tdccanrcp674a
- valory/decision_maker_abci:0.1.0:bafybeiaw5kilhtdu7fcmk5553l3m6v366myaflekvipe4qa2vp4arbzlva
- valory/tx_settlement_multiplexer_abci:0.1.0:bafybeidkr6vthdixhjxe423bur7deeynzfwqmqc2gyylhxadfsbzo22faa
- valory/decision_maker_abci:0.1.0:bafybeibb2qs73ug56hazdmuirwa2avbbdmenuhbmmwcp7zn4fdgztd6sjy
- valory/tx_settlement_multiplexer_abci:0.1.0:bafybeiaybhcckjudnmelw3vc4mhw7fmydisyguanvlb2h6kdbhke3kjsvi
- valory/staking_abci:0.1.0:bafybeigo7bicej5t2rbki37cmcwkzgwpcnopokn7ijhylmkihsbqw47xr4
- valory/check_stop_trading_abci:0.1.0:bafybeickfeuqlpmryegnfvfu2duk2v4ycowwloohu3xxrafd5md6xl5swi
- valory/mech_interact_abci:0.1.0:bafybeih2cck5xu6yaibomwtm5zbcp6llghr3ighdnk56fzwu3ihu5xx35e
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ protocols:
- valory/ledger_api:1.0.0:bafybeihdk6psr4guxmbcrc26jr2cbgzpd5aljkqvpwo64bvaz7tdti2oni
skills:
- valory/abstract_round_abci:0.1.0:bafybeiar2yhzxacfe3qqamqhaihtlcimquwedffctw55sowx6rac3cm3ui
- valory/decision_maker_abci:0.1.0:bafybeiaw5kilhtdu7fcmk5553l3m6v366myaflekvipe4qa2vp4arbzlva
- valory/decision_maker_abci:0.1.0:bafybeibb2qs73ug56hazdmuirwa2avbbdmenuhbmmwcp7zn4fdgztd6sjy
- valory/staking_abci:0.1.0:bafybeigo7bicej5t2rbki37cmcwkzgwpcnopokn7ijhylmkihsbqw47xr4
- valory/mech_interact_abci:0.1.0:bafybeih2cck5xu6yaibomwtm5zbcp6llghr3ighdnk56fzwu3ihu5xx35e
behaviours:
Expand Down

0 comments on commit 0c584db

Please sign in to comment.