/// <summary> /// Method is an entry point that controls the operation of the algorithm (the sequence of launching other methods). /// </summary> public void Start() { // We read the settings and create the initial state of the world. // ReadUserSettingsInput(); CreateInitialState(); CreateConstraints(); GenerateNewPDDLDomains(); // We create a start node (root) based on the start state of the world. StoryNode root = new StoryNode(); root.SetWorldState(currentStoryState); root.SetActivePlayer(false); root.SetActiveAgent(currentStoryState.GetFirstAgent()); newStoryGraph.SetRoot(root); // We go through all the agents and remember their goals. storyworldConvergence.ExtractGoals(currentStoryState); // The algorithm calculates a SPECIFIC story. newStoryGraph = CreateStoryGraph(newStoryGraph.GetRoot()); // Create a visual graph. graphСonstructor.CreateGraph(newStoryGraph, @"D:\Graphviz\bin\newStoryGraph.dt"); // Create an HTML file including Twine engine and generated history. //twineGraphConstructor.ConstructGraph(newStoryGraph); //twineGraphConstructor.CreateHTMLFileWithGame(); // SaveFile(); }
/// <summary> /// The method that controls the creation of the history graph. /// </summary> public StoryGraph CreateStoryGraph(StoryNode rootNode) { // We clone the root into a separate variable. StoryNode originRoot = (StoryNode)rootNode.Clone(); // We continue to work until at least one target state is reached. while (!reachedGoalState) { // We create a new graph by starting to expand the root. BFSTraversing(newStoryGraph.GetRoot()); // We go through the created graph, looking for target states in it. BFSGoalAchieveControl(newStoryGraph.GetRoot()); // If it was not possible to find even one target state in the constructed graph. if (!reachedGoalState || newStoryGraph.GetNodes().Count > maxNodes) { graphСonstructor.CreateGraph(newStoryGraph, @"D:\Graphviz\bin\newStoryGraph.dt"); // Then we clear the graph, and all the links added to the root. newStoryGraph = new StoryGraph(); originRoot.GetEdges().Clear(); originRoot.GetLinks().Clear(); // After that, we reassign to the previously cleared column an indication of the root. newStoryGraph.SetRoot((StoryNode)originRoot.Clone()); reachedGoalState = false; } } // We return a graph that is guaranteed to have at least one target state. return(newStoryGraph); }
public bool DuplicateControl(WorldDynamic currentState, PlanAction action, StoryGraph currentGraph, KeyValuePair <AgentStateStatic, AgentStateDynamic> agent, StoryNode currentNode, int globalNodeNumber, bool succsessControl) { StoryNode testNode = currentGraph.CreateTestNode(currentState, action, agent, currentNode, false, globalNodeNumber, succsessControl); testNode.UpdateHashCode(); foreach (var checkedNode in currentGraph.GetNodes()) { checkedNode.UpdateHashCode(); if (currentGraph.TwoNodesComparison(testNode, checkedNode)) { currentGraph.DeleteTestNode(ref testNode); return(false); } } currentGraph.DeleteTestNode(ref testNode); return(true); }
public bool CyclesControl(WorldDynamic currentState, PlanAction action, StoryGraph currentGraph, KeyValuePair <AgentStateStatic, AgentStateDynamic> agent, StoryNode currentNode, bool duplicated, int globalNodeNumber, bool succsessControl) { bool result = false; // We create a test node similar to the one we are going to add to the graph as a result of the current action. StoryNode testNode = currentGraph.CreateTestNode(currentState, action, agent, currentNode, !duplicated, globalNodeNumber, succsessControl); StoryNode duplicatedNode = null; Edge testEdge = new Edge(); if (!duplicated) { duplicatedNode = currentGraph.GetNode(testNode); if (currentNode.Equals(duplicatedNode)) { return(false); } testEdge.SetUpperNode(ref currentNode); testEdge.SetLowerNode(ref duplicatedNode); currentNode.AddEdge(testEdge); duplicatedNode.AddEdge(testEdge); currentNode.AddLinkToNode(ref duplicatedNode); duplicatedNode.AddLinkToNode(ref currentNode); } string[] colors = new string[currentGraph.GetNodes().Count + 2]; for (int i = 0; i < currentGraph.GetNodes().Count + 2; i++) { colors[i] = "white"; } result = TarjanAlgStep(currentGraph.GetRoot(), ref colors, !duplicated, duplicatedNode); if (!duplicated) { currentNode.RemoveEdge(testEdge); currentNode.DeleteLink(duplicatedNode); duplicatedNode.RemoveEdge(testEdge); duplicatedNode.DeleteLink(currentNode); testEdge.ClearUpperNode(); testEdge.ClearLowerNode(); testEdge = null; } // We delete the test node and mark the loop test as passed. currentGraph.DeleteTestNode(ref testNode); return(result); }
public void MultiAVandAC(ref PlanAction receivedAction, WorldDynamic currentState, KeyValuePair <AgentStateStatic, AgentStateDynamic> agent, CSP_Module cspModule, StoryGraph currentGraph, StoryNode currentNode, bool root, ref int globalNodeNumber, ref Queue <StoryNode> queue) { List <PlanAction> actionsList = cspModule.MassiveAssignVariables(ref receivedAction, currentState, agent); AgentStateStatic sCurrentAgent = (AgentStateStatic)agent.Key.Clone(); AgentStateDynamic dCurrentAgent = (AgentStateDynamic)agent.Value.Clone(); KeyValuePair <AgentStateStatic, AgentStateDynamic> currentAgent = new KeyValuePair <AgentStateStatic, AgentStateDynamic>(sCurrentAgent, dCurrentAgent); WorldDynamic statePrefab = (WorldDynamic)currentState.Clone(); foreach (var a in actionsList) { ActionControl(a, currentGraph, currentAgent, statePrefab, currentNode, root, ref globalNodeNumber, ref queue); } // Cleaning actionsList = null; currentNode = null; statePrefab = null; GC.Collect(); }
public void CounterreactionControl(PlanAction action, StoryGraph currentGraph, KeyValuePair <AgentStateStatic, AgentStateDynamic> agent, WorldDynamic currentState, StoryNode currentNode, bool root, ref int globalNodeNumber, ref Queue <StoryNode> queue, ref bool controlOne, ref bool controlTwo) { bool succsessControl = ProbabilityCalculating(action); action.success = succsessControl; action.fail = !succsessControl; bool constraintsControl = ConstraintsControl(currentState, action, succsessControl); bool deadEndsControl = DeadEndsControl(action, currentState, agent, succsessControl); bool duplicateControl = DuplicateControl(currentState, action, currentGraph, agent, currentNode, globalNodeNumber, succsessControl); //bool cyclesControl = CyclesControl(currentState, action, currentGraph, agent, currentNode, duplicateControl, globalNodeNumber, succsessControl); bool cyclesControl = true; controlOne = constraintsControl & deadEndsControl & cyclesControl; controlTwo = duplicateControl; }
public Graph CreateRiversGraph(StoryGraph storyGraph, string graphName) { var graph = new Graph(); graph.Name = graphName; int counter = 0; string nodeName1 = ""; string nodeName2 = ""; string inActionName = ""; string inActionRole = ""; string action = ""; foreach (var node in storyGraph.GetNodes()) { counter++; inActionName = node.GetActiveAgent().Key.GetName(); inActionRole = node.GetActiveAgent().Key.GetRole().ToString(); action = node.GetEdges().First().GetAction().GetType().ToString().Remove(0, 20); if (nodeName1 == "" && nodeName2 == "") { nodeName1 = "On the move: " + inActionName + " Role: " + inActionRole + " Action: " + action; graph.Nodes.Add(new Node(nodeName1)); } else if (nodeName1 != "" && nodeName2 == "") { nodeName2 = "On the move: " + inActionName + " Role: " + inActionRole + " Action: " + action; graph.Nodes.Add(new Node(nodeName2)); if (counter > 1) { graph.Edges.Add(new Rivers.Edge(graph.Nodes[nodeName1], graph.Nodes[nodeName2])); } nodeName1 = ""; } else if (nodeName1 == "" && nodeName2 != "") { nodeName1 = "On the move: " + inActionName + " Role: " + inActionRole + " Action: " + action; graph.Nodes.Add(new Node(nodeName1)); if (counter > 1) { graph.Edges.Add(new Rivers.Edge(graph.Nodes[nodeName2], graph.Nodes[nodeName1])); } nodeName2 = ""; } } return(graph); }
/// <summary> /// Checking the action for violation of the established constraints and the reachability of the goal state (control of cycles and deadends). /// </summary> public void ActionControl(PlanAction action, StoryGraph currentGraph, KeyValuePair <AgentStateStatic, AgentStateDynamic> agent, WorldDynamic currentState, StoryNode currentNode, bool root, ref int globalNodeNumber, ref Queue <StoryNode> queue) { bool succsessControl = ProbabilityCalculating(action); action.success = succsessControl; action.fail = !succsessControl; bool constraintsControl = ConstraintsControl(currentState, action, succsessControl); bool deadEndsControl = DeadEndsControl(action, currentState, agent, succsessControl); bool duplicateControl = DuplicateControl(currentState, action, currentGraph, agent, currentNode, globalNodeNumber, succsessControl); //bool cyclesControl = CyclesControl(currentState, action, currentGraph, agent, currentNode, duplicateControl, globalNodeNumber, succsessControl); bool cyclesControl = true; if (constraintsControl && deadEndsControl && cyclesControl && duplicateControl) { // If all checks are passed, then we apply the action. ApplyAction(action, currentGraph, agent, currentState, currentNode, root, ref globalNodeNumber, succsessControl, false); } else if (!constraintsControl && deadEndsControl && cyclesControl && duplicateControl) { // If the action violates the constraints, then convergence will not apply it, but will apply its counter-reaction. ActionCounteract(action, currentGraph, agent, currentState, currentNode, root, ref globalNodeNumber, ref queue); } else if (!duplicateControl && cyclesControl) { bool skip = false; // connection current node --> finded node currentGraph.DuplicateNodeConnecting(currentState, action, agent, currentNode, globalNodeNumber, ref queue, succsessControl, ref skip); if (skip) { //NothingToDo newAction = new NothingToDo(); //newAction.Arguments.Add(agent); //ApplyAction(newAction, currentGraph, agent, currentState, currentNode, root, ref globalNodeNumber, succsessControl, true); ActionCounteract(action, currentGraph, agent, currentState, currentNode, root, ref globalNodeNumber, ref queue); } } else { ActionCounteract(action, currentGraph, agent, currentState, currentNode, root, ref globalNodeNumber, ref queue); } }
public void SingleAVandAC(ref PlanAction receivedAction, WorldDynamic currentState, KeyValuePair <AgentStateStatic, AgentStateDynamic> agent, CSP_Module cspModule, StoryGraph currentGraph, StoryNode currentNode, bool root, ref int globalNodeNumber, ref Queue <StoryNode> queue) { cspModule.AssignVariables(ref receivedAction, currentState, agent); ActionControl(receivedAction, currentGraph, agent, currentState, currentNode, root, ref globalNodeNumber, ref queue); // Cleaning currentNode = null; GC.Collect(); }
/// <summary> /// The probability of success of the action is calculated, and if successful, it is applied. /// </summary> public void ApplyAction(PlanAction action, StoryGraph currentGraph, KeyValuePair <AgentStateStatic, AgentStateDynamic> agent, WorldDynamic currentState, StoryNode currentNode, bool root, ref int globalNodeNumber, bool succsessControl, bool counteract) { // We apply a successful/unsuccessful option to perform an action. if (root) { currentGraph.CreateRootNode(action, agent, currentState, currentNode, ref globalNodeNumber, succsessControl); } else { currentGraph.CreateNewNode(action, agent, currentState, currentNode, ref globalNodeNumber, succsessControl, counteract); } }
/// <summary> /// A method that describes the transmitted story graph in text format and creates a visualization based on it. /// </summary> public void CreateGraph(StoryGraph storyGraph, string graphName) { HashSet <Edge> edges = new HashSet <Edge>(); string graphSTR = "digraph G { \r\n"; // Generating a list of nodes. foreach (var node in storyGraph.GetNodes()) { // We compose a line with information about the node under consideration. if (node.goalState) { graphSTR = graphSTR.Insert(graphSTR.Length, node.GetNumberInSequence() + " [shape =" + '"' + "circle" + '"' + " label =" + '"' + " " + node.GetNumberInSequence() + '"' + " " + "style = " + '"' + "filled" + '"' + " fillcolor = " + '"' + "yellow" + '"' + "] \r\n"); } else if (node.skiped) { graphSTR = graphSTR.Insert(graphSTR.Length, node.GetNumberInSequence() + " [shape =" + '"' + "circle" + '"' + " label =" + '"' + " " + node.GetNumberInSequence() + '"' + " " + "style = " + '"' + "filled" + '"' + " fillcolor = " + '"' + "green" + '"' + "] \r\n"); } else if (node.counteract) { graphSTR = graphSTR.Insert(graphSTR.Length, node.GetNumberInSequence() + " [shape =" + '"' + "circle" + '"' + " label =" + '"' + " " + node.GetNumberInSequence() + '"' + " " + "style = " + '"' + "filled" + '"' + " fillcolor = " + '"' + "aquamarine" + '"' + "] \r\n"); } else { graphSTR = graphSTR.Insert(graphSTR.Length, node.GetNumberInSequence() + " [shape =" + '"' + "circle" + '"' + " label =" + '"' + " " + node.GetNumberInSequence() + '"' + "] \r\n"); } } // Generating graph edges. foreach (var node in storyGraph.GetNodes()) { // If the node under consideration is not the last. if (node != storyGraph.GetLastNode()) { // Then we go along the edges attached to this node. foreach (var edge in node.GetEdges()) { bool skip = false; // We go through the list of already traversed edges and compare // if we have previously met the edge that we are considering now. foreach (var e in edges) { if (e.Equals(edge)) { skip = true; break; } } // If already met, then move on to the next one. if (skip) { continue; } // If the edge has an attached action and the bottom end is connected to some node. if (edge.GetAction() != null && edge.GetLowerNode() != null) { // Create a line with information about the edge. if (edge.GetAction() is Kill || edge.GetAction() is CounterKill) { graphSTR = graphSTR.Insert(graphSTR.Length, edge.GetUpperNode().GetNumberInSequence() + "->" + edge.GetLowerNode().GetNumberInSequence() + "[label = " + " " + '"' + ((KeyValuePair <AgentStateStatic, AgentStateDynamic>)edge.GetAction().Arguments[1]).Key.GetName() + Environment.NewLine + " " + edge.GetAction().ToString().Remove(0, 20) + Environment.NewLine + " " + "whom: " + ((KeyValuePair <AgentStateStatic, AgentStateDynamic>)edge.GetAction().Arguments[0]).Key.GetName() + Environment.NewLine + " " + "where: " + ((KeyValuePair <LocationStatic, LocationDynamic>)edge.GetAction().Arguments[2]).Key.GetName() + Environment.NewLine + " " + "Success: " + edge.GetAction().success.ToString() + '"' + " color = " + '"' + " red" + '"' + "] \r\n"); } else if (edge.GetAction() is Entrap || edge.GetAction() is CounterEntrap) { graphSTR = graphSTR.Insert(graphSTR.Length, edge.GetUpperNode().GetNumberInSequence() + "->" + edge.GetLowerNode().GetNumberInSequence() + "[label = " + " " + '"' + ((KeyValuePair <AgentStateStatic, AgentStateDynamic>)edge.GetAction().Arguments[1]).Key.GetName() + Environment.NewLine + " " + edge.GetAction().ToString().Remove(0, 20) + Environment.NewLine + " " + "whom: " + ((KeyValuePair <AgentStateStatic, AgentStateDynamic>)edge.GetAction().Arguments[0]).Key.GetName() + Environment.NewLine + " " + "where: " + ((KeyValuePair <LocationStatic, LocationDynamic>)edge.GetAction().Arguments[2]).Key.GetName() + Environment.NewLine + " " + "Success: " + edge.GetAction().success.ToString() + '"' + "] \r\n"); } else if (edge.GetAction() is Move || edge.GetAction() is CounterMove) { graphSTR = graphSTR.Insert(graphSTR.Length, edge.GetUpperNode().GetNumberInSequence() + "->" + edge.GetLowerNode().GetNumberInSequence() + "[label = " + " " + '"' + ((KeyValuePair <AgentStateStatic, AgentStateDynamic>)edge.GetAction().Arguments[0]).Key.GetName() + Environment.NewLine + " " + edge.GetAction().ToString().Remove(0, 20) + Environment.NewLine + " " + "from: " + ((KeyValuePair <LocationStatic, LocationDynamic>)edge.GetAction().Arguments[1]).Key.GetName() + Environment.NewLine + " " + "to: " + ((KeyValuePair <LocationStatic, LocationDynamic>)edge.GetAction().Arguments[2]).Key.GetName() + Environment.NewLine + " " + "Success: " + edge.GetAction().success.ToString() + '"' + "] \r\n"); } else if (edge.GetAction() is InvestigateRoom || edge.GetAction() is CounterInvestigateRoom) { graphSTR = graphSTR.Insert(graphSTR.Length, edge.GetUpperNode().GetNumberInSequence() + "->" + edge.GetLowerNode().GetNumberInSequence() + "[label = " + " " + '"' + ((KeyValuePair <AgentStateStatic, AgentStateDynamic>)edge.GetAction().Arguments[0]).Key.GetName() + Environment.NewLine + " " + edge.GetAction().ToString().Remove(0, 20) + Environment.NewLine + " " + "where: " + ((KeyValuePair <LocationStatic, LocationDynamic>)edge.GetAction().Arguments[2]).Key.GetName() + Environment.NewLine + " " + "Success: " + edge.GetAction().success.ToString() + '"' + " color = " + '"' + " blue" + '"' + "] \r\n"); } else if (edge.GetAction() is Fight || edge.GetAction() is CounterFight) { graphSTR = graphSTR.Insert(graphSTR.Length, edge.GetUpperNode().GetNumberInSequence() + "->" + edge.GetLowerNode().GetNumberInSequence() + "[label = " + " " + '"' + ((KeyValuePair <AgentStateStatic, AgentStateDynamic>)edge.GetAction().Arguments[0]).Key.GetName() + Environment.NewLine + " " + edge.GetAction().ToString().Remove(0, 20) + Environment.NewLine + " " + "with: " + ((KeyValuePair <AgentStateStatic, AgentStateDynamic>)edge.GetAction().Arguments[1]).Key.GetName() + Environment.NewLine + " " + "where: " + ((KeyValuePair <LocationStatic, LocationDynamic>)edge.GetAction().Arguments[2]).Key.GetName() + Environment.NewLine + " " + "Success: " + edge.GetAction().success.ToString() + '"' + " color = " + '"' + " red" + '"' + "] \r\n"); } else if (edge.GetAction() is NeutralizeKiller || edge.GetAction() is CounterNeutralizeKiller) { graphSTR = graphSTR.Insert(graphSTR.Length, edge.GetUpperNode().GetNumberInSequence() + "->" + edge.GetLowerNode().GetNumberInSequence() + "[label = " + " " + '"' + ((KeyValuePair <AgentStateStatic, AgentStateDynamic>)edge.GetAction().Arguments[0]).Key.GetName() + Environment.NewLine + " " + edge.GetAction().ToString().Remove(0, 20) + Environment.NewLine + " " + "whom: " + ((KeyValuePair <AgentStateStatic, AgentStateDynamic>)edge.GetAction().Arguments[1]).Key.GetName() + Environment.NewLine + " " + "where: " + ((KeyValuePair <LocationStatic, LocationDynamic>)edge.GetAction().Arguments[2]).Key.GetName() + Environment.NewLine + " " + "Success: " + edge.GetAction().success.ToString() + '"' + " color = " + '"' + " red" + '"' + "] \r\n"); } else if (edge.GetAction() is Reassure || edge.GetAction() is CounterReassure) { graphSTR = graphSTR.Insert(graphSTR.Length, edge.GetUpperNode().GetNumberInSequence() + "->" + edge.GetLowerNode().GetNumberInSequence() + "[label = " + " " + '"' + ((KeyValuePair <AgentStateStatic, AgentStateDynamic>)edge.GetAction().Arguments[0]).Key.GetName() + Environment.NewLine + " " + edge.GetAction().ToString().Remove(0, 20) + Environment.NewLine + " " + "who: " + ((KeyValuePair <AgentStateStatic, AgentStateDynamic>)edge.GetAction().Arguments[1]).Key.GetName() + Environment.NewLine + " " + "Success: " + edge.GetAction().success.ToString() + '"' + "] \r\n"); } else if (edge.GetAction() is Run || edge.GetAction() is CounterRun) { graphSTR = graphSTR.Insert(graphSTR.Length, edge.GetUpperNode().GetNumberInSequence() + "->" + edge.GetLowerNode().GetNumberInSequence() + "[label = " + " " + '"' + ((KeyValuePair <AgentStateStatic, AgentStateDynamic>)edge.GetAction().Arguments[0]).Key.GetName() + Environment.NewLine + " " + edge.GetAction().ToString().Remove(0, 20) + Environment.NewLine + " " + "from: " + ((KeyValuePair <LocationStatic, LocationDynamic>)edge.GetAction().Arguments[1]).Key.GetName() + Environment.NewLine + " " + "to: " + ((KeyValuePair <LocationStatic, LocationDynamic>)edge.GetAction().Arguments[2]).Key.GetName() + Environment.NewLine + " " + "Success: " + edge.GetAction().success.ToString() + '"' + "] \r\n"); } else if (edge.GetAction() is Talk || edge.GetAction() is CounterTalk) { graphSTR = graphSTR.Insert(graphSTR.Length, edge.GetUpperNode().GetNumberInSequence() + "->" + edge.GetLowerNode().GetNumberInSequence() + "[label = " + " " + '"' + ((KeyValuePair <AgentStateStatic, AgentStateDynamic>)edge.GetAction().Arguments[0]).Key.GetName() + Environment.NewLine + " " + edge.GetAction().ToString().Remove(0, 20) + Environment.NewLine + " " + "with: " + ((KeyValuePair <AgentStateStatic, AgentStateDynamic>)edge.GetAction().Arguments[1]).Key.GetName() + Environment.NewLine + " " + "where: " + ((KeyValuePair <AgentStateStatic, AgentStateDynamic>)edge.GetAction().Arguments[0]).Value.GetBeliefs().GetMyLocation().GetName() + Environment.NewLine + " " + "Success: " + edge.GetAction().success.ToString() + '"' + "] \r\n"); } else if (edge.GetAction() is TellAboutASuspicious || edge.GetAction() is CounterTellAboutASuspicious) { graphSTR = graphSTR.Insert(graphSTR.Length, edge.GetUpperNode().GetNumberInSequence() + "->" + edge.GetLowerNode().GetNumberInSequence() + "[label = " + " " + '"' + ((KeyValuePair <AgentStateStatic, AgentStateDynamic>)edge.GetAction().Arguments[1]).Key.GetName() + Environment.NewLine + " " + edge.GetAction().ToString().Remove(0, 20) + Environment.NewLine + " " + "who: " + ((KeyValuePair <AgentStateStatic, AgentStateDynamic>)edge.GetAction().Arguments[1]).Key.GetName() + Environment.NewLine + " " + "whom: " + ((KeyValuePair <AgentStateStatic, AgentStateDynamic>)edge.GetAction().Arguments[0]).Key.GetName() + Environment.NewLine + " " + "from: " + ((KeyValuePair <LocationStatic, LocationDynamic>)edge.GetAction().Arguments[2]).Key.GetName() + Environment.NewLine + " " + "to: " + ((KeyValuePair <LocationStatic, LocationDynamic>)edge.GetAction().Arguments[3]).Key.GetName() + Environment.NewLine + " " + "Success: " + edge.GetAction().success.ToString() + '"' + "] \r\n"); } else { graphSTR = graphSTR.Insert(graphSTR.Length, edge.GetUpperNode().GetNumberInSequence() + "->" + edge.GetLowerNode().GetNumberInSequence() + "[label = " + " " + '"' + ((KeyValuePair <AgentStateStatic, AgentStateDynamic>)edge.GetAction().Arguments[0]).Key.GetName() + Environment.NewLine + " " + edge.GetAction().ToString().Remove(0, 20) + Environment.NewLine + " " + "Success: " + edge.GetAction().success.ToString() + '"' + "] \r\n"); } } // If the edge has an attached action, but the bottom end is not attached to a node. else if (edge.GetAction() != null && edge.GetLowerNode() == null) { if (edge.GetAction() is Kill) { graphSTR = graphSTR.Insert(graphSTR.Length, edge.GetUpperNode().GetNumberInSequence() + "->" + "END" + "[label = " + " " + '"' + ((KeyValuePair <AgentStateStatic, AgentStateDynamic>)edge.GetAction().Arguments[1]).Key.GetName() + Environment.NewLine + " " + edge.GetAction().ToString().Remove(0, 20) + Environment.NewLine + " " + "whom: " + ((KeyValuePair <AgentStateStatic, AgentStateDynamic>)edge.GetAction().Arguments[0]).Key.GetName() + Environment.NewLine + " " + "where: " + ((KeyValuePair <LocationStatic, LocationDynamic>)edge.GetAction().Arguments[2]).Key.GetName() + Environment.NewLine + " " + edge.GetAction().success.ToString() + '"' + "] \r\n"); } else if (edge.GetAction() is Entrap) { graphSTR = graphSTR.Insert(graphSTR.Length, edge.GetUpperNode().GetNumberInSequence() + "->" + "END" + "[label = " + " " + '"' + ((KeyValuePair <AgentStateStatic, AgentStateDynamic>)edge.GetAction().Arguments[1]).Key.GetName() + Environment.NewLine + " " + edge.GetAction().ToString().Remove(0, 20) + Environment.NewLine + " " + "whom: " + ((KeyValuePair <AgentStateStatic, AgentStateDynamic>)edge.GetAction().Arguments[0]).Key.GetName() + Environment.NewLine + " " + "where: " + ((KeyValuePair <LocationStatic, LocationDynamic>)edge.GetAction().Arguments[2]).Key.GetName() + Environment.NewLine + " " + edge.GetAction().success.ToString() + '"' + "] \r\n"); } else if (edge.GetAction() is Move) { graphSTR = graphSTR.Insert(graphSTR.Length, edge.GetUpperNode().GetNumberInSequence() + "->" + "END" + "[label = " + " " + '"' + ((KeyValuePair <AgentStateStatic, AgentStateDynamic>)edge.GetAction().Arguments[0]).Key.GetName() + Environment.NewLine + " " + edge.GetAction().ToString().Remove(0, 20) + Environment.NewLine + " " + "from: " + ((KeyValuePair <LocationStatic, LocationDynamic>)edge.GetAction().Arguments[1]).Key.GetName() + Environment.NewLine + " " + "to: " + ((KeyValuePair <LocationStatic, LocationDynamic>)edge.GetAction().Arguments[2]).Key.GetName() + Environment.NewLine + " " + edge.GetAction().success.ToString() + '"' + "] \r\n"); } else if (edge.GetAction() is InvestigateRoom) { graphSTR = graphSTR.Insert(graphSTR.Length, edge.GetUpperNode().GetNumberInSequence() + "->" + "END" + "[label = " + " " + '"' + ((KeyValuePair <AgentStateStatic, AgentStateDynamic>)edge.GetAction().Arguments[0]).Key.GetName() + Environment.NewLine + " " + edge.GetAction().ToString().Remove(0, 20) + Environment.NewLine + " " + "where: " + ((KeyValuePair <LocationStatic, LocationDynamic>)edge.GetAction().Arguments[2]).Key.GetName() + Environment.NewLine + " " + edge.GetAction().success.ToString() + '"' + "] \r\n"); } else { graphSTR = graphSTR.Insert(graphSTR.Length, edge.GetUpperNode().GetNumberInSequence() + "->" + "END" + "[label = " + " " + '"' + ((KeyValuePair <AgentStateStatic, AgentStateDynamic>)edge.GetAction().Arguments[0]).Key.GetName() + Environment.NewLine + " " + edge.GetAction().ToString().Remove(0, 20) + Environment.NewLine + " " + edge.GetAction().success.ToString() + '"' + "] \r\n"); } } // Add the considered edge to the list. edges.Add(edge); } } // We separately process the case when the considered node is the last one. else { foreach (var edge in node.GetEdges()) { bool skip = false; foreach (var e in edges) { if (e.Equals(edge)) { skip = true; break; } } if (skip) { continue; } graphSTR = graphSTR.Insert(graphSTR.Length, edge.GetUpperNode().GetNumberInSequence() + "->" + "End" + "[label = " + '"' + edge.GetAction().ToString().Remove(0, 20) + '"' + "] \r\n"); edges.Add(edge); } } } graphSTR = graphSTR.Insert(graphSTR.Length, "}"); // Save the resulting graph to a text file. SaveGraph(graphName, graphSTR); // Then we render the resulting graph. // PrintGraph(graphName); }
public void ActionCounteract(PlanAction action, StoryGraph currentGraph, KeyValuePair <AgentStateStatic, AgentStateDynamic> agent, WorldDynamic currentState, StoryNode currentNode, bool root, ref int globalNodeNumber, ref Queue <StoryNode> queue) { CSP_Module cspModule = new CSP_Module(); bool stageOne_NewNode = true; bool stageTwo_ConnectedNode = false; bool counterreactionFound = false; bool skip = false; string currentAction = action.GetType().ToString().Remove(0, 20); while (!counterreactionFound) { PlanAction counterreactionTalk = new CounterTalk(); bool assignVariables = cspModule.AssignVariables(ref counterreactionTalk, currentState, agent); counterreactionTalk.Arguments.Add(currentAction); if (assignVariables && counterreactionTalk.CheckPreconditions(currentState)) { bool constractionAndDeadEndAndCicle = false; bool duplicate = false; CounterreactionControl(counterreactionTalk, currentGraph, agent, currentState, currentNode, root, ref globalNodeNumber, ref queue, ref constractionAndDeadEndAndCicle, ref duplicate); if (stageOne_NewNode) { if (constractionAndDeadEndAndCicle && duplicate) { ApplyAction(counterreactionTalk, currentGraph, agent, currentState, currentNode, root, ref globalNodeNumber, true, true); counterreactionFound = true; } } else if (stageTwo_ConnectedNode) { if (constractionAndDeadEndAndCicle && !duplicate) { currentGraph.DuplicateNodeConnecting(currentState, counterreactionTalk, agent, currentNode, globalNodeNumber, ref queue, true, ref skip); counterreactionFound = true; } } } if (!counterreactionFound) { PlanAction counterreactionEntrap = new CounterEntrap(); assignVariables = cspModule.AssignVariables(ref counterreactionEntrap, currentState, agent); counterreactionEntrap.Arguments.Add(currentAction); if (assignVariables && counterreactionEntrap.CheckPreconditions(currentState)) { bool constractionAndDeadEndAndCicle = false; bool duplicate = false; CounterreactionControl(counterreactionEntrap, currentGraph, agent, currentState, currentNode, root, ref globalNodeNumber, ref queue, ref constractionAndDeadEndAndCicle, ref duplicate); if (stageOne_NewNode) { if (constractionAndDeadEndAndCicle && duplicate) { ApplyAction(counterreactionEntrap, currentGraph, agent, currentState, currentNode, root, ref globalNodeNumber, true, true); counterreactionFound = true; } } else if (stageTwo_ConnectedNode) { if (constractionAndDeadEndAndCicle && !duplicate) { currentGraph.DuplicateNodeConnecting(currentState, counterreactionEntrap, agent, currentNode, globalNodeNumber, ref queue, true, ref skip); counterreactionFound = true; } } } } if (!counterreactionFound) { PlanAction counterreactionKill = new CounterKill(); assignVariables = cspModule.AssignVariables(ref counterreactionKill, currentState, agent); counterreactionKill.Arguments.Add(currentAction); if (assignVariables && counterreactionKill.CheckPreconditions(currentState)) { bool constractionAndDeadEndAndCicle = false; bool duplicate = false; CounterreactionControl(counterreactionKill, currentGraph, agent, currentState, currentNode, root, ref globalNodeNumber, ref queue, ref constractionAndDeadEndAndCicle, ref duplicate); if (stageOne_NewNode) { if (constractionAndDeadEndAndCicle && duplicate) { ApplyAction(counterreactionKill, currentGraph, agent, currentState, currentNode, root, ref globalNodeNumber, true, true); counterreactionFound = true; } } else if (stageTwo_ConnectedNode) { if (constractionAndDeadEndAndCicle && !duplicate) { currentGraph.DuplicateNodeConnecting(currentState, counterreactionKill, agent, currentNode, globalNodeNumber, ref queue, true, ref skip); counterreactionFound = true; } } } } if (!counterreactionFound) { PlanAction counterreactionIR = new InvestigateRoom(); assignVariables = cspModule.AssignVariables(ref counterreactionIR, currentState, agent); counterreactionIR.Arguments.Add(currentAction); if (assignVariables && counterreactionIR.CheckPreconditions(currentState)) { bool constractionAndDeadEndAndCicle = false; bool duplicate = false; CounterreactionControl(counterreactionIR, currentGraph, agent, currentState, currentNode, root, ref globalNodeNumber, ref queue, ref constractionAndDeadEndAndCicle, ref duplicate); if (stageOne_NewNode) { if (constractionAndDeadEndAndCicle && duplicate) { ApplyAction(counterreactionIR, currentGraph, agent, currentState, currentNode, root, ref globalNodeNumber, true, true); counterreactionFound = true; } } else if (stageTwo_ConnectedNode) { if (constractionAndDeadEndAndCicle && !duplicate) { currentGraph.DuplicateNodeConnecting(currentState, counterreactionIR, agent, currentNode, globalNodeNumber, ref queue, true, ref skip); counterreactionFound = true; } } } } if (!counterreactionFound) { PlanAction counterreactionNK = new CounterNeutralizeKiller(); assignVariables = cspModule.AssignVariables(ref counterreactionNK, currentState, agent); counterreactionNK.Arguments.Add(currentAction); if (assignVariables && counterreactionNK.CheckPreconditions(currentState)) { bool constractionAndDeadEndAndCicle = false; bool duplicate = false; CounterreactionControl(counterreactionNK, currentGraph, agent, currentState, currentNode, root, ref globalNodeNumber, ref queue, ref constractionAndDeadEndAndCicle, ref duplicate); if (stageOne_NewNode) { if (constractionAndDeadEndAndCicle && duplicate) { ApplyAction(counterreactionNK, currentGraph, agent, currentState, currentNode, root, ref globalNodeNumber, true, true); counterreactionFound = true; } } else if (stageTwo_ConnectedNode) { if (constractionAndDeadEndAndCicle && !duplicate) { currentGraph.DuplicateNodeConnecting(currentState, counterreactionNK, agent, currentNode, globalNodeNumber, ref queue, true, ref skip); counterreactionFound = true; } } } } if (!counterreactionFound) { PlanAction counterreactionTalkAboutSuspicious = new CounterTellAboutASuspicious(); assignVariables = cspModule.AssignVariables(ref counterreactionTalkAboutSuspicious, currentState, agent); counterreactionTalkAboutSuspicious.Arguments.Add(currentAction); if (assignVariables && counterreactionTalkAboutSuspicious.CheckPreconditions(currentState)) { bool constractionAndDeadEndAndCicle = false; bool duplicate = false; CounterreactionControl(counterreactionTalkAboutSuspicious, currentGraph, agent, currentState, currentNode, root, ref globalNodeNumber, ref queue, ref constractionAndDeadEndAndCicle, ref duplicate); if (stageOne_NewNode) { if (constractionAndDeadEndAndCicle && duplicate) { ApplyAction(counterreactionTalkAboutSuspicious, currentGraph, agent, currentState, currentNode, root, ref globalNodeNumber, true, true); counterreactionFound = true; } } else if (stageTwo_ConnectedNode) { if (constractionAndDeadEndAndCicle && !duplicate) { currentGraph.DuplicateNodeConnecting(currentState, counterreactionTalkAboutSuspicious, agent, currentNode, globalNodeNumber, ref queue, true, ref skip); counterreactionFound = true; } } } } if (!counterreactionFound) { PlanAction counterreactionFight = new CounterFight(); assignVariables = cspModule.AssignVariables(ref counterreactionFight, currentState, agent); counterreactionFight.Arguments.Add(currentAction); if (assignVariables && counterreactionFight.CheckPreconditions(currentState)) { bool constractionAndDeadEndAndCicle = false; bool duplicate = false; CounterreactionControl(counterreactionFight, currentGraph, agent, currentState, currentNode, root, ref globalNodeNumber, ref queue, ref constractionAndDeadEndAndCicle, ref duplicate); if (stageOne_NewNode) { if (constractionAndDeadEndAndCicle && duplicate) { ApplyAction(counterreactionFight, currentGraph, agent, currentState, currentNode, root, ref globalNodeNumber, true, true); counterreactionFound = true; } } else if (stageTwo_ConnectedNode) { if (constractionAndDeadEndAndCicle && !duplicate) { currentGraph.DuplicateNodeConnecting(currentState, counterreactionFight, agent, currentNode, globalNodeNumber, ref queue, true, ref skip); counterreactionFound = true; } } } } if (!counterreactionFound) { PlanAction counterreactionReassure = new CounterReassure(); assignVariables = cspModule.AssignVariables(ref counterreactionReassure, currentState, agent); counterreactionReassure.Arguments.Add(currentAction); if (assignVariables && counterreactionReassure.CheckPreconditions(currentState)) { bool constractionAndDeadEndAndCicle = false; bool duplicate = false; CounterreactionControl(counterreactionReassure, currentGraph, agent, currentState, currentNode, root, ref globalNodeNumber, ref queue, ref constractionAndDeadEndAndCicle, ref duplicate); if (stageOne_NewNode) { if (constractionAndDeadEndAndCicle && duplicate) { ApplyAction(counterreactionReassure, currentGraph, agent, currentState, currentNode, root, ref globalNodeNumber, true, true); counterreactionFound = true; } } else if (stageTwo_ConnectedNode) { if (constractionAndDeadEndAndCicle && !duplicate) { currentGraph.DuplicateNodeConnecting(currentState, counterreactionReassure, agent, currentNode, globalNodeNumber, ref queue, true, ref skip); counterreactionFound = true; } } } } if (!counterreactionFound) { PlanAction counterreactionRun = new CounterRun(); assignVariables = cspModule.AssignVariables(ref counterreactionRun, currentState, agent); counterreactionRun.Arguments.Add(currentAction); if (assignVariables && counterreactionRun.CheckPreconditions(currentState)) { bool constractionAndDeadEndAndCicle = false; bool duplicate = false; CounterreactionControl(counterreactionRun, currentGraph, agent, currentState, currentNode, root, ref globalNodeNumber, ref queue, ref constractionAndDeadEndAndCicle, ref duplicate); if (stageOne_NewNode) { if (constractionAndDeadEndAndCicle && duplicate) { ApplyAction(counterreactionRun, currentGraph, agent, currentState, currentNode, root, ref globalNodeNumber, true, true); counterreactionFound = true; } } else if (stageTwo_ConnectedNode) { if (constractionAndDeadEndAndCicle && !duplicate) { currentGraph.DuplicateNodeConnecting(currentState, counterreactionRun, agent, currentNode, globalNodeNumber, ref queue, true, ref skip); counterreactionFound = true; } } } } if (!counterreactionFound) { PlanAction counterreactionMove = new CounterMove(); assignVariables = cspModule.AssignVariables(ref counterreactionMove, currentState, agent); counterreactionMove.Arguments.Add(currentAction); if (assignVariables && counterreactionMove.CheckPreconditions(currentState)) { bool constractionAndDeadEndAndCicle = false; bool duplicate = false; CounterreactionControl(counterreactionMove, currentGraph, agent, currentState, currentNode, root, ref globalNodeNumber, ref queue, ref constractionAndDeadEndAndCicle, ref duplicate); if (stageOne_NewNode) { if (constractionAndDeadEndAndCicle && duplicate) { ApplyAction(counterreactionMove, currentGraph, agent, currentState, currentNode, root, ref globalNodeNumber, true, true); counterreactionFound = true; } } else if (stageTwo_ConnectedNode) { if (constractionAndDeadEndAndCicle && !duplicate) { currentGraph.DuplicateNodeConnecting(currentState, counterreactionMove, agent, currentNode, globalNodeNumber, ref queue, true, ref skip); counterreactionFound = true; } } } } if (stageTwo_ConnectedNode) { PlanAction counterreactionSkip = new NothingToDo(); counterreactionSkip.Arguments.Add(agent); bool constractionAndDeadEndAndCicle = false; bool duplicate = false; CounterreactionControl(counterreactionSkip, currentGraph, agent, currentState, currentNode, root, ref globalNodeNumber, ref queue, ref constractionAndDeadEndAndCicle, ref duplicate); if (constractionAndDeadEndAndCicle && duplicate) { ApplyAction(counterreactionSkip, currentGraph, agent, currentState, currentNode, root, ref globalNodeNumber, true, true); counterreactionFound = true; } else if (constractionAndDeadEndAndCicle && !duplicate) { currentGraph.DuplicateNodeConnecting(currentState, counterreactionSkip, agent, currentNode, globalNodeNumber, ref queue, true, ref skip); counterreactionFound = true; } } //stageOne_NewNode = false; //stageTwo_ConnectedNode = true; if (stageOne_NewNode) { stageOne_NewNode = false; stageTwo_ConnectedNode = true; } else if (stageTwo_ConnectedNode) { stageOne_NewNode = true; stageTwo_ConnectedNode = false; } } }
/// <summary> /// The agent updates his beliefs, calculates a plan, chooses an action, assigns variables to it, and sends it for further control. /// </summary> public void ActionRequest(KeyValuePair <AgentStateStatic, AgentStateDynamic> agent, ref StoryGraph currentGraph, ref WorldDynamic currentState, StoryNode currentNode, bool root, ref int globalNodeNumber, ref Queue <StoryNode> queue) { CSP_Module cspModule = new CSP_Module(); if (agent.Key.GetRole().Equals(AgentRole.PLAYER)) { agent.Value.RefreshBeliefsAboutTheWorld(currentState, agent); agent.Value.GenerateNewPDDLProblem(agent, currentState); agent.Value.ReceiveAvailableActions(agent); List <PlanAction> receivedActions = agent.Value.GetAvailableActions(); for (int i = 0; i < receivedActions.Count; i++) { PlanAction receivedAction = receivedActions[i]; if (receivedAction != null) { switch (receivedAction.GetType().ToString().Remove(0, 20)) { case "Move": MultiAVandAC(ref receivedAction, currentState, agent, cspModule, currentGraph, currentNode, root, ref globalNodeNumber, ref queue); break; case "Fight": // Not relevant yet break; case "InvestigateRoom": SingleAVandAC(ref receivedAction, currentState, agent, cspModule, currentGraph, currentNode, root, ref globalNodeNumber, ref queue); break; case "NeutralizeKiller": // Not relevant yet break; case "NothingToDo": SkipTurn(currentState); break; case "Reassure": // Not relevant yet break; case "Run": // Not relevant yet break; case "Talk": // Not relevant yet break; } // Cleaning receivedAction = null; GC.Collect(); } } // Cleaning receivedActions = null; GC.Collect(); } else { agent.Value.RefreshBeliefsAboutTheWorld(currentState, agent); agent.Value.GenerateNewPDDLProblem(agent, currentState); agent.Value.CalculatePlan(agent, currentState); agent.Value.ReceiveAvailableActions(agent); PlanAction receivedAction = agent.Value.ChooseAction(); if (receivedAction != null) { cspModule.AssignVariables(ref receivedAction, currentState, agent); ActionControl(receivedAction, currentGraph, agent, currentState, currentNode, root, ref globalNodeNumber, ref queue); // Cleaning receivedAction = null; currentNode = null; GC.Collect(); } else { SkipTurn(currentState); } } }