// Creates a single tree of specified depth. private static List <Tuple <String, String> > SingleTree(string domainName, string timeStamp, int depth) { // 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.GetProblem(Parser.GetTopDirectory() + @"Benchmarks\" + domainName + @"\prob01.pddl"); // Find an initial plan. Plan plan = FastDownward.Plan(domain, problem); // Create a stopwatch object. Stopwatch watch = new Stopwatch(); // Start the stopwatch. watch.Start(); // Use the state space mediator to build the tree. StateSpaceNode root = StateSpaceMediator.BuildTree(Planner.FastDownward, domain, problem, plan, plan.Initial as State, depth); // Stop the stopwatch. watch.Stop(); // Write the tree to disk using HTML files and return the summary object. return(WriteTree(domainName, timeStamp, depth, watch, root)); }
private void UpdatePlan() { if (frontier.ContainsKey(currentEdge)) { //Debug.Log ("Cached"); StateSpaceNode oldRoot = root; root = frontier[currentEdge] as StateSpaceNode; root.parent = oldRoot; problem = root.problem; plan = root.plan; state = root.state; } else { //Debug.Log ("Not Cached"); frontierThread.Abort(); StateSpaceNode oldRoot = root; root = StateSpaceMediator.ExpandTree(domain, problem, plan, state, currentEdge, 0); root.parent = oldRoot; problem = root.problem; plan = root.plan; state = root.state; } needUpdate = true; }
public void StateSpaceMediatorZombieConstituentUpdateTest() { StateSpaceNode root = StateSpaceMediator.BuildTree(Planner.FastDownward, testDomain, testProblem, testPlan, testPlan.Initial as State, 1); StateSpaceNode child = root.children[root.outgoing[0]] as StateSpaceNode; Assert.AreNotEqual(root.problem.Initial, child.problem.Initial); }
/// <summary> /// Moves along the outgoing wait edge from the current node in mediation space. /// </summary> public static void Wait() { // Provide feedback to the player. Console.Out.WriteLine(); Console.Out.WriteLine("Time passes..."); // Loop through the outgoing edges... foreach (StateSpaceEdge edge in root.outgoing) { // Identify if (edge.Action.Name.Equals("do-nothing")) { if (frontier.ContainsKey(edge)) { root = frontier[edge] as StateSpaceNode; domain = root.domain; problem = root.problem; plan = root.plan; state = root.state; } else { root = StateSpaceMediator.ExpandTree(planner, domain, problem, plan, state, edge, 0); domain = root.domain; problem = root.problem; plan = root.plan; state = root.state; } } } Look(); }
// Updates the plan that solves the planning problem from the player's current state. private void UpdatePlayerPlan( ) { // Cache the old root StateSpaceNode oldRoot = root; // Update the root: if we've already computed it, get it, otherwise, expand (i.e. compute it). if (frontier.ContainsKey(playerActionEdge) && !playerActionEdge.Action.Name.Equals("donothing")) { root = frontier[playerActionEdge] as StateSpaceNode; } else { frontierThread.Abort(); root = StateSpaceMediator.ExpandTree(planner, domain, problem, plan, state, playerActionEdge, 0); } // Set the root's parent. root.parent = oldRoot; // Cache the new root's properties. problem = root.problem; plan = root.plan; state = root.state; // Update the player model with the player's action and the resulting state predicates. playerEnvironmentModel.UpdateModel(playerActionEdge.Action, state.Predicates); // Let the mediator take its turn. stateNeedsUpdate = true; }
public void StateSpaceMediatorExceptionalActionTest() { StateSpaceNode root = StateSpaceMediator.BuildTree(Planner.Glaive, testDomain, testProblem, testPlan, testPlan.Initial as State, 3); Assert.AreEqual(root.outgoing[1].ActionType, ActionType.Exceptional); Assert.AreEqual(root.outgoing[1].Action.Name, "move-location"); }
public void StateSpaceMediatorExceptionalUpdateIntentionTest() { StateSpaceNode root = StateSpaceMediator.BuildTree(Planner.Glaive, testDomain, testProblem, testPlan, testPlan.Initial as State, 1); StateSpaceNode child = root.children[root.outgoing[1]] as StateSpaceNode; Assert.AreEqual(child.problem.Intentions.Count, 2); Assert.AreNotEqual(child.plan.Steps.Count, 4); }
public void StateSpaceMediatorExampleStateUpdateTest() { StateSpaceMediator.CEDeletion = true; StateSpaceNode root = StateSpaceMediator.BuildTree(Planner.FastDownward, testDomain, testProblem, testPlan, testPlan.Initial as State, 3); StateSpaceNode child = root.children[root.outgoing[2]] as StateSpaceNode; Assert.IsTrue(child.Goal); }
public void StateSpaceMediatorExampleCEDeleteTest() { StateSpaceMediator.CEDeletion = true; StateSpaceNode root = StateSpaceMediator.BuildTree(Planner.FastDownward, testDomain, testProblem, testPlan, testPlan.Initial as State, 3); StateSpaceNode child = root.children[root.outgoing[2]] as StateSpaceNode; Assert.IsTrue(child.incoming.Action.Conditionals.Count == 0); }
public void StateSpaceMediatorExampleCEParseTest() { StateSpaceNode root = StateSpaceMediator.BuildTree(Planner.FastDownward, testDomain, testProblem, testPlan, testPlan.Initial as State, 3); StateSpaceNode child = root.children[root.outgoing[1]] as StateSpaceNode; Assert.IsFalse(child.incoming.Action.Effects.Contains(new Predicate("when", new List <ITerm> { }, true))); }
// Expand the frontier. private void ExpandFrontier( ) { foreach (StateSpaceEdge edge in root.outgoing) { StateSpaceNode newNode = StateSpaceMediator.ExpandTree(planner, domain, problem, plan, state, edge, 0); frontier.Add(edge, newNode); } }
public void JumpToState(List <Predicate> newPredicates) { // Create and store the new state object. state = new State(newPredicates, null, null); // Create the initial node of mediation. root = StateSpaceMediator.BuildTree(domain, problem, plan, state, 0); // Set the state manager's predicates. stateManager.Predicates = state.Predicates; // Ask the state manager to refresh the world. stateManager.Refresh(); }
public static void Unknown() { StateSpaceEdge matchingEdge = null; foreach (StateSpaceEdge edge in root.outgoing) { if (command.Equals(edge.Action.Name) && arguments.Count == edge.Action.Arity) { bool match = true; for (int i = 0; i < arguments.Count; i++) { if (!arguments[i].ToLower().Equals(edge.Action.Predicate.TermAt(i))) { if (!arguments[i].ToLower().Equals(edge.Action.TermAt(i))) { match = false; } } } if (match) { matchingEdge = edge; } } } if (matchingEdge != null) { root = StateSpaceMediator.ExpandTree(planner, domain, problem, plan, state, matchingEdge, 0); problem = root.problem; plan = root.plan; state = root.state; Console.Clear(); Look(); } else { frontierThread.Abort(); Console.Out.WriteLine(); Random r = new Random(); Console.Out.WriteLine(responses[r.Next(0, responses.Length)]); Console.Out.WriteLine("Try typing 'help'."); } }
// Build a range of tree depths. public static void MultipleTrees(string domainName, int startDepth, int endDepth) { // 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"); // Loop through the depths. for (int depth = startDepth; depth <= endDepth; depth++) { // Build the tree and save its summary. summaries.Add(SingleTree(domainName, timeStamp, depth)); // Clear the mediator's memory. StateSpaceMediator.Clear(); } // Write the summary CSV file to disk. WriteSummary(domainName, timeStamp, summaries); // Use the CSV file to create an Excel spreadsheet and graphs of each summary element. Grapher.CreateGraphs(domainName, timeStamp, endDepth - startDepth + 2, Parser.GetTopDirectory() + @"TestLogs\" + domainName + @"\" + timeStamp + @"\", summaries); }
public static void TwoArgs() { if (arguments.Count != 2) { Unknown(); return; } StateSpaceEdge matchingEdge = null; foreach (StateSpaceEdge edge in root.outgoing) { if (edge.Action.Name.Count(x => x == '-') == 2) { if (edge.Action.Name.Contains('-')) { string[] split = edge.Action.Name.Split('-'); if (command.Equals(split[0])) { if (arguments[0].ToLower().Equals(edge.Action.TermAt(1))) { if (arguments[1].ToLower().Equals(edge.Action.TermAt(2))) { matchingEdge = edge; } } } } else if (command.Equals(edge.Action.Name)) { if (arguments[0].ToLower().Equals(edge.Action.TermAt(1))) { if (arguments[1].ToLower().Equals(edge.Action.TermAt(2))) { matchingEdge = edge; } } } } } if (matchingEdge != null) { if (frontier.ContainsKey(matchingEdge)) { root = frontier[matchingEdge] as StateSpaceNode; domain = root.domain; problem = root.problem; plan = root.plan; state = root.state; } else { frontierThread.Abort(); root = StateSpaceMediator.ExpandTree(planner, domain, problem, plan, state, matchingEdge, 0); domain = root.domain; problem = root.problem; plan = root.plan; state = root.state; } Console.Clear(); Look(); } else { Console.Out.WriteLine(); Random r = new Random(); Console.Out.WriteLine(responses[r.Next(0, responses.Length)]); Console.Out.WriteLine("Try typing 'help'."); } }
// Use this for initialization void Start( ) { // Set the path to the top directory. Parser.path = Directory.GetParent(Application.dataPath).FullName + "/"; // Parse the domain and problem files, and get the initial plan. domain = Parser.GetDomain(Parser.GetTopDirectory() + @"Benchmarks/" + domainName + @"/domain.pddl", PlanType.StateSpace); problem = Parser.GetProblem(Parser.GetTopDirectory() + @"Benchmarks/" + domainName + "/" + problemName + ".pddl"); plan = PlannerInterface.Plan(planner, domain, problem); // A test for the planner. if (plan.Steps.Count > 0) { Debug.Log("System loaded."); } else { Debug.Log("System not working or no plan exists."); } state = plan.GetFirstState(); // Find the first state. validState = true; // Initialize the valid state. GameObject level = GameObject.Find("Level"); // Find the level game object. stateManager = level.GetComponent <StateManager>(); // Set the state manager. stateManager.Problem = problem; stateManager.Predicates = state.Predicates; // Set the state manager's predicates. generator = level.GetComponent <MapManager>(); // Set the level generator. generator.CreateLevel(); // Generate the level. // Create the initial node of mediation. root = StateSpaceMediator.BuildTree(planner, domain, problem, plan, state, 0); frontier = new Hashtable(); // Initialize the frontier. frontierThread = new Thread(ExpandFrontier); // Expand the frontier in a new thread. frontierThread.Start(); // Start the thread. // Initialize the player's environment model and action log. playerEnvironmentModel = new EnvironmentModel(stateManager.PlayerName, domain, problem); actionLog = new List <Tuple <string, string> >(); // Initialize the player log with a decent size. playerLog = new Queue <IOperator>(3 * plan.Steps.Count); #region Initialize the Experiment // Generate the participant's ID participantIDGenerator = new RNGCryptoServiceProvider(); byte[] idBytes = new byte[4]; participantIDGenerator.GetBytes(idBytes); participantID = BitConverter.ToInt32(idBytes, 0); PlayerPrefs.SetInt("participantID", participantID); // Compute and create the participant's output folder. participantFolder = Parser.GetTopDirectory() + @"Benchmarks/" + domain.Name.ToLower() + @"/output/" + participantID + @"/"; Directory.CreateDirectory(participantFolder); playerTurnCounter = 0; #endregion // Start the wizard conversation. DialogueManager.StartConversation("Wizard"); }
/// <summary> /// An online text-based command line game that is a traversal of mediation space. /// </summary> /// <param name="domainName">The game domain.</param> /// <param name="debug">Whether or not debug mode is enabled.</param> public static void Play() { Console.Clear(); string domainName = ""; // Make sure the file exists. while (!File.Exists(Parser.GetTopDirectory() + @"Benchmarks\" + domainName + @"\domain.pddl") || !File.Exists(Parser.GetTopDirectory() + @"Benchmarks\" + domainName + @"\prob01.pddl")) { // Prompt the user for game name. Console.WriteLine("What would you like to play?"); Console.WriteLine(" Type 'exit' to end program."); Console.WriteLine(" Type 'path' to modify the directory."); Console.WriteLine(" Type 'planner' to choose the planner."); Console.WriteLine(" Type 'help' to print game titles."); Console.Write(" Type 'debug' to turn debug mode "); if (debug) { Console.WriteLine("off."); } else { Console.WriteLine("on."); } Console.WriteLine(); Console.Write(">"); // Read in the game to load. domainName = Console.ReadLine().ToLower(); // Print domains if prompted. if (domainName.Equals("help")) { Console.WriteLine(); if (System.IO.Directory.Exists(Parser.GetTopDirectory() + @"Benchmarks\")) { foreach (string file in Directory.GetFileSystemEntries(Parser.GetTopDirectory() + @"Benchmarks\")) { Console.WriteLine(Path.GetFileName(file)); } Console.WriteLine(); Console.Write(">"); // Read in the game to load. domainName = Console.ReadLine().ToLower(); } else { Console.WriteLine(Parser.GetTopDirectory() + @"Benchmarks\ does not exist!"); Console.ReadKey(); domainName = "482990adkdlllifkdlkfjlaoow"; } } // Rewrite directory if prompted. if (domainName.Equals("path")) { Console.WriteLine(); Console.WriteLine("Your current game directory is: " + Parser.GetTopDirectory()); Console.WriteLine("Enter new path."); Console.WriteLine(); Console.Write(">"); string newPath = Console.ReadLine(); Console.WriteLine(); if (Directory.Exists(newPath + @"Benchmarks\")) { Parser.path = newPath; } else { Console.WriteLine("Sorry, " + newPath + @"Benchmarks\" + " does not exist."); Console.ReadKey(); } } // Change the planner if prompted. if (domainName.Equals("planner")) { Console.WriteLine(); Console.WriteLine("Your current planner is: " + planner); Console.WriteLine("Enter new planner."); Console.WriteLine(); Console.Write(">"); string newPlanner = Console.ReadLine(); Console.WriteLine(); if (Enum.IsDefined(typeof(Planner), newPlanner)) { planner = (Planner)Enum.Parse(typeof(Planner), newPlanner, true); Console.WriteLine("Your planner is now " + planner); Console.ReadKey(); } else { Console.WriteLine("Sorry, " + newPlanner + " does not exist."); Console.ReadKey(); } } // Exit if prompted. if (domainName.Equals("exit")) { Environment.Exit(0); } // Toggle debug if prompted if (domainName.Equals("debug")) { debug = !debug; } if ((!File.Exists(Parser.GetTopDirectory() + @"Benchmarks\" + domainName + @"\domain.pddl") || !File.Exists(Parser.GetTopDirectory() + @"Benchmarks\" + domainName + @"\prob01.pddl")) && !domainName.Equals("debug") && !domainName.Equals("path") && !domainName.Equals("planner") && !domainName.Equals("482990adkdlllifkdlkfjlaoow")) { // Prompt that the game doesn't exist. Console.WriteLine(); Console.WriteLine("I'm sorry, but I can't find " + domainName.ToUpper()); Console.WriteLine(); Console.ReadKey(); } // Clear the console screen. Console.Clear(); } // Parse the domain file. domain = Parser.GetDomain(Parser.GetTopDirectory() + @"Benchmarks\" + domainName + @"\domain.pddl", PlanType.StateSpace); // Parse the problem file. problem = Parser.GetProblemWithTypes(Parser.GetTopDirectory() + @"Benchmarks\" + domainName + @"\prob01.pddl", domain); // Find the initial plan. if (planner.Equals(Planner.FastDownward)) { plan = FastDownward.Plan(domain, problem); } else if (planner.Equals(Planner.Glaive)) { plan = Glaive.Plan(domain, problem); } // Welcome the player to the game. Console.WriteLine("Welcome to " + domain.Name); // Find the first state. state = plan.GetFirstState(); // Create the initial node of mediation space. root = StateSpaceMediator.BuildTree(planner, domain, problem, plan, state, 0); // Initialize a stopwatch for debugging. Stopwatch watch = new Stopwatch(); // Present information about the player. command = "look"; arguments = new List <string> { "me" }; Look(); Console.WriteLine(); // Present the initial state. command = ""; Look(); // Loop while this is false. bool exit = false; while (!exit) { // Initialize the frontier. frontier = new Hashtable(); // Expand the frontier in a new thread. frontierThread = new Thread(ExpandFrontier); // Start the thread. frontierThread.Start(); // Ask for input. Console.WriteLine(); Console.Write(">"); input = Console.ReadLine(); // If in debug mode, start the stop watch. if (debug) { watch.Reset(); watch.Start(); } // Parse the command and its arguments. command = ParseCommand(input).ToLower(); arguments = ParseArguments(input); // Interpret the command. switch (command) { case "exit": exit = true; break; case "clear": Console.Clear(); break; case "cls": Console.Clear(); break; case "look": Look(); break; case "help": Help(); break; case "wait": Console.Clear(); Wait(); break; default: OneArg(); break; } // If debugging, write the current plan and the elapsed time. if (debug && plan.Steps.Count > 0 && !command.Equals("clear") && !command.Equals("cls")) { Console.Out.WriteLine(); Console.WriteLine("Narrative Trajectory:"); int longestName = 0; foreach (Operator step in plan.Steps) { if (step.TermAt(0).Length > longestName) { longestName = step.TermAt(0).Length; } } string lastName = ""; foreach (Operator step in plan.Steps) { if (!step.TermAt(0).Equals(lastName)) { lastName = step.TermAt(0); Console.Out.Write("".PadLeft(5) + UppercaseFirst(step.TermAt(0)) + "".PadLeft(longestName - step.TermAt(0).Length + 1)); } else { Console.Out.Write("".PadLeft(5) + "".PadLeft(longestName + 1)); } string[] splitName = step.Name.Split('-'); Console.Out.Write(splitName[0] + "s "); for (int i = 1; i < step.Name.Count(x => x == '-') + 1; i++) { Console.Out.Write(UppercaseFirst(step.TermAt(i)) + " "); } Console.Out.WriteLine(); } Console.Out.WriteLine(); Console.Write("Elapsed time: "); Console.Out.Write(watch.ElapsedMilliseconds); Console.WriteLine("ms"); } // Check for goal state. if (root.Goal) { Console.WriteLine("GOAL STATE"); Console.ReadKey(); exit = true; } // Check for incompatible state. else if (root.Incompatible) { Console.WriteLine("UNWINNABLE STATE"); Console.ReadKey(); exit = true; } if (exit) { Console.Clear(); } // Kill the frontier thread (this should be okay). frontierThread.Abort(); } Game.Play(); }
// Use this for initialization void Start() { Debug.Log(Screen.width + " " + Screen.height); int num = UnityEngine.Random.Range(1, 4); domainName += num.ToString(); Debug.Log(domainName + " " + num); // Set the path to the top directory. Parser.path = Path.GetFullPath(".") + "\\"; Debug.Log(Parser.path); // Parse the domain file. domain = Parser.GetDomain(Parser.GetTopDirectory() + @"Benchmarks\" + domainName + @"\domain.pddl", PlanType.StateSpace); // Parse the problem file. problem = Parser.GetProblem(Parser.GetTopDirectory() + @"Benchmarks\" + domainName + @"\prob01.pddl"); // Find the initial plan. plan = FastDownward.Plan(domain, problem); // A test for the planner. if (plan.Steps.Count > 0) { Debug.Log("Planner is working."); } else { Debug.Log("Planner is not working or no plan exists."); RestartGame(); } // Find the first state. state = plan.GetFirstState(); // Find the level game object. GameObject level = GameObject.Find("Level"); // Set the state manager. stateManager = level.GetComponent <StateManager>(); // Set the level generator. generator = level.GetComponent <LevelGenerator>(); // Set the state manager's predicates. stateManager.Predicates = state.Predicates; // Find the UI game object. GameObject ui = GameObject.Find("UI"); // Set the ui generator. uiGenerator = ui.GetComponent <UI>(); // Setup the UI. uiGenerator.GenerateUI(); // Generate the level. generator.CreateLevel(); // Create the initial node of mediation. root = StateSpaceMediator.BuildTree(domain, problem, plan, state, 0); // Initialize the frontier. frontier = new Hashtable(); // Expand the frontier in a new thread. frontierThread = new Thread(ExpandFrontier); // Start the thread. frontierThread.Start(); // Initialize the camera fade. iTween.CameraFadeAdd(); // Initialize the valid state. validState = true; goal = false; needUpdate = false; }
public void StateSpaceMediatorArthActionTest() { StateSpaceNode root = StateSpaceMediator.BuildTree(Planner.Glaive, testDomain, testProblem, testPlan, testPlan.Initial as State, 3); Assert.AreEqual(root.outgoing.Count, 3); }