浏览代码

Rename to ObservationType

/MLA-1734-demo-provider
Arthur Juliani 4 年前
当前提交
e4b8e7e2
共有 10 个文件被更改,包括 47 次插入47 次删除
  1. 4
      com.unity.ml-agents/Runtime/Communicator/GrpcExtensions.cs
  2. 12
      com.unity.ml-agents/Runtime/Sensors/ITypedSensor.cs
  3. 8
      docs/Python-API.md
  4. 16
      ml-agents-envs/mlagents_envs/base_env.py
  5. 8
      ml-agents-envs/mlagents_envs/rpc_utils.py
  6. 14
      ml-agents-envs/mlagents_envs/tests/test_envs.py
  7. 18
      ml-agents-envs/mlagents_envs/tests/test_rpc_utils.py
  8. 6
      ml-agents-envs/mlagents_envs/tests/test_steps.py
  9. 2
      ml-agents/mlagents/trainers/tests/dummy_config.py
  10. 6
      protobuf-definitions/proto/mlagents_envs/communicator_objects/observation.proto

4
com.unity.ml-agents/Runtime/Communicator/GrpcExtensions.cs


var typeSensor = sensor as ITypedSensor;
if (typeSensor != null)
{
observationProto.SensorType = (SensorTypeProto)typeSensor.GetSensorType();
observationProto.ObservationType = (ObservationTypeProto)typeSensor.GetObservationType();
observationProto.SensorType = SensorTypeProto.Observation;
observationProto.ObservationType = ObservationTypeProto.Default;
}
return observationProto;
}

12
com.unity.ml-agents/Runtime/Sensors/ITypedSensor.cs


{
/// <summary>
/// The SensorType enum of the observation
/// The ObservationType enum of the Sensor.
internal enum SensorType
internal enum ObservationType
Observation = 0,
Default = 0,
Goal = 1,
Reward = 2,
Message = 3,

internal interface ITypedSensor
{
/// <summary>
/// Returns the SensorType enum corresponding to the type of the sensor.
/// Returns the ObservationType enum corresponding to the type of the sensor.
/// <returns>The SensorType enum</returns>
SensorType GetSensorType();
/// <returns>The ObservationType enum</returns>
ObservationType GetObservationType();
}
}

8
docs/Python-API.md


A `BehaviorSpec` has the following fields :
- `sensor_specs` is a List of `SensorSpec` objects : Each `SensorSpec`
- `observation_specs` is a List of `ObservationSpec` objects : Each `ObservationSpec`
data should be processed in the corresponding dimension. `type` is an enum
corresponding to what type of sensor is generating the data (i.e., observation, goal,
etc). Note that the `SensorSpec` have the same ordering as the ordering of observations
data should be processed in the corresponding dimension. `observation_type` is an enum
corresponding to what type of observation is generating the data (i.e., default, goal,
etc). Note that the `ObservationSpec` have the same ordering as the ordering of observations
in the DecisionSteps, DecisionStep, TerminalSteps and TerminalStep.
- `action_spec` is an `ActionSpec` namedtuple that defines the number and types
of actions for the Agent.

16
ml-agents-envs/mlagents_envs/base_env.py


