Example #1
0
    def training_started(self, behavior_name: str,
                         config: TrainerSettings) -> None:
        msg = TrainingBehaviorInitialized(
            behavior_name=behavior_name,
            trainer_type=config.trainer_type.value,
            extrinsic_reward_enabled=(RewardSignalType.EXTRINSIC
                                      in config.reward_signals),
            gail_reward_enabled=(RewardSignalType.GAIL
                                 in config.reward_signals),
            curiosity_reward_enabled=(RewardSignalType.CURIOSITY
                                      in config.reward_signals),
            rnd_reward_enabled=(RewardSignalType.RND in config.reward_signals),
            behavioral_cloning_enabled=config.behavioral_cloning is not None,
            recurrent_enabled=config.network_settings.memory is not None,
            visual_encoder=config.network_settings.vis_encode_type.value,
            num_network_layers=config.network_settings.num_layers,
            num_network_hidden_units=config.network_settings.hidden_units,
            trainer_threaded=config.threaded,
            self_play_enabled=config.self_play is not None,
            curriculum_enabled=self._behavior_uses_curriculum(behavior_name),
        )

        any_message = Any()
        any_message.Pack(msg)

        training_start_msg = OutgoingMessage()
        training_start_msg.set_raw_bytes(any_message.SerializeToString())

        super().queue_message_to_send(training_start_msg)
Example #2
0
 def send_raw_data(self, data: bytearray) -> None:
     """
     Queues a message to be sent by the environment at the next call to
     step.
     """
     msg = OutgoingMessage()
     msg.set_raw_bytes(data)
     super().queue_message_to_send(msg)
def test_message_string():
    val = "mlagents!"
    msg_out = OutgoingMessage()
    msg_out.write_string(val)

    msg_in = IncomingMessage(msg_out.buffer)
    read_val = msg_in.read_string()
    assert val == read_val
def test_message_int32():
    val = 1337
    msg_out = OutgoingMessage()
    msg_out.write_int32(val)

    msg_in = IncomingMessage(msg_out.buffer)
    read_val = msg_in.read_int32()
    assert val == read_val
def test_message_float_list():
    val = [1.0, 3.0, 9.0]
    msg_out = OutgoingMessage()
    msg_out.write_float32_list(val)

    msg_in = IncomingMessage(msg_out.buffer)
    read_val = msg_in.read_float32_list()
    # These won't be exactly equal in general, since python floats are 64-bit.
    assert val == read_val
def test_message_bool():
    vals = [True, False]
    msg_out = OutgoingMessage()
    for v in vals:
        msg_out.write_bool(v)

    msg_in = IncomingMessage(msg_out.buffer)
    read_vals = []
    for _ in range(len(vals)):
        read_vals.append(msg_in.read_bool())
    assert vals == read_vals
Example #7
0
def test_message_int32():
    val = 1337
    msg_out = OutgoingMessage()
    msg_out.write_int32(val)

    msg_in = IncomingMessage(msg_out.buffer)
    read_val = msg_in.read_int32()
    assert val == read_val

    # Test reading with defaults
    assert 0 == msg_in.read_int32()
    assert val == msg_in.read_int32(default_value=val)
Example #8
0
    def serialize_int_list_prop(key: str, value: List[int]) -> OutgoingMessage:
        result = bytearray()
        encoded_key = key.encode("ascii")
        result += struct.pack("<i", len(encoded_key))
        result += encoded_key
        result += struct.pack("<i", len(value))

        for v in value:
            result += struct.pack("<I", v)
        msg = OutgoingMessage()
        msg.set_raw_bytes(result)
        return msg
Example #9
0
def test_message_string():
    val = "mlagents!"
    msg_out = OutgoingMessage()
    msg_out.write_string(val)

    msg_in = IncomingMessage(msg_out.buffer)
    read_val = msg_in.read_string()
    assert val == read_val

    # Test reading with defaults
    assert "" == msg_in.read_string()
    assert val == msg_in.read_string(default_value=val)
Example #10
0
def test_message_float32():
    val = 42.0
    msg_out = OutgoingMessage()
    msg_out.write_float32(val)

    msg_in = IncomingMessage(msg_out.buffer)
    read_val = msg_in.read_float32()
    # These won't be exactly equal in general, since python floats are 64-bit.
    assert val == read_val

    # Test reading with defaults
    assert 0.0 == msg_in.read_float32()
    assert val == msg_in.read_float32(default_value=val)
