Пример #1
0
        //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));
            }
        }