Exemplo n.º 1
0
    public override void AgentAction(float[] vectorAction, string textAction)
    {
        Vector3 relativePosition = Target.transform.localPosition - this.transform.localPosition;
        float   arcTanToTar      = Mathf.Atan(relativePosition.z / relativePosition.x) * 180 / Mathf.PI;
        float   relativeRotation = 0;

        if (relativePosition.x > 0 && relativePosition.z > 0)
        {
            relativeRotation = (90 - (arcTanToTar));
        }
        else if (relativePosition.x < 0 && relativePosition.z > 0)
        {
            relativeRotation = (90 - (arcTanToTar)) + 180;
        }
        else if (relativePosition.x < 0 && relativePosition.z < 0)
        {
            relativeRotation = (90 - (arcTanToTar)) + 180;
        }
        else if (relativePosition.x > 0 && relativePosition.z < 0)
        {
            relativeRotation = (90 - (arcTanToTar));
        }
        relativeRotation = relativeRotation - this.transform.rotation.eulerAngles.y;
        if (relativeRotation > 180)
        {
            relativeRotation = relativeRotation - 360;
        }
        else if (relativeRotation < -180)
        {
            relativeRotation = relativeRotation + 360;
        }
        float absReRo = Mathf.Abs(relativeRotation);

        timer += Time.deltaTime;


        float directionX = 0;
        float directionZ = 0;
        float directionY = 0;
        float rotataionY = 0;

        rotataionY = vectorAction[0];
        directionY = Mathf.Clamp(vectorAction[2], -1f, 1f);
        directionZ = Mathf.Clamp(vectorAction[1], -1f, 1f);
        directionX = Mathf.Clamp(vectorAction[3], -1f, 1f);

        //gameObject.GetComponent<Rigidbody>().AddRelativeForce(new Vector3(directionZ*100, directionY* 100, directionX* 100));
        float distanceToTarget = Vector3.Distance(this.transform.localPosition,
                                                  Target.localPosition);


        //print(relativeRotation);
        //if (relativeRotation < 0 && Mathf.Abs(relativeRotation) > 3)
        //{
        //    DM.Rotation(-0.1f);
        //}
        //else if (relativeRotation > 0 && Mathf.Abs(relativeRotation) > 3)
        //{
        //    DM.Rotation(0.1f);
        //}
        //if (Target.transform.position.y > this.transform.position.y)
        //{
        //    DM.PropellerForce(directionZ, 0, 0.5f);
        //}
        //else if (Target.transform.position.y < this.transform.position.y)
        //{
        //    DM.PropellerForce(directionZ, 0, 0.3f);
        //}
        //print(DM.AutoMove + " " + directionZ + " " + directionX);
        DM.Rotation(rotataionY);

        DM.PropellerForce(directionZ, directionX, directionY);
        DM.MovemenForward(directionZ, directionX, directionY);
        DM.Swerve(directionZ, directionX);



        //if (distanceToTarget < previousDistance)
        //{
        //    AddReward(0.001f * Mathf.Pow(Mathf.Pow(rBody.velocity.z, 2) + Mathf.Pow(rBody.velocity.x, 2), 0.5f));
        //    if (5f < previousDistance - distanceToTarget)
        //    {
        //        AddReward(0.005f);
        //    }

        //}

        if (this.transform.position.y > 50)
        {
            AddReward(-0.01f * (this.transform.position.y - 50 + 1));
        }

        AddReward(0.01f / (distanceToTarget + 1));

        //if(distanceToTarget > previousDistance){
        //    AddReward(-0.1f * (distanceToTarget - previousDistance));
        //}
        AddReward(-0.01f * (distanceToTarget - previousDistance));


        AddReward(-0.0001f * timer);

        //if(absReRo > 90){
        //    AddReward(-0.001f * absReRo);
        //}

        if (this.transform.position.y < 1)
        {
            AddReward(-0.5f);
        }



        //if (absReRo < previousRotation)
        //{
        //    AddReward(0.0001f);
        //}
        //if (absReRo > previousRotation)
        //{
        //    AddReward(-0.0001f);
        //}

        //if(distanceToTarget > previousDistance){
        //    AddReward(-0.003f);
        //}


        if (this.transform.position.y < -1.0 ||
            this.transform.position.y > 200
            ||
            Mathf.Abs(this.transform.localPosition.z) > 150 ||
            Mathf.Abs(this.transform.localPosition.x) > 50 ||
            this.transform.localPosition.z < -10
            )
        {
            SetReward(-5f);
            resetTarget();
            timer = 0;
            t     = 0;
            Done();
        }
        if (timer > 30)
        {
            resetTarget();
            timer = 0;
            t     = 0;
            SetReward(-3f);
            Done();
        }
        //print(rBody.velocity);
        previousDistance = distanceToTarget;
        previousRotation = absReRo;
        //print(timer);
        //print(GetReward());
        print(counter + " " + goalCounter + " " + TotalTime);
    }
