Exemplo n.º 1
0
 public override void CollectObservations(VectorSensor sensor)
 {
     sensor.AddObservation(gameObject.transform.rotation.z);
     sensor.AddObservation(gameObject.transform.rotation.x);
     sensor.AddObservation((ball.transform.position - gameObject.transform.position));
 }
Exemplo 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];

            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;
            }
        }
Exemplo n.º 3
0
    public void LogVectorObs(string obsName, Vector3 obsVector, Vector3 min, Vector3 max, VectorSensor sensor)
    {
        if (!variances.ContainsKey(obsName))
        {
            variances.Add(obsName, new WelfordVariance());
        }
        variances[obsName].UpdateVariance(new Quaternion(obsVector.x, obsVector.y, obsVector.z, 0));

        var normalizedObs = new Vector3(((obsVector.x - min.x) / (max.x - min.x)),
                                        ((obsVector.y - min.y) / (max.y - min.y)),
                                        ((obsVector.z - min.z) / (max.z - min.z)));

        sensor.AddObservation(normalizedObs);

        if (Application.isEditor)
        {
            obsDisplayText += string.Format("\n{0:0.00} {1} \n{2:0.00} \n{3:0.00}", obsVector.x, obsName, obsVector.y, obsVector.z);
            obsDisplayText += string.Format("\n{0:0.00} {1} \n{2:0.00} \n{3:0.00}", normalizedObs.x, obsName + " normalized", normalizedObs.y, normalizedObs.z);
        }
    }
Exemplo n.º 4
0
 public override void CollectObservations(VectorSensor sensor)
 {
     sensor.AddObservation(transform.localPosition);
     sensor.AddObservation(transform.localRotation);
     sensor.AddObservation(targetTransform.localPosition);
 }
Exemplo n.º 5
0
 public override void CollectObservations(VectorSensor sensor)
 {
     sensor.AddOneHotObservation(m_Position, 20);
 }
Exemplo n.º 6
0
 public override void CollectObservations(VectorSensor sensor)
 {
     sensor.AddObservation(m_Rigidbody.velocity);
 }
Exemplo n.º 7
0
    public override void CollectObservations(VectorSensor sensor)
    {
        obsDisplayText = "";
        LogVectorObs("position", planet.transform.rotation * this.transform.localPosition, new Vector3(-0.5f, 0, -0.5f), new Vector3(0.5f, 0.6f, 0.5f), sensor);
        LogVectorObs("Planet angular velocity", planet.angularVelocity, new Vector3(-3, -3, -3), new Vector3(3, 3, 3), sensor);
        LogVectorObs("Self Tangential Velocity", planet.GetPointVelocity(this.transform.position), new Vector3(-3, -3, -3), new Vector3(3, 3, 3), sensor);
        LogVectorObs("lightning target position", new Vector3(0, 0, 0), new Vector3(-0.5f, 0, -0.5f), new Vector3(0.5f, 0.6f, 0.5f), sensor);
        LogVectorObs("Can Consume", canConsume, sensor);
        LogVectorObs("Consuming", consuming, sensor);
        LogVectorObs("Grounded", false, sensor);
        LogVectorObs("Sitting", false, sensor);
        LogVectorObs("Can Push", false, sensor);
        LogVectorObs("Pushing", false, sensor);
        if (canConsume)
        {
            LogVectorObs("Consume Timer", 1, sensor);
        }
        else
        {
            LogVectorObs("Consume Timer", (Time.fixedTime - consumeTime) / 10, sensor);
        }

        int agentnum = 0;

        pushTarget = null;

        foreach (var agent in agents)
        {
            if (Vector3.Distance(agent.transform.position, this.transform.position) < 2f)
            {
                pushTarget = agent.GetComponent <Conscientious>();
            }
            LogVectorObs("position", planet.transform.rotation * agent.transform.localPosition, new Vector3(-0.5f, 0, -0.5f), new Vector3(0.5f, 0.6f, 0.5f), sensor);
            LogVectorObs("Other Agent's Tangential Velocity", planet.GetPointVelocity(agent.transform.position), new Vector3(-3, -3, -3), new Vector3(3, 3, 3), sensor);
            agentnum++;
        }
        for (int i = agentnum; i < 5; i++)
        {
            LogVectorObs("position", new Vector3(0, 0, 0), new Vector3(-0.5f, 0, -0.5f), new Vector3(0.5f, 0.6f, 0.5f), sensor);
            LogVectorObs("Other Agent's Tangential Velocity", new Vector3(0, 0, 0), new Vector3(-3, -3, -3), new Vector3(3, 3, 3), sensor);
        }

        int weightNum = 0;

        foreach (var weight in weights)
        {
            LogVectorObs("Weight position", planet.transform.rotation * weight.transform.localPosition, new Vector3(-0.5f, 0, -0.5f), new Vector3(0.5f, 0.6f, 0.5f), sensor);
            LogVectorObs("Weight Tangential Velocity", planet.GetPointVelocity(weight.transform.position), new Vector3(-3, -3, -3), new Vector3(3, 3, 3), sensor);
            foreach (var consumePoint in weight.consumePoints)
            {
                //LogVectorObs("Consume Point position", planet.transform.rotation * consumePoint.transform.localPosition, new Vector3(-0.5f, 0, -0.5f), new Vector3(0.5f, 0.6f, 0.5f), sensor);
                LogVectorObs("Can Consume at Point", consumePoint.CanConsume, sensor);
            }
            weightNum++;
        }
        for (int i = weightNum; i < 5; i++)
        {
            LogVectorObs("Weight position", new Vector3(0, 0, 0), new Vector3(-0.5f, 0, -0.5f), new Vector3(0.5f, 0.6f, 0.5f), sensor);
            LogVectorObs("Weight Tangential Velocity", new Vector3(0, 0, 0), new Vector3(-3, -3, -3), new Vector3(3, 3, 3), sensor);
            for (int j = 0; j < 3; i++)
            {
                //LogVectorObs("Consume Point position", new Vector3(0, 0, 0), new Vector3(-0.5f, 0, -0.5f), new Vector3(0.5f, 0.6f, 0.5f), sensor);
                LogVectorObs("Can Consume at Point", true, sensor);
            }
        }

        if (priorConsumePoint)
        {
            LogVectorObs("Prior Consume Point position", planet.transform.rotation * priorConsumePoint.transform.localPosition, new Vector3(-0.5f, 0, -0.5f), new Vector3(0.5f, 0.6f, 0.5f), sensor);
        }
        else
        {
            LogVectorObs("Consume Point position", new Vector3(0, 0, 0), new Vector3(-0.5f, 0, -0.5f), new Vector3(0.5f, 0.6f, 0.5f), sensor);
        }

        obsSize = sensor.GetObservationShape().Length;
    }
