Exemple #1
0
        /// <summary>
        /// Performs a single environment update to the Academy, and Agent
        /// objects within the environment.
        /// </summary>
        public void EnvironmentStep()
        {
            if (!m_HadFirstReset)
            {
                ForcedFullReset();
            }

            AgentPreStep?.Invoke(m_StepCount);

            m_StepCount      += 1;
            m_TotalStepCount += 1;
            AgentIncrementStep?.Invoke();

            using (TimerStack.Instance.Scoped("AgentSendState"))
            {
                AgentSendState?.Invoke();
            }

            using (TimerStack.Instance.Scoped("DecideAction"))
            {
                DecideAction?.Invoke();
            }

            // If the communicator is not on, we need to clear the SideChannel sending queue
            if (!IsCommunicatorOn)
            {
                SideChannelUtils.GetSideChannelMessage();
            }

            using (TimerStack.Instance.Scoped("AgentAct"))
            {
                AgentAct?.Invoke();
            }
        }
Exemple #2
0
    public override void Initialize()
    {
        m_JdController = GetComponent <JointDriveController>();
        m_JdController.SetupBodyPart(hips);
        m_JdController.SetupBodyPart(chest);
        m_JdController.SetupBodyPart(spine);
        m_JdController.SetupBodyPart(head);
        m_JdController.SetupBodyPart(thighL);
        m_JdController.SetupBodyPart(shinL);
        m_JdController.SetupBodyPart(footL);
        m_JdController.SetupBodyPart(thighR);
        m_JdController.SetupBodyPart(shinR);
        m_JdController.SetupBodyPart(footR);
        m_JdController.SetupBodyPart(armL);
        m_JdController.SetupBodyPart(forearmL);
        m_JdController.SetupBodyPart(handL);
        m_JdController.SetupBodyPart(armR);
        m_JdController.SetupBodyPart(forearmR);
        m_JdController.SetupBodyPart(handR);

        m_HipsRb  = hips.GetComponent <Rigidbody>();
        m_ChestRb = chest.GetComponent <Rigidbody>();
        m_SpineRb = spine.GetComponent <Rigidbody>();

        m_ResetParams = SideChannelUtils.GetSideChannel <FloatPropertiesChannel>();

        SetResetParameters();
    }
        public void TestRawBytesSideChannel()
        {
            var str1 = "Test string";
            var str2 = "Test string, second";

            var strSender   = new RawBytesChannel(new Guid("9a5b8954-4f82-11ea-b238-784f4387d1f7"));
            var strReceiver = new RawBytesChannel(new Guid("9a5b8954-4f82-11ea-b238-784f4387d1f7"));
            var dictSender  = new Dictionary <Guid, SideChannel> {
                { strSender.ChannelId, strSender }
            };
            var dictReceiver = new Dictionary <Guid, SideChannel> {
                { strReceiver.ChannelId, strReceiver }
            };

            strSender.SendRawBytes(Encoding.ASCII.GetBytes(str1));
            strSender.SendRawBytes(Encoding.ASCII.GetBytes(str2));

            byte[] fakeData = SideChannelUtils.GetSideChannelMessage(dictSender);
            SideChannelUtils.ProcessSideChannelData(dictReceiver, fakeData);

            var messages = strReceiver.GetAndClearReceivedMessages();

            Assert.AreEqual(messages.Count, 2);
            Assert.AreEqual(Encoding.ASCII.GetString(messages[0]), str1);
            Assert.AreEqual(Encoding.ASCII.GetString(messages[1]), str2);
        }
