public override void InitializeMCTSearch() { base.InitializeMCTSearch(); lastAction = null; lastActionValue = UnityEngine.Mathf.Infinity; if (BestSelection) { lastestActions.Clear(); } }
private MCTSNode Expand(MCTSNode parent, GOB.Action action) { MCTSNode childNode = new MCTSNode(parent.State.GenerateChildWorldModel()) { Action = action, Parent = parent, PlayerID = 0, }; childNode.Action.ApplyActionEffects(childNode.State); childNode.State.CalculateNextPlayer(); parent.ChildNodes.Add(childNode); return(childNode); }
protected override Reward Playout(WorldModel initialPlayoutState) { WorldModel currentState = initialPlayoutState; while (!currentState.IsTerminal()) { GOB.Action[] actions = currentState.GetExecutableActions(); if (actions.Length == 0) { continue; } int index = this.RandomGenerator.Next(0, actions.Length); GOB.Action action = actions[index]; Pair <int, Action> pair = new Pair <int, Action>(currentState.GetNextPlayer(), action); ActionHistory.Add(pair); currentState = currentState.GenerateChildWorldModel(); action.ApplyActionEffects(currentState); this.CurrentDepth++; } Reward reward = new Reward(); reward.Value = currentState.GetScore(); return(reward); }
protected override MCTSNode Selection(MCTSNode initialNode) { GOB.Action nextAction = initialNode.State.GetNextAction(); var currentWorld = initialNode.State; if (BiasSelection) { if (lastAction != null) { //vai estabilizando o mcts para valores mais baixos de HValues while (nextAction != null && nextAction.getHValue(currentWorld) > lastActionValue) { nextAction = initialNode.State.GetNextAction(); } //senao houver HValues mais baixos faz reset lastAction = nextAction; if (lastAction != null) { lastActionValue = nextAction.getHValue(currentWorld); } } else { lastAction = nextAction; if (lastAction != null) { lastActionValue = nextAction.getHValue(currentWorld); } } } else if (BestSelection) { var actions = currentWorld.GetExecutableActions(); var smallestNumber = UnityEngine.Mathf.Infinity; GOB.Action smallestAction = null; if (actions != null) { foreach (GOB.Action a in actions) { var value = a.getHValue(currentWorld); if (smallestNumber > value && !lastestActions.Contains(a)) { smallestNumber = value; smallestAction = a; } } lastestActions.Add(smallestAction); nextAction = smallestAction; } } //nextAction = currentNode.State.GetExecutableActions()[RandomGenerator.Next(currentNode.State.GetExecutableActions().Length)]; /* * while v is nonterminal do * if v not fully expanded then * return Expand(v) * else * v < -BestChild(v) * return v */ MCTSNode currentNode = initialNode; while (!currentNode.State.IsTerminal()) { if (nextAction != null) { return(Expand(currentNode, nextAction)); } else { currentNode = BestUCTChild(currentNode); nextAction = currentNode.State.GetNextAction(); } } return(currentNode); }