Exemplo n.º 1
0
        public void TestAddObservationQuaternion()
        {
            var sensor = new VectorSensor(4);

            sensor.AddObservation(Quaternion.identity);
            SensorTestHelper.CompareObservation(sensor, new[] { 0f, 0f, 0f, 1f });
        }
    public override void CollectObservations(VectorSensor vectorSensor)
    {
        float angle = Vector3.SignedAngle(_track.forward, transform.forward, Vector3.up);


        var v1 = ObserveRay(1.5f, .5f, 25f);
        var v2 = ObserveRay(1.5f, 0f, 0f);
        var v3 = ObserveRay(1.5f, -.5f, -25f);
        var v4 = ObserveRay(-1.5f, 0, 180f);

        vectorSensor.AddObservation(angle / 180f);
        vectorSensor.AddObservation(v1.value);
        vectorSensor.AddObservation(v2.value);
        vectorSensor.AddObservation(v3.value);
        vectorSensor.AddObservation(v4.value);
    }
    void AddRaycastVectorObs(VectorSensor sensor, Transform ray)
    {
        // Cast a ray around the car
        RaycastHit hitInfo = new RaycastHit();
        var        hit     = Physics.Raycast(ray.position, ray.forward, out hitInfo, raycastDistance, raycastLayers.value, QueryTriggerInteraction.Ignore);

        // This is valid only if it directly hit off-track object
        var distance = hitInfo.distance;

        if (!hit)
        {
            distance = raycastDistance;
        }
        var obs = distance / raycastDistance;

        sensor.AddObservation(obs);

        // If hit wall
        if (distance < hitDist)
        {
            // Discourage high speed near the wall
            // Facilitate finding new path
            AddReward(-10 * car.speed * speedReward);

            this.EndEpisode();
            this.OnEpisodeBegin();
        }
        Debug.DrawRay(ray.position, ray.forward * distance, Color.Lerp(Color.red, Color.green, obs), Time.deltaTime * debugRaycastTime);
    }
Exemplo n.º 4
0
 public override void CollectObservations(VectorSensor sensor)
 {
     if (useVectorObs)
     {
         sensor.AddObservation(StepCount / (float)MaxStep);
     }
 }
Exemplo n.º 5
0
        public void TestAddObservationFloat()
        {
            var sensor = new VectorSensor(1);

            sensor.AddObservation(1.2f);
            SensorTestHelper.CompareObservation(sensor, new[] { 1.2f });
        }
    public override void CollectObservations(VectorSensor sensor)
    {
        var spectrum = new float[1024];

        sensor.AddObservation(pianos[num].GetData(spectrum, 0));
        for (int i = 1; i < spectrum.Length - 1; ++i)
        {
            Debug.DrawLine(
                new Vector3(i - 1, spectrum[i] + 10, 0),
                new Vector3(i, spectrum[i + 1] + 10, 0),
                Color.red);
            Debug.DrawLine(
                new Vector3(i - 1, Mathf.Log(spectrum[i - 1]) + 10, 2),
                new Vector3(i, Mathf.Log(spectrum[i]) + 10, 2),
                Color.cyan);
            Debug.DrawLine(
                new Vector3(Mathf.Log(i - 1), spectrum[i - 1] - 10, 1),
                new Vector3(Mathf.Log(i), spectrum[i] - 10, 1),
                Color.green);
            Debug.DrawLine(
                new Vector3(Mathf.Log(i - 1), Mathf.Log(spectrum[i - 1]), 3),
                new Vector3(Mathf.Log(i), Mathf.Log(spectrum[i]), 3),
                Color.yellow);
        }
    }
Exemplo n.º 7
0
        public void TestAddObservationInt()
        {
            var sensor = new VectorSensor(1);

            sensor.AddObservation(42);
            SensorTestHelper.CompareObservation(sensor, new[] { 42f });
        }