Exemple #4
0
    public override void CollectDiscreteActionMasks(DiscreteActionMasker actionMasker)
    {
        // Mask the necessary actions if selected by the user.
        if (maskActions)
        {
            // Prevents the agent from picking an action that would make it collide with a wall
            var positionX   = (int)transform.position.x;
            var positionZ   = (int)transform.position.z;
            var maxPosition = (int)SideChannelUtils.GetSideChannel <FloatPropertiesChannel>().GetPropertyWithDefault("gridSize", 5f) - 1;

            if (positionX == 0)
            {
                actionMasker.SetMask(0, new int[] { k_Left });
            }

            if (positionX == maxPosition)
            {
                actionMasker.SetMask(0, new int[] { k_Right });
            }

            if (positionZ == 0)
            {
                actionMasker.SetMask(0, new int[] { k_Down });
            }

            if (positionZ == maxPosition)
            {
                actionMasker.SetMask(0, new int[] { k_Up });
            }
        }
    }
Exemple #5
0
        /// <summary>
        /// Shut down the Academy.
        /// </summary>
        public void Dispose()
        {
            DisableAutomaticStepping();

            // Signal to listeners that the academy is being destroyed now
            DestroyAction?.Invoke();

            Communicator?.Dispose();
            Communicator = null;
            SideChannelUtils.UnregisterAllSideChannels();

            if (m_ModelRunners != null)
            {
                foreach (var mr in m_ModelRunners)
                {
                    mr.Dispose();
                }

                m_ModelRunners = null;
            }

            // Clear out the actions so we're not keeping references to any old objects
            ResetActions();

            // TODO - Pass worker ID or some other identifier,
            // so that multiple envs won't overwrite each others stats.
            TimerStack.Instance.SaveJsonTimers();
            m_Initialized = false;

            // Reset the Lazy instance
            s_Lazy = new Lazy <Academy>(() => new Academy());
        }
Exemple #6
0
 // スタート時に呼ばれる
 public override void Initialize()
 {
     SettingRewards = new SettingReward();
     curic          = 0; //ぶつからないだけでRewardのパターン
     curriculum     = SideChannelUtils.GetSideChannel <FloatPropertiesChannel>();
     current_speed  = 0.3f;
 }
Exemple #7
0
        /// <summary>
        /// Initializes the environment, configures it and initialized the Academy.
        /// </summary>
        void InitializeEnvironment()
        {
            EnableAutomaticStepping();

            SideChannelUtils.RegisterSideChannel(new EngineConfigurationChannel());
            SideChannelUtils.RegisterSideChannel(new FloatPropertiesChannel());
            SideChannelUtils.RegisterSideChannel(new StatsSideChannel());

            // Try to launch the communicator by using the arguments passed at launch
            var port = ReadPortFromArgs();

            if (port > 0)
            {
                Communicator = new RpcCommunicator(
                    new CommunicatorInitParameters
                {
                    port = port
                }
                    );
            }

            if (Communicator != null)
            {
                // We try to exchange the first message with Python. If this fails, it means
                // no Python Process is ready to train the environment. In this case, the
                //environment must use Inference.
                try
                {
                    var unityRlInitParameters = Communicator.Initialize(
                        new CommunicatorInitParameters
                    {
                        unityCommunicationVersion = k_ApiVersion,
                        unityPackageVersion       = k_PackageVersion,
                        name = "AcademySingleton",
                    });
                    UnityEngine.Random.InitState(unityRlInitParameters.seed);
                }
                catch
                {
                    Debug.Log($"" +
                              $"Couldn't connect to trainer on port {port} using API version {k_ApiVersion}. " +
                              "Will perform inference instead."
                              );
                    Communicator = null;
                }

                if (Communicator != null)
                {
                    Communicator.QuitCommandReceived  += OnQuitCommandReceived;
                    Communicator.ResetCommandReceived += OnResetCommand;
                }
            }

            // If a communicator is enabled/provided, then we assume we are in
            // training mode. In the absence of a communicator, we assume we are
            // in inference mode.

            ResetActions();
        }
 public void Awake()
 {
     SideChannelUtils.GetSideChannel <FloatPropertiesChannel>().RegisterCallback("gridSize", f =>
     {
         MainCamera.transform.position = new Vector3(-(f - 1) / 2f, f * 1.25f, -(f - 1) / 2f);
         MainCamera.orthographicSize   = (f + 5f) / 2f;
     });
 }
    public void SetGroundMaterialFriction()
    {
        var resetParams = SideChannelUtils.GetSideChannel <FloatPropertiesChannel>();

        var groundCollider = ground.GetComponent <Collider>();

        groundCollider.material.dynamicFriction = resetParams.GetPropertyWithDefault("dynamic_friction", 0);
        groundCollider.material.staticFriction  = resetParams.GetPropertyWithDefault("static_friction", 0);
    }
