Exemplo n.º 1
0
 public void AddAction(ActionDST action)
 {
     if (NowCanDo(action.Name))
     {
         this.AvailableActions.Add(action);
     }
 }
Exemplo n.º 2
0
        protected MCTSNode Expand(MCTSNode parent, ActionDST action)
        {
            //Console.WriteLine("Expand...");

            MCTSNode child = new MCTSNode(parent.State.GenerateChildWorldModel())
            {
                Action = action,
                Parent = parent,
                Q      = 0,
                N      = 0
            };

            parent.ChildNodes.Add(child);
            child.Action.ApplyActionEffects(child.State);
            return(child);
        }
Exemplo n.º 3
0
        protected MCTSNode Expand(MCTSNode parent, ActionDST action)
        {
            WorldModelDST newState = parent.State.GenerateChildWorldModel();

            action.ApplyActionEffects(newState);

            var child = new MCTSNode(newState)
            {
                Action = action,
                Parent = parent,
            };

            parent.ChildNodes.Add(child);

            return(child);
        }
Exemplo n.º 4
0
        protected float Playout(WorldModelDST initialPlayoutState)
        {
            List <ActionDST> executableActions;
            ActionDST        action = null;
            int           randomIndex;
            WorldModelDST state = initialPlayoutState;

            while (this.CurrentDepth < this.MaxPlayoutDepthReached)
            {
                executableActions = state.GetExecutableActions();

                if (executableActions.Count > 0)
                {
                    this.CurrentDepth++;

                    randomIndex = this.RandomGenerator.Next(0, executableActions.Count);
                    action      = executableActions[randomIndex];
                    state       = state.GenerateChildWorldModel();
                    action.ApplyActionEffects(state);
                }
                else
                {
                    this.CurrentDepth++;
                }
            }

            if (this.CurrentDepth < this.MaxPlayoutDepthReached)
            {
                this.CurrentDepth = this.MaxPlayoutDepthReached;
            }

            //Console.WriteLine("Value Heuristic:");
            //Console.WriteLine(PlayoutHeuristic(state));
            //Console.WriteLine("");

            return(PlayoutHeuristic(state));
        }
Exemplo n.º 5
0
        protected MCTSNode Expand(MCTSNode parent, ActionDST action)
        {
            //TO DO

            return(new MCTSNode(new WorldModelDST()));
        }
Exemplo n.º 6
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));
            }
        }