Ejemplo n.º 1
0
        public override void CollectObservations(VectorSensor sensor)
        {
            sensor.AddObservation(m_Kart.LocalSpeed());
            // Add an observation for direction of the agent to the next checkpoint.
            var next         = (m_CheckpointIndex + 1) % Colliders.Length;
            var nextCollider = Colliders[next];

            if (nextCollider == null)
            {
                return;
            }

            var   direction         = (nextCollider.transform.position - m_Kart.transform.position).normalized;
            float distanceColliders = Vector3.Distance(nextCollider.transform.position, Colliders[m_CheckpointIndex].transform.position);

            sensor.AddObservation(Vector3.Dot(m_Kart.Rigidbody.velocity.normalized, direction));
            sensor.AddObservation(Vector3.Distance(nextCollider.transform.position, m_Kart.transform.position) <= distanceColliders + 4f);
            sensor.AddObservation(Vector3.Distance(nextCollider.transform.position, m_Kart.transform.position));

            if (ShowRaycasts)
            {
                Debug.DrawLine(AgentSensorTransform.position, nextCollider.transform.position, Color.magenta);
            }

            m_LastAccumulatedReward = 0.0f;
            m_EndEpisode            = false;
            for (var i = 0; i < Sensors.Length; i++)
            {
                var current = Sensors[i];
                var xform   = current.Transform;
                var hit     = Physics.Raycast(AgentSensorTransform.position, xform.forward, out var hitInfo,
                                              current.RayDistance, Mask, QueryTriggerInteraction.Ignore);

                if (ShowRaycasts)
                {
                    Debug.DrawRay(AgentSensorTransform.position, xform.forward * current.RayDistance, Color.green);
                    Debug.DrawRay(AgentSensorTransform.position, xform.forward * current.HitValidationDistance,
                                  Color.red);

                    if (hit && hitInfo.distance < current.HitValidationDistance)
                    {
                        Debug.DrawRay(hitInfo.point, Vector3.up * 3.0f, Color.blue);
                    }
                }

                if (hit)
                {
                    if (hitInfo.distance < current.HitValidationDistance)
                    {
                        m_LastAccumulatedReward += HitPenalty;
                        m_EndEpisode             = true;
                    }
                }

                sensor.AddObservation(hit ? hitInfo.distance : current.RayDistance);
            }
            sensor.AddObservation(m_Acceleration);
        }
Ejemplo n.º 2
0
        public override void CollectObservations(VectorSensor sensor)
        {
            sensor.AddObservation(m_Kart.LocalSpeed());

            // Add an observation for direction of the agent to the next checkpoint.
            var next         = (m_CheckpointIndex + 1) % Colliders.Length;
            var nextCollider = Colliders[next];
            var direction    = (nextCollider.transform.position - m_Kart.transform.position).normalized;

            sensor.AddObservation(Vector3.Dot(m_Kart.Rigidbody.velocity.normalized, direction));

            if (ShowRaycasts)
            {
                Debug.DrawLine(AgentSensorTransform.position, nextCollider.transform.position, Color.magenta);
            }

            var accumulatedReward = 0.0f;
            var endEpisode        = false;

            for (var i = 0; i < Sensors.Length; i++)
            {
                var current = Sensors[i];
                var xform   = current.Transform;
                var hit     = Physics.Raycast(AgentSensorTransform.position, xform.forward, out var hitInfo,
                                              RaycastDistance, Mask, QueryTriggerInteraction.Ignore);

                if (ShowRaycasts)
                {
                    Debug.DrawRay(AgentSensorTransform.position, xform.forward * RaycastDistance, Color.green);
                    Debug.DrawRay(AgentSensorTransform.position, xform.forward * RaycastDistance * current.HitThreshold,
                                  Color.red);
                }

                var hitDistance = (hit ? hitInfo.distance : RaycastDistance) / RaycastDistance;
                sensor.AddObservation(hitDistance);

                if (hitDistance < current.HitThreshold)
                {
                    accumulatedReward += HitPenalty;
                    endEpisode         = true;
                }
            }

            if (endEpisode)
            {
                AddReward(accumulatedReward);
                EndEpisode();
                OnEpisodeBegin();
            }
        }
Ejemplo n.º 3
0
        public override void CollectObservations()
        {
            AddVectorObs(kart.LocalSpeed());

            // Add an observation for direction of the agent to the next checkpoint.
            var next         = (checkpointIndex + 1) % Colliders.Length;
            var nextCollider = Colliders[next];
            var direction    = (nextCollider.transform.position - kart.transform.position).normalized;

            AddVectorObs(Vector3.Dot(kart.Rigidbody.velocity.normalized, direction));

            if (ShowRaycasts)
            {
                Debug.DrawLine(AgentSensorTransform.position, nextCollider.transform.position, Color.magenta);
            }

            for (int i = 0; i < Sensors.Length; i++)
            {
                var current = Sensors[i];
                var xform   = current.Transform;
                var hit     = Physics.Raycast(AgentSensorTransform.position, xform.forward, out var hitInfo,
                                              RaycastDistance, Mask, QueryTriggerInteraction.Ignore);

                if (ShowRaycasts)
                {
                    Debug.DrawRay(AgentSensorTransform.position, xform.forward * RaycastDistance, Color.green);
                    Debug.DrawRay(AgentSensorTransform.position, xform.forward * RaycastDistance * current.HitThreshold,
                                  Color.red);
                }

                var hitDistance = (hit ? hitInfo.distance : RaycastDistance) / RaycastDistance;
                AddVectorObs(hitDistance);

                if (hitDistance < current.HitThreshold)
                {
                    AddReward(HitPenalty);
                    Done();
                    AgentReset();
                }
            }
        }
