// Create the applicable constituent action for a state. public static MediationTreeEdge GetConstituentAction(string actor, MediationTreeNode node) { // Iterate through the plan to find the next player step. for (int i = 0; i < node.Plan.Steps.Count; i++) { if (node.Plan.Steps[i].Arity > 0) { // Check to see if the next step in the plan is performed by the player. if (node.Plan.Steps[i].TermAt(0).Equals(actor)) { // Create a new plan. Plan newPlan = node.Plan.Clone() as Plan; // Remove the current step from the new plan. newPlan.Steps.RemoveAt(i); // Add the current step to the start of the steps. newPlan.Steps.Insert(0, node.Plan.Steps[i].Clone() as Operator); // Check to see if the new plan is valid. if (PlanSimulator.VerifyPlan(newPlan, node.State, node.Problem.Objects)) { // The current step is constituent. return(new MediationTreeEdge(node.Plan.Steps[i].Clone() as Operator, ActionType.Constituent, node.ID)); } // Exit the loop. i = node.Plan.Steps.Count; } } } // Otherwise, return a blank step as the user's constituent action. return(new MediationTreeEdge(new Operator("do nothing"), ActionType.Constituent, node.ID)); }
public void BatmanTestEventRevision() { path = Parser.GetTopDirectory() + @"MediationTrees\Data\Unit-Tests\Batman\event\"; Assert.IsTrue(testPlan.Steps.Count > 0); tree = new MediationTree(testDomain, testProblem, path, false, true, false); Assert.IsTrue(tree.Root.Plan.Steps.Count > 0); Assert.IsTrue(tree.Root.Outgoing.Count == 1); MediationTreeNode current = tree.GetNode(tree.Root.Domain, tree.Root.Problem, tree.Root.Outgoing[0]); Assert.IsTrue(current.Outgoing.Count == 1); current = tree.GetNode(tree.Root.Domain, tree.Root.Problem, current.Outgoing[0]); Assert.IsTrue(current.Outgoing.Count == 1); current = tree.GetNode(tree.Root.Domain, tree.Root.Problem, current.Outgoing[0]); Assert.IsTrue(current.Outgoing.Count == 2); current = tree.GetNode(tree.Root.Domain, tree.Root.Problem, current.Outgoing[0]); Assert.IsTrue(current.Outgoing.Count == 2); current = tree.GetNode(tree.Root.Domain, tree.Root.Problem, current.Outgoing[0]); current = tree.GetNode(tree.Root.Domain, tree.Root.Problem, current.Outgoing[0]); current = tree.GetNode(tree.Root.Domain, tree.Root.Problem, current.Outgoing[0]); current = tree.GetNode(tree.Root.Domain, tree.Root.Problem, current.Outgoing[0]); current = tree.GetNode(tree.Root.Domain, tree.Root.Problem, current.Outgoing[0]); current = tree.GetNode(tree.Root.Domain, tree.Root.Problem, current.Outgoing[0]); current = tree.GetNode(tree.Root.Domain, tree.Root.Problem, current.Outgoing[0]); current = tree.GetNode(tree.Root.Domain, tree.Root.Problem, current.Outgoing[1]); Assert.IsFalse(current.DeadEnd); }
/// <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 (MediationTreeEdge edge in current.Outgoing) { // Identify if (edge.Action.Name.Equals("do-nothing")) { if (frontier.ContainsKey(edge)) { current = frontier[edge] as MediationTreeNode; } else { current = tree.GetNode(current.Domain, current.Problem, edge); } } } Look(); }
public void DomainRevisionTest() { tree = new MediationTree(testDomain, testProblem, pathDomain, true, false); MediationTreeNode child = tree.GetNode(tree.Root.Domain, tree.Root.Problem, tree.Root.Outgoing.Find(x => x.Action.Name.Equals("move-location") && x.Action.TermAt(1).Equals("right"))); Assert.AreEqual(child.Outgoing.Count, 2); Assert.AreEqual(child.Outgoing.Find(x => x.Action.Name.Equals("move-location")).ActionType, ActionType.Constituent); Assert.AreEqual(child.Outgoing.Find(x => x.Action.Name.Equals("toggle-green")).ActionType, ActionType.Consistent); Assert.AreEqual(child.Domain.Operators.Find(x => x.Name.Equals("detonate-explosive")).Conditionals.Count, 2); child = tree.GetNode(child.Domain, child.Problem, child.Outgoing.Find(x => x.Action.Name.Equals("move-location"))); child = tree.GetNode(child.Domain, child.Problem, child.Outgoing.Find(x => x.Action.Name.Equals("make-trap-wire"))); Assert.AreEqual(child.Outgoing.Find(x => x.Action.Name.Equals("place-explosive-thing") && x.Action.TermAt(2).Equals("gears")).ActionType, ActionType.Constituent); child = tree.GetNode(child.Domain, child.Problem, child.Outgoing.Find(x => x.Action.Name.Equals("place-explosive-thing") && x.Action.TermAt(2).Equals("terminal1"))); child = tree.GetNode(child.Domain, child.Problem, child.Outgoing.Find(x => x.Action.Name.Equals("set-trap"))); child = tree.GetNode(child.Domain, child.Problem, child.Outgoing.Find(x => x.Action.Name.Equals("detonate-explosive"))); Assert.IsFalse(child.DeadEnd); Assert.AreEqual(child.Domain.Operators.Find(x => x.Name.Equals("detonate-explosive")).Conditionals.Count, 1); Assert.AreEqual(child.Domain.Operators.Find(x => x.Name.Equals("detonate-explosive")).Conditionals[0].Preconditions[0].Name, "green"); child = tree.GetNode(child.Domain, child.Problem, child.Outgoing.Find(x => x.Action.Name.Equals("move-location"))); child = tree.GetNode(child.Domain, child.Problem, child.Outgoing.Find(x => x.Action.Name.Equals("take-explosive"))); child = tree.GetNode(child.Domain, child.Problem, child.Outgoing.Find(x => x.Action.Name.Equals("use-computer"))); child = tree.GetNode(child.Domain, child.Problem, child.Outgoing.Find(x => x.Action.Name.Equals("toggle-green"))); child = tree.GetNode(child.Domain, child.Problem, child.Outgoing.Find(x => x.Action.Name.Equals("link-phone"))); child = tree.GetNode(child.Domain, child.Problem, child.Outgoing.Find(x => x.Action.Name.Equals("place-explosive-thing") && x.Action.TermAt(2).Equals("terminal1"))); child = tree.GetNode(child.Domain, child.Problem, child.Outgoing.Find(x => x.Action.Name.Equals("do-nothing"))); child = tree.GetNode(child.Domain, child.Problem, child.Outgoing.Find(x => x.Action.Name.Equals("detonate-explosive"))); Assert.IsTrue(child.DeadEnd); }
public void EventRevisorHistoryTest() { MediationTreeNode child = BuildTree(); List <MediationTreeNode> history = EventRevisor.GetWorldHistory(child, tree); Assert.AreEqual(history[0].ID, 0); Assert.AreEqual(history.Count, 7); }
public void EventRevisorProblemPairTest() { MediationTreeNode child = BuildTree(); Mediation.Utilities.Tuple <Domain, Problem> pair = EventRevisor.GetEventRevisionPair(child, tree); Plan newPlan = FastDownward.Plan(pair.First, pair.Second); Assert.AreNotEqual(newPlan.Steps.Count, 0); }
public void EventRevisorKnowledgeTest() { MediationTreeNode child = BuildTree(); List <Mediation.Utilities.Tuple <Operator, State> > history = EventRevisor.GetWorldKnowledge("snake", child, tree); Assert.AreEqual(history[0].First, null); Assert.AreEqual(history[0].Second.Predicates.FindAll(x => x.Name.Equals("at") && x.TermAt(0).Equals(new Term("boss", true))).Count, 1); Assert.AreEqual(history[0].Second.Predicates.FindAll(x => x.Name.Equals("at") && x.TermAt(0).Equals(new Term("snake", true))).Count, 5); Assert.AreEqual(history.Count, 7); }
static void TakeTurn() { MediationTreeEdge matchingEdge = current.Outgoing.Find(x => x.ActionType.Equals(ActionType.Constituent)); if (matchingEdge != null) { current = tree.GetNode(current.Domain, current.Problem, matchingEdge); computerActions.Add(matchingEdge.Action); } }
static void ExpandFrontier() { foreach (MediationTreeEdge edge in current.Outgoing) { if (exploreFrontier) { MediationTreeNode newNode = tree.GetNode(current.Domain, current.Problem, edge); frontier[edge] = newNode; } } }
public void MediationTreeUpdateTest() { tree = new MediationTree(testDomain, testProblem, pathEvent); MediationTreeNode child = tree.GetNode(tree.Root.Domain, tree.Root.Problem, tree.Root.Outgoing.Find(x => x.ActionType == ActionType.Constituent)); Assert.AreEqual(child.Outgoing.Count, 2); Assert.AreEqual(child.Outgoing.Find(x => x.Action.Name.Equals("move-location")).ActionType, ActionType.Constituent); Assert.AreEqual(child.Outgoing.Find(x => x.Action.Name.Equals("toggle-green")).ActionType, ActionType.Consistent); child = tree.GetNode(child.Domain, child.Problem, child.Outgoing.Find(x => x.Action.Name.Equals("move-location"))); Assert.AreEqual(child.Outgoing.FindAll(x => x.ActionType == ActionType.Constituent).Count, 1); }
public void EventRevisorObservedActionTest() { MediationTreeNode child = BuildTree(); List <MediationTreeNode> history = EventRevisor.GetWorldHistory(child, tree); List <Mediation.Utilities.Tuple <Operator, State> > knowledge = new List <Mediation.Utilities.Tuple <Operator, State> >(); foreach (MediationTreeNode node in history) { if (node.Incoming != null) { knowledge.Add(new Mediation.Utilities.Tuple <Operator, State>(node.Incoming.Action as Operator, new State())); } } Operator template = EventRevisor.GetObservedActionTemplate(1, knowledge[1].First); Assert.AreEqual(template.Name, "move-location*snake*gear*elevator"); Problem newProb = testProblem.Clone() as Problem; Domain newDom = testDomain.Clone() as Domain; newProb.Name = "rob"; newProb.Initial.Add(new Predicate("state-depth", new List <ITerm> { new Term("depth0", true) }, true)); newProb.Objects.AddRange(new List <IObject> { new Obj("depth0", "integer"), new Obj("depth1", "integer"), new Obj("depth2", "integer"), new Obj("depth3", "integer"), new Obj("depth4", "integer"), new Obj("depth5", "integer") }); newDom.AddTypePair("integer", "number"); newDom.Predicates.Add(new Predicate("state-depth", new List <ITerm> { new Term("?integer", "", "number") }, true)); newDom.Operators = new List <IOperator>(); for (int i = 0; i < knowledge.Count - 1; i++) { newDom.Operators.Add(EventRevisor.GetObservedActionTemplate(i, knowledge[i].First)); } foreach (Operator op in testDomain.Operators) { Operator newOp = op.Clone() as Operator; newOp.Preconditions.Add(new Predicate("state-depth", new List <ITerm> { new Term("depth5", true) }, true)); newDom.Operators.Add(newOp); } Plan newPlan = FastDownward.Plan(newDom, newProb); Assert.AreNotEqual(newPlan.Steps.Count, 0); }
private MediationTreeNode BuildTree() { tree = new MediationTree(testDomain, testProblem, path, false, true); MediationTreeNode child = tree.GetNode(tree.Root.Domain, tree.Root.Problem, tree.Root.Outgoing.Find(x => x.Action.Name.Equals("move-location") && x.Action.TermAt(1).Equals("right"))); child = tree.GetNode(child.Domain, child.Problem, child.Outgoing.Find(x => x.Action.Name.Equals("move-location"))); child = tree.GetNode(child.Domain, child.Problem, child.Outgoing.Find(x => x.Action.Name.Equals("make-trap-wire"))); child = tree.GetNode(child.Domain, child.Problem, child.Outgoing.Find(x => x.Action.Name.Equals("place-explosive-thing") && x.Action.TermAt(2).Equals("gears"))); child = tree.GetNode(child.Domain, child.Problem, child.Outgoing.Find(x => x.Action.Name.Equals("set-trap"))); child = tree.GetNode(child.Domain, child.Problem, child.Outgoing.Find(x => x.Action.Name.Equals("move-location") && x.Action.TermAt(1).Equals("left"))); return(child); }
public static void Unknown() { MediationTreeEdge matchingEdge = null; foreach (MediationTreeEdge edge in current.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) { current = tree.GetNode(current.Domain, current.Problem, matchingEdge); Console.Clear(); Look(); } else { exploreFrontier = false; while (frontierThread.IsAlive) { Thread.Sleep(50); } Console.Out.WriteLine(); Random r = new Random(); Console.Out.WriteLine(responses[r.Next(0, responses.Length)]); Console.Out.WriteLine("Try typing 'help'."); } }
public void EventRevisionTest() { tree = new MediationTree(testDomain, testProblem, pathEvent, false, true); MediationTreeNode child = tree.GetNode(tree.Root.Domain, tree.Root.Problem, tree.Root.Outgoing.Find(x => x.Action.Name.Equals("move-location") && x.Action.TermAt(1).Equals("right"))); Assert.AreEqual(child.Outgoing.Count, 2); Assert.AreEqual(child.Outgoing.Find(x => x.Action.Name.Equals("move-location")).ActionType, ActionType.Constituent); Assert.AreEqual(child.Outgoing.Find(x => x.Action.Name.Equals("toggle-green")).ActionType, ActionType.Consistent); child = tree.GetNode(child.Domain, child.Problem, child.Outgoing.Find(x => x.Action.Name.Equals("move-location"))); child = tree.GetNode(child.Domain, child.Problem, child.Outgoing.Find(x => x.Action.Name.Equals("make-trap-wire"))); Assert.AreEqual(child.Outgoing.Find(x => x.Action.Name.Equals("place-explosive-thing") && x.Action.TermAt(2).Equals("gears")).ActionType, ActionType.Constituent); child = tree.GetNode(child.Domain, child.Problem, child.Outgoing.Find(x => x.Action.Name.Equals("place-explosive-thing") && x.Action.TermAt(2).Equals("gears"))); child = tree.GetNode(child.Domain, child.Problem, child.Outgoing.Find(x => x.Action.Name.Equals("set-trap"))); Assert.AreEqual(child.Outgoing.Find(x => x.Action.Name.Equals("move-location") && x.Action.TermAt(1).Equals("right")).ActionType, ActionType.Constituent); child = tree.GetNode(child.Domain, child.Problem, child.Outgoing.Find(x => x.Action.Name.Equals("move-location") && x.Action.TermAt(1).Equals("left"))); Assert.IsFalse(child.DeadEnd); Assert.AreEqual(child.Plan.Steps.Count, 7); }
// Create the applicable constituent action for a state. public static MediationTreeEdge GetConstituentAction(MediationTreeNode node) { return(GetConstituentAction(node.Problem.Player, node)); }
// Creates a list of all possible things the player can do. public static List <MediationTreeEdge> GetAllPossibleActions(MediationTreeNode node) { return(GetAllPossibleActions(node.Problem.Player.ToLower(), node)); }
/// <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.Write(" Type 'turns' to switch turn-taking "); if (twoTurns) { 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 (domainName.Equals("turns")) { twoTurns = !twoTurns; } 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") && !domainName.Equals("turns")) { // 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 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); // Welcome the player to the game. Console.WriteLine("Welcome to " + domain.Name); // Create the initial node of mediation space. tree = new MediationTree(domain, problem, Parser.GetTopDirectory() + @"MediationTrees\Data\" + domain.Name + @"\"); if (debug) { Console.WriteLine(); Console.WriteLine("Nodes Expanded: " + tree.TotalNodes); Console.WriteLine("Dead Ends Expanded: " + tree.DeadEndCount); Console.WriteLine("Goal States Expanded: " + tree.GoalStateCount); Console.WriteLine("Lowest Depth Expanded: " + tree.LowestDepth); } current = tree.Root; computerActions = new List <IOperator>(); if (!twoTurns) { while (!tree.GetTurnAtIndex(current.Depth).Equals(current.Problem.Player)) { TakeTurn(); } } // Initialize a stopwatch for debugging. Stopwatch watch = new Stopwatch(); Console.Out.WriteLine(); // Present the initial state. command = ""; Look(); // Loop while this is false. bool exit = false; while (!exit) { exploreFrontier = true; // 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 && current.Plan.Steps.Count > 0 && !command.Equals("clear") && !command.Equals("cls")) { Console.Out.WriteLine(); Console.WriteLine("Narrative Trajectory:"); int longestName = 0; foreach (Operator step in current.Plan.Steps) { if (step.TermAt(0).Length > longestName) { longestName = step.TermAt(0).Length; } } string lastName = ""; foreach (Operator step in current.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 (current.IsGoal) { Console.WriteLine("GOAL STATE"); Console.ReadKey(); exit = true; } // Check for incompatible state. else if (current.DeadEnd) { Console.WriteLine("UNWINNABLE STATE"); Console.ReadKey(); exit = true; } if (exit) { Console.Clear(); } // Kill the frontier thread (this should be okay). exploreFrontier = false; } MTGame.Play(); }
public static void TwoArgs() { if (arguments.Count != 2) { Unknown(); return; } MediationTreeEdge matchingEdge = null; foreach (MediationTreeEdge edge in current.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)) { current = frontier[matchingEdge] as MediationTreeNode; } else { exploreFrontier = false; while (frontierThread.IsAlive) { Thread.Sleep(50); } current = tree.GetNode(current.Domain, current.Problem, matchingEdge); } 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'."); } }
// Creates a list of all possible things a character can do. public static List <MediationTreeEdge> GetAllPossibleActions(string character, MediationTreeNode node) { // Create a list of possible player actions in the current state. List <Operator> possibleActions = GetActions(character, node.Domain, node.Problem, node.State); // Create a list of the causal links that span the state. List <CausalLink> spanningLinks = (List <CausalLink>)GetSpanningLinks(node.Plan); // Create an empty list of exceptional actions. List <MediationTreeEdge> exceptionalActions = new List <MediationTreeEdge>(); // Store the exceptional actions to be removed later. List <Operator> exceptions = new List <Operator>(); // Store exceptional effects. Hashtable exceptionalEffects = new Hashtable(); // Loop through every possible action. foreach (Operator action in possibleActions) { // Loop through every action's effects. foreach (Predicate effect in action.Effects) { // Loop through every spanning link. foreach (CausalLink link in spanningLinks) { // If the effect is the inverse of the protected predicate... if (effect.IsInverse(link.Predicate)) { // Identify the exceptional effect. if (exceptionalEffects.ContainsKey(action.Predicate.ToString())) { List <IPredicate> list = exceptionalEffects[action.Predicate.ToString()] as List <IPredicate>; list.Add(effect.Clone() as Predicate); exceptionalEffects[action.Predicate.ToString()] = list; } else { exceptionalEffects[action.Predicate.ToString()] = new List <IPredicate> { effect.Clone() as Predicate } }; // Add the current action to the list of exceptional actions. exceptionalActions.Add(new MediationTreeEdge(action.Clone() as Operator, ActionType.Exceptional, node.ID)); // Add the actual action to the exception list. exceptions.Add(action); } } } //Loop through the action's axioms foreach (IAxiom conditional in node.State.ApplicableConditionals(action, node.Problem.Objects)) { // Loop through every conditional's effects. foreach (IPredicate effect in conditional.Effects) { // Loop through every spanning link. foreach (IDependency link in spanningLinks) { // If the effect is the inverse of the protected predicate... if (effect.IsInverse(link.Predicate)) { // Identify the exceptional effect. if (exceptionalEffects.ContainsKey(action.Predicate.ToString())) { List <IPredicate> list = exceptionalEffects[action.Predicate.ToString()] as List <IPredicate>; list.Add(effect.Clone() as Predicate); exceptionalEffects[action.Predicate.ToString()] = list; } else { exceptionalEffects[action.Predicate.ToString()] = new List <IPredicate> { effect.Clone() as Predicate } }; // Add the current action to the list of exceptional actions. exceptionalActions.Add(new MediationTreeEdge(action.Clone() as Operator, ActionType.Exceptional, node.ID)); // Add the actual action to the exception list. exceptions.Add(action); } } } } } // Remove the exceptions from the possible actions. foreach (Operator exception in exceptions) { possibleActions.Remove(exception); } // This is a hack to remove duplicate exceptional actions with a Remove By Property method. List <int> remove = new List <int>(); // Remove duplicate actions. for (int i = 0; i < exceptionalActions.Count - 1; i++) { for (int j = i + 1; j < exceptionalActions.Count; j++) { if (exceptionalActions[i].Action.Equals(exceptionalActions[j].Action)) { remove.Add(j); } } } for (int i = exceptionalActions.Count - 1; i >= 0; i--) { if (remove.Contains(i)) { exceptionalActions.RemoveAt(i); } else { exceptionalActions[i].Action.ExceptionalEffects = exceptionalEffects[exceptionalActions[i].Action.Predicate.ToString()] as List <IPredicate>; } } // Create an empty list of consistent actions. List <MediationTreeEdge> consistentActions = new List <MediationTreeEdge>(); // Loop through the remaining actions. foreach (Operator action in possibleActions) { // Add the current action to the list of consistent actions. consistentActions.Add(new MediationTreeEdge(action.Clone() as Operator, ActionType.Consistent, node.ID)); } // Make the set of outgoing edges. List <MediationTreeEdge> actions = new List <MediationTreeEdge>(); // Find the current constituent action. MediationTreeEdge constituent = GetConstituentAction(character, node); // Add the constituent action to the possible actions. actions.Add(constituent); // Remove any exceptional actions that correspond to the constituent step. foreach (MediationTreeEdge exception in exceptionalActions) { if (!exception.Action.Equals(constituent.Action)) { actions.Add(exception); } } // Remove any exceptional actions that correspond to the constituent step. foreach (MediationTreeEdge consistent in consistentActions) { if (!consistent.Action.Equals(constituent.Action)) { actions.Add(consistent); } } // Return the outgoing edges. return(actions); } } }
// 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); } }