Exemplo n.º 2
0
    public override void AgentAction(float[] vectorAction, string textAction)
    {
        Vector3 relativePosition = Target.transform.localPosition - this.transform.localPosition;
        float   arcTanToTar      = Mathf.Atan(relativePosition.z / relativePosition.x) * 180 / Mathf.PI;
        float   relativeRotation = 0;

        if (relativePosition.x > 0 && relativePosition.z > 0)
        {
            relativeRotation = (90 - (arcTanToTar));
        }
        else if (relativePosition.x < 0 && relativePosition.z > 0)
        {
            relativeRotation = (90 - (arcTanToTar)) + 180;
        }
        else if (relativePosition.x < 0 && relativePosition.z < 0)
        {
            relativeRotation = (90 - (arcTanToTar)) + 180;
        }
        else if (relativePosition.x > 0 && relativePosition.z < 0)
        {
            relativeRotation = (90 - (arcTanToTar));
        }
        relativeRotation = relativeRotation - this.transform.rotation.eulerAngles.y;
        if (relativeRotation > 180)
        {
            relativeRotation = relativeRotation - 360;
        }
        else if (relativeRotation < -180)
        {
            relativeRotation = relativeRotation + 360;
        }
        float absReRo = Mathf.Abs(relativeRotation);

        timer += Time.deltaTime;


        float directionX = 0;
        float directionZ = 0;
        float directionY = 0;
        float rotataionY = 0;

        rotataionY = vectorAction[0];
        directionY = Mathf.Clamp(vectorAction[2], -1f, 1f);
        directionZ = Mathf.Clamp(vectorAction[1], -1f, 1f);
        directionX = Mathf.Clamp(vectorAction[3], -1f, 1f);

        //gameObject.GetComponent<Rigidbody>().AddRelativeForce(new Vector3(directionZ*100, directionY* 100, directionX* 100));
        float distanceToTarget = Vector3.Distance(this.transform.localPosition,
                                                  Target.localPosition);


        //print(relativeRotation);
        //if (relativeRotation < 0 && Mathf.Abs(relativeRotation) > 3)
        //{
        //    DM.Rotation(-0.1f);
        //}
        //else if (relativeRotation > 0 && Mathf.Abs(relativeRotation) > 3)
        //{
        //    DM.Rotation(0.1f);
        //}
        //if (Target.transform.position.y > this.transform.position.y)
        //{
        //    DM.PropellerForce(directionZ, 0, 0.5f);
        //}
        //else if (Target.transform.position.y < this.transform.position.y)
        //{
        //    DM.PropellerForce(directionZ, 0, 0.3f);
        //}
        //if(absReRo > 90){
        //    AddReward(-0.001f*absReRo);
        //}
        //print(DM.AutoMove + " " + directionZ + " " + directionX);
        DM.Rotation(rotataionY);

        DM.PropellerForce(directionZ, directionX, directionY);
        DM.MovemenForward(directionZ, directionX, directionY);
        DM.Swerve(directionZ, directionX);



        if (this.transform.position.y > 50)
        {
            AddReward(-0.01f * (this.transform.position.y - 50 + 1));
        }

        AddReward(0.1f / (distanceToTarget + 1));

        AddReward(0.0001f * (mindis - distanceToTarget));
        if (distanceToTarget < mindis)
        {
            mindis = distanceToTarget;
        }

        AddReward(0.0001f * (minRo - absReRo));
        if (absReRo < minRo)
        {
            minRo = absReRo;
        }
        if (absReRo > 100 && distanceToTarget > 1)
        {
            AddReward(-0.7f);
        }



        AddReward(-0.0001f * timer);


        //if (absReRo < previousRotation)
        //{
        //    AddReward(0.0001f);
        //}
        //if (absReRo > previousRotation)
        //{
        //    AddReward(-0.0001f);
        //}

        //if(distanceToTarget > previousDistance){
        //    AddReward(-0.003f);
        //}


        //      if (this.transform.position.y < -3.0 ||
        //          this.transform.position.y > 60
        //          ||
        //          Mathf.Abs(this.transform.localPosition.z) > 180||
        //          Mathf.Abs(this.transform.localPosition.x) > 180
        //         )
        //{

        //          SetReward(-60f);
        //          //resetTarget();
        //          timer = 0;
        //          t = 0;
        //          Done();

        //}
        //if (timer > 30+failcount * 2)
        //{
        //    AddReward(-40f);
        //    timer = 0;
        //    t = 0;
        //    failcount += 1;
        //    Done();
        //    resetTarget();

        //    //AddReward(-0.00001f * Mathf.Pow((timer - 30), 2));

        //}
        if (failcount > 5)
        {
            resetTarget();
            failcount = 0;
        }
        //if (timer > 50)
        //{
        //resetTarget();
        //timer = 0;
        //t = 0;
        //SetReward(-60f);
        //Done();

        //}
        //print(rBody.velocity);
        previousDistance = distanceToTarget;
        previousRotation = absReRo;
        //print(timer);
        //print(GetReward());
        print(counter + " " + goalCounter + " " + collcount + " " + TotalTime + " " + GetReward());
    }
