Exemplo n.º 1
0
    public void MoveAgent(ActionBuffers actionBuffers)
    {
        var dirToGo   = Vector3.zero;
        var rotateDir = Vector3.zero;

        var continuousActions = actionBuffers.ContinuousActions;
        var discreteActions   = actionBuffers.DiscreteActions;

        var forward = Mathf.Clamp(continuousActions[0], -1f, 1f);
        var right   = Mathf.Clamp(continuousActions[1], -1f, 1f);
        var rotate  = Mathf.Clamp(continuousActions[2], -1f, 1f);

        dirToGo   = transform.forward * forward;
        dirToGo  += transform.right * right;
        rotateDir = -transform.up * rotate;

        m_AgentRb.AddForce(dirToGo * moveSpeed, ForceMode.VelocityChange);
        transform.Rotate(rotateDir, Time.fixedDeltaTime * turnSpeed);

        if (m_AgentRb.velocity.sqrMagnitude > 25f) // slow it down
        {
            m_AgentRb.velocity *= 0.95f;
        }
    }
Exemplo n.º 2
0
        public void TestDiscreteApply()
        {
            var actionSpec = ActionSpec.MakeDiscrete(3, 2);

            var applier  = new DiscreteActionOutputApplier(actionSpec, 2020, null);
            var agentIds = new List <int> {
                42, 1337
            };
            var actionBuffers = new Dictionary <int, ActionBuffers>();

            actionBuffers[42]   = new ActionBuffers(actionSpec);
            actionBuffers[1337] = new ActionBuffers(actionSpec);

            var actionTensor = new TensorProxy
            {
                data = new Tensor(
                    2,
                    2,
                    new[]
                {
                    2.0f,     // Agent 0, branch 0
                    1.0f,     // Agent 0, branch 1
                    0.0f,     // Agent 1, branch 0
                    0.0f      // Agent 1, branch 1
                }),
                shape     = new long[] { 2, 2 },
                valueType = TensorProxy.TensorType.FloatingPoint
            };

            applier.Apply(actionTensor, agentIds, actionBuffers);
            Assert.AreEqual(2, actionBuffers[42].DiscreteActions[0]);
            Assert.AreEqual(1, actionBuffers[42].DiscreteActions[1]);

            Assert.AreEqual(0, actionBuffers[1337].DiscreteActions[0]);
            Assert.AreEqual(0, actionBuffers[1337].DiscreteActions[1]);
        }
Exemplo n.º 3
0
        public override void OnActionReceived(ActionBuffers actionBuffers)
        {
            var actions = actionBuffers.ContinuousActions.Array;
            var step    = m_Requester.ActionStepCount % m_Requester.DecisionInterval;

            m_IsPreDecisionStep = step == 0;

            if (m_IsPreDecisionStep)
            {
                Array.Copy(actions, 0, m_PrevActions, 0, actions.Length);
            }
            else
            {
                float t = step / (float)m_Requester.DecisionInterval;
                for (int i = 0; i < actions.Length; i++)
                {
                    actions[i] = Mathf.Lerp(m_PrevActions[i], actions[i], t);
                }
            }

            m_Walker.NormTargetSpeed     = actions[0];
            m_Walker.NormTargetWalkAngle = actions[1];
            m_Walker.NormTargetLookAngle = actions[2];
        }
Exemplo n.º 4
0
    public override void OnActionReceived(ActionBuffers actionBuffers)
    {
        // The dictionary with all the body parts in it are in the jdController
        var bpDict = m_JdController.bodyPartsDict;

        var i = -1;
        var continuousActions = actionBuffers.ContinuousActions;

        // Pick a new target joint rotation
        bpDict[bodySegment0].SetJointTargetRotation(continuousActions[++i], continuousActions[++i], 0);
        bpDict[bodySegment1].SetJointTargetRotation(continuousActions[++i], continuousActions[++i], 0);
        bpDict[bodySegment2].SetJointTargetRotation(continuousActions[++i], continuousActions[++i], 0);

        // Update joint strength
        bpDict[bodySegment0].SetJointStrength(continuousActions[++i]);
        bpDict[bodySegment1].SetJointStrength(continuousActions[++i]);
        bpDict[bodySegment2].SetJointStrength(continuousActions[++i]);

        //Reset if Worm fell through floor;
        if (bodySegment0.position.y < m_StartingPos.y - 2)
        {
            EndEpisode();
        }
    }
Exemplo n.º 5
0
    /// <summary>
    /// Called when an action os received from either the player input or neural network
    /// actions.ContinuousActions[i] represents:
    /// Index 0: move vector x (+1 = right, -1 = left)
    /// Index 1: move vector y (+1 = up, -1 = down)
    /// Index 2: move vector z (+1 = forward, -1 = backward)
    /// Index 3: pitch angle (+1 = pitch up, -1 = pitch down)
    /// Index 4: yaw angele (+1 = turn right, -1 = turn left)
    /// </summary>
    /// <returns></returns>
    public override void OnActionReceived(ActionBuffers actions)
    {
        //base.OnActionReceived(actions);
        if (frozen)
        {
            return;
        }

        var continuousActions = actions.ContinuousActions;
        //var discreteActions = actions.DiscreteActions;

        Vector3 move = new Vector3(continuousActions[0], continuousActions[1], continuousActions[2]);

        //Vector3 rotate = new Vector3(actions.DiscreteActions[3], actions.DiscreteActions[4], 0f);
        _rigidbody.AddForce(move * moveForce);
        //_rigidbody.AddTorque(torque * );

        Vector3 rotationVector = transform.rotation.eulerAngles;
        float   pitchChange    = continuousActions[3];
        float   yawChange      = continuousActions[4];

        _smoothPitchChange = Mathf.MoveTowards(_smoothPitchChange, pitchChange, 2f * Time.fixedDeltaTime);
        _smoothYawChange   = Mathf.MoveTowards(_smoothYawChange, yawChange, 2f * Time.fixedDeltaTime);

        float pitch = rotationVector.x + _smoothPitchChange * Time.fixedDeltaTime * pitchSpeed;

        if (pitch > 180f)
        {
            pitch -= 360;
        }
        pitch = Mathf.Clamp(pitch, -MaxPitchAngle, MaxPitchAngle);

        float yaw = rotationVector.y + _smoothYawChange * Time.fixedDeltaTime * yawSpeed;

        transform.rotation = Quaternion.Euler(pitch, yaw, 0f);
    }