Exemplo n.º 8
0
    public override void CollectObservations(VectorSensor sensor)
    {
        // update pos to target
        m_DirToTarget     = Target.position - body.position;
        m_LookRotation    = Quaternion.LookRotation(m_DirToTarget);
        m_TargetDirMatrix = Matrix4x4.TRS(Vector3.zero, m_LookRotation, Vector3.one);

        // Forward & up to help with orientation
        var bodyForwardRelativeToLookRotationToTarget = m_TargetDirMatrix.inverse.MultiplyVector(body.forward);

        sensor.AddObservation(bodyForwardRelativeToLookRotationToTarget);

        var bodyUpRelativeToLookRotationToTarget = m_TargetDirMatrix.inverse.MultiplyVector(body.up);

        sensor.AddObservation(bodyUpRelativeToLookRotationToTarget);
    }
Exemplo n.º 9
0
        public void TestAddObservationBool()
        {
            var sensor = new VectorSensor(1);

            sensor.AddObservation(true);
            SensorTestHelper.CompareObservation(sensor, new[] { 1f });
        }
Exemplo n.º 10
0
        public void TestVectorStackingReset()
        {
            VectorSensor wrapped = new VectorSensor(2);
            ISensor      sensor  = new StackingSensor(wrapped, 3);

            wrapped.AddObservation(new[] { 1f, 2f });
            SensorTestHelper.CompareObservation(sensor, new[] { 0f, 0f, 0f, 0f, 1f, 2f });

            sensor.Update();
            wrapped.AddObservation(new[] { 3f, 4f });
            SensorTestHelper.CompareObservation(sensor, new[] { 0f, 0f, 1f, 2f, 3f, 4f });

            sensor.Reset();
            wrapped.AddObservation(new[] { 5f, 6f });
            SensorTestHelper.CompareObservation(sensor, new[] { 0f, 0f, 0f, 0f, 5f, 6f });
        }
Exemplo n.º 11
0
    /// <summary>
    /// Collect all non-Raycast observations
    /// </summary>
    public override void CollectObservations(VectorSensor sensor)
    {
        // Whether the penguin has eaten a fish (1 float = 1 value)
        sensor.AddObservation(isFull);

        // Distance to the baby (1 float = 1 value)
        sensor.AddObservation(Vector3.Distance(baby.transform.position, transform.position));

        // Direction to baby (1 Vector3 = 3 values)
        sensor.AddObservation((baby.transform.position - transform.position).normalized);

        // Direction penguin is facing (1 Vector3 = 3 values)
        sensor.AddObservation(transform.forward);

        // 1 + 1 + 3 + 3 = 8 total values
    }
    public override void CollectObservations(VectorSensor sensor)
    {
        EmotionValues = new List <float> {
            EmotionDetector.Happy
            , EmotionDetector.Sad
            , EmotionDetector.Angry
            , EmotionDetector.Disgusted
            , EmotionDetector.Fearful
            , EmotionDetector.Neutral
            , EmotionDetector.Surprised
            , EmotionDetector.Contempt
        };

        sensor.AddObservation(EmotionValues);
        sensor.AddObservation(facialActionValues);
    }
    public override void CollectObservations(VectorSensor sensor)
    {
        Vector3 dirToTarget = (target.position - this.transform.position).normalized;

        // Target position in agent frame
        sensor.AddObservation(
            this.transform.InverseTransformPoint(target.transform.position)); // vec 3

        // Agent velocity in agent frame
        sensor.AddObservation(
            this.transform.InverseTransformVector(AgentVelocity)); // vec 3

        // Direction to target in agent frame
        sensor.AddObservation(
            this.transform.InverseTransformDirection(dirToTarget)); // vec 3
    }
