public TicTacToeMCTSStrategy(int iterations, MCTSAlgorithm mcts = null) { if (mcts == null) { mcts = new MCTSAlgorithm(new UCTTreeNodeCreator()); } this.mcts = mcts; this.iterations = iterations; }
public IEnumerable <string> GetMacromanagementStuff() { Current_Tree = new MCTSAlgorithm(Owned_Agent.GetDeepCopy(), Enemy_Agent.GetDeepCopy()); foreach (var result in Current_Tree.GeneratePredictedAction(Owned_Agent.Created_Time.AddSeconds(15))) { if (result == null) { yield return("SURRENDER"); break; } yield return(result.Item1); } }
public IEnumerable <IEnumerable <double> > GetMacromanagementAccuracyReport(int number_of_simulations, AIAlgorithm algorithm) { var pomdp_results = new List <List <CostWorth> >(); var mcts_results = new List <List <CostWorth> >(); switch (algorithm) { case AIAlgorithm.POMDP: Current_Tree = new POMDPAlgorithm(Owned_Agent.GetDeepCopy(), Enemy_Agent.GetDeepCopy()); break; case AIAlgorithm.MCTS: Current_Tree = new MCTSAlgorithm(Owned_Agent.GetDeepCopy(), Enemy_Agent.GetDeepCopy()); break; } try { for (int simulated = 0; simulated < number_of_simulations; simulated++) { mcts_results.Add(new List <CostWorth>()); DateTime end; if (Owned_Agent.EndTime > Enemy_Agent.EndTime) { end = Owned_Agent.EndTime; } else { end = Enemy_Agent.EndTime; } foreach (var result in Current_Tree.GeneratePredictedAction(end)) { if (result == null) { break; } mcts_results[0].Add(result.Item2); } } } catch (ArgumentException ex) { } var random = Services.ModelRepositoryService.ModelService.GetModelService(); //Perform Euclidean Operations try { var enemy_basis = String.Join(",", Enemy_Agent.Basis.Select(basis => Convert.ToDouble(basis.Item3))); } catch (ArgumentNullException ex) { } string owned_basis = String.Join(",", Owned_Agent.Basis.Select(basis => Convert.ToDouble(basis.Item3)).Take(5)); var owned_results_mcts = mcts_results.Select(result => String.Join(",", result.Select(costworth => Convert.ToDouble(costworth)))).Take(5); yield return(random.GetEuclideanMetric(owned_basis, owned_results_mcts)); }
//This is where the main body of the MCTS Search must be implemented private IEnumerable <DynamicPropertyResult> MCTSSearch(IQueryContext context, Name actionVar, Name targetVar) { //How to clone the KB with our JSON serializer var jsonSerializer = new JSONSerializer(); var memStream = new MemoryStream(); var json = jsonSerializer.SerializeToJson(this.m_kb); var kbCloned = jsonSerializer.DeserializeFromJson <KB>(json); //Escrever comentário if (this.NextActionInfo.Item1 != "") { NextAction PriorityAction = new NextAction(NextActionInfo.Item1, kbCloned); if (this.NextActionInfo.Item2 <= 1) { this.NextActionInfo = new Pair <string, int>("", 0); } else { this.NextActionInfo.Item2 -= 1; } Pair <string, string> pairOfPriorityAction = PriorityAction.ConstructNextAction(); this.ToDoActionsList.Add(pairOfPriorityAction); } if (this.ToDoActionsList.Count == 0) { PreWorldState preWorldState = new PreWorldState(kbCloned); //, this.UnequippableTorches, this.EquippedItems); WorldModelDST worldModel = new WorldModelDST(preWorldState); foreach (var action in worldModel.AvailableActions) { Console.WriteLine(action.Name); } Console.WriteLine(""); this.Mcts = new MCTSAlgorithm(worldModel); this.Mcts.InitializeMCTSearch(); ActionDST MacroAction = this.Mcts.Run(); //this.LastActionInfo = MacroAction.Name; this.ToDoActionsList = MacroAction.Decompose(preWorldState); this.NextActionInfo = MacroAction.NextActionInfo(); this.NextActionInfo.Item1 = preWorldState.CompleteNextActionInfo(this.NextActionInfo.Item1); } Pair <string, string> CurrentAction = this.ToDoActionsList[0]; this.ToDoActionsList.Remove(CurrentAction); Console.WriteLine("Next Action:"); Console.WriteLine(CurrentAction.Item1 + " " + CurrentAction.Item2); Console.WriteLine(""); //UpdateEquippedItems(CurrentAction); var actionSub = new Substitution(actionVar, new ComplexValue(Name.BuildName(CurrentAction.Item1))); var targetSub = new Substitution(targetVar, new ComplexValue(Name.BuildName(CurrentAction.Item2))); //var actionSub = new Substitution(actionVar, new ComplexValue(Name.BuildName("Action(WALKTO, -, "+ posxWalter.ToString() +", " + poszWalter.ToString() + ", -)"))); //var targetSub = new Substitution(targetVar, new ComplexValue(Name.BuildName("-"))); foreach (var subSet in context.Constraints) { subSet.AddSubstitution(actionSub); subSet.AddSubstitution(targetSub); yield return(new DynamicPropertyResult(new ComplexValue(Name.BuildName(true), 1.0f), subSet)); } }