Exemplo n.º 6
0
    public override void OnActionReceived(ActionBuffers actionBuffers)

    {
        MoveAgent(actionBuffers.DiscreteActions);
    }
 public override void OnActionReceived(ActionBuffers buffers)
 {
     agentActionCalls           += 1;
     agentActionCallsForEpisode += 1;
     AddReward(0.1f);
 }
Exemplo n.º 8
0
 public override void OnActionReceived(ActionBuffers actionBuffers)
 {
     ActionHandler(actionBuffers.DiscreteActions);
 }
 public void OnActionReceived(ActionBuffers actionBuffers)
 {
     LastActionBuffers = actionBuffers;
 }
Exemplo n.º 10
0
    public override void OnActionReceived(ActionBuffers actionBuffers)
    {
        //// Actions, size = 2
        //Vector3 controlSignal = Vector3.zero;
        //controlSignal.x = actionBuffers.ContinuousActions[0];
        //controlSignal.z = actionBuffers.ContinuousActions[1];
        //WheelDrive.angle= controlSignal.x;
        //WheelDrive.torque = controlSignal.z;

        //// Rewards
        //float distanceToTarget = Vector3.Distance(this.transform.localPosition, Target.localPosition);

        //var cubeForward = Target.transform.forward;

        //var lookAtTargetReward = (Vector3.Dot(cubeForward, this.transform.forward) + 1) * .5F;
        ////Debug.Log(lookAtTargetReward);

        //AddReward(lookAtTargetReward/10);

        //AddReward((distanceToTargetprev- distanceToTarget)/ distanceToTarget);
        ////Debug.Log((distanceToTargetprev - distanceToTarget) / distanceToTarget);
        //distanceToTargetprev = Vector3.Distance(this.transform.localPosition, Target.localPosition);
        ////Debug.Log(lookAtTargetReward * ((limit - distanceToTarget) / limit));
        //// Reached target


        //if (distanceToTarget < 1.42f)
        //{
        //    SetReward(1.0f);
        //    EndEpisode();
        //}

        //// Fell off platform
        //else if (Mathf.Abs(this.transform.localPosition.x) > limit  || Mathf.Abs(this.transform.localPosition.z) > limit || this.transform.localPosition.y < 0  )
        //{
        //    SetReward(-1f);
        //    EndEpisode();
        //}
        //AddReward(-0.1f);

        // Actions, size = 2
        Vector3 controlSignal = Vector3.zero;

        controlSignal.x   = actionBuffers.ContinuousActions[0];
        controlSignal.z   = actionBuffers.ContinuousActions[1];
        WheelDrive.angle  = controlSignal.x;
        WheelDrive.torque = controlSignal.z;

        // Rewards
        float distanceToTarget = Vector3.Distance(this.transform.localPosition, Target.localPosition);

        var cubeForward = Target.transform.forward;

        var lookAtTargetReward = (Vector3.Dot(cubeForward, this.transform.forward) + 1) * .5F;

        //Debug.Log(lookAtTargetReward);

        AddReward(lookAtTargetReward / 10);

        AddReward((distanceToTargetprev - distanceToTarget) / distanceToTarget);
        Debug.Log((distanceToTargetprev - distanceToTarget) / distanceToTarget);
        distanceToTargetprev = Vector3.Distance(this.transform.localPosition, Target.localPosition);
        //Debug.Log(lookAtTargetReward * ((limit - distanceToTarget) / limit));
        // Reached target


        if (distanceToTarget < 1.42f)
        {
            SetReward(1.0f);
            EndEpisode();
        }

        // Fell off platform
        else if (Mathf.Abs(this.transform.localPosition.x) > limit || Mathf.Abs(this.transform.localPosition.z) > limit || this.transform.localPosition.y < 0)
        {
            SetReward(-1f);
            EndEpisode();
        }
        AddReward(-0.1f);
    }
Exemplo n.º 11
0
 /// <summary>
 /// Called every step of the engine. Here the agent takes an action.
 /// </summary>
 public override void OnActionReceived(ActionBuffers actionBuffers)
 {
     // Penalty given each step to encourage agent to finish task quickly.
     AddReward(-1f / MaxStep);
 }