Exemple #10
0
    public override void Initialize()
    {
        m_Rb      = gameObject.GetComponent <Rigidbody>();
        m_LookDir = Vector3.zero;

        m_ResetParams = SideChannelUtils.GetSideChannel <FloatPropertiesChannel>();

        SetResetParameters();
    }
Exemple #11
0
    public void SetResetParameters()
    {
        var fp = SideChannelUtils.GetSideChannel <FloatPropertiesChannel>();

        m_GoalSize      = fp.GetPropertyWithDefault("goal_size", 5);
        m_GoalSpeed     = Random.Range(-1f, 1f) * fp.GetPropertyWithDefault("goal_speed", 1);
        m_Deviation     = fp.GetPropertyWithDefault("deviation", 0);
        m_DeviationFreq = fp.GetPropertyWithDefault("deviation_freq", 0);
    }
    public void ResetBall()
    {
        ball.transform.position = ballStartingPos;
        ballRb.velocity         = Vector3.zero;
        ballRb.angularVelocity  = Vector3.zero;

        var ballScale = SideChannelUtils.GetSideChannel <FloatPropertiesChannel>().GetPropertyWithDefault("ball_scale", 0.015f);

        ballRb.transform.localScale = new Vector3(ballScale, ballScale, ballScale);
    }
    public void SetBlockProperties()
    {
        var resetParams = SideChannelUtils.GetSideChannel <FloatPropertiesChannel>();

        var scale = resetParams.GetPropertyWithDefault("block_scale", 2);

        //Set the scale of the block
        m_BlockRb.transform.localScale = new Vector3(scale, 0.75f, scale);

        // Set the drag of the block
        m_BlockRb.drag = resetParams.GetPropertyWithDefault("block_drag", 0.5f);
    }
Exemple #14
0
        public void TestAcademyDispose()
        {
            var floatProperties1 = SideChannelUtils.GetSideChannel <FloatPropertiesChannel>();

            Academy.Instance.Dispose();

            Academy.Instance.LazyInitialize();
            var floatProperties2 = SideChannelUtils.GetSideChannel <FloatPropertiesChannel>();

            Academy.Instance.Dispose();

            Assert.AreNotEqual(floatProperties1, floatProperties2);
        }
Exemple #15
0
    public override void Initialize()
    {
        m_WallJumpSettings = FindObjectOfType <WallJumpSettings>();
        m_Configuration    = Random.Range(0, 5);

        m_AgentRb         = GetComponent <Rigidbody>();
        m_ShortBlockRb    = shortBlock.GetComponent <Rigidbody>();
        m_SpawnAreaBounds = spawnArea.GetComponent <Collider>().bounds;
        m_GroundRenderer  = ground.GetComponent <Renderer>();
        m_GroundMaterial  = m_GroundRenderer.material;

        spawnArea.SetActive(false);

        m_FloatProperties = SideChannelUtils.GetSideChannel <FloatPropertiesChannel>();
    }
        public void TestFloatPropertiesSideChannel()
        {
            var k1        = "gravity";
            var k2        = "length";
            int wasCalled = 0;

            var propA        = new FloatPropertiesChannel();
            var propB        = new FloatPropertiesChannel();
            var dictReceiver = new Dictionary <Guid, SideChannel> {
                { propA.ChannelId, propA }
            };
            var dictSender = new Dictionary <Guid, SideChannel> {
                { propB.ChannelId, propB }
            };

            propA.RegisterCallback(k1, f => { wasCalled++; });
            var tmp = propB.GetPropertyWithDefault(k2, 3.0f);

            Assert.AreEqual(tmp, 3.0f);
            propB.SetProperty(k2, 1.0f);
            tmp = propB.GetPropertyWithDefault(k2, 3.0f);
            Assert.AreEqual(tmp, 1.0f);

            byte[] fakeData = SideChannelUtils.GetSideChannelMessage(dictSender);
            SideChannelUtils.ProcessSideChannelData(dictReceiver, fakeData);

            tmp = propA.GetPropertyWithDefault(k2, 3.0f);
            Assert.AreEqual(tmp, 1.0f);

            Assert.AreEqual(wasCalled, 0);
            propB.SetProperty(k1, 1.0f);
            Assert.AreEqual(wasCalled, 0);
            fakeData = SideChannelUtils.GetSideChannelMessage(dictSender);
            SideChannelUtils.ProcessSideChannelData(dictReceiver, fakeData);
            Assert.AreEqual(wasCalled, 1);

            var keysA = propA.ListProperties();

            Assert.AreEqual(2, keysA.Count);
            Assert.IsTrue(keysA.Contains(k1));
            Assert.IsTrue(keysA.Contains(k2));

            var keysB = propA.ListProperties();

            Assert.AreEqual(2, keysB.Count);
            Assert.IsTrue(keysB.Contains(k1));
            Assert.IsTrue(keysB.Contains(k2));
        }
