浏览代码

from set_sampler_params => set_{samplertype}_params

/sampler-refactor-copy
Andrew Cohen 5 年前
当前提交
953f4e09
共有 5 个文件被更改,包括 123 次插入102 次删除
  1. 68
      com.unity.ml-agents/Runtime/Sampler.cs
  2. 48
      com.unity.ml-agents/Runtime/SideChannels/EnvironmentParametersChannel.cs
  3. 57
      ml-agents-envs/mlagents_envs/side_channel/environment_parameters_channel.py
  4. 34
      ml-agents/mlagents/trainers/settings.py
  5. 18
      ml-agents/mlagents/trainers/subprocess_env_manager.py

68
com.unity.ml-agents/Runtime/Sampler.cs


namespace Unity.MLAgents
{
/// <summary>
/// The types of distributions from which to sample reset parameters.
/// </summary>
internal enum SamplerType
{
/// <summary>
/// Samples a reset parameter from a uniform distribution.
/// </summary>
Uniform = 0,
/// <summary>
/// Samples a reset parameter from a Gaussian distribution.
/// </summary>
Gaussian = 1,
/// <summary>
/// Samples a reset parameter from a Gaussian distribution.
/// </summary>
MultiRangeUniform = 2
}
/// <summary>
/// Takes a list of floats that encode a sampling distribution and returns the sampling function.

{
}
/// <summary>
/// Create the sampling distribution described by the encoding.
/// </summary>
/// <param name="encoding"> List of floats the describe sampling destribution.</param>
public Func<float> CreateSampler(IList<float> encoding, int seed)
{
if ((int)encoding[0] == (int)SamplerType.Uniform)
{
return CreateUniformSampler(encoding[1], encoding[2], seed);
}
else if ((int)encoding[0] == (int)SamplerType.Gaussian)
{
return CreateGaussianSampler(encoding[1], encoding[2], seed);
}
else if ((int)encoding[0] == (int)SamplerType.MultiRangeUniform)
{
return CreateMultiRangeUniformSampler(encoding, seed);
}
else{
Debug.LogWarning("EnvironmentParametersChannel received an unknown data type.");
return () => 0;
}
}
internal Func<float> CreateUniformSampler(float min, float max, int seed)
public Func<float> CreateUniformSampler(float min, float max, int seed)
internal Func<float> CreateGaussianSampler(float mean, float stddev, int seed)
public Func<float> CreateGaussianSampler(float mean, float stddev, int seed)
internal Func<float> CreateMultiRangeUniformSampler(IList<float> encoding, int seed)
public Func<float> CreateMultiRangeUniformSampler(IList<float> intervals, int seed)
// Skip type of distribution since already checked to get into this function
var samplerEncoding = encoding.Skip(1);
// Will be used to normalize intervals
// Will be used to normalize intervalFuncs
int numIntervals = (int)(samplerEncoding.Count()/2);
int numIntervals = (int)(intervals.Count()/2);
IList<Func<float>> intervals = new Func<float>[numIntervals];
IList<Func<float>> intervalFuncs = new Func<float>[numIntervals];
var min = samplerEncoding.ElementAt(2 * i);
var max = samplerEncoding.ElementAt(2 * i + 1);
var min = intervals.ElementAt(2 * i);
var max = intervals.ElementAt(2 * i + 1);
intervals[i] = () => min + (float)distr.NextDouble() * intervalSize;
intervalFuncs[i] = () => min + (float)distr.NextDouble() * intervalSize;
}
// Normalize interval lengths
for(int i = 0; i < numIntervals; i++)

float MultiRange()
{
int sampledInterval = intervalDistr.Sample(intervalSizes);
return intervals[sampledInterval].Invoke();
return intervalFuncs[sampledInterval].Invoke();
}
return MultiRange;
}

48
com.unity.ml-agents/Runtime/SideChannels/EnvironmentParametersChannel.cs


}
/// <summary>
/// The types of distributions from which to sample reset parameters.
/// </summary>
internal enum SamplerType
{
/// <summary>
/// Samples a reset parameter from a uniform distribution.
/// </summary>
Uniform = 0,
/// <summary>
/// Samples a reset parameter from a Gaussian distribution.
/// </summary>
Gaussian = 1,
/// <summary>
/// Samples a reset parameter from a Gaussian distribution.
/// </summary>
MultiRangeUniform = 2
}
/// <summary>
/// A side channel that manages the environment parameter values from Python. Currently
/// limited to parameters of type float.
/// </summary>