Exemplo n.º 14
0
    public override void CollectObservations(VectorSensor sensor)
    {
        // ball position relative to agent
        Vector3 ballPosition;

        if (area.phase == RectArea.GamePhase.Start && this.isServing)
        {
            ballPosition = area.ball.transform.localPosition;
        }
        else
        {
            ballPosition = this.transform.InverseTransformPoint(area.ball.transform.position);
        }
        // Debug.Log("ballPosition for " + this.gameObject.name + " is " + ballPosition.ToString());
        sensor.AddObservation(ballPosition);
        // Debug.Log("ballPosition relative to " + this.gameObject.name + " is " + ballPosition.ToString() );

        // net position relative to agent
        Vector3 netPosition = this.transform.InverseTransformPoint(this.transform.parent.position);

        sensor.AddObservation(new Vector3(netPosition.x, -netPosition.y, netPosition.z));
        // Debug.Log("net position relative to " + this.gameObject.name + " is " + netPosition.ToString());

        // agent y rotation ( depends on team )
        Quaternion inputRotation = this.transform.localRotation;

        if (team == RectTeam.Blue)
        {
            inputRotation *= Quaternion.Euler(0, -180, 0);
        }
        sensor.AddObservation(inputRotation);
        // Debug.Log( "agent rotation for " + this.gameObject.name + " is " + inputRotation.ToString());

        // friendlyAgent position
        Vector3 friendlyAgentPos = this.transform.InverseTransformPoint(friendlyAgent.transform.position);

        sensor.AddObservation(friendlyAgentPos.x);
        sensor.AddObservation(friendlyAgentPos.z);
        // Debug.Log(" friendlyAgent position relative to " + this.gameObject.name + " is " + friendlyAgentPos.ToString());
        // enemyAgent1 position
        Vector3 enemyAgent1Pos = this.transform.InverseTransformPoint(enemyAgent1.transform.position);

        sensor.AddObservation(enemyAgent1Pos.x);
        sensor.AddObservation(enemyAgent1Pos.z);
        // Debug.Log("enemyAgent1 position relative to " + this.gameObject.name + " is " + enemyAgent1Pos.ToString());
        // enemyAgent2 position
        Vector3 enemyAgent2Pos = this.transform.InverseTransformPoint(enemyAgent2.transform.position);

        sensor.AddObservation(enemyAgent2Pos.x);
        sensor.AddObservation(enemyAgent2Pos.z);
        // Debug.Log(" enemyAgent2 position relative to " + this.gameObject.name + " is " + enemyAgent2Pos.ToString());
    }
Exemplo n.º 15
0
        private void TraditionalObservable(VectorSensor _sensor)
        {
            var state = m_Controller.State;

            // Need value function
            int NeedValue(int _value)
            {
                if (_value < 0)
                {
                    return(-1);
                }
                else if (_value > 0)
                {
                    return(1);
                }
                else
                {
                    return(0);
                }
            }

            // Head and Food position
            var head            = state.Head;
            var food            = state.Food;
            var horizontal      = NeedValue((int)(food.x - head.x));
            var vertical        = NeedValue((int)(food.y - head.y));
            var vectorDirection = new Vector2(horizontal, vertical);

            // Direction list lead to food
            var foodDirections = vectorDirection.GetDirectionType().ToList();

            foodDirections.Remove(State.GetNegativeDirection(m_Controller.Direction));

            // Direction list not lead to food
            var noFoodDirections = DirectionTypeExtension.AllDirections().Except(foodDirections).ToList();

            noFoodDirections.Remove(State.GetNegativeDirection(m_Controller.Direction));

            _sensor.AddObservation(vectorDirection);

            // Food Info
            _sensor.AddObservation(foodDirections.Contains(DirectionType.Up));
            _sensor.AddObservation(foodDirections.Contains(DirectionType.Down));
            _sensor.AddObservation(foodDirections.Contains(DirectionType.Right));
            _sensor.AddObservation(foodDirections.Contains(DirectionType.Left));

            // Non-Food Info
            _sensor.AddObservation(noFoodDirections.Contains(DirectionType.Up));
            _sensor.AddObservation(noFoodDirections.Contains(DirectionType.Down));
            _sensor.AddObservation(noFoodDirections.Contains(DirectionType.Right));
            _sensor.AddObservation(noFoodDirections.Contains(DirectionType.Left));
        }
