using System; using System.Collections.Generic; using Unity.MLAgents; using Unity.MLAgents.Inference.Utils; using UnityEngine; using Random=UnityEngine.Random; namespace Unity.MLAgents { /// /// The types of distributions from which to sample reset parameters. /// public enum SamplerType { /// /// Samples a reset parameter from a uniform distribution. /// Uniform = 0, /// /// Samples a reset parameter from a Gaussian distribution. /// Gaussian = 1 } /// /// Takes a list of floats that encode a sampling distribution and returns the sampling function. /// public sealed class SamplerFactory { int m_Seed; /// /// Constructor. /// internal SamplerFactory(int seed) { m_Seed = seed; } /// /// Create the sampling distribution described by the encoding. /// /// List of floats the describe sampling destribution. public Func CreateSampler(IList encoding) { if ((int)encoding[0] == (int)SamplerType.Uniform) { return CreateUniformSampler(encoding[1], encoding[2]); } else if ((int)encoding[0] == (int)SamplerType.Gaussian) { return CreateGaussianSampler(encoding[1], encoding[2]); } else{ Debug.LogWarning("EnvironmentParametersChannel received an unknown data type."); return () => 0; } } public Func CreateUniformSampler(float min, float max) { return () => Random.Range(min, max); } public Func CreateGaussianSampler(float mean, float stddev) { RandomNormal distr = new RandomNormal(m_Seed, mean, stddev); return () => (float)distr.NextDouble(); } } }