Exemplo n.º 12
0
    public override void OnActionReceived(ActionBuffers actions)
    {
        // handle movement
        var discreteActionsOut = actions.DiscreteActions;

        MoveAgent(discreteActionsOut[0]);
        SteerAgent(discreteActionsOut[1]);

        // mete out rewards and punishment!!

        // make sure all 4 wheels are on the road!


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

        AddReward((0.0001f * velocityAlignment) / curriculum);

        // Die if you fall off
        if (transform.localPosition.y < -.5f)
        {
            AddReward(-.5f);
            if (useRoadBuilder)
            {
                roadbuilder.DestroyRoad();
            }
            EndEpisode();
        }

        if (rBody.velocity.magnitude <= .4f)
        {
            if (!isSlow)
            {
                slowTime = time;
            }
            isSlow = true;
            if (time - slowTime > 20f)
            {
                AddReward(-1f);
                if (useRoadBuilder)
                {
                    roadbuilder.DestroyRoad();
                }
                EndEpisode();
            }
        }

        // Add minute remward that increases as speed increases
        if (rBody.velocity.magnitude > .4f)
        {
            isSlow = false;
            AddReward((.0001f * rBody.velocity.magnitude) / curriculum);
        }


        float distanceToTarget = Vector3.Distance(this.transform.position, targetPoint.position);

        //Debug.Log(distanceToTarget);

        // add rewards if a target point is reached
        if (distanceToTarget < 2f)
        {
            AddReward(1.5f);

            // increment target point, end episode if at end
            //if (incrementTargetPoint() == null)

            if (useRoadBuilder)
            {
                roadbuilder.DestroyRoad();
                //roadbuilder.RoadLength++;
            }
            EndEpisode();
        }
    }
Exemplo n.º 13
0
    public override void OnActionReceived(ActionBuffers actions)
    {
        if (team == Team.Seeker && StepCount <= _config.preparingPhaseLength)
        {
            return;
        }

        Vector3 controlSignal = Vector3.zero;

        controlSignal.x = actions.ContinuousActions[0];
        controlSignal.z = actions.ContinuousActions[1];

        _rBody.AddForce(controlSignal * _config.agentsForceMultiplier);
        _rBody.AddTorque(transform.up * _config.agentsRotationMultiplier * actions.ContinuousActions[2]);

        Vector3 lPos = transform.localPosition;

        if (lPos.x < -10.0 || lPos.x > 10.0 || lPos.z < -10.0 || lPos.z > 10.0)
        {
            AddReward(_config.penaltyForLeaving * _config.rewardScale);
        }

        /* Discrete actions mapping:
         *   0 -- no action
         *   1 -- drop box
         *   2 -- grab box
         *   3 -- unlock box
         *   4 -- lock box
         */
        int action = actions.DiscreteActions[0];

        if (_carryingMovable != null && action == 1)
        {
            _carryingMovable.Drop();
            _carryingMovable = null;
            Destroy(_fJoint);
            _fJoint = null;
        }
        else if (_carryingMovable == null)
        {
            GameObject nearestCube   = null;
            float      minDistToCube = Single.MaxValue;

            foreach (GameObject obj in _viewField.collectVisibleObjects())
            {
                if (!obj.CompareTag(cubeTag) && !obj.CompareTag(rampTag))
                {
                    continue;
                }

                float dist = Vector3.Distance(transform.localPosition, obj.transform.localPosition);
                if (dist < minDistToCube)
                {
                    nearestCube   = obj;
                    minDistToCube = dist;
                }
            }

            // Check if agent see box nearby
            if (nearestCube == null || _config.maxDistToInteractWithBox < minDistToCube)
            {
                return;
            }

            MovableScript movableScript = nearestCube.GetComponent <MovableScript>();

            switch (action)
            {
            case 2:
            {
                if (!movableScript.Grab(_rBody))
                {
                    return;
                }
                _carryingMovable      = movableScript;
                _fJoint               = gameObject.AddComponent <FixedJoint>();
                _fJoint.connectedBody = nearestCube.GetComponent <Rigidbody>();
                break;
            }

            case 3:
            {
                movableScript.Unlock(team);
                break;
            }

            case 4:
            {
                movableScript.Lock(team);
                break;
            }

            default:
                return;
            }
        }
    }
Exemplo n.º 14
0
    public override void OnActionReceived(ActionBuffers actionBuffers)
    {
        var bFire = 2f * Mathf.Clamp(actionBuffers.ContinuousActions[0], -1f, 1f);
        //   var bHyper = 2f * Mathf.Clamp(actionBuffers.ContinuousActions[1], -1f, 1f);
        var   bRocket  = 2f * Mathf.Clamp(actionBuffers.ContinuousActions[1], -1f, 1f);
        var   bTurn    = 2f * Mathf.Clamp(actionBuffers.ContinuousActions[2], -1f, 1f);
        float velocity = Mathf.Clamp(rb.velocity.magnitude, 0f, moveMax * transform.lossyScale.y);

        if (rb.velocity.magnitude > moveMax)
        {
            //Debug.Log("constrain velocity");
            rb.velocity = rb.velocity.normalized * moveMax;
        }
        int aMove = 0, aTurn = 0;

        if (bFire > 0)
        {
            Fire();
        }

        /*  if (bHyper>0)
         * {
         *    HyperspaceJump();
         * }*/
        if (bRocket > 0)
        {
            if (engine)
            {
                engine.SetActive(true);
            }
            aMove = 1;
        }
        else
        {
            if (engine)
            {
                engine.SetActive(false);
            }
            aMove = 0;
        }
        if (bTurn < -0.3)
        {
            aTurn = -1;
        }
        else if (bTurn > 0.3)
        {
            aTurn = 1;
        }
        Vector3 aVel = rb.velocity;

        aVel.y      = 0f;
        rb.velocity = aVel;
        Vector3 bRot = rb.transform.localRotation.eulerAngles;

        bRot.x = 0; bRot.z = 0;
        rb.transform.localRotation = Quaternion.Euler(bRot);


        ProcessTurn(aTurn, aMove);
        SetReward(myReward);
        myReward = 0f;
        if (killPlayer)
        {
            // ExplosionInfo info = new ExplosionInfo();
            //   info.origin = transform.position;
            //   info.strength = 1.0f;
            //gc.SendMessage("MakeExplosion", info);
            EndEpisode();
            Debug.Log("End episode");

            SetResetParameters();
            // GameObject.Find("GameController").SendMessage("KillPlayer");
        }
    }
    public override void OnActionReceived(ActionBuffers actionBuffers)
    {
        var vectorAction = actionBuffers.DiscreteActions;

        //set the list of action of the robot and for each decision how he has to do

        /*
         *
         * vectorActions:
         * vectorAction[0] : move forward,behind or not
         * vectorAction[1] : rotation
         *
         *
         */


        //forward

        if (vectorAction[0] == 1)
        {
            movez = 1f;
            movex = 0;
        }

        //backward
        else if (vectorAction[0] == 2)
        {
            movez = -1f;
            movex = 0;
        }

        //stopping
        else if (vectorAction[0] == 0)
        {
            movez = 0f;
            movex = 0;
        }


        //rotation

        //right
        else if (vectorAction[0] == 3)
        {
            movex = 3f;
            movez = 0;
        }
        //left
        else if (vectorAction[0] == 4)
        {
            movex = -3f;
            movez = 0;
        }



        Vector3 v = (transform.forward * movez);

        this.transform.Rotate(0, movex, 0);
        rb.AddForce(v * speed);

        //set a negative reward for wasting time.
        //this is for first run configuration
        // AddReward(-1f / MaxStep);
    }