:param spec: The BehaviorSpec for the DecisionSteps
"""
obs: List[np.ndarray] = []
for sen_spec in spec.sensor_specs:
for sen_spec in spec.observation_specs:
obs += [np.zeros((0,) + sen_spec.shape, dtype=np.float32)]
return DecisionSteps(
obs=obs,

:param spec: The BehaviorSpec for the TerminalSteps
"""
obs: List[np.ndarray] = []
for sen_spec in spec.sensor_specs:
for sen_spec in spec.observation_specs:
obs += [np.zeros((0,) + sen_spec.shape, dtype=np.float32)]
return TerminalSteps(
obs=obs,

VARIABLE_SIZE = 4
class SensorType(Enum):
OBSERVATION = 0
class ObservationType(Enum):
DEFAULT = 0
class SensorSpec(NamedTuple):
class ObservationSpec(NamedTuple):
"""
A NamedTuple containing information about the observation of Agents.
- shape is a Tuple of int : It corresponds to the shape of

- type is an enum of SensorType.
- observation_type is an enum of ObservationType.
type: SensorType
observation_type: ObservationType
class BehaviorSpec(NamedTuple):

- action_spec is an ActionSpec NamedTuple.
"""
sensor_specs: List[SensorSpec]
observation_specs: List[ObservationSpec]
action_spec: ActionSpec

8
ml-agents-envs/mlagents_envs/rpc_utils.py


from mlagents_envs.base_env import (
ActionSpec,
SensorSpec,
ObservationSpec,
SensorType,
ObservationType,
)
from mlagents_envs.exception import UnityObservationException
from mlagents_envs.timers import hierarchical_timer, timed

sensor_specs = []
for obs in agent_info.observations:
sensor_specs.append(
SensorSpec(
ObservationSpec(
tuple(obs.shape),
tuple(DimensionProperty(dim) for dim in obs.dimension_properties),
SensorType(obs.sensor_type),

]
decision_obs_list: List[np.ndarray] = []
terminal_obs_list: List[np.ndarray] = []
for obs_index, sensor_specs in enumerate(behavior_spec.sensor_specs):
for obs_index, sensor_specs in enumerate(behavior_spec.observation_specs):
is_visual = len(sensor_specs.shape) == 3
if is_visual:
obs_shape = cast(Tuple[int, int, int], sensor_specs.shape)

14
ml-agents-envs/mlagents_envs/tests/test_envs.py


env.close()
assert isinstance(decision_steps, DecisionSteps)
assert isinstance(terminal_steps, TerminalSteps)
assert len(spec.sensor_specs) == len(decision_steps.obs)
assert len(spec.sensor_specs) == len(terminal_steps.obs)
assert len(spec.observation_specs) == len(decision_steps.obs)
assert len(spec.observation_specs) == len(terminal_steps.obs)
for sen_spec, obs in zip(spec.sensor_specs, decision_steps.obs):
for sen_spec, obs in zip(spec.observation_specs, decision_steps.obs):
for sen_spec, obs in zip(spec.sensor_specs, terminal_steps.obs):
for sen_spec, obs in zip(spec.observation_specs, terminal_steps.obs):
assert (n_agents,) + sen_spec.shape == obs.shape

env.close()
assert isinstance(decision_steps, DecisionSteps)
assert isinstance(terminal_steps, TerminalSteps)
assert len(spec.sensor_specs) == len(decision_steps.obs)
assert len(spec.sensor_specs) == len(terminal_steps.obs)
for spec, obs in zip(spec.sensor_specs, decision_steps.obs):
assert len(spec.observation_specs) == len(decision_steps.obs)
assert len(spec.observation_specs) == len(terminal_steps.obs)
for spec, obs in zip(spec.observation_specs, decision_steps.obs):
assert (n_agents,) + spec.shape == obs.shape
assert 0 in decision_steps
assert 2 in terminal_steps

18
ml-agents-envs/mlagents_envs/tests/test_rpc_utils.py


steps_from_proto,
)
from PIL import Image
from mlagents.trainers.tests.dummy_config import create_sensor_specs_with_shapes
from mlagents.trainers.tests.dummy_config import create_observation_specs_with_shapes
def generate_list_agent_proto(

n_agents = 10
shapes = [(3,), (4,)]
spec = BehaviorSpec(
create_sensor_specs_with_shapes(shapes), ActionSpec.create_continuous(3)
create_observation_specs_with_shapes(shapes), ActionSpec.create_continuous(3)
)
ap_list = generate_list_agent_proto(n_agents, shapes)
decision_steps, terminal_steps = steps_from_proto(ap_list, spec)

n_agents = 10
shapes = [(3,), (4,)]
behavior_spec = BehaviorSpec(
create_sensor_specs_with_shapes(shapes), ActionSpec.create_discrete((7, 3))
create_observation_specs_with_shapes(shapes), ActionSpec.create_discrete((7, 3))
)
ap_list = generate_list_agent_proto(n_agents, shapes)
decision_steps, terminal_steps = steps_from_proto(ap_list, behavior_spec)

n_agents = 10
shapes = [(3,), (4,)]
behavior_spec = BehaviorSpec(
create_sensor_specs_with_shapes(shapes), ActionSpec.create_discrete((10,))
create_observation_specs_with_shapes(shapes), ActionSpec.create_discrete((10,))
)
ap_list = generate_list_agent_proto(n_agents, shapes)
decision_steps, terminal_steps = steps_from_proto(ap_list, behavior_spec)

n_agents = 10
shapes = [(3,), (4,)]
behavior_spec = BehaviorSpec(
create_sensor_specs_with_shapes(shapes), ActionSpec.create_discrete((2, 2, 6))
create_observation_specs_with_shapes(shapes), ActionSpec.create_discrete((2, 2, 6))
)
ap_list = generate_list_agent_proto(n_agents, shapes)
decision_steps, terminal_steps = steps_from_proto(ap_list, behavior_spec)

n_agents = 10
shapes = [(3,), (4,)]
behavior_spec = BehaviorSpec(
create_sensor_specs_with_shapes(shapes), ActionSpec.create_continuous(10)
create_observation_specs_with_shapes(shapes), ActionSpec.create_continuous(10)
)
ap_list = generate_list_agent_proto(n_agents, shapes)
decision_steps, terminal_steps = steps_from_proto(ap_list, behavior_spec)

behavior_spec = behavior_spec_from_proto(bp, agent_proto)
assert behavior_spec.action_spec.is_discrete()
assert not behavior_spec.action_spec.is_continuous()
assert [spec.shape for spec in behavior_spec.sensor_specs] == [(3,), (4,)]
assert [spec.shape for spec in behavior_spec.observation_specs] == [(3,), (4,)]
assert behavior_spec.action_spec.discrete_branches == (5, 4)
assert behavior_spec.action_spec.discrete_size == 2
bp = BrainParametersProto()

n_agents = 10
shapes = [(3,), (4,)]
behavior_spec = BehaviorSpec(
create_sensor_specs_with_shapes(shapes), ActionSpec.create_continuous(3)
create_observation_specs_with_shapes(shapes), ActionSpec.create_continuous(3)
)
ap_list = generate_list_agent_proto(n_agents, shapes, infinite_rewards=True)
with pytest.raises(RuntimeError):

n_agents = 10
shapes = [(3,), (4,)]
behavior_spec = BehaviorSpec(
create_sensor_specs_with_shapes(shapes), ActionSpec.create_continuous(3)
create_observation_specs_with_shapes(shapes), ActionSpec.create_continuous(3)
)
ap_list = generate_list_agent_proto(n_agents, shapes, nan_observations=True)
with pytest.raises(RuntimeError):

6
ml-agents-envs/mlagents_envs/tests/test_steps.py


ActionSpec,
BehaviorSpec,
)
from mlagents.trainers.tests.dummy_config import create_sensor_specs_with_shapes
from mlagents.trainers.tests.dummy_config import create_observation_specs_with_shapes
def test_decision_steps():

def test_empty_decision_steps():
specs = BehaviorSpec(
sensor_specs=create_sensor_specs_with_shapes([(3, 2), (5,)]),
sensor_specs=create_observation_specs_with_shapes([(3, 2), (5,)]),
action_spec=ActionSpec.create_continuous(3),
)
ds = DecisionSteps.empty(specs)

def test_empty_terminal_steps():
specs = BehaviorSpec(
sensor_specs=create_sensor_specs_with_shapes([(3, 2), (5,)]),
sensor_specs=create_observation_specs_with_shapes([(3, 2), (5,)]),
action_spec=ActionSpec.create_continuous(3),
)
ts = TerminalSteps.empty(specs)

2
ml-agents/mlagents/trainers/tests/dummy_config.py


return {RewardSignalType.EXTRINSIC: RewardSignalSettings()}
def create_sensor_specs_with_shapes(shapes: List[Tuple[int, ...]]) -> List[SensorSpec]:
def create_observation_specs_with_shapes(shapes: List[Tuple[int, ...]]) -> List[SensorSpec]:
sen_spec: List[SensorSpec] = []
for shape in shapes:
dim_prop = (DimensionProperty.UNSPECIFIED,) * len(shape)

6
protobuf-definitions/proto/mlagents_envs/communicator_objects/observation.proto


PNG = 1;
}
enum SensorTypeProto {
OBSERVATION = 0;
enum ObservationTypeProto {
DEFAULT = 0;
GOAL = 1;
REWARD = 2;
MESSAGE = 3;

}
repeated int32 compressed_channel_mapping = 5;
repeated int32 dimension_properties = 6;
SensorTypeProto sensor_type = 7;
ObservationTypeProto observation_type = 7;
}
正在加载...
取消
保存