public void GetByPredicate() { var lst = new List <IPlan>() { _plan.Clone(), _plan.Clone(), _plan.Clone(), _plan.Clone(), _plan.Clone(), _plan.Clone(), _plan.Clone(), _plan.Clone(), _plan.Clone(), _plan.Clone() }; lst[0].IsDone = true; lst = rep.SaveMany(lst.Select(c => c as Plan)).ToList().Select(c => c as IPlan).ToList(); var res = rep.GetByPredicate(c => c.IsDone, null).FirstOrDefault(); rep.DeleteMany(lst.Select(c => c as Plan)); Assert.AreEqual(res.Id, lst.First().Id); }
public void StateSpaceToolsGetPlayerActionsCountUpdatePlanTest() { Plan newPlan = testPlan.Clone() as Plan; State newState = testPlan.Initial.Clone() as State; newPlan.Initial = newState.NewState(testPlan.Steps[0] as Operator, testProblem.Objects); newPlan.Steps.RemoveAt(0); List <Operator> actions = StateSpaceTools.GetPlayerActions(testDomain, testProblem, newPlan.Initial as State); Assert.AreEqual(2, actions.Count); }
// Create the applicable constituent action for a state. public static StateSpaceEdge GetConstituentAction(Domain domain, Problem problem, Plan plan, State state) { // Iterate through the plan to find the next player step. for (int i = 0; i < plan.Steps.Count; i++) { if (plan.Steps[i].Arity > 0) { // Check to see if the next step in the plan is performed by the player. if (plan.Steps[i].TermAt(0).Equals(problem.Player)) { // Create a new plan. Plan newPlan = 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, plan.Steps[i].Clone() as Operator); // Check to see if the new plan is valid. if (PlanSimulator.VerifyPlan(newPlan, state, problem.Objects)) { // The current step is constituent. return(new StateSpaceEdge(plan.Steps[i].Clone() as Operator, ActionType.Constituent)); } // Exit the loop. i = plan.Steps.Count; } } } // Otherwise, return a blank step as the user's constituent action. return(new StateSpaceEdge(new Operator("do nothing"), ActionType.Constituent)); }
// Given a plan and the current state, verify it can be executed. public static bool VerifyPlan(Plan plan, State state, List <IObject> objects) { // If there are steps left in the plan... if (plan.Steps.Count > 0) { // If the next plan step can be executed... if (state.Satisfies(plan.Steps.First().Preconditions)) { // Create a new plan. Plan newPlan = plan.Clone() as Plan; // Remove the first step. newPlan.Steps.RemoveAt(0); // Update the world state and recursively call the function. return(VerifyPlan(newPlan, state.NewState(plan.Steps.First().Clone() as Operator, objects), objects)); } else { // This is not a valid plan. return(false); } } // This is a valid plan. return(true); }
static void Main(string[] args) { Console.Write("hello world\n"); Parser.path = @"D:\documents\frostbow\VHSP-Csharp-Frostbow\"; var directory = Parser.GetTopDirectory() + @"/Results/"; var cutoff = 6000f; var k = 1; 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"); var initPlan = new Plan(new State(testProblem.Initial) as IState, new State(testProblem.Goal) as IState); initPlan.Objects = testProblem.Objects; // BFS RunPlanner(initPlan.Clone() as IPlan, testDomain.Operators, new Nada(new ZeroHeuristic()), new BFS(), k, cutoff, directory, 1); Console.ReadLine(); //RunPlanner(initPlan.Clone() as IPlan, testDomain.Operators, new E0(new AddReuseHeuristic()), new ADstar(), k, cutoff, directory, 1); //Console.ReadLine(); //RunPlanner(initPlan.Clone() as IPlan, new Nada(new ZeroHeuristic()), new DFS(), k, cutoff, directory, 1); Console.ReadLine(); }
/// <summary> /// When the user clicks "load", import the plan. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnLoad_Click(object sender, EventArgs e) { m_selectedPlan = lbPlan.SelectedItem as Plan; m_selectedChar = cbCharacter.SelectedItem as Character; m_targetPlan = m_selectedPlan.Clone(m_targetCharacter); DialogResult = DialogResult.OK; this.Close(); }
// Creates a list of actions for computer controlled characters to perform. public static List <IOperator> GetSystemActions(Plan plan, List <string> moved, List <IOperator> actions) { // Iterate through the plan to find the next player step. for (int i = 0; i < plan.Steps.Count; i++) { // Make sure the step has arity. if (plan.Steps[i].Arity > 0) { // A placeholder to check if the character has already moved. bool hasMoved = false; // Loop through the characters that have moved. foreach (string character in moved) { // If it matches the actor... if (plan.Steps[i].TermAt(0).Equals(character)) { // ...mark as already moved. hasMoved = true; } } // If the character has not moved yet. if (!hasMoved) { // Create a new plan. Plan newPlan = 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, plan.Steps[i].Clone() as Operator); // Check to see if the new plan is valid. if (PlanSimulator.VerifyPlan(newPlan, plan.Initial as State, plan.Problem.Objects)) { // Add the step to the list of actions. actions.Add(newPlan.Steps[0]); // Remove the first step from the plan. newPlan.Steps.RemoveAt(0); // Add the character to the list of moved characters. moved.Add(plan.Steps[i].TermAt(0)); // Recursively call the function. return(GetSystemActions(newPlan, moved, actions)); } } } } // Return the list of actions to take. return(actions); }
public void PlanSimulatorVerifyPlanTestFalse() { Plan newPlan = testPlan.Clone() as Plan; newPlan.Steps = new List <IOperator> { new Operator ( new Predicate("move", new List <ITerm> { new Term("arthur", true), new Term("woods", true), new Term("lake", true) }, true), new List <IPredicate> { new Predicate("at", new List <ITerm> { new Term("arthur", true), new Term("woods", true) }, true), new Predicate("at", new List <ITerm> { new Term("arthur", true), new Term("lake", true) }, false), new Predicate("character", new List <ITerm> { new Term("arthur", true) }, true), new Predicate("alive", new List <ITerm> { new Term("arthur", true) }, true), new Predicate("location", new List <ITerm> { new Term("woods", true) }, true), new Predicate("location", new List <ITerm> { new Term("lake", true) }, true), new Predicate("connected", new List <ITerm> { new Term("lake", true), new Term("woods", true) }, true), }, new List <IPredicate> { new Predicate("at", new List <ITerm> { new Term("arthur", true), new Term("woods", true) }, false), new Predicate("at", new List <ITerm> { new Term("arthur", true), new Term("lake", true) }, true), } ), new Operator ( new Predicate("move", new List <ITerm> { new Term("arthur", true), new Term("woods", true), new Term("lake", true) }, true), new List <IPredicate> { new Predicate("at", new List <ITerm> { new Term("arthur", true), new Term("woods", true) }, true), new Predicate("at", new List <ITerm> { new Term("arthur", true), new Term("lake", true) }, false), new Predicate("character", new List <ITerm> { new Term("arthur", true) }, true), new Predicate("alive", new List <ITerm> { new Term("arthur", true) }, true), new Predicate("location", new List <ITerm> { new Term("woods", true) }, true), new Predicate("location", new List <ITerm> { new Term("lake", true) }, true), new Predicate("connected", new List <ITerm> { new Term("lake", true), new Term("woods", true) }, true), }, new List <IPredicate> { new Predicate("at", new List <ITerm> { new Term("arthur", true), new Term("woods", true) }, false), new Predicate("at", new List <ITerm> { new Term("arthur", true), new Term("lake", true) }, true), } ) }; Assert.IsFalse(PlanSimulator.VerifyPlan(newPlan, newPlan.Initial as State, new List <IObject> { new Obj("arthur", ""), new Obj("woods", ""), new Obj("lake", ""), new Obj("excalibur", "") })); }
// Creates a child node. private static StateSpaceNode CreateChild(Planner planner, Domain domain, Problem problem, Plan plan, State state, StateSpaceEdge edge) { // Create a new problem object. Problem newProblem = new Problem(); // Create a new domain object. Domain newDomain = domain; // Create a new plan object. Plan newPlan = new Plan(); // Store actions the system takes. List <IOperator> systemActions = new List <IOperator>(); // If the outgoing action is an exceptional step... if (edge.ActionType == ActionType.Exceptional) { // Count the exceptional edge. Exceptional++; // Create a new problem object. newProblem = NewProblem(newDomain, problem, state, edge.Action); // Find a new plan. newPlan = PlannerInterface.Plan(planner, newDomain, newProblem); // If the action was accommodated and the first step of the new plan isn't taken by the player... if (newPlan.Steps.Count > 0) { // Add the action to the system action list. systemActions = GetSystemActions(newPlan, new List <string> { problem.Player }, new List <IOperator>()); // Update the problem object with the system's next move. newProblem = NewProblem(newDomain, newProblem, new State(newProblem.Initial, newPlan.InitialStep, (Operator)newPlan.Steps.First()), systemActions); // Update the plan with the system's next move. newPlan = newPlan.GetPlanUpdate(newProblem, systemActions); } else if (CEDeletion) { // Try to find a domain revision alibi. Tuple <Domain, Operator> alibi = ReviseDomain(planner, newDomain, problem, state, edge.Action as Operator); // If domain revision worked. if (alibi != null) { // Remember the new domain. newDomain = alibi.First; // Push the modified action to the edge object. edge.Action = alibi.Second; // Create a new problem object. newProblem = NewProblem(newDomain, problem, state, edge.Action); // Find a new plan. newPlan = PlannerInterface.Plan(planner, newDomain, newProblem); // Add the action to the system action list. systemActions = GetSystemActions(newPlan, new List <string> { problem.Player }, new List <IOperator>()); // Update the problem object with the system's next move. newProblem = NewProblem(newDomain, newProblem, new State(newProblem.Initial, newPlan.InitialStep, (Operator)newPlan.Steps.First()), systemActions); // Update the plan with the system's next move. newPlan = newPlan.GetPlanUpdate(newProblem, systemActions); } } } // Otherwise, if the action is a consistent step... else if (edge.ActionType == ActionType.Consistent) { // Count the consistent edge. Consistent++; // Create a new problem object. newProblem = NewProblem(newDomain, problem, state, edge.Action); // Add the action to the system action list. systemActions = GetSystemActions(plan, new List <string> { problem.Player }, new List <IOperator>()); // Create a new state. State newState = new State(newProblem.Initial, state.nextStep, (Operator)plan.Steps.First()); // Create a new problem object. newProblem = NewProblem(newDomain, newProblem, newState, systemActions); // Create a new plan. newPlan = plan.GetPlanUpdate(newProblem, systemActions); } // Otherwise, the action is a constituent step... else { // Count the constituent edge. Constituent++; // If there are effects of the constituent action... if (edge.Action.Effects.Count > 0) { // Create a new problem object. newProblem = NewProblem(newDomain, problem, state, edge.Action); // Create a new plan. newPlan = plan.GetPlanUpdate(newProblem, edge.Action as Operator); } // Otherwise, initialize to the old problem and plan... else { newProblem = problem; newPlan = plan.Clone() as Plan; } // If there are still plan actions... if (newPlan.Steps.Count > 0) { // Add the action to the system action list. systemActions = GetSystemActions(newPlan, new List <string> { problem.Player }, new List <IOperator>()); // Update the problem object with the system's next move. newProblem = NewProblem(newDomain, newProblem, new State(newProblem.Initial, newPlan.InitialStep, (Operator)newPlan.Steps.First()), systemActions); // Update the plan with the system's next move. newPlan = newPlan.GetPlanUpdate(newProblem, systemActions); } } // Add the system actions to the current edge. edge.SystemActions = systemActions; // Create an empty child node. StateSpaceNode child = null; // If there are remaining plan steps... if (newPlan.Steps.Count > 0) { // Build a new tree using the first step of the plan as the next step. child = StateSpaceSearchTools.CreateNode(planner, newDomain, newProblem, newPlan, new State(newProblem.Initial, newPlan.InitialStep, (Operator)newPlan.Steps.First())); } else { // Terminate the tree by adding a goal node. child = StateSpaceSearchTools.CreateNode(planner, newDomain, newProblem, newPlan, new State(newProblem.Initial, newPlan.InitialStep, newPlan.GoalStep)); } // Store the system actions. child.systemActions = systemActions; // Record the action that triggered the node's generation. child.incoming = edge; return(child); }
// Expand an edge. public static StateSpaceNode ExpandTree(Planner planner, Domain domain, Problem problem, Plan plan, State state, StateSpaceEdge edge, int depth) { // Create a new problem object. Problem newProblem = new Problem(); // Create a new plan object. Plan newPlan = new Plan(); // Store actions the system takes. List <IOperator> systemActions = new List <IOperator>(); // If the action is an exceptional step... if (edge.ActionType == ActionType.Exceptional) { // Count the exceptional edge. Exceptional++; // Create a new problem object. newProblem = NewProblem(domain, problem, state, edge.Action); // Find a new plan. newPlan = PlannerInterface.Plan(planner, domain, newProblem); // If the action was accommodated and the first step of the new plan isn't taken by the player... if (newPlan.Steps.Count > 0) { // Add the action to the system action list. systemActions = GetSystemActions(newPlan, new List <string> { problem.Player }, new List <IOperator>()); // Update the problem object with the system's next move. newProblem = NewProblem(domain, newProblem, new State(newProblem.Initial, newPlan.InitialStep, (Operator)newPlan.Steps.First()), systemActions); // Update the plan with the system's next move. newPlan = newPlan.GetPlanUpdate(newProblem, systemActions); } else if (CEDeletion) { StateSpaceNode alibi = GenerateAlibi(planner, domain, problem, plan, state, edge, depth); if (alibi != null) { return(alibi); } } } // Otherwise, if the action is a consistent step... else if (edge.ActionType == ActionType.Consistent) { // Count the consistent edge. Consistent++; // Create a new problem object. newProblem = NewProblem(domain, problem, state, edge.Action); // Add the action to the system action list. systemActions = GetSystemActions(plan, new List <string> { problem.Player }, new List <IOperator>()); // Create a new state. State newState = new State(newProblem.Initial, state.nextStep, (Operator)plan.Steps.First()); // Create a new problem object. newProblem = NewProblem(domain, newProblem, newState, systemActions); // Create a new plan. newPlan = plan.GetPlanUpdate(newProblem, systemActions); } // Otherwise, the action is a constituent step... else { // Count the constituent edge. Constituent++; // If there are effects of the constituent action... if (edge.Action.Effects.Count > 0) { // Create a new problem object. newProblem = NewProblem(domain, problem, state, edge.Action); // Create a new plan. newPlan = plan.GetPlanUpdate(newProblem, edge.Action as Operator); } // Otherwise, initialize to the old problem and plan... else { newProblem = problem; newPlan = plan.Clone() as Plan; } // If there are still plan actions... if (newPlan.Steps.Count > 0) { // Add the action to the system action list. systemActions = GetSystemActions(newPlan, new List <string> { problem.Player }, new List <IOperator>()); // Update the problem object with the system's next move. newProblem = NewProblem(domain, newProblem, new State(newProblem.Initial, newPlan.InitialStep, (Operator)newPlan.Steps.First()), systemActions); // Update the plan with the system's next move. newPlan = newPlan.GetPlanUpdate(newProblem, systemActions); } } // Add the system actions to the current edge. edge.SystemActions = systemActions; // Create an empty child node. StateSpaceNode child = null; // If there are remaining plan steps... if (newPlan.Steps.Count > 0) { // Build a new tree using the first step of the plan as the next step. child = BuildTree(planner, domain, newProblem, newPlan, new State(newProblem.Initial, newPlan.InitialStep, (Operator)newPlan.Steps.First()), depth - 1); } else { // Terminate the tree by adding a goal node. child = BuildTree(planner, domain, newProblem, newPlan, new State(newProblem.Initial, newPlan.InitialStep, newPlan.GoalStep), depth - 1); } // Store the system actions. child.systemActions = systemActions; // Record the action that triggered the node's generation. child.incoming = edge; return(child); }
// Build the mediation tree using a depth-first strategy. public static StateSpaceNode BuildTree(Planner planner, Domain domain, Problem problem, Plan plan, State state, int depth) { // Create a node for the current state. StateSpaceNode root = new StateSpaceNode(); // Record that a node was created. NodeCount++; // Return any empty plans. if (plan.Steps.Count == 0) { // If the goal is satisfied... if (state.Satisfies(plan.GoalStep.Preconditions)) { // Differentiate a satisfied goal. root.satisfiesGoal = true; // Record that a goal state was reached. GoalStateCount++; } else { // Record that a dead end state was reached. DeadEndCount++; } // Return the leaf node. return(root); } // Set the node's domain. root.domain = domain; // Set the node's problem. root.problem = problem; // Set the node's plan. root.plan = plan; // Set the node's state. root.state = state; // Find out what the player knows. root.problem.Initial = RobertsonMicrotheory.Annotate(problem.Initial, problem.Player); // Find all outgoing user actions from this state. root.outgoing = StateSpaceTools.GetAllPossibleActions(domain, problem, plan, state); // Return the node if the depth limit has been reached. if (depth <= 0) { return(root); } // Loop through the possible actions. foreach (StateSpaceEdge edge in root.outgoing) { StateSpaceNode child = ExpandTree(planner, domain, problem, plan.Clone() as Plan, state.Clone() as State, edge, depth); // Set the child's parent to this node. child.parent = root; // Add the child to the parent's collection of children, by way of the current action. root.children[edge] = child; } // Return the current node. return(root); }