How to pass Agent Observations from Unity to Python
Updated: Sep 15, 2021
FloatPropertiesChannel of MLagents (0.14.1)
In our example, agents observations can be anything the agent perceives in its environment through vision, touch, audio or proprioception. In this particular case, we are trying to send observations in the form of float from Unity to Python. This is just a trial scenario. The actual observations will be vectors.
Float properties of the instance are accessed via FloatPropertiesChannel, part of the plugin Python API. This side channel consists of a collection of float properties which can be used to transfer data that is designated as a float property in the Unity environment.
The FloatPropertiesChannel, among others, supports two methods : setproperty() and getproperty(). The setproperty method is used to set the value of the property. The getproperty method is used to access the value of the property with Python.
In Unity – C # script attached to the agent
# setting variable to be of the type floatproperty
var variable_name = Academy.Instance.FloatProperties; variable_name.SetProperty("property_name", (float)property_to_be_passed);
import mlagents_envs from mlagents_envs.environment import UnityEnvironment from mlagents_envs.side_channel.float_properties_channel import FloatPropertiesChannel
# creating a float properties channel
channel_name = FloatPropertiesChannel()
# assigning the Unity environment executable to a variable
env_name = UnityEnvironment(base_port = port_number, file_name = "env_executable_name, " side_channels = [channel_name])
# attribute value assigned to var_name variable
done = False while not done : env.step() var_name = channel_name.get_property("property_name")