Exemplo n.º 16
0
 void InterpretDiscreteActions(ActionBuffers actions)
 {
     m_Steering     = actions.DiscreteActions[0] - 1f;
     m_Acceleration = actions.DiscreteActions[1] >= 1.0f;
     m_Brake        = actions.DiscreteActions[1] < 1.0f;
 }
Exemplo n.º 17
0
 public override void OnActionReceived(ActionBuffers actionBuffers)
 {
     AddReward(0.01f);
     MoveAgent(actionBuffers.DiscreteActions);
 }
Exemplo n.º 18
0
    /// <summary>
    /// Called when an action is received from either the player input or the neural network
    /// </summary>
    /// vectorAction[i] represents:
    /// Index 0: move vector z (+1 = right -1 = left)
    /// Index 1: yaw angle z (+1 = right -1 = left)
    /// Index 1: pitch angle z (+1 = right -1 = left)
    /// <param name="actionBuffers"> The actions to take </param>
    public override void OnActionReceived(ActionBuffers actionBuffers)
    {
        ActionSegment <int> act  = actionBuffers.DiscreteActions;
        var dirToGo              = Vector3.zero;
        var rotateDir            = Vector3.zero;
        var rotateMonitor        = 0f;
        var dirToGoForwardAction = act[0];
        var rotateDirAction      = act[1];
        var rotateMonitorAction  = act[2];

        if (dirToGoForwardAction == 1)
        {
            dirToGo = 1f * transform.forward;
        }
        else if (dirToGoForwardAction == 2)
        {
            dirToGo = -1f * transform.forward;
        }
        if (rotateDirAction == 1)
        {
            rotateDir = transform.up * -1f;
        }
        else if (rotateDirAction == 2)
        {
            rotateDir = transform.up * 1f;
        }
        if (rotateMonitorAction == 1)
        {
            rotateMonitor = -1f;
        }
        else if (rotateMonitorAction == 2)
        {
            rotateMonitor = 1f;
        }

        rigidbody.AddForce(dirToGo * moveForce, ForceMode.VelocityChange);
        transform.Rotate(rotateDir, Time.fixedDeltaTime * yawSpeed);

        monitorLocalAngle += rotateMonitor * pitchSpeed * Time.fixedDeltaTime;
        monitorLocalAngle  = Mathf.Clamp(monitorLocalAngle, MinPitchAngle, MaxPitchAngle);

        monitor.transform.localRotation = Quaternion.Euler(monitorLocalAngle, 0, 0);

        if (rigidbody.velocity.sqrMagnitude > 0.5f) // slow it down
        {
            rigidbody.velocity *= 0.7f;
        }

        Vector3 screenPos = agentCamera.WorldToScreenPoint(dockingStation.transform.position);

        steps++;
        stepsEpoch++;

        if (stepsEpoch == MaxStep - 1)
        {
            successfulDocking = false;
            dockingTime       = 0.02f * MaxStep;

            String first  = distanceFromDocker.ToString();
            String second = spawningAngle.ToString();
            String third  = MaxStep.ToString();
            String fourth = successfulDocking.ToString();
            String fifth  = dockingTime.ToString();
            Debug.Log("Docking not successful in epoch " + epoch);
            var newLine = string.Format("{0};{1};{2};{3};{4}", first, second, third, fourth, fifth);
            Debug.Log(newLine);
            csv.AppendLine(newLine);
        }

        if (steps >= 1)
        {
            //bool patternFound = Utils.isPatternVisible(agentCamera);
            bool patternFound = (screenPos.x > 0) & (screenPos.x < agentCamera.pixelWidth) & (screenPos.y > 0) & (screenPos.y < agentCamera.pixelHeight) & (screenPos.z > 0) & (screenPos.z < 10);
            //Debug.Log("Pattern found? " + patternFound);
            if (patternFound)
            {
                float reward_x = Mathf.Abs(screenPos.x - (agentCamera.pixelWidth / 2));  // [0 - agentCamera.pixelWidth/2]
                reward_x = -reward_x / (agentCamera.pixelWidth / 4) + 3;                 // [1-3]
                float reward_y = Mathf.Abs(screenPos.y - (agentCamera.pixelHeight / 2)); // [0 - agentCamera.pixelHeight/2]
                reward_y = -reward_y / (agentCamera.pixelHeight / 4) + 3;                // [1-3]
                //float reward_scale = screenPos.z<1.25f?3:0; // [0-3]
                float reward_scale   = Mathf.Clamp(3 - screenPos.z, 0, 3);
                float reward_pattern = ((reward_x + reward_y + reward_scale) * steps) / (MaxStep * 9f);
                //AddReward(reward_pattern);
                //Debug.Log("Reward for detecting the pattern of: " + reward_pattern + ", detected at coordinates: " + screenPos);
            }
            else
            {
                float reward_pattern = -((float)steps / (float)MaxStep);
                //AddReward(reward_pattern);
                //Debug.Log("Reward for not detecting the pattern of: " + reward_pattern);
            }
            steps = steps % 1;
        }
    }