Exemple #17
0
 public override void OnEpisodeBegin()
 {
     timePenalty = 0;
     m_BallTouch = SideChannelUtils.GetSideChannel <FloatPropertiesChannel>().GetPropertyWithDefault("ball_touch", 0);
     if (team == Team.Purple)
     {
         transform.rotation = Quaternion.Euler(0f, -90f, 0f);
     }
     else
     {
         transform.rotation = Quaternion.Euler(0f, 90f, 0f);
     }
     transform.position      = m_Transform;
     agentRb.velocity        = Vector3.zero;
     agentRb.angularVelocity = Vector3.zero;
     SetResetParameters();
 }
        public void Awake()
        {
            // Save the original values
            m_OriginalGravity                  = Physics.gravity;
            m_OriginalFixedDeltaTime           = Time.fixedDeltaTime;
            m_OriginalMaximumDeltaTime         = Time.maximumDeltaTime;
            m_OriginalSolverIterations         = Physics.defaultSolverIterations;
            m_OriginalSolverVelocityIterations = Physics.defaultSolverVelocityIterations;

            // Override
            Physics.gravity                        *= gravityMultiplier;
            Time.fixedDeltaTime                     = fixedDeltaTime;
            Time.maximumDeltaTime                   = maximumDeltaTime;
            Physics.defaultSolverIterations         = solverIterations;
            Physics.defaultSolverVelocityIterations = solverVelocityIterations;

            SideChannelUtils.GetSideChannel <FloatPropertiesChannel>().RegisterCallback("gravity", f => { Physics.gravity = new Vector3(0, -f, 0); });
        }
Exemple #19
0
    public void Start()
    {
        m_ResetParameters = SideChannelUtils.GetSideChannel <FloatPropertiesChannel>();

        m_Objects = new[] { goalPref, pitPref };

        m_AgentCam = transform.Find("agentCam").GetComponent <Camera>();

        actorObjs = new List <GameObject>();

        var sceneTransform = transform.Find("scene");

        m_Plane           = sceneTransform.Find("Plane").gameObject;
        m_Sn              = sceneTransform.Find("sN").gameObject;
        m_Ss              = sceneTransform.Find("sS").gameObject;
        m_Sw              = sceneTransform.Find("sW").gameObject;
        m_Se              = sceneTransform.Find("sE").gameObject;
        m_InitialPosition = transform.position;
    }
Exemple #20
0
    public override void Initialize()
    {
        m_AgentRb = GetComponent <Rigidbody>();
        m_BallRb  = ball.GetComponent <Rigidbody>();
        var        canvas = GameObject.Find(k_CanvasName);
        GameObject scoreBoard;

        m_ResetParams = SideChannelUtils.GetSideChannel <FloatPropertiesChannel>();
        if (invertX)
        {
            scoreBoard = canvas.transform.Find(k_ScoreBoardBName).gameObject;
        }
        else
        {
            scoreBoard = canvas.transform.Find(k_ScoreBoardAName).gameObject;
        }
        m_TextComponent = scoreBoard.GetComponent <Text>();
        SetResetParameters();
    }
