Browse Source

Merge old actionmask with new system

Development
Tobi 4 years ago
parent
commit
d75c202b8d
1 changed files with 52 additions and 45 deletions
  1. 52
    45
      Assets/Scripts/ML Cozmo/CozmoAgent.cs

+ 52
- 45
Assets/Scripts/ML Cozmo/CozmoAgent.cs View File

public Camera renderCamera; public Camera renderCamera;
[Tooltip("Reference to the CozmoMovement script")] [Tooltip("Reference to the CozmoMovement script")]
public CozmoMovementController movementController; public CozmoMovementController movementController;
[Tooltip("The time between decisions at inference")]
public float timeBetweenDecisionsAtInference; public float timeBetweenDecisionsAtInference;
[Tooltip("The amout of actions that should be remembered by the agent.")]
public int maxStoredMovementStates = 1; public int maxStoredMovementStates = 1;
[Tooltip("If activated the maxStoredMovementStates has no impact on the training.")]
public bool useOriginalActionMasking = true;


private Academy academy; // CozmoAcademy private Academy academy; // CozmoAcademy
private float timeSinceDecision; // time since last decision private float timeSinceDecision; // time since last decision
private int nearAreaLimit = 0; // X coordinate limit for the near to the imagecenter area private int nearAreaLimit = 0; // X coordinate limit for the near to the imagecenter area
private int centerOfImageX = 0; // Middle of the image in x direction private int centerOfImageX = 0; // Middle of the image in x direction
private MovementState lastChosenMovement = MovementState.Stop; // The last action/movement that was executed private MovementState lastChosenMovement = MovementState.Stop; // The last action/movement that was executed
private Queue<MovementState> lastActions = new Queue<MovementState>(); // Queue to store the last chosen Actions


private Queue<MovementState> lastActions = new Queue<MovementState>();

private double startTime = Time.time;
private double startTime = 0;


private void Start() private void Start()
{ {
startTime = Time.time;
academy = FindObjectOfType(typeof(CozmoAcademy)) as CozmoAcademy; academy = FindObjectOfType(typeof(CozmoAcademy)) as CozmoAcademy;
imageProcessor = renderCamera.GetComponent<ImageProcessor>(); imageProcessor = renderCamera.GetComponent<ImageProcessor>();
nearAreaLimit = (int)(renderCamera.targetTexture.width / 2 * NEAR_AREA_PERCENTAGE_OFFSET); nearAreaLimit = (int)(renderCamera.targetTexture.width / 2 * NEAR_AREA_PERCENTAGE_OFFSET);
} }


// Set ActionMask for training // Set ActionMask for training
// Needs to be called from CollectObservations()
private void SetMask() private void SetMask()
{ {
// Do not allow stop decision after a stop
if (lastChosenMovement == MovementState.Stop)
if (useOriginalActionMasking)
{ {
SetActionMask(STOP);
// Stop is never allowed
switch (lastChosenMovement)
{
// Do not allow stop decision after a stop
case (MovementState.Stop):
SetActionMask(STOP);
break;
// Do not allow stop after forward
case (MovementState.Forward):
SetActionMask(STOP);
break;
// Do not allow stop & left after right
case (MovementState.Right):
SetActionMask(STOP);
SetActionMask(LEFT);
break;
// Do not allow stop & right after left
case (MovementState.Left):
SetActionMask(STOP);
SetActionMask(RIGHT);
break;
default:
throw new ArgumentException("Invalid MovementState.");
}
} }

// Do not allow left decision if right was in the last actions
if (lastActions.Contains(MovementState.Right))
else
{ {
SetActionMask(LEFT);
}
// Do not allow stop decision after a stop
if (lastChosenMovement == MovementState.Stop)
{
SetActionMask(STOP);
}


// Do not allow right decision if left was in the last actions
if (lastActions.Contains(MovementState.Left))
{
SetActionMask(RIGHT);
}
// Do not allow left decision if right was in the last actions
if (lastActions.Contains(MovementState.Right))
{
SetActionMask(LEFT);
}


//switch (lastChosenMovement)
//{
// // Do not allow stop decision after a stop
// case (MovementState.Stop):
// SetActionMask(STOP);
// break;
// // Do not allow stop after forward
// case (MovementState.Forward):
// //SetActionMask(STOP);
// break;
// // Do not allow stop & left after right
// case (MovementState.Right):
// //SetActionMask(STOP);
// if (lastActions.Contains(MovementState.))
// SetActionMask(LEFT);
// break;
// // Do not allow stop & right after left
// case (MovementState.Left):
// //SetActionMask(STOP);
// SetActionMask(RIGHT);
// break;
// default:
// throw new ArgumentException("Invalid MovementState.");
//}
// Do not allow right decision if left was in the last actions
if (lastActions.Contains(MovementState.Left))
{
SetActionMask(RIGHT);
}
}
} }


// to be implemented by the developer // to be implemented by the developer
case STOP: case STOP:
movementController.currentMovementState = MovementState.Stop; movementController.currentMovementState = MovementState.Stop;
lastChosenMovement = MovementState.Stop; lastChosenMovement = MovementState.Stop;
//Test
SetReward(-0.02f); SetReward(-0.02f);
break; break;
case FORWARD: case FORWARD:
movementController.currentMovementState = MovementState.Forward; movementController.currentMovementState = MovementState.Forward;
lastChosenMovement = MovementState.Forward; lastChosenMovement = MovementState.Forward;
//Test
SetReward(0.02f); SetReward(0.02f);
break; break;
case RIGHT: case RIGHT:
movementController.currentMovementState = MovementState.Right; movementController.currentMovementState = MovementState.Right;
lastChosenMovement = MovementState.Right; lastChosenMovement = MovementState.Right;
//Test
SetReward(0.01f); SetReward(0.01f);
break; break;
case LEFT: case LEFT:
movementController.currentMovementState = MovementState.Left; movementController.currentMovementState = MovementState.Left;
lastChosenMovement = MovementState.Left; lastChosenMovement = MovementState.Left;
//Test
SetReward(0.01f); SetReward(0.01f);
break; break;
default: default:
throw new ArgumentException("Invalid action value. Stop movement."); throw new ArgumentException("Invalid action value. Stop movement.");
} }


CollectLastMovementStates(lastChosenMovement);
if (!useOriginalActionMasking)
CollectLastMovementStates(lastChosenMovement);


// Render new image after movement in order to update the centerOfGravity // Render new image after movement in order to update the centerOfGravity
if (renderCamera != null) if (renderCamera != null)

Loading…
Cancel
Save