Exemplo n.º 19
0
    public override void OnActionReceived(ActionBuffers actions)
    {
        int value = actions.DiscreteActions[0];

        PersonalitySimulator.Instance.nextSection(value);
    }
Exemplo n.º 20
0
 public override void OnActionReceived(ActionBuffers actionBuffers)
 {
     h = actionBuffers.ContinuousActions[0];
     v = actionBuffers.ContinuousActions[1];
 }
Exemplo n.º 21
0
    public override void OnActionReceived(ActionBuffers actions)
    {
        int value = actions.DiscreteActions[0];

        RedRunner.TerrainGeneration.TerrainGenerator.Singleton.GenerateMiddle(value);
    }
Exemplo n.º 22
0
    public override void OnActionReceived(ActionBuffers actionBuffers)

    {
    }
Exemplo n.º 23
0
    public override void OnActionReceived(ActionBuffers actionBuffers)
    {
        if (StepCount >= MaxStep - 2)
        {
            //print("ending manually " + points + " " +oldPoints);
            // worst score to best from -1 to 1
            // used in self-play to calculate elo score
            //AddReward(points / (maxPoints*2) - 1f);
            float score = -1;
            foreach (Agent a in otherBotAgents)
            {
                //print("a loop " + points + " " + a.oldPoints + " " + a.gameObject.name);
                if (a.oldPoints < points)
                {
                    score += 2;
                }
                else if (a.oldPoints == points)
                {
                    score += 1;
                }
            }
            //print("final score " + score + " " + gameObject.name);
            AddReward(score);
            EndEpisode();
        }
        if (StepCount >= MaxStep - 50)
        {
            oldPoints = points;
        }
        // Apply linear movement
        Vector3 move = actionBuffers.ContinuousActions[0] * transform.right + actionBuffers.ContinuousActions[1] * transform.forward;

        if (move.magnitude > 1)
        {
            move = move.normalized;
        }
        move = move * botSpeed;
        body.MovePosition(body.position + move * Time.fixedDeltaTime);

        // Apply rotation
        Quaternion newAngles = body.rotation;
        Vector3    euler     = newAngles.eulerAngles;

        euler.y += Mathf.Clamp(actionBuffers.ContinuousActions[2], -1f, 1f) * Time.fixedDeltaTime * botRotationSpeed;
        newAngles.eulerAngles = euler;
        body.MoveRotation(newAngles);

        // Shoot
        if (power < maxPower)
        {
            power = Mathf.Min(maxPower, power + powerIncreaseSpeed);
        }
        if (cooldown > 0)
        {
            cooldown = Mathf.Max(0, cooldown - cooldownSpeed);
        }
        if (power > minPower && cooldown <= 0)
        {
            if (actionBuffers.DiscreteActions[0] == 1)
            {
                GameObject bullet = Instantiate(bulletPrefab, bananaTip.position, bananaTip.rotation, transform) as GameObject;
                bullet.GetComponent <Rigidbody>().velocity = transform.forward * power;
                bullet.GetComponent <Light>().color        = bananaLightColor;
                power   -= powerLoss;
                cooldown = maxCooldown;
                bullets.Add(bullet);
            }
        }
    }
Exemplo n.º 24
0
    public void MoveAgent(ActionBuffers actionBuffers)
    {
        m_Shoot = false;

        if (Time.time > m_FrozenTime + 4f && m_Frozen)
        {
            Unfreeze();
        }
        if (Time.time > m_EffectTime + 0.5f)
        {
            if (m_Poisoned)
            {
                Unpoison();
            }
            if (m_Satiated)
            {
                Unsatiate();
            }
        }

        var dirToGo   = Vector3.zero;
        var rotateDir = Vector3.zero;

        var continuousActions = actionBuffers.ContinuousActions;
        var discreteActions   = actionBuffers.DiscreteActions;

        if (!m_Frozen)
        {
            var forward = Mathf.Clamp(continuousActions[0], -1f, 1f);
            var right   = Mathf.Clamp(continuousActions[1], -1f, 1f);
            var rotate  = Mathf.Clamp(continuousActions[2], -1f, 1f);

            dirToGo   = transform.forward * forward;
            dirToGo  += transform.right * right;
            rotateDir = -transform.up * rotate;

            var shootCommand = (int)discreteActions[0] > 0;
            if (shootCommand)
            {
                m_Shoot             = true;
                dirToGo            *= 0.5f;
                m_AgentRb.velocity *= 0.75f;
            }
            m_AgentRb.AddForce(dirToGo * moveSpeed, ForceMode.VelocityChange);
            transform.Rotate(rotateDir, Time.fixedDeltaTime * turnSpeed);
        }

        if (m_AgentRb.velocity.sqrMagnitude > 25f) // slow it down
        {
            m_AgentRb.velocity *= 0.95f;
        }

        if (m_Shoot)
        {
            var myTransform = transform;
            myLaser.transform.localScale = new Vector3(1f, 1f, m_LaserLength);
            var rayDir = 25.0f * myTransform.forward;
            Debug.DrawRay(myTransform.position, rayDir, Color.red, 0f, true);
            RaycastHit hit;
            if (Physics.SphereCast(transform.position, 2f, rayDir, out hit, 25f))
            {
                if (hit.collider.gameObject.CompareTag("agent"))
                {
                    hit.collider.gameObject.GetComponent <FoodCollectorAgent>().Freeze();
                }
            }
        }
        else
        {
            myLaser.transform.localScale = new Vector3(0f, 0f, 0f);
        }
    }
