Skip to content

Creating an environment dynamic and language channel

Cornelius W edited this page Jan 28, 2024 · 7 revisions

Minimal Example and Template

This minimal example showcases the basic structure of an EnvironmentDynamic class that doesn't perform any specific actions but includes all the necessary functions and variables.

import numpy as np

class EnvironmentDynamic:
    def __init__(self, environment):
        self.environment = environment
        self.observation_space = {"low": [], "high": []}
        self.action_space = {"low": [], "high": []}

    def dynamic(self, agent, actions):
        return 0, np.array([]), False, {}

Explanation:

The provided code defines an EnvironmentDynamic class that acts as a template for an environment dynamic within a system. It includes all the essential components while not implementing any specific functionality.

Here's a breakdown of the code:

  1. __init__ method: This method is executed when an instance of the class is created. It takes an environment parameter representing the specific environment or context associated with the dynamic. The method initializes the class attributes, including the environment reference, empty observation_space, action_space, and data_store dictionaries. Both observation_space and action_space dictionaries are defined with empty lists for the "low" and "high" keys, indicating the absence of predefined numerical ranges.

  2. dynamic method: This method represents the dynamic behavior of the EnvironmentDynamic class within the associated environment. However, in this minimal example, it doesn't perform any specific actions. Instead, it simply returns a tuple containing 0 (indicating success) and an empty NumPy array, np.array([]). Furthermore, the done boolean is returned simply as false and there are no additional infos created, so an empty dictionary is returned. The agent parameter represents the agent associated with the dynamic, and the actions parameter represents the actions taken by the agent.

Although this minimal example doesn't implement any functionality, it provides a foundation for building upon and defining specific behaviors and interactions within the dynamic environment.

Language Channel

Here is a quick default implementation of a language channel between two agents in the environment.

import numpy as np

class Language:
    def __init__(self, environment):
        self.environment = environment
        self.observation_space = {"low": [0, 0, 0], "high": [1, 1, 1]}
        self.action_space = {"low": [0, 0, 0], "high": [1, 1, 1]}

    def dynamic(self, agent, actions):
        if "utterance" not in self.environment.data_store[agent].keys():
            self.environment.data_store[agent]["utterance"] = 0

        utterance = [0, 0, 0]
        utterance[np.argmax(actions)] = 1
        self.environment.data_store[agent]["utterance"] = utterance

        otherAgent = [other for other in self.environment.agents if other != agent][0]

        if "utterance" in self.environment.data_store[otherAgent]:
            utteranceOtherAgent = self.environment.data_store[otherAgent]["utterance"]
            return 0, np.array(utteranceOtherAgent)
        else:
            return 0, np.array([0, 0, 0]), False, {}

The dynamic method is used to handle the language dynamics between agents. It takes two parameters: agent (representing the current agent) and actions (a list of actions taken by the agent). Here's what it does:

  • It checks if the "utterance" key is not present in the data_store of the current agent (self.environment.data_store[agent]). If it's not present, it initializes it with a default value of 0.
  • It creates a list called utterance with three zeros as communication is one-hot-encoded.
  • It sets the element at the index of the maximum value in the actions list to 1. This means it assigns a value of 1 to the element that corresponds to the action with the highest value.
  • It updates the "utterance" value in the data_store of the current agent with the newly created utterance.
  • It finds the other agent (not the current agent) in the environment by iterating over self.environment.agents and selecting the first agent that is not equal to the current agent.
  • It checks if the "utterance" key is present in the data_store of the other agent (self.environment.data_store[otherAgent]). If it is present, it retrieves the "utterance" value and returns it as a NumPy array.
  • If the "utterance" key is not present in the data_store of the other agent, it returns a NumPy array of zeros.

Note that this function only works with two agents in one environment. If there is only one agent or more than two, the environment won't work.