Exemplo n.º 16
0
    public override void CollectObservations(VectorSensor sensor)
    {
        // Agent positions (3 observations)
        sensor.AddObservation(this.transform.localPosition);

        // Target position
        // sensor.AddObservation(Target.localPosition);

        // Position of center of face
        // Vector3 facePosition = transform.localPosition + transform.forward * 0.5f;

        // Vector from center of face to target
        // Vector3 toTarget = Target.localPosition - facePosition;

        // Vector from cube center to target center
        Vector3 toTarget = Target.localPosition - transform.localPosition;

        // convert to local coordinate system
        Vector3 toTargetLocal = this.transform.InverseTransformDirection(toTarget);

        // Observe a normalized vector pointing to the target (3 observations)
        sensor.AddObservation(toTargetLocal.normalized);

        // Add reward if dot product of agent direction and toTarget is > 0.9
        // i.e. if agent is pointing towards target
        float dotToTarget = Vector3.Dot(transform.forward.normalized, toTarget.normalized);

        if (dotToTarget > 0.9)
        {
            // Pointing towards target, increase positive reward
            AddReward(dirReward);
            // accumulatedDirReward += dirReward;
            // Debug.Log(accumulatedDirReward);
        }

        // Agent velocity (3 observations)
        sensor.AddObservation(rBody.velocity);

        // Observe the agent's local rotation (4 observations)
        sensor.AddObservation(transform.localRotation.normalized);

        // Observe the distance from agent to target surface (1 observation)
        // sensor.AddObservation(toTarget.magnitude - 0.5f);

        // Observe the angular velocity of the agent (3 observations)
        sensor.AddObservation(rBody.angularVelocity);
    }
Exemplo n.º 17
0
 public void LogVectorObs(string obsName, Quaternion obsVector, VectorSensor sensor)
 {
     if (Application.isEditor)
     {
         obsDisplayText += string.Format("\n{0:0.00} {1} \n{2:0.00} \n{3:0.00} \n{4:0.00}", obsVector.x, obsName, obsVector.y, obsVector.z, obsVector.w);
     }
     sensor.AddObservation(obsVector);
 }
Exemplo n.º 18
0
 void DoCollectObservationsForPlayer(VectorSensor sensor, int playerId)
 {
     foreach (var cell in _cells)
     {
         bool status = cell.TeamId == playerId;
         sensor.AddObservation(status);
     }
 }
Exemplo n.º 19
0
    public override void CollectObservations(VectorSensor sensor)
    {
        // Prepare calculations
        Vector2 trainingAreaPos          = trainingArea.position;                                                      // Cast from Vector3 to Vector2
        Vector2 positionBetweenDroneFeet = (droneInterface.leftFoot.position + droneInterface.rightFoot.position) / 2; // Cast from Vector3 to Vector2
        Vector2 platformPosition         = target.position;                                                            // Center of the platform, cast from Vector3 to Vector2

        // Information about the drone itself
        sensor.AddObservation(droneRB.position - trainingAreaPos); // Current drone position
        sensor.AddObservation(droneRB.rotation);                   // Current drone rotation
        sensor.AddObservation(droneRB.velocity);                   // Current drone velocity
        sensor.AddObservation(droneRB.angularVelocity);            // Current drone rotational velocity

        // Information of the drone relative to its environment
        sensor.AddObservation(platformPosition - positionBetweenDroneFeet); // Distance FROM drone TO platform
        // sensor.AddObservation(droneInterface.windDir * droneInterface.windSpeed); // Speed of the wind, which most drones cannot read
    }
Exemplo n.º 20
0
 public void LogVectorObs(string obsName, float obsVector, VectorSensor sensor)
 {
     if (Application.isEditor)
     {
         obsDisplayText += string.Format("\n{0:0.00} {1}", obsVector, obsName);
     }
     sensor.AddObservation(obsVector);
 }
Exemplo n.º 21
0
 public override void CollectObservations(VectorSensor sensor)
 {
     for (Position position = new Position(0, 0); position.x < game.board.size.x; ++position.x)
     {
         for (position.y = 0; position.y < game.board.size.y; ++position.y)
         {
             sensor.AddObservation(game.board.GetState(position) == -id);
         }
     }
     for (Position position = new Position(0, 0); position.x < game.board.size.x; ++position.x)
     {
         for (position.y = 0; position.y < game.board.size.y; ++position.y)
         {
             sensor.AddObservation(game.board.GetState(position) == +id);
         }
     }
 }