Exemplo n.º 25
0
 /// <summary>
 /// Called every step of the engine. Here the agent takes an action.
 /// </summary>
 public override void OnActionReceived(ActionBuffers actionBuffers)
 {
     // Move the agent using the action.
     MoveAgent(actionBuffers.DiscreteActions);
 }
Exemplo n.º 26
0
    public override void OnActionReceived(ActionBuffers actionBuffers)
    {
        var act0     = actionBuffers.DiscreteActions[0];
        var nextMode = 0;

        switch (act0)
        {
        case 1:
            nextMode = 1;    //카메라 타겟 오브젝트가 모서리를 따라 움직임
            break;

        case 2:
            nextMode = 2;    //추정 라인을 조작함
            break;
        }

        if (nextMode == 1)
        {
            Move_Init_setting();
        }

        if (nextMode == 2)
        {
            var DA_Line_up         = actionBuffers.DiscreteActions[line_up];
            var DA_Line_down       = actionBuffers.DiscreteActions[line_down];
            var DA_Line_left       = actionBuffers.DiscreteActions[line_left];
            var DA_Line_right      = actionBuffers.DiscreteActions[line_right];
            var DA_Line_turn_left  = actionBuffers.DiscreteActions[line_turn_left];
            var DA_Line_turn_right = actionBuffers.DiscreteActions[line_turn_right];
            var linePosy           = redLine_rectTransform.anchoredPosition.y;
            var linePosx           = redLine_rectTransform.anchoredPosition.x;
            var lineRot            = Mathf.Round(redLine_rectTransform.eulerAngles.z);
            if (DA_Line_up == 1)
            {
                if (linePosy < line_max_y_pos)
                {
                    redLine_rectTransform.anchoredPosition = new Vector3(linePosx, linePosy + line_move_unit, 0f);
                }
            }
            else if (DA_Line_down == 1)
            {
                if (linePosy > line_min_y_pos)
                {
                    redLine_rectTransform.anchoredPosition = new Vector3(linePosx, linePosy - line_move_unit, 0f);
                }
            }
            else if (DA_Line_left == 1)
            {
                if (linePosx > line_min_x_pos)
                {
                    redLine_rectTransform.anchoredPosition = new Vector3(linePosx - line_move_unit, linePosy, 0f);
                }
            }
            else if (DA_Line_right == 1)
            {
                if (linePosx < line_max_x_pos)
                {
                    redLine_rectTransform.anchoredPosition = new Vector3(linePosx + line_move_unit, linePosy, 0f);
                }
            }
            else if (DA_Line_turn_left == 1)
            {
                if (lineRot > line_min_z_lot || lineRot < line_max_z_lot)
                {
                    redLine_rectTransform.eulerAngles += new Vector3(0f, 0f, line_lot_unit);
                }
                else if (lineRot == line_min_z_lot)
                {
                    redLine_rectTransform.eulerAngles += new Vector3(0f, 0f, line_lot_unit);
                }
            }
            else if (DA_Line_turn_right == 1)
            {
                if (lineRot > line_min_z_lot || lineRot < line_max_z_lot)
                {
                    redLine_rectTransform.eulerAngles -= new Vector3(0f, 0f, line_lot_unit);
                }
                else if (lineRot == line_max_z_lot)
                {
                    redLine_rectTransform.eulerAngles -= new Vector3(0f, 0f, line_lot_unit);
                }
            }
        }

        B0Line.transform.position = redLine.transform.position;
        B0Line.transform.rotation = redLine.transform.rotation;

        B1Line.transform.position = redLine.transform.position;
        B1Line.transform.rotation = redLine.transform.rotation;

        B2Line.transform.position = redLine.transform.position;
        B2Line.transform.rotation = redLine.transform.rotation;

        B3Line.transform.position = redLine.transform.position;
        B3Line.transform.rotation = redLine.transform.rotation;

        Linecenter.transform.position = redLine.transform.position;
        Linecenter.transform.rotation = redLine.transform.rotation;
    }