Exemplo n.º 3
0
    public override void AgentAction(float[] vectorAction, string textAction)
    {
        Vector3 relativePosition = Target.transform.localPosition - this.transform.localPosition;
        float   arcTanToTar      = Mathf.Atan(relativePosition.z / relativePosition.x) * 180 / Mathf.PI;
        float   relativeRotation = 0;

        if (relativePosition.x > 0 && relativePosition.z > 0)
        {
            relativeRotation = (90 - (arcTanToTar));
        }
        else if (relativePosition.x < 0 && relativePosition.z > 0)
        {
            relativeRotation = (90 - (arcTanToTar)) + 180;
        }
        else if (relativePosition.x < 0 && relativePosition.z < 0)
        {
            relativeRotation = (90 - (arcTanToTar)) + 180;
        }
        else if (relativePosition.x > 0 && relativePosition.z < 0)
        {
            relativeRotation = (90 - (arcTanToTar));
        }
        relativeRotation = relativeRotation - this.transform.rotation.eulerAngles.y;
        if (relativeRotation > 180)
        {
            relativeRotation = relativeRotation - 360;
        }
        else if (relativeRotation < -180)
        {
            relativeRotation = relativeRotation + 360;
        }
        float absReRo = Mathf.Abs(relativeRotation);



        float directionX = 0;
        float directionZ = 0;
        float directionY = 0;
        float rotataionY = 0;

        rotataionY = vectorAction[0];
        directionY = Mathf.Clamp(vectorAction[2], -1f, 1f);
        directionZ = Mathf.Clamp(vectorAction[1], -1f, 1f);
        //directionX = Mathf.Clamp(vectorAction[3], -1f, 1f);
        //print(" "+ directionZ + " " + directionX);

        //gameObject.GetComponent<Rigidbody>().AddRelativeForce(new Vector3(directionZ*100, directionY* 100, directionX* 100));
        float distanceToTarget = Vector3.Distance(this.transform.localPosition,
                                                  Target.localPosition);


        //print(relativeRotation);
        DM.Rotation(rotataionY);

        DM.PropellerForce(directionZ, directionX, directionY);
        DM.Swerve(directionZ, directionX, directionY);
        DM.MovemenForward(directionZ, directionX, directionY);
        if (relativeRotation < 0 && Mathf.Abs(relativeRotation) > 3)
        {
            DM.Rotation(-0.3f);
        }
        else if (relativeRotation > 0 && Mathf.Abs(relativeRotation) > 3)
        {
            DM.Rotation(0.3f);
        }
        if (Target.transform.position.y > this.transform.position.y && Mathf.Abs(Target.transform.position.y - this.transform.position.y) > 0.3f)
        {
            DM.PropellerForce(directionZ, 0, 0.6f);
        }
        else if (Target.transform.position.y < this.transform.position.y && Mathf.Abs(Target.transform.position.y - this.transform.position.y) > 0.3f)
        {
            DM.PropellerForce(directionZ, 0, 0.3f);
        }
        else
        {
            DM.PropellerForce(directionZ, 0, 0.412f);
        }



        //if (distanceToTarget < previousDistance)
        //{
        //    AddReward(0.001f * Mathf.Pow(Mathf.Pow(rBody.velocity.z, 2) + Mathf.Pow(rBody.velocity.x, 2), 0.5f));
        //    //if (2f < previousDistance - distanceToTarget)
        //    //{
        //    //    AddReward(0.0005f);
        //    //}

        //}

        AddReward(0.01f / (distanceToTarget + 1));
        //print(distanceToTarget);

        AddReward(-0.001f * absReRo);

        if (this.transform.position.y < 1)
        {
            AddReward(-0.5f);
        }


        AddReward(-0.01f * (distanceToTarget - previousDistance));



        //if ((this.transform.position.y < -1.0 ||
        //    this.transform.position.y > 100
        //    ||
        //    Mathf.Abs(this.transform.localPosition.z) > planeL/2 + 5 ||
        //    Mathf.Abs(this.transform.localPosition.x) > planeW/2 + 5
        //   )&& !notTraining)
        //{

        //    SetReward(-1f);
        //    resetTarget();
        //    timer = 0;
        //    Done();
        //}
        //if (t > 35 && !notTraining)
        //{
        //    resetTarget();
        //    timer = 0;
        //    t = 0;
        //    SetReward(-1f);
        //    Done();

        //}
        //print(rBody.velocity);
        previousDistance = distanceToTarget;
        previousRotation = absReRo;
        //print(timer);
        //print(GetReward());
        t += Time.deltaTime;
        //print(t + " " + distanceToTarget);
    }