/// <summary> /// Take an action /// </summary> /// <param name="vectorAction">The action to take</param> /// <param name="textAction">The name of the action</param> public override void AgentAction(float[] vectorAction, string textAction) { base.AgentAction(vectorAction, textAction); lastActions = vectorAction; lastLoss = MLActionFactory.GetLoss(vectorAction); if (!isFighting) { return; } // Only perform confident moves var confidence = MLActionFactory.GetProbabilityFromVector(MLActionFactory.GetAction(vectorAction), vectorAction); if (!Mathf.Approximately(confidence, 0) && confidence < minConfidence) { return; } // Try to perform action TryToTakeAction(MLActionFactory.GetAction(vectorAction)); // Interrupt dodges early if (endDodgeEarly && MLActionFactory.IsDodge(currentAction)) { if (MLActionFactory.IsPunch(lastEnemyState) && !MLActionFactory.IsPunch(opponent.GetCurrentAction())) { // Opponent finished punching, so stop dodging dodgeAction.Interrupt(); } } lastEnemyState = opponent.GetCurrentAction(); }
private bool IsGoodMove() { if (opponent == null) { return(false); } var opponentMove = opponent.GetCurrentAction(); var myMove = MLActionFactory.GetAction(lastActions); if (myMove == MLAction.NOTHING) { return(true); } if (MLActionFactory.IsPunch(opponentMove) && !MLActionFactory.IsDodge(myMove)) { return(false); } if (!MLActionFactory.IsPunch(opponentMove) && MLActionFactory.IsDodge(myMove)) { return(false); } return(true); }
private void Update() { var match = trainee.currentAction == coach.currentAction;//MLActionFactory.GetAction(trainee.lastActions) == MLActionFactory.GetAction(coach.lastActions); if (match) { matchingEvent.Invoke(); } var desiredAction = MLActionFactory.GetAction(coach.lastActions); var probability = MLActionFactory.GetProbabilityFromVector(desiredAction, trainee.lastActions); crossEntropy += MathUtils.CrossEntropy(probability); //correctness += probability; var alpha = 0.995f; runningAverageCorrectness = alpha * runningAverageCorrectness + (1 - alpha) * probability; AddSample(match); }
/// <summary> /// Collect the observations (internal punch) /// </summary> public override void CollectObservations() { AddVectorObs(!punchAction.IsOnCooldown() && !punchAction.IsRunning()); AddVectorObs(!dodgeAction.IsOnCooldown() && !dodgeAction.IsRunning()); bool[] move; int opponentComboState = 0; if (opponent != null) { move = new bool[] { opponent.currentAction == MLAction.PUNCH_RIGHT, opponent.currentAction == MLAction.PUNCH_LEFT, opponent.currentAction == MLAction.DODGE_LEFT, opponent.currentAction == MLAction.DODGE_RIGHT }; opponentComboState = opponent.comboTracker.GetState(); } else { move = new bool[] { false, false, false, false }; opponentComboState = 0; } AddVectorObs(Encoder.encodeInt(comboTracker.GetState(), 0, comboTracker.GetTotalStates())); foreach (var m in move) { AddVectorObs(m); } AddVectorObs(currentAction == MLAction.PUNCH_LEFT); AddVectorObs(currentAction == MLAction.PUNCH_RIGHT); AddVectorObs(currentAction == MLAction.DODGE_LEFT); AddVectorObs(currentAction == MLAction.DODGE_RIGHT); if (bufferSize >= maxBufferSize) { SetTextObs((isTeacher && isFighting && MLActionFactory.GetAction(lastActions) != MLAction.NOTHING) + "," + true); bufferSize = 0; } else { if (MLActionFactory.IsPunch(MLActionFactory.GetAction(lastActions))) { punchCount++; } else if (MLActionFactory.IsDodge(MLActionFactory.GetAction(lastActions))) { dodgeCount++; } if (isTeacher) { //Debug.Log(punchCount + ", " + dodgeCount); } var training = isTeacher && isFighting && (MLActionFactory.GetAction(lastActions) != MLAction.NOTHING || nothingBuffer < nothingBufferSize) && IsGoodMove(); if (training) { bufferSize++; } if (MLActionFactory.GetAction(lastActions) == MLAction.NOTHING) { nothingBuffer++; } SetTextObs(training + "," + false); } }