Exemplo n.º 8
0
 public override void CollectObservations(VectorSensor sensor)
 {
     sensor.AddObservation(this.transform.localPosition);
     sensor.AddObservation(target.transform.localPosition);
     sensor.AddObservation(danger ? 1f: 0f);
 }
Exemplo n.º 9
0
    public override void CollectObservations(VectorSensor sensor)
    {
        sensor.AddObservation(car.position);
        sensor.AddObservation(m_Ball.position);
        sensor.AddObservation(m_Rigidbody.position);
        sensor.AddObservation(goal1.position);
        sensor.AddObservation(goal2.position);
        sensor.AddObservation(CurriuculumRewardZone.position);
        sensor.AddObservation(CurriuculumRewardZone02.position);
        sensor.AddObservation(CurriuculumRewardZone03.position);
        sensor.AddObservation(CurriuculumRewardZone1.position);
        sensor.AddObservation(CurriuculumRewardZone12.position);
        sensor.AddObservation(CurriuculumRewardZone13.position);
        sensor.AddObservation(CurriuculumRewardZone2.position);
        sensor.AddObservation(CurriuculumRewardZone22.position);
        sensor.AddObservation(CurriuculumRewardZone23.position);
        sensor.AddObservation(CurriculumNegativeRewardZone.position);


        //distance to reward zones
        sensor.AddObservation(Vector3.Distance(CurriuculumRewardZone.transform.position, transform.position));
        sensor.AddObservation(Vector3.Distance(CurriuculumRewardZone02.transform.position, transform.position));
        sensor.AddObservation(Vector3.Distance(CurriuculumRewardZone03.transform.position, transform.position));
        sensor.AddObservation(Vector3.Distance(CurriuculumRewardZone1.transform.position, transform.position));
        sensor.AddObservation(Vector3.Distance(CurriuculumRewardZone12.transform.position, transform.position));
        sensor.AddObservation(Vector3.Distance(CurriuculumRewardZone13.transform.position, transform.position));
        sensor.AddObservation(Vector3.Distance(CurriuculumRewardZone2.transform.position, transform.position));
        sensor.AddObservation(Vector3.Distance(CurriuculumRewardZone22.transform.position, transform.position));
        sensor.AddObservation(Vector3.Distance(CurriuculumRewardZone23.transform.position, transform.position));
        //distance to neg zone
        sensor.AddObservation(Vector3.Distance(CurriculumNegativeRewardZone.transform.position, transform.position));

        //direction to reward Zones
        sensor.AddObservation((CurriuculumRewardZone.transform.position - transform.position).normalized);
        sensor.AddObservation((CurriuculumRewardZone02.transform.position - transform.position).normalized);
        sensor.AddObservation((CurriuculumRewardZone03.transform.position - transform.position).normalized);
        sensor.AddObservation((CurriuculumRewardZone1.transform.position - transform.position).normalized);
        sensor.AddObservation((CurriuculumRewardZone12.transform.position - transform.position).normalized);
        sensor.AddObservation((CurriuculumRewardZone13.transform.position - transform.position).normalized);
        sensor.AddObservation((CurriuculumRewardZone2.transform.position - transform.position).normalized);
        sensor.AddObservation((CurriuculumRewardZone22.transform.position - transform.position).normalized);
        sensor.AddObservation((CurriuculumRewardZone23.transform.position - transform.position).normalized);
        //direction to neg zone
        sensor.AddObservation((CurriculumNegativeRewardZone.transform.position - transform.position).normalized);

        //distance to opponent
        sensor.AddObservation(Vector3.Distance(car.transform.position, transform.position));
        //direction to ball
        sensor.AddObservation((car.transform.position - transform.position).normalized);

        //distance to ball
        sensor.AddObservation(Vector3.Distance(m_Ball.transform.position, transform.position));
        //direction to ball
        sensor.AddObservation((m_Ball.transform.position - transform.position).normalized);

        //distance from ball to goal1
        sensor.AddObservation(Vector3.Distance(m_Ball.transform.position, goal1.transform.position));
        //distance from ball to goal2
        sensor.AddObservation(Vector3.Distance(m_Ball.transform.position, goal2.transform.position));

        //distance to Goal1
        sensor.AddObservation(Vector3.Distance(goal1.transform.position, transform.position));
        //direction to Goal1
        sensor.AddObservation((goal1.transform.position - transform.position).normalized);

        //distance to Goal2
        sensor.AddObservation(Vector3.Distance(goal2.transform.position, transform.position));
        //direction to Goal2
        sensor.AddObservation((goal2.transform.position - transform.position).normalized);
    }
