private void SelectionAndExpantion() { int option = ChooseRandomAction(); if (!currentNode.checkChildren(option)) { MonteCarloTree.Node nodeToAdd = new MonteCarloTree.Node(); nodeToAdd.NodeOption = option; currentNode.AddChild(nodeToAdd); } float highestUCB = -1.0f; MonteCarloTree.Node testNode = currentNode; for (int i = 0; i < currentNode.GetNumberOfChildren(); i++) { float UCB = UpperConfidenceBound(currentNode.GetChild(i)); if (highestUCB < UCB) { testNode = currentNode.GetChild(i); highestUCB = UCB; } } currentNode = testNode; nodeOptions = (NodeOptions)currentNode.NodeOption; }
private void BackpropagationIfLost() { do { currentNode.VisitCount = currentNode.VisitCount + 1; currentNode = currentNode.ParentNode; } while (currentNode != null); currentNode = rootNode; }
// Use this for initialization void Start() { // Initialise the accessable script components _agentData = GetComponent <AgentData>(); _agentActions = GetComponent <AgentActions>(); _agentSenses = GetComponentInChildren <Sensing>(); _agentInventory = GetComponentInChildren <InventoryController>(); currentNode = rootNode; startingPostion = transform.position; // nodeOptions = NodeOptions.Nothing; }
private float UpperConfidenceBound(MonteCarloTree.Node node) { float UCB = 0.0f; if (node.VisitCount > 0) { UCB = (node.WinCount / node.VisitCount) + (ExplorationFactor * Mathf.Sqrt(Mathf.Log(node.ParentNode.VisitCount) / node.VisitCount)); } else if (isExploring) { UCB = int.MaxValue; } return(UCB); }
private void Checks() { if (_agentInventory.HasItem(Names.HealthKit)) { if (_agentData.CurrentHitPoints <= 50) { _agentActions.UseItem(_agentInventory.GetItem(Names.HealthKit)); } } GameObject ObjectToCheck = _agentSenses.GetObjectInViewByName(Names.HealthKit); if (ObjectToCheck != null) { if (!_agentInventory.HasItem(Names.HealthKit)) { if (_agentSenses.IsItemInReach(ObjectToCheck)) { _agentActions.CollectItem(ObjectToCheck); Simulation((int)nodeOptions); } else if (Vector3.Distance(transform.position, ObjectToCheck.transform.position) < 7.0f) { _agentActions.MoveTo(ObjectToCheck); } } } ObjectToCheck = _agentSenses.GetObjectInViewByName(Names.PowerUp); if (ObjectToCheck != null) { if (!_agentInventory.HasItem(Names.PowerUp)) { if (_agentSenses.IsItemInReach(ObjectToCheck)) { _agentActions.CollectItem(ObjectToCheck); Simulation((int)nodeOptions); } else if (Vector3.Distance(transform.position, ObjectToCheck.transform.position) < 7.0f) { _agentActions.MoveTo(ObjectToCheck); } } } if (this.tag == Tags.BlueTeam) { ObjectToCheck = _agentSenses.GetObjectInViewByName(Names.RedFlag); if (ObjectToCheck != null) { if (_agentSenses.IsItemInReach(ObjectToCheck)) { _agentActions.CollectItem(ObjectToCheck); Simulation((int)nodeOptions); } } ObjectToCheck = _agentSenses.GetObjectInViewByName(Names.BlueFlag); if (ObjectToCheck != null) { if (_agentSenses.IsItemInReach(ObjectToCheck)) { ObjectToCheck.GetComponent <Flag>().ResetPositionBlue(); Simulation((int)nodeOptions); } } if (_agentData.HasEnemyFlag) { if (Vector3.Distance(transform.position, _agentData.FriendlyBase.transform.position) <= 5.0f) { _agentActions.DropItem(_agentInventory.GetItem(Names.RedFlag)); hasWon = true; ResetPosition(); hasWon = false; currentNode = rootNode; Debug.Log("win" + this.gameObject); } } } if (this.tag == Tags.RedTeam) { ObjectToCheck = _agentSenses.GetObjectInViewByName(Names.BlueFlag); if (ObjectToCheck != null) { if (_agentSenses.IsItemInReach(ObjectToCheck)) { _agentActions.CollectItem(ObjectToCheck); Simulation((int)nodeOptions); } } ObjectToCheck = _agentSenses.GetObjectInViewByName(Names.RedFlag); if (ObjectToCheck != null) { if (_agentSenses.IsItemInReach(ObjectToCheck)) { ObjectToCheck.GetComponent <Flag>().ResetPositionRed(); Simulation((int)nodeOptions); } } if (_agentData.HasEnemyFlag) { if (Vector3.Distance(transform.position, _agentData.FriendlyBase.transform.position) <= 5.0f) { _agentActions.DropItem(_agentInventory.GetItem(Names.BlueFlag)); hasWon = true; ResetPosition(); hasWon = false; Debug.Log("win" + this.gameObject); } } } }