Example #1
0
    /// <summary>
    /// Is invoked after the LampAgent takes an action
    /// </summary>
    /// <param name="vectorAction">action that was taken</param>
    /// <param name="resultFlags">results of the action as a bitmask</param>
    /// <returns>reward for the action</returns>
    protected virtual float EvaluateAction(float[] vectorAction, out ActionResultFlags resultFlags)
    {
        // Assumes the actions is already taken
        // vectorActions parameter is only provided just in case the implementation needs to know which action was taken
        // resultFlags is a bitmask that indicates if the action was successful and/or terminal

        resultFlags = ActionResultFlags.None;

        // Calculate the target's x an y offset from the center of the camera
        // And determine if the target is infront of the camera (on the cameras local z axis)
        // Here we only consider the distance that the target is from the center of the camera (on the x and y axis)

        currentTargetViewportPosition = lamp.WorldToViewportPoint(target.position);
        bool  targetIsInFrontOfCamera = currentTargetViewportPosition.z > 0;
        float initialSqrOffset        = ((Vector2)initialTargetViewportPosition).sqrMagnitude;
        float currentSqrOffset        = ((Vector2)currentTargetViewportPosition).sqrMagnitude;

        if (Mathf.Abs(currentTargetViewportPosition.x) <= 1f && Mathf.Abs(currentTargetViewportPosition.y) <= 1f)
        {
            if (targetIsInFrontOfCamera)
            {
                if (currentSqrOffset <= initialSqrOffset)
                {
                    float reward = Mathf.Pow(0.1f, currentSqrOffset / initialSqrOffset);

                    if (currentSqrOffset <= academy.SuccessThreshold)
                    {
                        resultFlags |= ActionResultFlags.Success;
                        reward       = 1f;
                    }

                    return(reward);
                }
            }
        }

        return(-1f);
    }
Example #2
0
 private bool IsFlagSet(ActionResultFlags flags)
 {
     return((mFlags & flags) == flags);
 }
Example #3
0
 public ActionResult(ActionResultFlags flags)
 {
     mFlags = flags;
 }
Example #4
0
    protected override float EvaluateAction(float[] vectorAction, out ActionResultFlags resultFlags)
    {
        //resultFlags = ActionResultFlags.None;

        //currentTargetViewportPosition = lamp.WorldToViewportPoint(target.position);
        //bool targetIsInFrontOfCamera = currentTargetViewportPosition.z > 0;
        //float currentSqrOffset = ((Vector2)currentTargetViewportPosition).sqrMagnitude;
        //float maxSqrOffset = lamp.ViewPortExtends.sqrMagnitude;

        //if (targetIsInFrontOfCamera)
        //{
        //	if (currentSqrOffset <= academy.SuccessThreshold)
        //	{
        //		resultFlags |= ActionResultFlags.Success;
        //		return 1f;
        //	}

        //	return 1f - Mathf.Clamp01(currentSqrOffset / maxSqrOffset);
        //}

        //return -1f + Mathf.Clamp01(currentSqrOffset / maxSqrOffset);
        // ------------------------------------------------------------------------------------------------------------
        //resultFlags = ActionResultFlags.None;

        //currentTargetViewportPosition = lamp.WorldToViewportPoint(target.position);
        //bool targetIsInFrontOfCamera = currentTargetViewportPosition.z > 0;
        //float initialSqrOffset = ((Vector2)initialTargetViewportPosition).sqrMagnitude;
        //float currentSqrOffset = ((Vector2)currentTargetViewportPosition).sqrMagnitude;

        //if (targetIsInFrontOfCamera && currentSqrOffset <= academy.SuccessThreshold)
        //{
        //	resultFlags |= ActionResultFlags.Success;
        //	return 1f;
        //}

        //return targetIsInFrontOfCamera ? -Mathf.Clamp01(currentSqrOffset / initialSqrOffset) : -1f;
        // ------------------------------------------------------------------------------------------------------------
        resultFlags = ActionResultFlags.None;

        currentTargetViewportPosition = lamp.WorldToViewportPoint(target.position);
        bool  targetIsInFrontOfCamera = currentTargetViewportPosition.z > 0;
        float initialSqrOffset        = ((Vector2)initialTargetViewportPosition).sqrMagnitude;
        float currentSqrOffset        = ((Vector2)currentTargetViewportPosition).sqrMagnitude;
        float maxSqrOffset            = lamp.ViewPortExtends.sqrMagnitude;

        if (Mathf.Abs(currentTargetViewportPosition.x) <= 1f && Mathf.Abs(currentTargetViewportPosition.y) <= 1f)
        {
            if (targetIsInFrontOfCamera)
            {
                if (currentSqrOffset <= initialSqrOffset)
                {
                    float reward = Mathf.Pow(0.1f, currentSqrOffset / initialSqrOffset);

                    if (currentSqrOffset <= academy.SuccessThreshold)
                    {
                        resultFlags |= ActionResultFlags.Success;
                        reward       = 1f;
                    }

                    return(reward);
                }
            }
        }

        return(-1f);
    }