Exemplo n.º 22
0
        public void TestWriteEnumerable()
        {
            var sensor = new VectorSensor(4);

            sensor.AddObservation(new[] { 1f, 2f, 3f, 4f });

            SensorTestHelper.CompareObservation(sensor, new[] { 1f, 2f, 3f, 4f });
        }
Exemplo n.º 23
0
        public void TestWriteTooMany()
        {
            var sensor = new VectorSensor(2);

            sensor.AddObservation(new[] { 1f, 2f, 3f, 4f });

            SensorTestHelper.CompareObservation(sensor, new[] { 1f, 2f });
        }
Exemplo n.º 24
0
        public void TestWrite()
        {
            var sensor = new VectorSensor(4);

            sensor.AddObservation(1f);
            sensor.AddObservation(2f);
            sensor.AddObservation(3f);
            sensor.AddObservation(4f);

            SensorTestHelper.CompareObservation(sensor, new[] { 1f, 2f, 3f, 4f });
            // Check that if we don't call Update(), the same observations are produced
            SensorTestHelper.CompareObservation(sensor, new[] { 1f, 2f, 3f, 4f });

            // Check that Update() clears the data
            sensor.Update();
            SensorTestHelper.CompareObservation(sensor, new[] { 0f, 0f, 0f, 0f });
        }
Exemplo n.º 25
0
    public override void CollectObservations(VectorSensor sensor)
    {
        // Target and agent positions
        sensor.AddObservation(target.position);         // vec 3
        sensor.AddObservation(this.transform.position); // vec 3

        // Agent velocity
        sensor.AddObservation(AgentVelocity); // vec 3

        // The direction from the agent to the target
        Vector3 dirToTarget = (target.position - this.transform.position).normalized;
        // The alignment of the agent's velocity with this direction
        float velocityAlignment = Vector3.Dot(dirToTarget, AgentVelocity);

        // Small reward for moving in the direction of the target
        AddReward(RewardScalar * velocityAlignment);
    }
Exemplo n.º 26
0
 /// <summary>
 ///     Adds normalised 'trail' of visited locations to observations
 /// </summary>
 /// <param name="sensor">Sensor used to pass observations</param>
 protected void AddVisitedMemoryTrail(VectorSensor sensor) =>
 _agentMemory
 .GetAgentMemory(transform.localPosition)
 .ToList()
 .ForEach(f => sensor.AddObservation(NormalisedMemoryFloat(
                                         -MaxLocalDistance,
                                         MaxLocalDistance,
                                         f)));