Ejemplo n.º 4
0
    public override void CollectObservations(VectorSensor sensor)
    {
        sensor.AddObservation(m_tankCanonHandle.CurrentCanonAngle / m_tankCanonHandle.MaxCanonAngle);
        sensor.AddObservation(m_tankCanonHandle.CurrentTowerAngle / 360f);
        sensor.AddObservation(m_tankCanonHandle.CanShootNow());
        sensor.AddObservation(_currentHealth / (float)MaxHealth);
        sensor.AddObservation(m_Kart.LocalSpeed());
        Vector3 EnemyVector = EnemyAgent.transform.position - transform.position;

        sensor.AddObservation(Vector3.SignedAngle(EnemyVector, transform.forward, Vector3.up) / 180f);
        sensor.AddObservation(EnemyVector.magnitude / 30f);
    }
Ejemplo n.º 5
0
        public override void CollectObservations(VectorSensor sensor)
        {
            sensor.AddObservation(m_Kart.LocalSpeed());

            // Add an observation for direction of the agent to the next checkpoint.
            var next         = (m_CheckpointIndex + 1) % Colliders.Length;
            var nextCollider = Colliders[next];

            if (nextCollider == null)
            {
                return;
            }

            var direction = (nextCollider.transform.position - m_Kart.transform.position).normalized;

            sensor.AddObservation(Vector3.Dot(m_Kart.Rigidbody.velocity.normalized, direction));

            if (ShowRaycasts)
            {
                Debug.DrawLine(AgentSensorTransform.position, nextCollider.transform.position, Color.magenta);
            }

            m_LastAccumulatedReward = 0.0f;
            m_EndEpisode            = false;
            for (var i = 0; i < Sensors.Length; i++)
            {
                var current = Sensors[i];
                var xform   = current.Transform;
                var hit     = Physics.Raycast(AgentSensorTransform.position, xform.forward, out var hitInfo,
                                              current.RayDistance, Mask, QueryTriggerInteraction.Ignore);

                if (ShowRaycasts)
                {
                    Debug.DrawRay(AgentSensorTransform.position, xform.forward * current.RayDistance, Color.green);
                    Debug.DrawRay(AgentSensorTransform.position, xform.forward * current.HitValidationDistance,
                                  Color.red);

                    if (hit && hitInfo.distance < current.HitValidationDistance)
                    {
                        Debug.DrawRay(hitInfo.point, Vector3.up * 3.0f, Color.blue);
                    }
                }

                if (hit)
                {
                    Debug.Log("Name:" + name + "hit distance " + i.ToString() + ":" + hitInfo.distance);
                    if (hitInfo.distance < current.HitValidationDistance)
                    {
                        m_LastAccumulatedReward += HitPenalty;
                        m_EndEpisode             = true;
                        // Debug.Log("Hit Penalty:" + m_LastAccumulatedReward.ToString());
                    }
                }

                sensor.AddObservation(hit ? hitInfo.distance : current.RayDistance);
            }

            sensor.AddObservation(m_Acceleration);

            // 2022-03-22 YY Add Check if a machine reaches the goal (Point B)
            if ((point_b.transform.position.x - GOAL_SIZE <= transform.position.x &&
                 transform.position.x <= point_b.transform.position.x + GOAL_SIZE)
                &&
                (point_b.transform.position.z - GOAL_SIZE <= transform.position.z &&
                 transform.position.z <= point_b.transform.position.z + GOAL_SIZE))
            {
                Debug.Log("GOAL");
                m_LastAccumulatedReward += GOAL_REWARD;
                // m_EndEpisode = true;
                transform.position = initialPoint;
            }
        }
Ejemplo n.º 6
0
    void Update()
    {
        // Reset the trigger flag
        if (BounceFlag)
        {
            BounceFlag = false;
        }
        Vector3 origin = transform.position;

        origin.y += HeightOffset;

        for (int i = 0; i < Angles.Length; i++)
        {
            Vector3 direction = GetDirectionFromAngle(Angles[i], Vector3.up, transform.forward);

            if (Physics.Raycast(origin, direction, out RaycastHit hit, RayDistance, CollisionLayer) && Time.time > resumeTime && !hasCollided && kart.LocalSpeed() > 0)
            {
                // If the hit normal is pointing up, then we don't want to bounce
                if (Vector3.Dot(hit.normal, Vector3.up) > 0.2f)
                {
                    return;
                }

                // Calculate the incident vector of the kart colliding into whatever object
                Vector3 incidentVector = hit.point - origin;

                // Calculate the reflection vector using the incident vector of the collision
                Vector3 hitNormal = hit.normal.normalized;
                reflectionVector   = incidentVector - 2 * Vector3.Dot(incidentVector, hitNormal) * hitNormal;
                reflectionVector.y = 0;

                kart.Rigidbody.velocity /= 2;
                // Apply the bounce impulse with the reflectionVector
                kart.Rigidbody.AddForce(reflectionVector.normalized * BounceFactor, ForceMode.Impulse);

                // Mark that the vehicle has collided and the reset time.
                kart.SetCanMove(false);
                BounceFlag = hasCollided = true;
                resumeTime = Time.time + PauseTime;

                if (BounceSound)
                {
                    AudioUtility.CreateSFX(BounceSound, transform.position, AudioUtility.AudioGroups.Collision, 0f);
                }
                return;
            }
        }

        if (Time.time < resumeTime)
        {
            Vector3    targetPos      = origin + reflectionVector;
            Vector3    direction      = targetPos - origin;
            Quaternion targetRotation = Quaternion.LookRotation(direction);
            kart.transform.rotation = Quaternion.Slerp(kart.transform.rotation, targetRotation, RotationSpeed * Time.deltaTime);
        }
    }