Example #11
0
def test_message_bool():
    vals = [True, False]
    msg_out = OutgoingMessage()
    for v in vals:
        msg_out.write_bool(v)

    msg_in = IncomingMessage(msg_out.buffer)
    read_vals = []
    for _ in range(len(vals)):
        read_vals.append(msg_in.read_bool())
    assert vals == read_vals

    # Test reading with defaults
    assert msg_in.read_bool() is False
    assert msg_in.read_bool(default_value=True) is True
    def environment_initialized(self) -> None:
        # Tuple of (major, minor, patch)
        vi = sys.version_info

        msg = TrainingEnvironmentInitialized(
            python_version=f"{vi[0]}.{vi[1]}.{vi[2]}",
            mlagents_version="Custom",
            mlagents_envs_version=mlagents_envs.__version__,
            torch_version="Unknown",
            torch_device_type="Unknown",
        )
        any_message = Any()
        any_message.Pack(msg)

        env_init_msg = OutgoingMessage()
        env_init_msg.set_raw_bytes(
            any_message.SerializeToString())  # type: ignore
        super().queue_message_to_send(env_init_msg)
 def set_float_parameter(self, key: str, value: float) -> None:
     """
     Sets a float environment parameter in the Unity Environment.
     :param key: The string identifier of the parameter.
     :param value: The float value of the parameter.
     """
     msg = OutgoingMessage()
     msg.write_string(key)
     msg.write_int32(self.EnvironmentDataTypes.FLOAT)
     msg.write_float32(value)
     super().queue_message_to_send(msg)
Example #14
0
    def environment_initialized(self, run_options: RunOptions) -> None:
        self.run_options = run_options
        # Tuple of (major, minor, patch)
        vi = sys.version_info
        env_params = run_options.environment_parameters

        msg = TrainingEnvironmentInitialized(
            python_version=f"{vi[0]}.{vi[1]}.{vi[2]}",
            mlagents_version=mlagents.trainers.__version__,
            mlagents_envs_version=mlagents_envs.__version__,
            torch_version=torch_utils.torch.__version__,
            torch_device_type=torch_utils.default_device().type,
            num_envs=run_options.env_settings.num_envs,
            num_environment_parameters=len(env_params) if env_params else 0,
        )

        any_message = Any()
        any_message.Pack(msg)

        env_init_msg = OutgoingMessage()
        env_init_msg.set_raw_bytes(any_message.SerializeToString())
        super().queue_message_to_send(env_init_msg)
Example #15
0
def test_stats_channel():
    receiver = StatsSideChannel()
    message = OutgoingMessage()
    message.write_string("stats-1")
    message.write_float32(42.0)
    message.write_int32(1)  # corresponds to StatsAggregationMethod.MOST_RECENT

    receiver.on_message_received(IncomingMessage(message.buffer))

    stats = receiver.get_and_reset_stats()

    assert len(stats) == 1
    val, method = stats["stats-1"][0]
    assert val - 42.0 < 1e-8
    assert method == StatsAggregationMethod.MOST_RECENT
 def set_property(self, key: str, value: float) -> None:
     """
     Sets a property in the Unity Environment.
     :param key: The string identifier of the property.
     :param value: The float value of the property.
     """
     self._float_properties[key] = value
     msg = OutgoingMessage()
     msg.write_string(key)
     msg.write_float32(value)
     super().queue_message_to_send(msg)
 def set_multirangeuniform_sampler_parameters(self, key: str,
                                              intervals: List[Tuple[float,
                                                                    float]],
                                              seed: int) -> None:
     """
     Sets a multirangeuniform environment parameter sampler.
     :param key: The string identifier of the parameter.
     :param intervals: The lists of min and max that define each uniform distribution.
     :param seed: The random seed to initialize the sampler.
     """
     msg = OutgoingMessage()
     msg.write_string(key)
     msg.write_int32(self.EnvironmentDataTypes.SAMPLER)
     msg.write_int32(seed)
     msg.write_int32(self.SamplerTypes.MULTIRANGEUNIFORM)
     flattened_intervals = [
         value for interval in intervals for value in interval
     ]
     msg.write_float32_list(flattened_intervals)
     super().queue_message_to_send(msg)
 def set_gaussian_sampler_parameters(self, key: str, mean: float,
                                     st_dev: float, seed: int) -> None:
     """
     Sets a gaussian environment parameter sampler.
     :param key: The string identifier of the parameter.
     :param mean: The mean of the sampling distribution.
     :param st_dev: The standard deviation of the sampling distribution.
     :param seed: The random seed to initialize the sampler.
     """
     msg = OutgoingMessage()
     msg.write_string(key)
     msg.write_int32(self.EnvironmentDataTypes.SAMPLER)
     msg.write_int32(seed)
     msg.write_int32(self.SamplerTypes.GAUSSIAN)
     msg.write_float32(mean)
     msg.write_float32(st_dev)
     super().queue_message_to_send(msg)
 def set_uniform_sampler_parameters(self, key: str, min_value: float,
                                    max_value: float, seed: int) -> None:
     """
     Sets a uniform environment parameter sampler.
     :param key: The string identifier of the parameter.
     :param min_value: The minimum of the sampling distribution.
     :param max_value: The maximum of the sampling distribution.
     :param seed: The random seed to initialize the sampler.
     """
     msg = OutgoingMessage()
     msg.write_string(key)
     msg.write_int32(self.EnvironmentDataTypes.SAMPLER)
     msg.write_int32(seed)
     msg.write_int32(self.SamplerTypes.UNIFORM)
     msg.write_float32(min_value)
     msg.write_float32(max_value)
     super().queue_message_to_send(msg)
 def set_configuration_parameters(
     self,
     width: int = 80,
     height: int = 80,
     quality_level: int = 1,
     time_scale: float = 20.0,
     target_frame_rate: int = -1,
 ) -> None:
     """
     Sets the engine configuration. Takes as input the configurations of the
     engine.
     :param width: Defines the width of the display. Default 80.
     :param height: Defines the height of the display. Default 80.
     :param quality_level: Defines the quality level of the simulation.
     Default 1.
     :param time_scale: Defines the multiplier for the deltatime in the
     simulation. If set to a higher value, time will pass faster in the
     simulation but the physics might break. Default 20.
     :param target_frame_rate: Instructs simulation to try to render at a
     specified frame rate. Default -1.
     """
     msg = OutgoingMessage()
     msg.write_int32(width)
     msg.write_int32(height)
     msg.write_int32(quality_level)
     msg.write_float32(time_scale)
     msg.write_int32(target_frame_rate)
     super().queue_message_to_send(msg)
