private void TryToTakeAction(MLAction action) { if (IsPerformingMove()) { return; } if (isAnim) { return; } if (MLActionFactory.IsPunch(action)) { if (punchAction.IsOnCooldown()) { return; } punchAction.Run(action == MLAction.PUNCH_LEFT ? Direction.LEFT : Direction.RIGHT); } if (MLActionFactory.IsDodge(action)) { if (dodgeAction.IsOnCooldown()) { return; } dodgeAction.Run(action == MLAction.DODGE_LEFT ? Direction.LEFT : Direction.RIGHT); } }
/// <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(); }
/// <summary> /// Given the raw vectorActions, get the probability for a single action /// </summary> /// <param name="action">The action</param> /// <param name="vectorActions">The vector actions</param> /// <returns>The probability between 0 and 1 for performing the action</returns> public static float GetProbabilityFromVector(MLAction action, float[] vectorActions) { if (vectorActions.Length < 1) { return(1); } float scale = 10000; return(vectorActions[ActionToInt(action) + 1] / scale); }
// Update is called once per frame void Update() { if (!reactive) { SetEnabled(leftPunch); SetEnabled(rightPunch); SetEnabled(leftDodge); SetEnabled(rightDodge); SetButtonColor(leftPunch, buttonUnpressed); SetButtonColor(rightPunch, buttonUnpressed); SetButtonColor(leftDodge, buttonUnpressed); SetButtonColor(rightDodge, buttonUnpressed); return; } MLAction action = boxer.currentAction; bool punchCooldown = boxer.punchAction.IsOnCooldown(); bool dodgeCooldown = boxer.dodgeAction.IsOnCooldown(); SetButtonColor(leftPunch, Input.GetKey(KeyCode.F) ? buttonPressed : buttonUnpressed); SetButtonColor(rightPunch, Input.GetKey(KeyCode.J) ? buttonPressed : buttonUnpressed); SetButtonColor(leftDodge, Input.GetKey(KeyCode.D) ? buttonPressed : buttonUnpressed); SetButtonColor(rightDodge, Input.GetKey(KeyCode.K) ? buttonPressed : buttonUnpressed); if (action != MLAction.NOTHING) { SetDisabled(leftPunch); SetDisabled(rightPunch); SetDisabled(leftDodge); SetDisabled(rightDodge); } else { SetEnabled(leftPunch); SetEnabled(rightPunch); SetEnabled(leftDodge); SetEnabled(rightDodge); } if (punchCooldown) { SetDisabled(leftPunch); SetDisabled(rightPunch); } if (dodgeCooldown) { SetDisabled(leftDodge); SetDisabled(rightDodge); } }
/// <summary> /// Handles the damage taken from an opposing punch /// </summary> /// <param name="punch">The punch</param> /// <returns>The outcome of the punch</returns> public PunchOutcome onPunched(MLAction action, float damage) { stats.AddOpponentPunch(); // Dodged if (!inverted) // If the player is flipped, this appears opposite { if (currentAction == MLAction.DODGE_LEFT && action == MLAction.PUNCH_LEFT) { stats.AddSuccessfulDodge(); //if (!isTeacher) // dodgeAction.Interrupt(); return(PunchOutcome.DODGED); } else if (currentAction == MLAction.DODGE_RIGHT && action == MLAction.PUNCH_RIGHT) { stats.AddSuccessfulDodge(); //if (!isTeacher) // dodgeAction.Interrupt(); return(PunchOutcome.DODGED); } } else { if (currentAction == MLAction.DODGE_RIGHT && action == MLAction.PUNCH_LEFT) { stats.AddSuccessfulDodge(); //if (!isTeacher) // dodgeAction.Interrupt(); return(PunchOutcome.DODGED); } else if (currentAction == MLAction.DODGE_LEFT && action == MLAction.PUNCH_RIGHT) { stats.AddSuccessfulDodge(); //if (!isTeacher) // dodgeAction.Interrupt(); return(PunchOutcome.DODGED); } } // Hit TakeDamage(damage); if (IsKO()) { return(PunchOutcome.KO); } else { return(PunchOutcome.HIT); } }
private void OnEnable() { var idx = 0; // 0 - 4 MLAction[][] possibleMoves = new MLAction[][] { new MLAction[] { MLAction.PUNCH_LEFT }, new MLAction[] { MLAction.PUNCH_RIGHT }, new MLAction[] { MLAction.PUNCH_LEFT, MLAction.PUNCH_RIGHT }, new MLAction[] { MLAction.PUNCH_RIGHT, MLAction.PUNCH_LEFT }, new MLAction[] { MLAction.PUNCH_LEFT, MLAction.PUNCH_RIGHT, MLAction.PUNCH_LEFT } }; moves = new List <MLAction>(possibleMoves[idx]); shouldDodge = idx == 1 || idx == 3; }
public override float[] Decide(List <float> vectorObs, List <Texture2D> visualObs, float reward, bool done, List <float> memory) { if (!useOldVersion) { if (Mathf.Approximately(seqStartTime, 0)) { Reset(); } if (Time.time - seqStartTime > 15 && seqIdx == 0) { seqIdx = 1; moveIdx = 0; didAction = false; nothingStartTime = Time.time; } else if (Time.time - seqStartTime > 30 && seqIdx == 1) { seqIdx = 2; moveIdx = 0; didAction = false; nothingStartTime = Time.time; } MLAction nextMove = runMoves(vectorObs, moveSequences[seqIdx]); return(MLActionFactory.GetVectorAction(nextMove)); } else { if (done) { moveIdx = 0; return(NOTHING); } MLInput input = new MLInput(vectorObs.ToArray()); if (input.IsPunchReady() && input.IsDodgeReady()) // Can punch / dodge { float[] move = moves[moveIdx]; moveIdx = (moveIdx + 1) % moves.Length; return(move); } return(NOTHING); } }
private static int ActionToInt(MLAction action) { switch (action) { case MLAction.NOTHING: return(0); case MLAction.PUNCH_LEFT: return(1); case MLAction.PUNCH_RIGHT: return(2); case MLAction.DODGE_LEFT: return(3); case MLAction.DODGE_RIGHT: return(4); } return(0); }
public override float[] Decide(List <float> vectorObs, List <Texture2D> visualObs, float reward, bool done, List <float> memory) { MLInput input = new MLInput(vectorObs.ToArray()); MLAction currentMove = input.GetMyMove(); //if (MLActionFactory.IsDodge(currentMove)) //{ // return MLActionFactory.GetVectorAction(currentMove); //} if (currentMove != MLAction.NOTHING) { var maxMoveCount = MLActionFactory.IsDodge(currentMove) ? maxDodgeCount : maxPunchCount; if (moveCount < maxMoveCount) { moveCount++; return(MLActionFactory.GetVectorAction(currentMove)); } return(MLActionFactory.GetVectorAction(MLAction.NOTHING)); } else { moveCount = 0; } if (Input.GetKey(KeyCode.F)) { return(MLActionFactory.GetVectorAction(MLAction.PUNCH_LEFT)); } else if (Input.GetKey(KeyCode.J)) { return(MLActionFactory.GetVectorAction(MLAction.PUNCH_RIGHT)); } else if (Input.GetKey(KeyCode.D)) { return(MLActionFactory.GetVectorAction(MLAction.DODGE_LEFT)); } else if (Input.GetKey(KeyCode.K)) { return(MLActionFactory.GetVectorAction(MLAction.DODGE_RIGHT)); } return(MLActionFactory.GetVectorAction(MLAction.NOTHING)); }
private MLAction runNextMove(MLAction[] moves, MLInput input) { didAction = false; nothingStartTime = Time.time; moveIdx = (moveIdx + 1) % moves.Length; MLAction nextMove = moves[moveIdx]; if (MLActionFactory.IsPunch(nextMove)) { didAction = input.IsPunchReady(); } else if (MLActionFactory.IsDodge(nextMove)) { didAction = input.IsDodgeReady(); } return(nextMove); }
public float[] OnlyDoAndDodge(List <float> vectorObs, MLAction action) { MLInput input = new MLInput(vectorObs.ToArray()); if (lastAction != input.GetOpponentAction() && input.GetOpponentAction() == MLAction.PUNCH_LEFT) { lastAction = input.GetOpponentAction(); return(MLActionFactory.GetVectorAction(MLAction.DODGE_RIGHT)); } if (lastAction != input.GetOpponentAction() && input.GetOpponentAction() == MLAction.PUNCH_RIGHT) { lastAction = input.GetOpponentAction(); return(MLActionFactory.GetVectorAction(MLAction.DODGE_LEFT)); } lastAction = input.GetOpponentAction(); return(MLActionFactory.GetVectorAction(action)); }
/// <summary> /// Get the vector action from an action /// </summary> /// <param name="action">The action</param> /// <returns>The vectorized form of the action</returns> public static float[] GetVectorAction(MLAction action) { float scale = 10000f; switch (action) { case MLAction.NOTHING: return(new float[] { 0f, scale, 0f, 0f, 0f, 0f, 0f }); case MLAction.PUNCH_LEFT: return(new float[] { 1f, 0f, scale, 0f, 0f, 0f, 0f }); case MLAction.PUNCH_RIGHT: return(new float[] { 2f, 0f, 0f, scale, 0f, 0f, 0f }); case MLAction.DODGE_LEFT: return(new float[] { 3f, 0f, 0f, 0f, scale, 0f, 0f }); case MLAction.DODGE_RIGHT: return(new float[] { 4f, 0f, 0f, 0f, 0f, scale, 0f }); } return(new float[] { 0f, scale, 0f, 0f, 0f, 0f, 0f }); }
public virtual void Initialize(BaseAgent agent) { foreach (QuarkGroup group in Groups) { QuarkGroup qg = QuarkGroup.Instantiate(group); qg.Initialize(agent); myGroups.Add(qg); } foreach (MLObs obs in Observations) { MLObs o = MLObs.Instantiate(obs); o.Initialize(agent); myObservations.Add(o); } foreach (MLReset reset in Resets) { MLReset r = MLReset.Instantiate(reset); r.Initialize(agent); myResets.Add(r); } foreach (MLReward reward in Rewards) { MLReward r = MLReward.Instantiate(reward); r.Initialize(agent); myRewards.Add(r); } foreach (MLAction action in Actions) { MLAction a = MLAction.Instantiate(action); a.Initialize(agent); myActions.Add(a); } }
/// <summary> /// Initialize the agent /// </summary> public override void InitializeAgent() { base.InitializeAgent(); stats = new BoxerStats(); health = GetComponent <Health>(); comboTracker = GetComponent <ComboTracker>(); bufferSize = 0; dodgeAction = new Action(dodgeDuration, dodgeCooldown, dodgeEventDelay); if (isTeacher) { dodgeAction = new Action(dodgeCooldown); } dodgeAction.animationStart.AddListener(RegisterDodge); dodgeAction.animationEnd.AddListener(DeregisterDodge); punchAction = new Action(punchDuration, punchCooldown, punchEventDelay); punchAction.animationStart.AddListener(RegisterPunch); punchAction.animationEnd.AddListener(DeregisterPunch); hitEvent = new UnityEvent(); currentAction = MLAction.NOTHING; }
/// <summary> /// Determine if an action is a punch /// </summary> /// <param name="action">The action</param> /// <returns>True if the action is a punch, false otherwise</returns> public static bool IsPunch(MLAction action) { return(action == MLAction.PUNCH_LEFT || action == MLAction.PUNCH_RIGHT); }
/// <summary> /// Determine if an action is a dodge /// </summary> /// <param name="action">The action</param> /// <returns>True if the action is a dodge, false otherwise</returns> public static bool IsDodge(MLAction action) { return(action == MLAction.DODGE_LEFT || action == MLAction.DODGE_RIGHT); }
public float[] RepeatActions(List <float> vectorObs, List <MLAction> actions, bool shouldDodge) { MLInput input = new MLInput(vectorObs.ToArray()); // Sequence if (input.IsDodgeReady()) // Can punch / dodge { // Dodging if (shouldDodge && lastAction != input.GetOpponentAction() && input.GetOpponentAction() == MLAction.PUNCH_LEFT) { lastAction = input.GetOpponentAction(); actionIdx = 0; return(MLActionFactory.GetVectorAction(MLAction.DODGE_RIGHT)); } if (shouldDodge && lastAction != input.GetOpponentAction() && input.GetOpponentAction() == MLAction.PUNCH_RIGHT) { lastAction = input.GetOpponentAction(); actionIdx = 0; return(MLActionFactory.GetVectorAction(MLAction.DODGE_LEFT)); } lastAction = input.GetOpponentAction(); } if (input.IsPunchReady()) { int myComboState = input.GetMyComboState(); if (actions.Count == 2) { switch (myComboState) { case 0: return(MLActionFactory.GetVectorAction(actions[0])); case 1: return(MLActionFactory.GetVectorAction(actions[1])); case 2: return(MLActionFactory.GetVectorAction(actions[1])); default: return(MLActionFactory.GetVectorAction(MLAction.NOTHING)); } } if (actions.Count == 3) { switch (myComboState) { case 0: return(MLActionFactory.GetVectorAction(actions[0])); case 1: return(MLActionFactory.GetVectorAction(actions[1])); case 2: return(MLActionFactory.GetVectorAction(actions[1])); case 3: return(MLActionFactory.GetVectorAction(actions[2])); case 4: return(MLActionFactory.GetVectorAction(actions[2])); default: return(MLActionFactory.GetVectorAction(MLAction.NOTHING)); } } return(MLActionFactory.GetVectorAction(actions[0])); } return(MLActionFactory.GetVectorAction(MLAction.NOTHING)); }
private void DeregisterPunch(Direction direction) { currentAction = MLAction.NOTHING; }
private void RegisterPunch(Direction direction) { stats.AddPunch(); currentAction = direction == Direction.LEFT ? MLAction.PUNCH_LEFT : MLAction.PUNCH_RIGHT; }
private void RegisterDodge(Direction direction) { stats.AddDodge(); currentAction = direction == Direction.LEFT ? MLAction.DODGE_LEFT : MLAction.DODGE_RIGHT; }
public float[] OnlyDo(List <float> vectorObs, MLAction action) { return(MLActionFactory.GetVectorAction(action)); }