예제 #1
0
        public void Deserialize()
        {
            GroundActionFactory.GroundActions = new List <IOperator>();
            GroundActionFactory.GroundLibrary = new Dictionary <int, IOperator>();
            foreach (var file in Directory.GetFiles(Parser.GetTopDirectory() + @"Cached\CachedOperators\", testDomainName + "_" + testProblem.Name + "*.CachedOperator"))
            {
                var op = BinarySerializer.DeSerializeObject <IOperator>(file);
                GroundActionFactory.GroundActions.Add(op);
                GroundActionFactory.GroundLibrary[op.ID] = op;
            }
            // THIS is so that initial and goal steps created don't get matched with these
            Operator.SetCounterExternally(GroundActionFactory.GroundActions.Count + 1);

            Console.WriteLine("Finding Statics");
            GroundActionFactory.DetectStatics();
            foreach (var stat in GroundActionFactory.Statics)
            {
                Console.WriteLine(stat);
            }

            //Console.WriteLine("\nCmap\n");

            //var cmap = BinarySerializer.DeSerializeObject<TupleMap<IPredicate, List<int>>>(CausalMapFileName + ".CachedCausalMap");
            //CacheMaps.CausalTupleMap = cmap;

            //Console.WriteLine("\nTmap\n");
            //var tcmap = BinarySerializer.DeSerializeObject<TupleMap<IPredicate, List<int>>>(ThreatMapFileName + ".CachedThreatMap");
            //CacheMaps.ThreatTupleMap = tcmap;
        }
예제 #2
0
 public void Deserialize()
 {
     GroundActionFactory.GroundActions = new List <IOperator>();
     GroundActionFactory.GroundLibrary = new Dictionary <int, IOperator>();
     foreach (var file in Directory.GetFiles(Parser.GetTopDirectory() + @"Cached\CachedOperators\", testDomainName + "_" + testProblem.Name + "*.CachedOperator"))
     {
         var op = BinarySerializer.DeSerializeObject <IOperator>(file);
         GroundActionFactory.GroundActions.Add(op);
         GroundActionFactory.GroundLibrary[op.ID] = op;
     }
     // THIS is so that initial and goal steps created don't get matched with these
     Operator.SetCounterExternally(GroundActionFactory.GroundActions.Count + 1);
 }
예제 #3
0
        /// <summary>
        /// The base constructor.
        /// </summary>
        /// <param name="planner">The planner to use.</param>
        /// <param name="domain">The base domain object.</param>
        /// <param name="problem">The base problem object.</param>
        /// <param name="path">The path used to save files to disk.</param>
        /// <param name="domainRevision">Whether to use domain revision.</param>
        /// <param name="eventRevision">Whether to use event revision.</param>
        /// <param name="superpositionManipulation">Whether to use superposition manipulation.</param>
        public MediationTree(Domain domain, Problem problem, string path, bool domainRevision, bool eventRevision, bool superpositionManipulation)
        {
            // Set the path.
            this.path = path;

            // Check each path to see if it exists. If not, create the folder.
            if (!File.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            // If data already exists, load it from memory.
            if (File.Exists(path + "mediationtree"))
            {
                data = BinarySerializer.DeSerializeObject <MediationTreeData>(path + "mediationtree");
            }
            // Otherwise, initialize a new tree.
            else
            {
                data.eventRevision             = eventRevision;
                data.domainRevision            = domainRevision;
                data.superpositionManipulation = superpositionManipulation;

                // Store the domain and problem.
                data.domain  = domain;
                data.problem = problem;

                // Store the player's name.
                data.player = problem.Player.ToLower();

                // Get and store the NPC names.
                data.npcs = GetNPCs();

                // Establish the turn order.
                data.turnOrder = GetTurnOrder();

                // Create a new hashtable to store tree edges.
                data.tree = new Hashtable();

                // Initialize the session counters.
                data.nodeCounter    = 0;
                data.lowestDepth    = 0;
                data.goalStateCount = 0;
                data.deadEndCount   = 0;

                // Create the tree's root node.
                MediationTreeNode root = CreateNode(domain, problem, null);
            }
        }
예제 #4
0
        public void DecacheSteps()
        {
            Parser.path = @"D:\documents\frostbow\";
            var FileName = GetFileName();

            GroundActionFactory.GroundActions = new List <IOperator>();
            GroundActionFactory.GroundLibrary = new Dictionary <int, IOperator>();
            foreach (var file in Directory.GetFiles(Parser.GetTopDirectory() + @"Cached\CachedOperators\UnityBlocksWorld\", problemname + "*.CachedOperator"))
            {
                var op = BinarySerializer.DeSerializeObject <IOperator>(file);
                GroundActionFactory.GroundActions.Add(op);
                GroundActionFactory.GroundLibrary[op.ID] = op;
            }
            // THIS is so that initial and goal steps created don't get matched with these
            Operator.SetCounterExternally(GroundActionFactory.GroundActions.Count + 1);
        }
예제 #5
0
        public static void Deserialize(string directory)
        {
            GroundActionFactory.GroundActions = new List <IOperator>();
            GroundActionFactory.GroundLibrary = new Dictionary <int, IOperator>();
            foreach (var file in Directory.GetFiles(directory + "*.CachedOperator"))
            {
                var op = BinarySerializer.DeSerializeObject <IOperator>(file);
                GroundActionFactory.GroundActions.Add(op);
                GroundActionFactory.GroundLibrary[op.ID] = op;
            }
            // THIS is so that initial and goal steps created don't get matched with these
            Operator.SetCounterExternally(GroundActionFactory.GroundActions.Count + 1);

            Console.WriteLine("Finding Statics");
            GroundActionFactory.DetectStatics();
            foreach (var stat in GroundActionFactory.Statics)
            {
                Console.WriteLine(stat);
            }
        }
예제 #6
0
        public void DecacheSteps()
        {
            Parser.path = @"D:\documents\frostbow\";
            var FileName = GetFileName();

            GroundActionFactory.GroundActions = new List <IOperator>();
            GroundActionFactory.GroundLibrary = new Dictionary <int, IOperator>();
            int maxSeen     = 0;
            int maxStepSeen = 0;

            foreach (var file in Directory.GetFiles(Parser.GetTopDirectory() + @"Cached\CachedOperators\UnityBlocksWorld\", problemname + "*.CachedOperator"))
            {
                var op = BinarySerializer.DeSerializeObject <IOperator>(file);
                GroundActionFactory.GroundActions.Add(op);
                GroundActionFactory.GroundLibrary[op.ID] = op;
                if (op.ID > maxSeen)
                {
                    maxSeen = op.ID;
                }
                if (op is IComposite comp)
                {
                    foreach (var sub in comp.SubSteps)
                    {
                        if (sub.ID > maxStepSeen)
                        {
                            maxStepSeen = sub.ID;
                        }
                    }
                    if (comp.GoalStep.ID > maxStepSeen)
                    {
                        maxStepSeen = comp.GoalStep.ID;
                    }
                }
            }
            // THIS is so that initial and goal steps created don't get matched with these
            Operator.SetCounterExternally(maxSeen + 1);
            PlanStep.SetCounterExternally(maxStepSeen + 1);
        }
예제 #7
0
        public void DeCacheIt()
        {
            Parser.path = @"D:\documents\frostbow\";
            DecacheSteps();

            UnityPlanningInterface.AddObservedNegativeConditions(UPC);

            var CausalMapFileName = GetCausalMapFileName();
            var ThreatMapFileName = GetThreatMapFileName();
            var EffortMapFileName = GetEffortDictFileName();

            try
            {
                var cmap = BinarySerializer.DeSerializeObject <TupleMap <IPredicate, List <int> > >(CausalMapFileName + ".CachedCausalMap");
                CacheMaps.CausalTupleMap = cmap;

                var tcmap = BinarySerializer.DeSerializeObject <TupleMap <IPredicate, List <int> > >(ThreatMapFileName + ".CachedThreatMap");
                CacheMaps.ThreatTupleMap = tcmap;

                try
                {
                    var emap = BinarySerializer.DeSerializeObject <TupleMap <IPredicate, int> >(EffortMapFileName + ".CachedEffortMap");
                    HeuristicMethods.visitedPreds = emap;
                }
                catch
                {
                    CacheMaps.CacheAddReuseHeuristic(new State(UPC.initialPredicateList) as IState);
                    UnityGroundActionFactory.PrimaryEffectHack(new State(UPC.initialPredicateList) as IState);
                }
            }
            catch
            {
                CacheMaps.CacheLinks(GroundActionFactory.GroundActions);
                CacheMaps.CacheGoalLinks(GroundActionFactory.GroundActions, UPC.goalPredicateList);
                CacheMaps.CacheAddReuseHeuristic(new State(UPC.initialPredicateList) as IState);
                UnityGroundActionFactory.PrimaryEffectHack(new State(UPC.initialPredicateList) as IState);
            }
        }
예제 #8
0
 /// <summary>
 /// Read a node object from disk.
 /// </summary>
 /// <param name="nodeID">The node's ID.</param>
 /// <returns>The node object.</returns>
 public MediationTreeNode GetNode(int nodeID)
 {
     // Given the path and ID, deserializes the object from disk.
     return(BinarySerializer.DeSerializeObject <MediationTreeNode>(path + nodeID));
 }
예제 #9
0
    // Use this for initialization
    void Start()
    {
        var testDomainName      = "batman";
        var testDomainDirectory = Parser.GetTopDirectory() + @"Benchmarks\" + testDomainName + @"\domain.pddl";
        var testDomain          = Parser.GetDomain(Parser.GetTopDirectory() + @"Benchmarks\" + testDomainName + @"\domain.pddl", PlanType.PlanSpace);
        var testProblem         = Parser.GetProblem(Parser.GetTopDirectory() + @"Benchmarks\" + testDomainName + @"\prob01.pddl");

        string FileName          = Parser.GetTopDirectory() + @"Test\" + testDomainName + "_" + testProblem.Name;
        string CausalMapFileName = Parser.GetTopDirectory() + @"CausalMaps\" + testDomainName + "_" + testProblem.Name;
        string ThreatMapFileName = Parser.GetTopDirectory() + @"CausalMaps\" + testDomainName + "_" + testProblem.Name;

        if (RELOAD)
        {
            Debug.Log("Creating Ground Operators");
            GroundActionFactory.PopulateGroundActions(testDomain.Operators, testProblem);
            //BinarySerializer.SerializeObject(FileName, GroundActionFactory.GroundActions);
            foreach (var op in GroundActionFactory.GroundActions)
            {
                BinarySerializer.SerializeObject(FileName + op.GetHashCode().ToString(), op);
            }

            CacheMaps.CacheLinks(GroundActionFactory.GroundActions);
            CacheMaps.CacheGoalLinks(GroundActionFactory.GroundActions, testProblem.Goal);
            BinarySerializer.SerializeObject(CausalMapFileName, CacheMaps.CausalMap);
            BinarySerializer.SerializeObject(ThreatMapFileName, CacheMaps.ThreatMap);
        }
        else
        {
            List <IOperator> Operators = new List <IOperator>();
            foreach (string file in Directory.GetFiles(Parser.GetTopDirectory() + @"Cache\" + testDomainName))
            {
                var op = BinarySerializer.DeSerializeObject <IOperator>(file);
                Operators.Add(op);
                //string contents = File.ReadAllText(file);
            }
            //var actions = new List<IOperator>();

            GroundActionFactory.GroundActions = Operators;
        }

        Debug.Log("Caching Maps");


        Debug.Log("Finding static preconditions");

        GroundActionFactory.DetectStatics(CacheMaps.CausalMap, CacheMaps.ThreatMap);


        Debug.Log("Creating initial Plan");
        // Create Initial Plan
        // public Plan(List<IOperator> steps, IState initial, IState goal, Graph<IOperator> og, ICausalLinkGraph clg, Flawque flawQueue)
        // IState _initial, IState _goal, List<IOperator> _steps
        var initialPlan = new Plan(new State(testProblem.Initial) as IState, new State(testProblem.Goal) as IState);

        foreach (var goal in testProblem.Goal)
        {
            initialPlan.Flaws.Insert(initialPlan, new OpenCondition(goal, initialPlan.GoalStep as IOperator));
        }
        Debug.Log("Insert First Ordering");
        initialPlan.Orderings.Insert(initialPlan.InitialStep, initialPlan.GoalStep);

        Debug.Log("First POP");
        var AStarPOP           = new PlanSpacePlanner(initialPlan, SearchType.BestFirst, new AddReuseHeuristic().Heuristic);
        var bestFirstSolutions = AStarPOP.Solve(1, 6000f);

        Debug.Log(bestFirstSolutions[0]);

        var BFSPOP       = new PlanSpacePlanner(initialPlan, SearchType.BFS, new ZeroHeuristic().Heuristic);
        var BFSSolutions = BFSPOP.Solve(1, 6000f);

        Debug.Log(BFSSolutions[0]);

        var DFSPOP       = new PlanSpacePlanner(initialPlan, SearchType.DFS, new ZeroHeuristic().Heuristic);
        var DFSSolutions = DFSPOP.Solve(1, 6000f);

        Debug.Log(DFSSolutions[0]);
    }
예제 #10
0
        // Creates a single tree of specified depth without specifying a folder name.
        public static void BreadthFirst(string domainName, int endDepth, bool domainRevision, bool eventRevision, bool superposition)
        {
            string modifier = "vanilla";

            if (domainRevision && eventRevision)
            {
                modifier = "domain-event";
            }
            else if (domainRevision)
            {
                modifier = "domain";
            }
            else if (eventRevision)
            {
                modifier = "event";
            }
            else if (superposition)
            {
                modifier = "superposition";
            }

            // Parse the domain file.
            Domain domain = Parser.GetDomain(Parser.GetTopDirectory() + @"Benchmarks\" + domainName + @"\domain.pddl", PlanType.StateSpace);

            // Parse the problem file.
            Problem problem = Parser.GetProblemWithTypes(Parser.GetTopDirectory() + @"Benchmarks\" + domainName + @"\prob01.pddl", domain);

            // Create the initial node of mediation space.
            MediationTree tree = new MediationTree(domain, problem, Parser.GetTopDirectory() + @"MediationTrees\Data\" + domain.Name + @"\" + modifier + @"\", domainRevision, eventRevision, superposition);

            // Remember the game tree path.
            string dataPath = Parser.GetTopDirectory() + @"TestLogs\Level\" + domainName + @"\" + modifier + @"\";

            // Check each path to see if it exists. If not, create the folder.
            if (!File.Exists(dataPath))
            {
                Directory.CreateDirectory(dataPath);
            }

            TestData data = new TestData();

            Stopwatch watch = new Stopwatch();

            Console.WriteLine("Creating a " + modifier + " tree to level " + endDepth);

            // If data already exists, load it from memory.
            if (File.Exists(dataPath + "mediationtreedata"))
            {
                data = BinarySerializer.DeSerializeObject <TestData>(dataPath + "mediationtreedata");
            }
            else
            {
                data.elapsedMilliseconds = 0;
                data.frontier            = new List <int>()
                {
                    0
                };
                data.depth          = 0;
                data.nodeCounter    = 1;
                data.goalStateCount = 0;
                data.deadEndCount   = 0;
                data.summarySkip    = 1000;
                data.summaries      = new List <List <Tuple <string, string> > >();
            }

            Console.WriteLine("Beginning at level " + data.depth);
            Console.WriteLine("Beginning at node number " + data.nodeCounter);

            watch.Start();

            while (endDepth - data.depth > 0 && data.frontier.Count > 0)
            {
                MediationTreeNode current = tree.GetNode(data.frontier[0]);
                foreach (MediationTreeEdge edge in current.Outgoing)
                {
                    bool newDepth           = false;
                    MediationTreeNode child = tree.GetNode(current.Domain, current.Problem, edge);
                    if (child.Depth > data.depth)
                    {
                        data.depth = child.Depth;
                        newDepth   = true;
                        Console.WriteLine("Reached level " + data.depth);
                    }
                    data.nodeCounter++;
                    if (child.IsGoal)
                    {
                        data.goalStateCount++;
                    }
                    if (child.DeadEnd)
                    {
                        data.deadEndCount++;
                    }
                    data.frontier.Add(child.ID);
                    watch.Stop();
                    if (newDepth)
                    {
                        data.summaries.Add(CreateSummary(data));
                    }
                    watch.Start();
                }
                data.frontier.RemoveAt(0);
                data.elapsedMilliseconds += watch.ElapsedMilliseconds;
                watch.Reset();
                BinarySerializer.SerializeObject <TestData>(dataPath + "mediationtreedata", data);
                if (data.nodeCounter % 1000 == 0)
                {
                    Console.WriteLine("Reached node number " + data.nodeCounter);
                }
                watch.Start();
            }

            watch.Stop();
            int size = data.summaries.Count + 1;

            BinarySerializer.SerializeObject <TestData>(dataPath + "mediationtreedata", data);
            WriteSummary(@"\Level\" + domainName + @"\" + modifier, data.nodeCounter.ToString(), data.summaries);
            if (data.summaries.Count > 0)
            {
                Grapher.CreateGraphs(domainName, data.nodeCounter.ToString(), size, dataPath, data.summaries);
            }
        }
예제 #11
0
        // Creates a single tree of specified depth without specifying a folder name.
        public static void MCTSearch(string domainName, int totalPlays, int interval)
        {
            // Save the summaries of each build.
            List <List <Tuple <String, String> > > summaries = new List <List <Tuple <String, String> > >();

            // Remember a time stamp for the top directory.
            string timeStamp = DateTime.Now.ToString("MM-dd-yyyy-HH-mm-tt");

            // Read in the domain file.
            Domain domain = Parser.GetDomain(Parser.GetTopDirectory() + @"Benchmarks\" + domainName + @"\domain.pddl", PlanType.StateSpace);

            // Read in the problem file.
            Problem problem = Parser.GetProblemWithTypes(Parser.GetTopDirectory() + @"Benchmarks\" + domainName + @"\prob01.pddl", domain);

            // Create a stopwatch object.
            Stopwatch watch = new Stopwatch();

            // Remember the game tree path.
            string path = Parser.GetTopDirectory() + @"GameTrees\Data\" + domainName + @"\";

            // Check each path to see if it exists. If not, create the folder.
            if (!File.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            // Start the stopwatch.
            watch.Start();

            // Initialize the game tree.
            GameTree tree = null;

            if (File.Exists(path + "gametree"))
            {
                tree = BinarySerializer.DeSerializeObject <GameTree>(path + "gametree");
            }
            else
            {
                tree = new GameTree(domain, problem, path);
            }

            // Stop the stopwatch.
            watch.Stop();

            // Loop through the depths.
            for (int plays = 0; plays < totalPlays; plays = plays + interval)
            {
                Console.Out.WriteLine("Starting play " + plays);

                // Print the current tree.
                summaries.Add(WriteTree(domainName, timeStamp, plays, watch, tree, false));

                // Start the watch.
                watch.Start();

                // Play the next round.
                MCTS.Search(interval, tree);

                // Save the game tree.
                BinarySerializer.SerializeObject <GameTree>(path + "gametree", tree);

                // Stop the watch.
                watch.Stop();
            }

            Console.Out.WriteLine("Printing tree.");

            // Print the current tree.
            summaries.Add(WriteTree(domainName, timeStamp, totalPlays, watch, tree, false));

            // Write the summary CSV file to disk.
            WriteGTSummary(domainName, timeStamp, summaries);

            // Use the CSV file to create an Excel spreadsheet and graphs of each summary element.
            Grapher.CreateGTGraphs(domainName, timeStamp, (totalPlays / interval) + 2, summaries);
        }