Example #21
0
    def set_configuration_parameters(
        self,
        width: Optional[int] = None,
        height: Optional[int] = None,
        quality_level: Optional[int] = None,
        time_scale: Optional[float] = None,
        target_frame_rate: Optional[int] = None,
        capture_frame_rate: Optional[int] = None,
    ) -> None:
        """
        Sets the engine configuration. Takes as input the configurations of the
        engine.
        :param width: Defines the width of the display. (Must be set alongside height)
        :param height: Defines the height of the display. (Must be set alongside width)
        :param quality_level: Defines the quality level of the simulation.
        :param time_scale: Defines the multiplier for the deltatime in the
        simulation. If set to a higher value, time will pass faster in the
        simulation but the physics might break.
        :param target_frame_rate: Instructs simulation to try to render at a
        specified frame rate.
        :param capture_frame_rate: Instructs the simulation to consider time between
        updates to always be constant, regardless of the actual frame rate.
        """

        if (width is None and height is not None) or (width is not None
                                                      and height is None):
            raise UnitySideChannelException(
                "You cannot set the width/height of the screen resolution without also setting the height/width"
            )

        if width is not None and height is not None:
            screen_msg = OutgoingMessage()
            screen_msg.write_int32(self.ConfigurationType.SCREEN_RESOLUTION)
            screen_msg.write_int32(width)
            screen_msg.write_int32(height)
            super().queue_message_to_send(screen_msg)

        if quality_level is not None:
            quality_level_msg = OutgoingMessage()
            quality_level_msg.write_int32(self.ConfigurationType.QUALITY_LEVEL)
            quality_level_msg.write_int32(quality_level)
            super().queue_message_to_send(quality_level_msg)

        if time_scale is not None:
            time_scale_msg = OutgoingMessage()
            time_scale_msg.write_int32(self.ConfigurationType.TIME_SCALE)
            time_scale_msg.write_float32(time_scale)
            super().queue_message_to_send(time_scale_msg)

        if target_frame_rate is not None:
            target_frame_rate_msg = OutgoingMessage()
            target_frame_rate_msg.write_int32(
                self.ConfigurationType.TARGET_FRAME_RATE)
            target_frame_rate_msg.write_int32(target_frame_rate)
            super().queue_message_to_send(target_frame_rate_msg)

        if capture_frame_rate is not None:
            capture_frame_rate_msg = OutgoingMessage()
            capture_frame_rate_msg.write_int32(
                self.ConfigurationType.CAPTURE_FRAME_RATE)
            capture_frame_rate_msg.write_int32(capture_frame_rate)
            super().queue_message_to_send(capture_frame_rate_msg)
Example #22
0
 def send_int(self, value):
     msg = OutgoingMessage()
     msg.write_int32(value)
     super().queue_message_to_send(msg)