Exemple #21
0
        public void TestAcademy()
        {
            Assert.AreEqual(false, Academy.IsInitialized);
            var aca = Academy.Instance;

            Assert.AreEqual(true, Academy.IsInitialized);

            // Check that init is idempotent
            aca.LazyInitialize();
            aca.LazyInitialize();

            Assert.AreEqual(0, aca.EpisodeCount);
            Assert.AreEqual(0, aca.StepCount);
            Assert.AreEqual(0, aca.TotalStepCount);
            Assert.AreNotEqual(null, SideChannelUtils.GetSideChannel <FloatPropertiesChannel>());

            // Check that Dispose is idempotent
            aca.Dispose();
            Assert.AreEqual(false, Academy.IsInitialized);
            aca.Dispose();
        }
        public void Awake()
        {
            // Save the original values
            m_OriginalGravity                  = Physics.gravity;
            m_OriginalFixedDeltaTime           = Time.fixedDeltaTime;
            m_OriginalMaximumDeltaTime         = Time.maximumDeltaTime;
            m_OriginalSolverIterations         = Physics.defaultSolverIterations;
            m_OriginalSolverVelocityIterations = Physics.defaultSolverVelocityIterations;

            // Override
            Physics.gravity                        *= gravityMultiplier;
            Time.fixedDeltaTime                     = fixedDeltaTime;
            Time.maximumDeltaTime                   = maximumDeltaTime;
            Physics.defaultSolverIterations         = solverIterations;
            Physics.defaultSolverVelocityIterations = solverVelocityIterations;

            // Make sure the Academy singleton is initialized first, since it will create the SideChannels.
            var academy = Academy.Instance;

            SideChannelUtils.GetSideChannel <FloatPropertiesChannel>().RegisterCallback("gravity", f => { Physics.gravity = new Vector3(0, -f, 0); });
        }
        public void TestIntegerSideChannel()
        {
            var intSender   = new TestSideChannel();
            var intReceiver = new TestSideChannel();
            var dictSender  = new Dictionary <Guid, SideChannel> {
                { intSender.ChannelId, intSender }
            };
            var dictReceiver = new Dictionary <Guid, SideChannel> {
                { intReceiver.ChannelId, intReceiver }
            };

            intSender.SendInt(4);
            intSender.SendInt(5);
            intSender.SendInt(6);

            byte[] fakeData = SideChannelUtils.GetSideChannelMessage(dictSender);
            SideChannelUtils.ProcessSideChannelData(dictReceiver, fakeData);

            Assert.AreEqual(intReceiver.messagesReceived[0], 4);
            Assert.AreEqual(intReceiver.messagesReceived[1], 5);
            Assert.AreEqual(intReceiver.messagesReceived[2], 6);
        }
 public void Awake()
 {
     Academy.Instance.OnEnvironmentReset += EnvironmentReset;
     m_statsSideChannel = SideChannelUtils.GetSideChannel <StatsSideChannel>();
 }
 public void SetLaserLengths()
 {
     m_LaserLength = SideChannelUtils.GetSideChannel <FloatPropertiesChannel>().GetPropertyWithDefault("laser_length", 1.0f);
 }
    public void SetAgentScale()
    {
        float agentScale = SideChannelUtils.GetSideChannel <FloatPropertiesChannel>().GetPropertyWithDefault("agent_scale", 1.0f);

        gameObject.transform.localScale = new Vector3(agentScale, agentScale, agentScale);
    }
 public override void Initialize()
 {
     m_BallRb      = ball.GetComponent <Rigidbody>();
     m_ResetParams = SideChannelUtils.GetSideChannel <FloatPropertiesChannel>();
     SetResetParameters();
 }