Exemplo n.º 27
0
    public override void OnActionReceived(ActionBuffers actions)
    {
        // ContinuousActions-> DiscreteActions
        // output 一个七维向量: (w,a,s,d,k,o,j)
        id = int.Parse(transform.gameObject.name.Split('_')[1]);
        int[] A = new int[7] {
            0, 0, 0, 0, 0, 0, 0
        };
        A[actions.DiscreteActions[0]] = 1;
        int moveLeft  = A[0];
        int moveRight = A[1];
        int moveUp    = A[2];
        int moveDown  = A[3];
        int drink     = A[4];
        int eat       = A[5];
        int attack    = A[6];

        // Debug.Log($"{moveLeft}, {moveRight}, {moveUp}, {moveDown}, {drink}, {eat}");


        if (!GameDataMgr.GetInstance().playerInfos[id].alive)
        {
            if (!GameDataMgr.GetInstance().playerInfos[id].dead)
            {
                GameDataMgr.GetInstance().playerInfos[id].dead = true;
                SetReward(-5f);
                // Debug.Log($"Death of {id}");
                dead = true;
                GameObject player = GameObject.Find("player_" + id.ToString());
                player.transform.localScale = new Vector3(0, 0, 0);
                double aggressivity = 0f, averageStarveOnEat = 0f, averageThirstyOnDrink = 0f, averageHealthOnAttack = 0f;
                int    live = 0;
                foreach (int id in GameDataMgr.GetInstance().playerInfos.Keys)
                {
                    if (!GameDataMgr.GetInstance().playerInfos[id].alive)
                    {
                        continue;
                    }
                    live++;
                    aggressivity          += GameDataMgr.GetInstance().playerInfos[id].aggressivity;
                    averageStarveOnEat    += GameDataMgr.GetInstance().playerInfos[id].averageStarveOnEat;
                    averageThirstyOnDrink += GameDataMgr.GetInstance().playerInfos[id].averageThirstyOnDrink;
                    averageHealthOnAttack += GameDataMgr.GetInstance().playerInfos[id].averageHealthOnAttack;
                }
                aggressivity          = aggressivity / (float)live;
                averageStarveOnEat    = averageStarveOnEat / (float)live;
                averageThirstyOnDrink = averageThirstyOnDrink / (float)live;
                averageHealthOnAttack = averageHealthOnAttack / (float)live;
                Debug.Log($"Death {id}, Agg{aggressivity}, soe{averageStarveOnEat}, tod{averageThirstyOnDrink}, hoa{averageHealthOnAttack}");
            }
        }
        else
        {
            float reward = ((float)Math.Min(GameDataMgr.GetInstance().playerInfos[id].thirsty, GameDataMgr.GetInstance().playerInfos[id].starving) - 50f) / 50f;
            SetReward(reward);
        }
        // GameDataMgr.GetInstance().deathCounter[id] += 1;
        int cnt = 0;

        foreach (int id in GameDataMgr.GetInstance().playerInfos.Keys)
        {
            if (GameDataMgr.GetInstance().playerInfos[id].alive)
            {
                cnt++;
            }
        }

        if (cnt < 1)
        {
            GameDataMgr.GetInstance().restart = true;
            // while(GameDataMgr.GetInstance().end){
            // }
            foreach (int id in GameDataMgr.GetInstance().playerInfos.Keys)
            {
                GameDataMgr.GetInstance().playerRestart(id);
                GameObject player = GameObject.Find("player_" + id.ToString());
                player.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
            }
            EndEpisode();
        }

        float  posX = (float)GameDataMgr.GetInstance().playerInfos[id].posX;
        float  posY = (float)GameDataMgr.GetInstance().playerInfos[id].posY;
        double nx   = posX + moveDown - moveUp;
        double ny   = posY - moveLeft + moveRight;

        if (isAttackable((int)posX, (int)posY))
        {
            GameDataMgr.GetInstance().playerInfos[id].canAttackCount += 1;
        }
        if (drink == 1)
        {
            GameDataMgr.GetInstance().playerDrink(id, nx, ny);
        }
        if (eat == 1)
        {
            GameDataMgr.GetInstance().playerEat(id, nx, ny);
        }
        if (attack == 1)
        {
            GameDataMgr.GetInstance().playerAttack(id, posX, posY);
            // Debug.Log($"Attack, {id}");
        }
        GameDataMgr.GetInstance().updatePlayerPos(id, (float)nx, (float)ny);
    }
Exemplo n.º 28
0
 public override void OnActionReceived(ActionBuffers actions)
 {
     carMovement.SetInputs(actions.DiscreteActions[0], actions.ContinuousActions[0]);
 }
Exemplo n.º 29
0
    public override void OnActionReceived(ActionBuffers actionBuffers)
    {
        GameManager.InputManager.pitchInput = actionBuffers.ContinuousActions[0];
        GameManager.InputManager.yawInput   = actionBuffers.ContinuousActions[1];
        GameManager.InputManager.isBoost    = actionBuffers.ContinuousActions[2] > 0;
        //GameManager.InputManager.isBoost = actionBuffers.DiscreteActions[0] != 0;

        /*switch (actionBuffers.DiscreteActions[1])
         * {
         *  case 0:
         *      GameManager.InputManager.rollInput = 0;
         *      break;
         *  case 1:
         *      GameManager.InputManager.rollInput = 1;
         *      break;
         *  case 2:
         *      GameManager.InputManager.rollInput = -1;
         *      break;
         * }*/

        if (_controller.carState != CubeController.CarStates.Air)
        {
            AddReward(-1);
            EndEpisode();
        }
        else
        {
            AddReward(0.1f);
        }

        /*switch (throttle)
         * {
         *  case 0:
         *      GameManager.InputManager.pitchInput = 0;
         *      break;
         *  case 1:
         *      GameManager.InputManager.pitchInput = 1;
         *      break;
         *  case 2:
         *      GameManager.InputManager.pitchInput = -1;
         *      break;
         *  default:
         *      GameManager.InputManager.pitchInput = 0;
         *      break;
         * }
         *
         * switch (steering)
         * {
         *  case 0:
         *      GameManager.InputManager.yawInput = 0;
         *      break;
         *  case 1:
         *      GameManager.InputManager.yawInput = 1;
         *      break;
         *  case 2:
         *      GameManager.InputManager.yawInput = -1;
         *      break;
         *  default:
         *      GameManager.InputManager.yawInput = 0;
         *      break;
         * }*/
    }