Exemplo n.º 10
0
    public override void CollectObservations(VectorSensor sensor)
    {
        if (testingFood.Length != food.Count)
        {
            testingFood = new bool[food.Count];
            for (var _f = 0; _f < testingFood.Length; _f++)
            {
                testingFood[_f] = true;
            }
        }

        float      closest     = Mathf.Infinity;
        Transform  enemySelect = null;
        RaycastHit hit;

        foreach (Transform p in e.allies)
        {
            float d = Vector3.Distance(transform.position, p.position);
            if (d < inSight)
            {
                if (Physics.Raycast(transform.position, (p.position - transform.position), out hit, inSight))
                {
                    if (hit.transform == p)
                    {
                        if (d < closest)
                        {
                            enemySelect = p;
                            closest     = d;
                        }
                    }
                }
            }
        }

        // 3
        if (enemySelect == null)
        {
            sensor.AddObservation(new float[3]);
        }
        else
        {
            sensor.AddObservation(enemySelect);
        }

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

        closest     = Mathf.Infinity;
        enemySelect = null;
        for (var f = 0; f < food.Count; f++)
        {
            float d = Vector3.Distance(transform.position, food[f].position);
            if (d < inSight && testingFood[f])
            {
                if (Physics.Raycast(transform.position, (food[f].position - transform.position), out hit, inSight))
                {
                    if (hit.transform == food[f])
                    {
                        if (d < closest)
                        {
                            enemySelect = food[f];
                            closest     = d;
                        }
                    }
                }
            }
        }

        // 3
        if (enemySelect == null)
        {
            sensor.AddObservation(new float[3]);
        }
        else
        {
            sensor.AddObservation(enemySelect);
        }
    }
 public override void CollectObservations(VectorSensor sensor)
 {
     sensor.AddObservation(ShotAvaliable);
     sensor.AddObservation(isAiming);
 }
