|
|
|
|
|
|
|
|
|
|
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; |
|
|
|
} |