Exemplo n.º 30
0
    public override void OnActionReceived(ActionBuffers actionBuffers)
    {
        // The dictionary with all the body parts in it are in the jdController
        var bpDict = m_JdController.bodyPartsDict;

        var continuousActions = actionBuffers.ContinuousActions;
        var i = -1;

        if (!touched)
        {
            // Pick a new target joint rotation

            /*bpDict[turner].SetJointTargetRotation(0, continuousActions[++i], 0);
             * bpDict[bone1].SetJointTargetRotation(continuousActions[++i], 0, 0);
             * bpDict[bone2].SetJointTargetRotation(continuousActions[++i], 0, 0);
             * bpDict[bone3].SetJointTargetRotation(continuousActions[++i], 0, 0);
             * bpDict[hand].SetJointTargetRotation(continuousActions[++i], 0, 0);*/
            /**/
            action1 = continuousActions[++i];
            action2 = continuousActions[++i];
            action3 = continuousActions[++i];
            action4 = continuousActions[++i];
            action5 = continuousActions[++i];

            bpDict[turner].SetJointTargetRotation(0, action1, 0);
            bpDict[bone1].SetJointTargetRotation(action2, 0, 0);
            bpDict[bone2].SetJointTargetRotation(action3, 0, 0);
            bpDict[bone3].SetJointTargetRotation(action4, 0, 0);
            bpDict[hand].SetJointTargetRotation(action5, 0, 0);
            //touched = true;
        }/*
          *
          * bpDict[finger1].SetJointTargetRotation(continuousActions[++i], 0, 0);
          * bpDict[finger2].SetJointTargetRotation(continuousActions[i], 0, 0);
          * bpDict[finger3].SetJointTargetRotation(continuousActions[i], 0, 0);
          * bpDict[finger4].SetJointTargetRotation(continuousActions[i], 0, 0);
          * bpDict[phalanx1].SetJointTargetRotation(continuousActions[++i], 0, 0);
          * bpDict[phalanx2].SetJointTargetRotation(continuousActions[i], 0, 0);
          * bpDict[phalanx3].SetJointTargetRotation(continuousActions[i], 0, 0);
          * bpDict[phalanx4].SetJointTargetRotation(continuousActions[i], 0, 0);
          * bpDict[forePhalanx1].SetJointTargetRotation(continuousActions[++i], 0, 0);
          * bpDict[forePhalanx2].SetJointTargetRotation(continuousActions[i], 0, 0);
          * bpDict[forePhalanx3].SetJointTargetRotation(continuousActions[i], 0, 0);
          * bpDict[forePhalanx4].SetJointTargetRotation(continuousActions[i], 0, 0);
          * bpDict[finger1].SetJointTargetRotation(curGrip, 0, 0);
          * bpDict[finger2].SetJointTargetRotation(curGrip, 0, 0);
          * bpDict[finger3].SetJointTargetRotation(curGrip, 0, 0);
          * bpDict[finger4].SetJointTargetRotation(curGrip, 0, 0);
          * bpDict[phalanx1].SetJointTargetRotation(curGrip, 0, 0);
          * bpDict[phalanx2].SetJointTargetRotation(curGrip, 0, 0);
          * bpDict[phalanx3].SetJointTargetRotation(curGrip, 0, 0);
          * bpDict[phalanx4].SetJointTargetRotation(curGrip, 0, 0);
          * bpDict[forePhalanx1].SetJointTargetRotation(curGrip, 0, 0);
          * bpDict[forePhalanx2].SetJointTargetRotation(curGrip, 0, 0);
          * bpDict[forePhalanx3].SetJointTargetRotation(curGrip, 0, 0);
          * bpDict[forePhalanx4].SetJointTargetRotation(curGrip, 0, 0);*/

        // Update joint strength
        bpDict[turner].SetJointStrength(1);
        bpDict[bone1].SetJointStrength(1);
        bpDict[bone2].SetJointStrength(1);
        bpDict[bone3].SetJointStrength(1);
        bpDict[hand].SetJointStrength(1);/*
                                          * bpDict[finger1].SetJointStrength(continuousActions[++i]);
                                          * bpDict[finger2].SetJointStrength(continuousActions[i]);
                                          * bpDict[finger3].SetJointStrength(continuousActions[i]);
                                          * bpDict[finger4].SetJointStrength(continuousActions[i]);
                                          * bpDict[phalanx1].SetJointStrength(continuousActions[++i]);
                                          * bpDict[phalanx2].SetJointStrength(continuousActions[i]);
                                          * bpDict[phalanx3].SetJointStrength(continuousActions[i]);
                                          * bpDict[phalanx4].SetJointStrength(continuousActions[i]);
                                          * bpDict[forePhalanx1].SetJointStrength(continuousActions[++i]);
                                          * bpDict[forePhalanx2].SetJointStrength(continuousActions[i]);
                                          * bpDict[forePhalanx3].SetJointStrength(continuousActions[i]);
                                          * bpDict[forePhalanx4].SetJointStrength(continuousActions[i]);
                                          * bpDict[finger1].SetJointStrength(1);
                                          * bpDict[finger2].SetJointStrength(1);
                                          * bpDict[finger3].SetJointStrength(1);
                                          * bpDict[finger4].SetJointStrength(1);
                                          * bpDict[phalanx1].SetJointStrength(1);
                                          * bpDict[phalanx2].SetJointStrength(1);
                                          * bpDict[phalanx3].SetJointStrength(1);
                                          * bpDict[phalanx4].SetJointStrength(1);
                                          * bpDict[forePhalanx1].SetJointStrength(1);
                                          * bpDict[forePhalanx2].SetJointStrength(1);
                                          * bpDict[forePhalanx3].SetJointStrength(1);
                                          * bpDict[forePhalanx4].SetJointStrength(1);*/

        curangle += speed * Time.fixedDeltaTime;

        /*if(curangle > 1.0f) {
         *  speed *= -1.0f;
         * } else if(curangle < -1.0f) {
         *  speed *= -1.0f;
         * }*/
    }