else if ((int)EnvironmentDataTypes.Sampler == type)
{
int seed = msg.ReadInt32();
var encoding = msg.ReadFloatList();
m_Parameters[key] = m_SamplerFactory.CreateSampler(encoding, seed);
int samplerType = msg.ReadInt32();
Func<float> sampler = () => 0.0f;
if ((int)SamplerType.Uniform == samplerType)
{
float min = msg.ReadFloat32();
float max = msg.ReadFloat32();
sampler = m_SamplerFactory.CreateUniformSampler(min, max, seed);
}
else if ((int)SamplerType.Gaussian == samplerType)
{
float mean = msg.ReadFloat32();
float stddev = msg.ReadFloat32();
sampler = m_SamplerFactory.CreateGaussianSampler(mean, stddev, seed);
}
else if ((int)SamplerType.MultiRangeUniform == samplerType)
{
IList<float> intervals = msg.ReadFloatList();
sampler = m_SamplerFactory.CreateMultiRangeUniformSampler(intervals, seed);
}
else{
Debug.LogWarning("EnvironmentParametersChannel received an unknown data type.");
}
m_Parameters[key] = sampler;
}
else
{

57
ml-agents-envs/mlagents_envs/side_channel/environment_parameters_channel.py


FLOAT = 0
SAMPLER = 1
class SamplerTypes(IntEnum):
UNIFORM = 0
GAUSSIAN = 1
MULTIRANGEUNIFORM = 2
def __init__(self) -> None:
channel_id = uuid.UUID(("534c891e-810f-11ea-a9d0-822485860400"))
super().__init__(channel_id)

msg.write_float32(value)
super().queue_message_to_send(msg)
def set_sampler_parameters(
self, key: str, encoding: List[float], seed: int
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_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_multirangeuniform_sampler_parameters(
self, key: str, intervals: List[float], seed: int
Sets a float encoding of an environment parameter sampler.
Sets a gaussian environment parameter sampler.
:param encoding: The float encoding of the sampler.
:param intervals: The min and max that define each uniform distribution.
:param seed: The random seed to initialize the sampler.
"""
msg = OutgoingMessage()

# for read float list in C#
msg.write_int32(len(encoding))
for value in encoding:
msg.write_int32(self.SamplerTypes.MULTIRANGEUNIFORM)
msg.write_int32(len(intervals))
for value in intervals:
msg.write_float32(value)
super().queue_message_to_send(msg)

34
ml-agents/mlagents/trainers/settings.py


from enum import Enum
import collections
import argparse
import abc
from mlagents.trainers.cli_utils import StoreConfigFile, DetectDefault, parser
from mlagents.trainers.cli_utils import load_config

}
return _mapping[self]
@staticmethod
def to_float(t: type) -> float:
_mapping: Dict[type, float] = {
UniformSettings: 0.0,
GaussianSettings: 1.0,
MultiRangeUniformSettings: 2.0,
}
return _mapping[t]
class ParameterRandomizationSettings(abc.ABC):
class ParameterRandomizationSettings:
seed: int = parser.get_default("seed")
@staticmethod

d_final[param] = strict_to_cls(val, t)
return d_final
@abc.abstractmethod
def to_float_encoding(self) -> List[float]:
"Returns the float encoding of the sampler"
pass
@attr.s(auto_attribs=True)
class UniformSettings(ParameterRandomizationSettings):

"Minimum value is greater than maximum value in uniform sampler."
)
def to_float_encoding(self) -> List[float]:
"Returns the sampler type followed by the min and max values"
return [
ParameterRandomizationType.to_float(type(self)),
self.min_value,
self.max_value,
]
def to_float_encoding(self) -> List[float]:
"Returns the sampler type followed by the mean and standard deviation"
return [ParameterRandomizationType.to_float(type(self)), self.mean, self.st_dev]
@attr.s(auto_attribs=True)

def to_float_encoding(self) -> List[float]:
"Returns the sampler type followed by a flattened list of the interval values"
floats: List[float] = []
for interval in self.intervals:
floats += interval
return [ParameterRandomizationType.to_float(type(self))] + floats
return [value for interval in self.intervals for value in interval]
@attr.s(auto_attribs=True)

18
ml-agents/mlagents/trainers/subprocess_env_manager.py


get_timer_root,
)
from mlagents.trainers.brain import BrainParameters
from mlagents.trainers.settings import ParameterRandomizationSettings
from mlagents.trainers.settings import (
UniformSettings,
GaussianSettings,
MultiRangeUniformSettings,
)
from mlagents.trainers.action_info import ActionInfo
from mlagents_envs.side_channel.environment_parameters_channel import (
EnvironmentParametersChannel,

for k, v in req.payload.items():
if isinstance(v, float):
env_parameters.set_float_parameter(k, v)
elif isinstance(v, ParameterRandomizationSettings):
env_parameters.set_sampler_parameters(
elif isinstance(v, UniformSettings):
env_parameters.set_uniform_sampler_parameters(
k, v.min_value, v.max_value, v.seed
)
elif isinstance(v, GaussianSettings):
env_parameters.set_gaussian_sampler_parameters(
k, v.mean, v.st_dev, v.seed
)
elif isinstance(v, MultiRangeUniformSettings):
env_parameters.set_multirangeuniform_sampler_parameters(
k, v.to_float_encoding(), v.seed
)
env.reset()

正在加载...
取消
保存