Exemplo n.º 12
0
    public override void CollectObservations(VectorSensor sensor)
    {
        _lookedCoin = null;

        var rotAngle = transform.rotation.eulerAngles.y;

        sensor.AddObservation(rotAngle / 360f);
        sensor.AddObservation(_speed);
        sensor.AddObservation(_rotSpeed);
        sensor.AddObservation(CanFire);

        var nearCoin = Spawner.GetNearlyCoin(transform.position);

        if (nearCoin != null)
        {
            //var coinAngle = Vector3.SignedAngle(transform.position, nearCoin.transform.position, Vector3.up);
            //sensor.AddObservation(coinAngle / 360f);
            var p = (transform.position - nearCoin.transform.position) / _rayDistance;
            sensor.AddObservation(p.x);
            sensor.AddObservation(p.z);
        }
        else
        {
            sensor.AddObservation(1f);
            sensor.AddObservation(1f);
        }

        for (int i = 0; i < Rays.Length; i++)
        {
            /*if(i > 0)
             * {
             *      sensor.AddObservation(0.5f);
             *      continue;
             * }*/

            var   d  = _rayDistance;
            Color rc = Color.white;

            var baseDir = Rays[i].normalized;
            var dir     = Quaternion.LookRotation(baseDir) * transform.forward;
            var r       = new Ray(transform.position + RayOffset, dir);
            if (Physics.Raycast(r, out var hitInfo, _rayDistance) && hitInfo.transform != null)
            {
                var idx  = Targets.IndexOf(hitInfo.transform.tag);
                var dist = hitInfo.distance / _rayDistance;
                sensor.AddObservation(dist);

                d = hitInfo.distance;

                if (i == 0)
                {
                    if (hitInfo.transform.CompareTag("Coin"))
                    {
                        _lookedCoin       = hitInfo.transform.GetComponentInParent <CoinAgent>();
                        _lookedCoinDist   = dist;
                        _lookedCoinTimer += Time.fixedDeltaTime;

                        rc = Color.red;
                    }
                    else
                    {
                        _lookedCoinTimer = 0f;
                    }
                }
            }
Exemplo n.º 13
0
 public override void CollectObservations(VectorSensor sensor)
 {
     sensor.AddObservation(gameObject.transform.rotation.z);
 }
Exemplo n.º 14
0
 public override void CollectObservations(VectorSensor sensor)
 {
     sensor.AddObservation(this.transform.position);
     sensor.AddObservation(this.transform.rotation);
     sensor.AddObservation(Target);
 }
 public override void CollectObservations(VectorSensor sensor)
 {
     sensor.AddObservation(rb.velocity / speed);
     sensor.AddObservation((transform.position - puck.position) / 15f);
 }
Exemplo n.º 16
0
 public override void CollectObservations(VectorSensor sensor)
 {
     sensor.AddObservation(rBody.velocity.x);
     sensor.AddObservation(rBody.velocity.z);
 }
Exemplo n.º 17
0
 public override void CollectObservations(VectorSensor sensor)
 {
     //This Method is used to collect Vector Observations of the agnet for the step
     sensor.AddObservation(transform.position.x);
 }
Exemplo n.º 18
0
    public override void CollectObservations(VectorSensor sensor)
    {
        _isPrepared = false;

        // Agent position
        var position = transform.localPosition;

        sensor.AddObservation(position.x);
        sensor.AddObservation(position.z);
        // Agent velocity
        var selfVelocity = _rBody.velocity;

        sensor.AddObservation(selfVelocity.x);
        sensor.AddObservation(selfVelocity.z);
        // Time left for preparation
        if (StepCount > _config.preparingPhaseLength)
        {
            sensor.AddObservation(0.0f);
        }
        else
        {
            sensor.AddObservation(_config.preparingPhaseLength - StepCount);
        }

        foreach (var obj in _viewField.collectVisibleObjects().Where(obj =>
                                                                     obj.CompareTag(hiderTag) || obj.CompareTag(seekerTag) || obj.CompareTag(cubeTag) || obj.CompareTag(rampTag)))
        {
            if (team == Team.Seeker && obj.CompareTag(hiderTag))
            {
                _seenHolder.isAnyHiderSeen = true;
                Debug.DrawLine(transform.position, obj.transform.position, Color.red);
            }

            float[]   observations    = new float[8];
            Rigidbody objectRigidBody = obj.GetComponent <Rigidbody>();
            // Other object position
            var localPosition = obj.transform.localPosition;
            observations[0] = localPosition.x / 10.0f;
            observations[1] = localPosition.z / 10.0f;
            // Other object velocity
            var velocity = objectRigidBody.velocity;
            observations[2] = velocity.x / 100.0f;
            observations[3] = velocity.z / 100.0f;
            // Other object team
            if (obj.CompareTag(hiderTag))
            {
                observations[4] = 1.0f;
            }
            else if (obj.CompareTag(seekerTag))
            {
                observations[5] = 1.0f;
            }
            else if (obj.CompareTag(cubeTag))
            {
                observations[6] = 1.0f;
            }
            else
            {
                observations[7] = 1.0f;
            }
            _bufferSensor.AppendObservation(observations);
        }
    }
Exemplo n.º 19
0
        /// <summary>
        /// This is an ML-agents framework method called when the agent observes its environment.
        /// These observations are passed to the model as inputs.
        /// They make up the agent's STATE (S) at the current step, or time (t).
        /// The model will use them to decide action to perform.
        ///
        /// NOTE: Aside from these manually created observations, we are also using
        ///     some others built-in to ML-agents, such as Ray Perception Sensor,
        ///     and stacking previous observations
        /// </summary>
        /// <param name="sensor">part of ML Agents API</param>
        public override void CollectObservations(VectorSensor sensor)
        {
            if (!target)
            {
                // if no target, we're in a bad state, so just pass data with all 0
                sensor.AddObservation(0f);
                sensor.AddObservation(0f);
                sensor.AddObservation(0f);
                sensor.AddObservation(0f);
                sensor.AddObservation(0f);
                sensor.AddObservation(0f);
                return;
            }

            // Observations used by SimpleTankAgent (you can use whatever you want for your own agents)
            // 1. Relative angle to enemy, where 0 is dead-on, -1 is negative 180, +1 is positive 180
            // 2. Distance to opponent, normalized
            // 3. Relative angle the enemy is pointing, where 0 is dead-on, -1 is negative 180, +1 is positive 180
            // 4. Our current Health, normalized from 0 to 1
            // 5. Enemy current Health, normalized from 0 to 1
            // 6. If gun is cooling down, true/false

            /* 1 */
            // calc relative angle between forward vector and vector from target to self, rotating about Y axis
            float relativeAngle = Vector3.SignedAngle(transform.forward,
                                                      (target.transform.position - transform.position),
                                                      Vector3.up);
            float relativeAngleObs = Mathf.Clamp(relativeAngle / 180f, -1f, 1f);

            sensor.AddObservation(relativeAngleObs);

            // we're adding a reward penalty here for when we're facing away from enemy,
            // hoping agent learns to generally face enemy to move toward them or fire at them
            if (relativeAngleObs < -0.5f || relativeAngleObs > 0.5f)
            {
                AddReward(-0.001f);
            }

            /* 2 */
            // calc distance
            float distance           = Vector3.Distance(target.transform.position, transform.position);
            float normalizedDistance = distance / 70f;  // map hyponteneuse is roughly 70, use for normalizing
            float distanceObs        = Mathf.Clamp(normalizedDistance, 0f, 1.0f);

            sensor.AddObservation(distanceObs);

            /* 3 */
            // calc relative angle of opponent the same way
            float enemyRelativeAngle = Vector3.SignedAngle(target.transform.forward,
                                                           (transform.position - target.transform.position),
                                                           Vector3.up);
            float enemyRelativeAngleObs = Mathf.Clamp(enemyRelativeAngle / 180f, -1f, 1f);

            sensor.AddObservation(enemyRelativeAngleObs);

            /* 4 */
            // our own health, normalized from 0-1
            if (health)
            {
                sensor.AddObservation(health.NormalizedHealth);
            }
            else
            {
                sensor.AddObservation(0f);
            }

            /* 5 */
            // target's health, normalized from 0-1
            if (targetHealth)
            {
                sensor.AddObservation(targetHealth.NormalizedHealth);
            }
            else
            {
                sensor.AddObservation(0f);
            }

            /* 6 */
            // observe whether our gun is cooling down and can't fire
            // this might not be useful, but agent might learn difference between weak/strong shots,
            // and it might want to fire faster/slower depending on situation
            if (shooting)
            {
                sensor.AddObservation(shooting.cooldown);
            }
            else
            {
                sensor.AddObservation(0f);
            }

            AddReward(-0.0001f);  // tiny negative reward over time to incentivize agent to hurry up

            // do some debug outputting here
            if (textOutput)
            {
                textOutput.output  = "<b>Agent" + playerNumber.ToString() + "</b>\n";
                textOutput.output += "<b>Relative Angle: </b>" + relativeAngleObs.ToString() + "\n";
                textOutput.output += "<b>Distance: </b>" + distanceObs.ToString() + "\n";
                textOutput.output += "<b>Enemy Relative Heading: </b>" + enemyRelativeAngleObs.ToString() + "\n";
                textOutput.output += "<b>Health: </b>" + health.NormalizedHealth.ToString() + "\n";
                textOutput.output += "<b>Enemy Health: </b>" + targetHealth.NormalizedHealth.ToString() + "\n";
                textOutput.output += "<b>Cannon Cooldown: </b>" + shooting.cooldown.ToString() + "\n";
            }

            Unity.MLAgents.Monitor.Log(gameObject.name, GetCumulativeReward());
        }
Exemplo n.º 20
0
    /// <summary>
    /// Collect vecotr observations from the environment
    /// </summary>
    /// <param name="sensor">The vector sensor</param>
    public override void CollectObservations(VectorSensor sensor)
    { // 여기서 수집한 observation vector는 다음 action값을 수집할 때 다음 input값으로 선택된다.
        //Debug.Log(" UserExist  " + mUserExist);
        // Debug.Log("In CollectObservations ");
        sensor.AddObservation(mUserExist);//1

        if (humanHandAvatar != null && humanHandAvatar.activeSelf == true)
        {
            Vector3 toHand = humanHandAvatar.transform.position - this.gameObject.transform.position;

            Vector3 targetDir = (toHand).normalized;
            float   dot       = Vector3.Dot(transform.forward, targetDir);

            //내적을 이용한 각 계산하기
            // thetha = cos^-1( a dot b / |a||b|)
            float AngleBetweenDegree = Mathf.Acos(dot) * Mathf.Rad2Deg;
            //Debug.Log("1  :   "+ AngleBetweenDegree + "  "+ map(AngleBetweenDegree,0,180,-1,1));
            float angle = 1 + (-1 + 1) * ((AngleBetweenDegree - 0) / (180 - 0));
            if (float.IsNaN(angle))
            {
                sensor.AddObservation(new float[1]); //1
            }
            else
            {
                sensor.AddObservation(angle);
            }

            sensor.AddObservation(this.gameObject.transform.localRotation.normalized); //4
            // pointing to nearest flower 3
            sensor.AddObservation(toHand.normalized);                                  // 3
            // dot product observation - beak tip in front of flower?
            // +1 -> infront, -1 -> behind 1
            sensor.AddObservation(toHand.magnitude / FlowerArea.AreaDiameter);//1
        }
        else
        {
            sensor.AddObservation(new float[9]);
        }



        if (nearestFlower != null)
        {
            // Observe the local rotation  4
            sensor.AddObservation(this.gameObject.transform.localRotation.normalized);
            Vector3 toFlower  = nearestFlower.FlowerCenterVector - beakTip.position;
            Vector3 targetDir = (toFlower).normalized;
            float   dot       = Vector3.Dot(transform.forward, targetDir);

            //내적을 이용한 각 계산하기
            // thetha = cos^-1( a dot b / |a||b|)
            float AngleBetweenDegree = Mathf.Acos(dot) * Mathf.Rad2Deg;
            // pointing to nearest flower  3
            sensor.AddObservation(toFlower.normalized);
            //// dot product observation - beak tip in front of flower?
            //// +1 -> infront, -1 -> behind  1
            //sensor.AddObservation(
            //    Vector3.Dot(toFlower.normalized, -nearestFlower.FlowerUpVector.normalized));
            //// beak tip point to flower  1
            //sensor.AddObservation(
            //    Vector3.Dot(beakTip.forward.normalized, -nearestFlower.FlowerUpVector.normalized));
            float angle = 1 + (-1 + 1) * ((AngleBetweenDegree - 0) / (180 - 0));
            if (float.IsNaN(angle))
            {
                sensor.AddObservation(new float[1]); //1
            }
            else
            {
                sensor.AddObservation(angle);
            }
            // relative distance from beek tip to flower  1
            sensor.AddObservation(toFlower.magnitude / FlowerArea.AreaDiameter);//1
            // 10 total observations
        }
        else
        {
            sensor.AddObservation(new float[9]);
        }
    }// End public override void CollectObservations(VectorSensor sensor)
Exemplo n.º 21
0
 public override void CollectObservations(VectorSensor sensor)
 {
 }
Exemplo n.º 22
0
 public override void CollectObservations(VectorSensor sensor)
 {
     ManagedUpdate();
     base.CollectObservations(sensor);
 }
Exemplo n.º 23
0
    public override void CollectObservations(VectorSensor sensor)
    {
        //if (VisionOnly)
        //{

        /*
         * // No observations, just the camera as input
         *
         * // Basic feature set (32+6=38 floats)
         *
         * // Ball coordinates
         * AddVectorObs(NormalizePositionToPlayArea(tm.BallsGo[0].transform.position));
         *
         * // Ball velocity
         * AddVectorObs(NormalizeVelocity(tm.BallsGo[0].GetComponent<Rigidbody>().velocity * 10.0f));
         *
         * //Debug.Log( NormalizeVelocity(tm.BallsGo[0].GetComponent<Rigidbody>().velocity * 10.0f));
         *
         * // Current player rods
         * //  x -> direction of play
         * //  y -> vertical
         * //  z -> direction of rods
         * foreach (GameObject rod in rods)
         * {
         *  // Position
         *  AddVectorObs(NormalizePositionToPlayArea(rod.GetComponent<Rigidbody>().transform.position).z * 3.0f);
         *  //if( player == 0 && rod == rods[0])
         *  //    Debug.Log(NormalizePositionToPlayArea(rod.GetComponent<Rigidbody>().transform.position));
         *  AddVectorObs(NormalizeVelocity(rod.GetComponent<Rigidbody>().velocity).z * 5.0f);
         *
         *
         *  // Rod Angle
         *  float angle = Vector3.SignedAngle(rod.transform.up, new Vector3(0.0f, 1.0f, 0.0f), rod.transform.forward);
         *  AddVectorObs(NormalizeAngle(angle));
         *  //if (player == 0 && rod == rods[0])
         *  //    Debug.Log( NormalizeAngle(angle) );
         *
         *  AddVectorObs(NormalizeAngularVelocity(rod.GetComponent<Rigidbody>().angularVelocity, 5.0f).z);
         *  //if (player == 0 && rod == rods[0])
         *  //    Debug.Log(NormalizeAngularVelocity(rod.GetComponent<Rigidbody>().angularVelocity, 5.0f).z);
         * }
         *
         * // Opponent player rods
         * foreach (GameObject rod in opponent.rods)
         * {
         *  // Position
         *  AddVectorObs(NormalizePositionToPlayArea(rod.GetComponent<Rigidbody>().transform.position).z * 3.0f);
         *  AddVectorObs(NormalizeVelocity(rod.GetComponent<Rigidbody>().velocity).z * 5.0f);
         *
         *  // Rod Angle
         *  float angle = Vector3.SignedAngle(rod.transform.up, new Vector3(0.0f, 1.0f, 0.0f), rod.transform.forward);
         *  AddVectorObs(NormalizeAngle(angle));
         *  AddVectorObs(NormalizeAngularVelocity(rod.GetComponent<Rigidbody>().angularVelocity, 5.0f).z);
         * }
         *
         * // Nearest ball position with respect to each current player physical rod players
         * foreach (GameObject player in players)
         * {
         *  Vector2 RelPosition2d_min = Vector2.positiveInfinity;
         *
         *  foreach (GameObject ball in tm.Balls.Keys)
         *  {
         *      Vector3 RelPosition = ball.transform.position - player.transform.position;
         *      Vector2 RelPosition2d = new Vector2(
         *              RelPosition.x / play_area.size.x,
         *              RelPosition.z / play_area.size.z
         *          );
         *
         *      if (RelPosition2d.magnitude < RelPosition2d_min.magnitude)
         *          RelPosition2d_min = RelPosition2d;
         *  }
         *
         *  // Add the player position to ball location vector
         *  AddVectorObs(NormalizeLogistic(RelPosition2d_min * 10.0f)); // 10x resolution scaling so it gets detailed when the ball is close to each player on each axis
         * }
         */
        //}
        //else
        //{
        // Features:
        //  2d map of where the ball(s) are
        //  2d map of where your player are
        //  2d map of where opponent players are
        //  float[8] position of each rod
        //  float[8] angular velocity of each rod
        //  float[8] velocity of each rod

        // To form the 2d maps, this outputs an array of Vector2 for each object. The python code is expected to build this into the map.
        // Build the 2d map of where the ball is

        //}

        /*
         * else
         * {
         *  // Add both player rod positions, angles, speeds, and ball position as features
         *
         *  // Ball coordinates
         *  //AddVectorObs(NormalizePositionToPlayArea(tm.ball.transform.position));
         *  //Debug.Log(NormalizePositionToPlayArea(tm.ball.transform.position));
         *
         *  // Ball velocity
         *  //AddVectorObs(NormalizeVelocity(tm.ball.GetComponent<Rigidbody>().velocity * 10.0f));
         *  //Debug.Log( NormalizeVelocity(tm.ball.GetComponent<Rigidbody>().velocity * 10.0f));
         *
         *  // Current player rods
         *  //  x -> direction of play
         *  //  y -> vertical
         *  //  z -> direction of rods
         *  foreach (GameObject rod in rods)
         *  {
         *      // Position
         *      AddVectorObs(NormalizePositionToPlayArea(rod.GetComponent<Rigidbody>().transform.position).z * 3.0f);
         *      //if( player == 0 && rod == rods[0])
         *      //    Debug.Log(NormalizePositionToPlayArea(rod.GetComponent<Rigidbody>().transform.position));
         *      AddVectorObs(NormalizeVelocity(rod.GetComponent<Rigidbody>().velocity).z * 5.0f);
         *
         *
         *      // Rod Angle
         *      float angle = Vector3.SignedAngle(rod.transform.up, new Vector3(0.0f, 1.0f, 0.0f), rod.transform.forward);
         *      AddVectorObs(NormalizeAngle(angle));
         *      //if (player == 0 && rod == rods[0])
         *      //    Debug.Log( NormalizeAngle(angle) );
         *
         *      AddVectorObs(NormalizeAngularVelocity(rod.GetComponent<Rigidbody>().angularVelocity, 5.0f).z);
         *      //if (player == 0 && rod == rods[0])
         *      //    Debug.Log(NormalizeAngularVelocity(rod.GetComponent<Rigidbody>().angularVelocity, 5.0f).z);
         *  }
         *
         *  // Opponent player rods
         *  foreach (GameObject rod in opponent.rods)
         *  {
         *      // Position
         *      AddVectorObs(NormalizePositionToPlayArea(rod.GetComponent<Rigidbody>().transform.position).z * 3.0f);
         *      AddVectorObs(NormalizeVelocity(rod.GetComponent<Rigidbody>().velocity).z * 5.0f);
         *
         *      // Rod Angle
         *      float angle = Vector3.SignedAngle(rod.transform.up, new Vector3(0.0f, 1.0f, 0.0f), rod.transform.forward);
         *      AddVectorObs(NormalizeAngle(angle));
         *      AddVectorObs(NormalizeAngularVelocity(rod.GetComponent<Rigidbody>().angularVelocity, 5.0f).z);
         *  }
         *
         *  // Nearest ball position with respect to each current player physical rod players
         *  foreach (GameObject player in players)
         *  {
         *      Vector2 RelPosition2d_min = Vector2.positiveInfinity;
         *
         *      foreach(GameObject ball in tm.Balls.Keys)
         *      {
         *          Vector3 RelPosition = ball.transform.position - player.transform.position;
         *          Vector2 RelPosition2d = new Vector2(
         *                  RelPosition.x / play_area.size.x,
         *                  RelPosition.z / play_area.size.z
         *              );
         *
         *          if (RelPosition2d.magnitude < RelPosition2d_min.magnitude)
         *              RelPosition2d_min = RelPosition2d;
         *      }
         *
         *      // Add the player position to ball location vector
         *      AddVectorObs(NormalizeLogistic(RelPosition2d_min * 10.0f)); // 10x resolution scaling so it gets detailed when the ball is close to each player on each axis
         *  }
         *
         *  // Total:
         *  // 3*3 + 4 * 4 + 4 * 4 = 41 float observations
         * }
         */
    }
Exemplo n.º 24
0
    public override void CollectObservations(VectorSensor sensor)
    {
        float x, y, z, min_z;

        // Adding the z-depth of the object that is closest to the camera, if any is present.
        // min_z = 99.0f;
        // // target_in_camera_coords = agent_camera.WorldToViewportPoint(this.Target.position);
        // // if(is_on_camera(target_in_camera_coords)){ // Inside X and Y
        // //     min_z = target_in_camera_coords.z;
        // // }
        // z = -1.0f;
        // y = -1.0f;
        // x = -1.0f;
        // target_in_camera_coords = agent_camera.WorldToViewportPoint(this.Obstacle.position);
        // if(is_on_camera(target_in_camera_coords)){ // Inside X and Y
        //     if(target_in_camera_coords.z < min_z){
        //         min_z = target_in_camera_coords.z;
        //         x = target_in_camera_coords.x;
        //         y = target_in_camera_coords.y;
        //     }
        // }
        // // target_in_camera_coords = agent_camera.WorldToViewportPoint(ac.Base.position);
        // // if(is_on_camera(target_in_camera_coords)){ // Inside X and Y
        // //     if(target_in_camera_coords.z < min_z){
        // //         min_z = target_in_camera_coords.z;
        // //         x = target_in_camera_coords.x;
        // //         y = target_in_camera_coords.y;
        // //     }
        // // }
        // // for(int i = 0; i < 5; i++){
        // //     target_in_camera_coords = agent_camera.WorldToViewportPoint(ac.GetJointPos(i, true));
        // //     if(is_on_camera(target_in_camera_coords)){ // Inside X and Y
        // //         if(target_in_camera_coords.z < min_z){
        // //             min_z = target_in_camera_coords.z;
        // //             x = target_in_camera_coords.x;
        // //             y = target_in_camera_coords.y;
        // //         }
        // //     }
        // // }
        // if(min_z != 99.0f)
        //     z = min_z;
        // else{
        //     z = 10.0f;
        //     y = 0.0f;
        //     x = 0.0f;
        // }
        sensor.AddObservation(Obstacle.localPosition.x);
        sensor.AddObservation(Obstacle.localPosition.y - 1);
        sensor.AddObservation(Obstacle.localPosition.z);
        // Debug.Log("X: " + x + ", Y: " + y + ", Z:" + z);
        // sensor.AddObservation(x);
        // sensor.AddObservation(y);
        // sensor.AddObservation(z);

        // Goal info (either spawn sphere or target)
        // sensor.AddObservation(TargetSpawnSphere.localPosition.x);
        // sensor.AddObservation(TargetSpawnSphere.localPosition.y);
        // sensor.AddObservation(TargetSpawnSphere.localPosition.z);
        sensor.AddObservation(Target.localPosition.x);
        sensor.AddObservation(Target.localPosition.y - 1);
        sensor.AddObservation(Target.localPosition.z);

        // Planner info
        // Debug.Log(transform.localPosition);
        sensor.AddObservation(transform.localPosition.x);
        sensor.AddObservation(transform.localPosition.y - 1);
        sensor.AddObservation(transform.localPosition.z);
        sensor.AddObservation(transform.localRotation);

        // Obstacle info (remove if using vision)
        // sensor.AddObservation(Obstacle.localPosition);


        // NO ARM TEST
        if (this.ac != null)
        {
            // Arm info:
            sensor.AddObservation(this.ac.JointAngles[0] / 180.0f);
            sensor.AddObservation(this.ac.JointAngles[1] / 180.0f);
            sensor.AddObservation(this.ac.JointAngles[3] / 180.0f);
            sensor.AddObservation(this.ac.JointAngles[4] / 180.0f);
            sensor.AddObservation(this.ac.JointAngles[5] / 180.0f);
            sensor.AddObservation(this.ac.JointAngles[6] / 180.0f);

            // sensor.AddObservation(ac.GetJointPos(3).x);
            // sensor.AddObservation(ac.GetJointPos(3).y-1);
            // sensor.AddObservation(ac.GetJointPos(3).z);

            // sensor.AddObservation(ac.GetJointPos(5).x);
            // sensor.AddObservation(ac.GetJointPos(5).y-1);
            // sensor.AddObservation(ac.GetJointPos(5).z);

            sensor.AddObservation(ac.GetJointPos(6).x);
            sensor.AddObservation(ac.GetJointPos(6).y - 1);
            sensor.AddObservation(ac.GetJointPos(6).z);

            sensor.AddObservation(ac.GetJointRot(6, true));
        }
    }
Exemplo n.º 25
0
 protected override void AddState(VectorSensor _sensor)
 {
     // Add State
     _sensor.AddObservation(m_Controller.State.Head);
     _sensor.AddObservation(m_Controller.State.Food);
 }
Exemplo n.º 26
0
 public override void CollectObservations(VectorSensor sensor)
 {
     collectObservationsCalls += 1;
     collectObservationsCallsSinceLastReset += 1;
     sensor.AddObservation(0f);
 }
Exemplo n.º 27
0
 public override void CollectObservations(VectorSensor sensor)
 {
     sensor.AddObservation(transform.position);
     sensor.AddObservation(transform.rotation);
     sensor.AddObservation(lightOn);
 }
Exemplo n.º 28
0
 public override void CollectObservations(VectorSensor sensor)
 {
     collectObservationsCalls           += 1;
     collectObservationsCallsForEpisode += 1;
     sensor.AddObservation(collectObservationsCallsForEpisode);
 }
Exemplo n.º 29
0
 public override void CollectObservations(VectorSensor sensor)
 {
     sensor.AddObservation(timer);
     sensor.AddObservation(currentHealth);
 }
Exemplo n.º 30
0
 public override void CollectObservations(VectorSensor sensor)
 {
     // Camera sensor specified with GUI
 }