Exemplo n.º 27
0
    public override void CollectObservations(VectorSensor sensor)
    {
        Vector3 mpos        = mobileBase.transform.localPosition;
        Vector3 tpos        = target.transform.localPosition;
        float   angleTarget = Mathf.Atan2(tpos.z - mpos.z, tpos.x - mpos.x);

        Vector3 pos_d           = mpos + mobileBase.transform.forward.normalized;
        float   angleMobileBase = Mathf.Atan2(pos_d.z - mpos.z, pos_d.x - mpos.x);

        var dist = Vector3.Distance(mobileBase.transform.localPosition, target.transform.localPosition);

        sensor.AddObservation(mpos);
        sensor.AddObservation(tpos);
        sensor.AddObservation(angleMobileBase);
        sensor.AddObservation(angleTarget);
        sensor.AddObservation(dist);
    }
    public override void CollectObservations(VectorSensor sensor)
    {
        base.CollectObservations(sensor);

        if (rocket)
        {
            // What I learned is that you should not overfeed the AI learning with more data than
            // it needs. Originaly I was trying to send it as much data as pssible but eventually
            // it seems to work best with just the Ray Sensors 3d and the angular velocity of the rocket
            // might make sense to send the rocket orientation in the same frame as the anugular velocity,
            // but it doesn't seem to need it. Also I noticed that when training in the big sphere the rockets
            // need to move faster and the learning sets the Unity3D Time base to 20, there was significat difference
            // in learning when running so fast when the rockets need to move fast, learning worked much better whene
            // Time base was set to a much lower number or even 1. Something to check if you find it's not training for
            // some reaon.

            //sensor.AddObservation(transform.rotation.eulerAngles); // ship on the sphere

            /*
             * sensor.AddObservation(transform.rotation.eulerAngles.x); // ship on the sphere
             * sensor.AddObservation(transform.rotation.eulerAngles.y); // ship on the sphere
             * sensor.AddObservation(transform.rotation.eulerAngles.z); // ship rotation
             */

            //sensor.AddObservation(transform.rotation); // ship rotation

            /*
             * sensor.AddObservation(transform.rotation.w); // ship rotation
             * sensor.AddObservation(transform.rotation.x); // ship rotation
             * sensor.AddObservation(transform.rotation.y); // ship rotation
             * sensor.AddObservation(transform.rotation.z); // ship rotation
             */

            //sensor.AddObservation(transform.position); // ship coords

            /*
             * sensor.AddObservation(transform.position.x); // ship coords
             * sensor.AddObservation(transform.position.y); // ship coords
             * sensor.AddObservation(transform.position.z); // ship coords
             */
            //Debug.Log("transform x " + transform.rotation.eulerAngles.x + " y " + transform.rotation.eulerAngles.y + " z " + transform.rotation.eulerAngles.z);

            sensor.AddObservation(rb.angularVelocity);

            //sensor.AddObservation(raySensor.RaySensor.);

            /*
             * sensor.AddObservation(rb.angularVelocity.x);
             * sensor.AddObservation(rb.angularVelocity.y);
             * sensor.AddObservation(rb.angularVelocity.z);
             */
            //sensor.AddObservation(rb.angularVelocity.magnitude);

            //sensor.AddObservation(rocket.countRotations);

            //Debug.Log("angularVelocity x " + rb.angularVelocity.x + " y " + rb.angularVelocity.y + " z " + rb.angularVelocity.z);
        }
    }
Exemplo n.º 29
0
    public override void CollectObservations(VectorSensor sensor)
    {
        sensor.AddObservation(m_InvertMult * (transform.position.x - myArea.transform.position.x));
        sensor.AddObservation(transform.position.y - myArea.transform.position.y);
        sensor.AddObservation(m_InvertMult * m_AgentRb.velocity.x);
        sensor.AddObservation(m_AgentRb.velocity.y);

        sensor.AddObservation(m_InvertMult * (ball.transform.position.x - myArea.transform.position.x));
        sensor.AddObservation(ball.transform.position.y - myArea.transform.position.y);
        sensor.AddObservation(m_InvertMult * m_BallRb.velocity.x);
        sensor.AddObservation(m_BallRb.velocity.y);

        sensor.AddObservation(m_InvertMult * gameObject.transform.rotation.z);
    }
Exemplo n.º 30
0
        public override void CollectObservations(VectorSensor sensor)
        {
            sensor.AddObservation(m_Ship.Throttle);
            sensor.AddObservation(m_Ship.Pitch);
            sensor.AddObservation(m_Ship.Roll);
            sensor.AddObservation(m_Ship.NormPosition);
            sensor.AddObservation(m_Ship.NormOrientation);
            sensor.AddObservation(Normalization.Sigmoid(m_Ship.LocalSpin));
            sensor.AddObservation(Normalization.Sigmoid(m_Ship.LocalVelocity));

            Vector3 pos = m_Ship.transform.position;
            Vector3 fwd = m_Ship.transform.forward;

            m_Targets.Clear();

            foreach (var target in m_SensorComponent.GetDetectedGameObjects(m_TargetTag))
            {
                Vector3 delta = target.transform.position - pos;
                if (Vector3.Angle(fwd, delta) < m_TargetFollowAngle &&
                    delta.sqrMagnitude < m_TargetFollowDistanceSqr)
                {
                    m_Targets.Add(target);
                }
            }
        }