/// <summary> /// Generate a new PDDL file with a problem for the specified agent, based on his beliefs. /// </summary> public void GenerateNewPDDLProblem(KeyValuePair <AgentStateStatic, AgentStateDynamic> agent, WorldDynamic currentWorldState, bool killerCantCreatePlan = false) { string fileName = ""; string problemName = ""; string domainName = ""; string objects = ""; string init = ""; string goal = ""; switch (agent.Key.GetRole()) { case AgentRole.USUAL: fileName = "AgentProblemTEST"; break; case AgentRole.KILLER: fileName = "KillerProblemTEST"; break; case AgentRole.PLAYER: fileName = "PlayerProblemTEST"; break; } problemName = "detective-problem"; domainName = "detective-domain"; foreach (var location in agent.Value.GetBeliefs().GetLocationsInWorld()) { objects = objects.Insert(objects.Length, location.GetName() + " "); init = init.Insert(init.Length, Environment.NewLine + "(ROOM " + location.GetName() + ") "); foreach (var connectedLocation in location.GetConnectedLocations()) { init = init.Insert(init.Length, "(connected " + location.GetName() + " " + connectedLocation.GetName() + ")"); } } foreach (var a in agent.Value.GetBeliefs().GetAgentsInWorld()) { objects = objects.Insert(objects.Length, a.GetInfo().GetName() + " "); switch (a.GetRole()) { case AgentRole.USUAL: init = init.Insert(init.Length, Environment.NewLine + "(AGENT " + a.GetInfo().GetName() + ") "); break; case AgentRole.KILLER: init = init.Insert(init.Length, Environment.NewLine + "(KILLER " + a.GetInfo().GetName() + ") "); break; case AgentRole.PLAYER: if (agent.Key.GetRole() == AgentRole.PLAYER) { init = init.Insert(init.Length, Environment.NewLine + "(PLAYER " + a.GetInfo().GetName() + ") "); } else { init = init.Insert(init.Length, Environment.NewLine + "(AGENT " + a.GetInfo().GetName() + ") "); } break; } switch (a.CheckStatus()) { case true: init = init.Insert(init.Length, "(alive " + a.GetInfo().GetName() + ") "); break; case false: init = init.Insert(init.Length, "(died " + a.GetInfo().GetName() + ") "); break; } switch (agent.Key.GetRole()) { case AgentRole.USUAL: // An agent can claim that some other agent is in a certain location only if he has this information. if (agent.Value.GetBeliefs().SearchAgentAmongLocations(a.GetInfo()) != null) { init = init.Insert(init.Length, "(in-room " + a.GetInfo().GetName() + " " + agent.Value.GetBeliefs().GetLocationByName(agent.Value.GetBeliefs().SearchAgentAmongLocations(a.GetInfo()).GetName()) .GetName() + ") "); if (agent.Key.GetName() == a.GetInfo().GetName()) { foreach (var loc in agent.Value.GetExploredLocations()) { init = init.Insert(init.Length, "(explored-room " + a.GetInfo().GetName() + " " + loc.GetName() + ")"); } if (agent.Value.GetObjectOfAngry() != null && agent.Value.AngryCheck()) { init = init.Insert(init.Length, "(angry-at " + agent.Key.GetName() + " " + agent.Value.GetObjectOfAngry().GetObjectOfAngry().GetName() + ")"); } } } break; case AgentRole.KILLER: init = init.Insert(init.Length, "(in-room " + a.GetInfo().GetName() + " " + agent.Value.GetBeliefs().GetLocationByName(currentWorldState.SearchAgentAmongLocations(a.GetInfo()).GetName()) .GetName() + ") "); break; case AgentRole.PLAYER: init = init.Insert(init.Length, "(in-room " + a.GetInfo().GetName() + " " + agent.Value.GetBeliefs().GetLocationByName(currentWorldState.SearchAgentAmongLocations(a.GetInfo()).GetName()) .GetName() + ") "); break; } } if (agent.Value.GetGoal().goalTypeIsStatus) { switch (agent.Key.GetRole()) { case AgentRole.USUAL: foreach (var a in agent.Value.GetGoal().GetGoalState().GetAgents()) { if (a.Key.GetRole() == AgentRole.KILLER) { if (a.Key.GetName() != null && a.Key.GetName() != "" && a.Key.GetName() != "???") { goal = goal.Insert(goal.Length, "(died " + a.Key.GetName() + ") "); } else if (agent.Value.GetObjectOfAngry() != null && agent.Value.AngryCheck()) { goal = goal.Insert(goal.Length, "(died " + agent.Value.GetObjectOfAngry().GetObjectOfAngry().GetName() + ") "); } else { if (currentWorldState.GetLocationByName(agent.Value.GetBeliefs().GetMyLocation().GetName()). Value.CountAliveAgents() >= 2) { goal = goal.Insert(goal.Length, "(talking " + agent.Key.GetName() + " " + currentWorldState.GetRandomAgentInMyLocation(agent).Key.GetName() + ") "); } else if (!CheckIfLocationIsExplored(agent.Value.GetMyLocation())) { goal = goal.Insert(goal.Length, "(explored-room " + agent.Key.GetName() + " " + agent.Value.GetMyLocation().GetName() + ") "); } else { if (timeToMove == 0) { if (agent.Value.GetTargetLocation() != null) { goal = goal.Insert(goal.Length, "(in-room " + agent.Key.GetName() + " " + agent.Value.GetTargetLocation().GetName() + ") "); } else { agent.Value.SetTargetLocation( currentWorldState.GetRandomLocationWithout( currentWorldState.GetLocationByName( agent.Value.GetBeliefs().GetMyLocation().GetName())).Key); goal = goal.Insert(goal.Length, "(in-room " + agent.Key.GetName() + " " + agent.Value.GetTargetLocation().GetName() + ") "); } } else { goal = goal.Insert(goal.Length, "(wait" + agent.Key.GetName() + " " + ") "); } } } } } break; case AgentRole.PLAYER: foreach (var a in agent.Value.GetGoal().GetGoalState().GetAgents()) { if (a.Key.GetRole() == AgentRole.KILLER) { if (a.Key.GetName() != null && a.Key.GetName() != "???" && a.Key.GetName() != "" && agent.Value.GetBeliefs().GetMyLocation().GetName() != currentWorldState.SearchAgentAmongLocationsByName(agent.Value.GetObjectOfAngry().GetObjectOfAngry().GetName()).GetName()) { goal = goal.Insert(goal.Length, "(died " + a.Key.GetName() + ") "); } else if (agent.Value.GetObjectOfAngry() != null && agent.Value.AngryCheck() && agent.Value.GetBeliefs().GetMyLocation().GetName() != currentWorldState.SearchAgentAmongLocationsByName(agent.Value.GetObjectOfAngry().GetObjectOfAngry().GetName()).GetName()) { goal = goal.Insert(goal.Length, "(died " + agent.Value.GetObjectOfAngry().GetObjectOfAngry().GetName() + ") "); } else { if (currentWorldState.GetLocationByName(agent.Value.GetBeliefs().GetMyLocation().GetName()).Value.GetAgents().Count >= 2) { goal = goal.Insert(goal.Length, "(talking " + agent.Key.GetName() + " " + currentWorldState.GetRandomAgent(agent).Key.GetName() + ") "); } else if (!CheckIfLocationIsExplored(agent.Value.GetMyLocation())) { goal = goal.Insert(goal.Length, "(explored-room " + agent.Key.GetName() + " " + agent.Value.GetMyLocation().GetName() + ") "); } else { if (agent.Value.GetTargetLocation() != null) { goal = goal.Insert(goal.Length, "(in-room " + agent.Key.GetName() + " " + agent.Value.GetTargetLocation().GetName() + ") "); } else { agent.Value.SetTargetLocation( currentWorldState.GetRandomLocationWithout( currentWorldState.GetLocationByName( agent.Value.GetBeliefs().GetMyLocation().GetName())).Key); goal = goal.Insert(goal.Length, "(in-room " + agent.Key.GetName() + " " + agent.Value.GetTargetLocation().GetName() + ") "); } } } } } break; case AgentRole.KILLER: if (currentWorldState.GetLocationByName(agent.Value.GetBeliefs().GetMyLocation().GetName()).Value.CountAliveAgents() == 2) { foreach (var a in currentWorldState.GetLocationByName(agent.Value.GetBeliefs().GetMyLocation().GetName()).Value.GetAgents()) { if (a.Value.GetStatus() && (a.Key.GetRole() == AgentRole.USUAL || a.Key.GetRole() == AgentRole.PLAYER)) { goal = goal.Insert(goal.Length, "(died " + a.Key.GetName() + ") "); } } } else if (currentWorldState.GetLocationByName(agent.Value.GetBeliefs().GetMyLocation().GetName()).Value.CountAliveAgents() == 1) { goal = goal.Insert(goal.Length, "(in-room " + currentWorldState.GetRandomAgent(agent).Key.GetName() + " " + currentWorldState.GetLocationByName(agent.Value.GetMyLocation().GetName()).Key.GetName() + ") "); } else { if (timeToMove == 0) { goal = goal.Insert(goal.Length, "(in-room " + agent.Key.GetName() + " " + currentWorldState.GetRandomConnectedLocation(currentWorldState.GetLocationByName(agent.Value.GetMyLocation().GetName())).Key.GetName() + ") "); } else { goal = goal.Insert(goal.Length, "(wait " + agent.Key.GetName() + " " + ") "); } } /*killerCantCreatePlan = true; * if (killerCantCreatePlan) * { * if (currentWorldState.GetLocationByName(agent.Value.GetBeliefs().GetMyLocation().GetName()).Value.CountAliveAgents() > 2 * && timeToMove == 0) * { * goal = goal.Insert(goal.Length, "(in-room " + agent.Key.GetName() + " " + currentWorldState.GetRandomConnectedLocation(currentWorldState.GetLocationByName(agent.Value.GetMyLocation().GetName())).Key.GetName() + ") "); + } + else if (timeToMove > 0) + { + + } + else + { + bool hasTarget = false; + + while (!hasTarget) + { + BeliefsAboutAgent a = null; + + foreach (var agnt in agent.Value.GetBeliefs().GetAgentsInWorld()) + { + if (agnt.CheckStatus() && (agnt.GetRole() == AgentRole.USUAL || agnt.GetRole() == AgentRole.PLAYER)) + { + a = agnt; + break; + } + } + + if (a.GetInfo().GetRole() == AgentRole.USUAL || a.GetInfo().GetRole() == AgentRole.PLAYER) + { + goal = goal.Insert(goal.Length, "(died " + a.GetInfo().GetName() + ") "); + hasTarget = true; + } + } + } + }*/ break; } } FileStream file = new FileStream(fileName + ".pddl", FileMode.Create, FileAccess.ReadWrite); StreamWriter streamWriter = new StreamWriter(file, Encoding.GetEncoding(1251)); streamWriter.WriteLine("(define (problem " + problemName + ")"); streamWriter.WriteLine("(:domain " + domainName + ")"); streamWriter.WriteLine("(:objects " + objects + ")"); streamWriter.WriteLine("(:init " + init + ")"); streamWriter.WriteLine("(:goal (and " + goal + "))"); streamWriter.WriteLine(")"); streamWriter.Close(); }
public bool AssignVariables(ref PlanAction action, WorldDynamic currentState, KeyValuePair<AgentStateStatic, AgentStateDynamic> initiator) { if (action is Entrap || action is CounterEntrap) { foreach (var agent in currentState.GetAgents()) { if ((agent.Key.GetRole() == AgentRole.USUAL || agent.Key.GetRole() == AgentRole.PLAYER) && agent.Value.GetStatus() && !currentState.GetLocation(currentState.SearchAgentAmongLocations(agent.Key)).Value.SearchAgent(initiator.Key) && !agent.Equals(initiator)) { action.Arguments.Add(agent); action.Arguments.Add(initiator); action.Arguments.Add(currentState.GetLocationByName(currentState.SearchAgentAmongLocations(initiator.Key).GetName())); return true; } } return false; } else if (action is Fight || action is CounterFight) { foreach (var agent in currentState.GetAgents()) { if ((agent.Key.GetRole() == AgentRole.USUAL || agent.Key.GetRole() == AgentRole.PLAYER) && agent.Value.GetStatus() && currentState.GetLocation(currentState.SearchAgentAmongLocations(initiator.Key)).Value.SearchAgent(agent.Key)) { action.Arguments.Add(initiator); action.Arguments.Add(agent); action.Arguments.Add(currentState.GetLocation(currentState.SearchAgentAmongLocations(initiator.Key))); return true; } } return false; } else if (action is InvestigateRoom || action is CounterInvestigateRoom) { foreach (var killer in currentState.GetAgents()) { if (killer.Key.GetRole() == AgentRole.KILLER && killer.Value.GetStatus()) { action.Arguments.Add(initiator); action.Arguments.Add(killer); action.Arguments.Add(currentState.GetLocationByName(currentState.SearchAgentAmongLocations(initiator.Key).GetName())); return true; } } return false; } else if (action is Kill || action is CounterKill) { foreach (var agent in currentState.GetAgents()) { if ((agent.Key.GetRole() == AgentRole.USUAL || agent.Key.GetRole() == AgentRole.PLAYER) && agent.Value.GetStatus() && currentState.GetLocation(currentState.SearchAgentAmongLocations(initiator.Key)).Value.SearchAgent(agent.Key)) { action.Arguments.Add(agent); action.Arguments.Add(initiator); action.Arguments.Add(currentState.GetLocation(currentState.SearchAgentAmongLocations(initiator.Key))); return true; } } return false; } else if (action is Move || action is CounterMove) { if (action.Arguments.Count() != 0) { List<string> arguments = new List<string>(); foreach (var argument in action.Arguments) { arguments.Add((string)argument); } action.Arguments.Clear(); action.Arguments.Add(initiator); action.Arguments.Add(currentState.GetLocationByName(arguments[1])); action.Arguments.Add(currentState.GetLocationByName(arguments[2])); return true; } else { action.Arguments.Add(initiator); action.Arguments.Add(currentState.GetLocationByName(currentState.SearchAgentAmongLocations(initiator.Key).GetName())); if (initiator.Value.GetTargetLocation() != null && initiator.Key.GetRole() != AgentRole.PLAYER && currentState.SearchAgentAmongLocations(initiator.Key).ConnectionChecking(initiator.Value.GetTargetLocation())) { action.Arguments.Add(currentState.GetLocationByName(initiator.Value.GetTargetLocation().GetName())); return true; } else { KeyValuePair<LocationStatic, LocationDynamic> randLoc = currentState. GetRandomConnectedLocation(currentState.GetLocationByName(currentState.SearchAgentAmongLocations(initiator.Key).GetName())); if (currentState.GetLocationByName(currentState.SearchAgentAmongLocations(initiator.Key).GetName()). Key.ConnectionChecking(randLoc.Key)) { action.Arguments.Add(randLoc); return true; } } } return false; } else if (action is NeutralizeKiller || action is CounterNeutralizeKiller) { foreach (var killer in currentState.GetAgents()) { if (killer.Key.GetRole() == AgentRole.KILLER && killer.Value.GetStatus()) { action.Arguments.Add(initiator); action.Arguments.Add(killer); action.Arguments.Add(currentState.GetLocation(currentState.SearchAgentAmongLocations(initiator.Key))); return true; } } return false; } else if (action is Reassure || action is CounterReassure) { foreach (var agent in currentState.GetAgents()) { if (agent.Key.GetRole() == AgentRole.USUAL && agent.Value.GetStatus() && currentState.GetLocation(currentState.SearchAgentAmongLocations(initiator.Key)).Value.SearchAgent(agent.Key) && !agent.Key.Equals(initiator.Key) && agent.Value.AngryCheck()) { action.Arguments.Add(agent); action.Arguments.Add(initiator); action.Arguments.Add(currentState.GetAgentByName(agent.Value.GetObjectOfAngry().GetObjectOfAngry().GetName())); break; } } foreach (var killer in currentState.GetAgents()) { if (killer.Key.GetRole() == AgentRole.KILLER && killer.Value.GetStatus()) { action.Arguments.Add(killer); break; } } action.Arguments.Add(currentState.GetLocation(currentState.SearchAgentAmongLocations(initiator.Key))); return true; } else if (action is Run || action is CounterRun) { action.Arguments.Add(initiator); action.Arguments.Add(currentState.GetLocation(currentState.SearchAgentAmongLocations(initiator.Key))); action.Arguments.Add(currentState. GetRandomLocationWithout(currentState.GetLocationByName(currentState.SearchAgentAmongLocations(initiator.Key).GetName()))); return true; } else if (action is NothingToDo) { action.Arguments.Add(initiator); } else if (action is TellAboutASuspicious || action is CounterTellAboutASuspicious) { foreach (var agent in currentState.GetAgents()) { if (agent.Key.GetRole() == AgentRole.USUAL && agent.Value.GetStatus() && currentState.GetLocation(currentState.SearchAgentAmongLocations(initiator.Key)).Value.SearchAgent(agent.Key)) { action.Arguments.Add(agent); break; } } action.Arguments.Add(initiator); action.Arguments.Add(currentState.GetLocation(currentState.SearchAgentAmongLocations(initiator.Key))); action.Arguments.Add(currentState.GetRandomLocationWithout (currentState.GetLocationByName(currentState.SearchAgentAmongLocations(initiator.Key).GetName()))); return true; } else if (action is Talk || action is CounterTalk) { action.Arguments.Add(initiator); foreach (var agent in currentState.GetAgents()) { if (agent.Value.GetStatus() && currentState.GetLocation(currentState.SearchAgentAmongLocations(initiator.Key)).Value.SearchAgent(agent.Key) && agent.Key.GetName() != initiator.Key.GetName()) { action.Arguments.Add(agent); return true; } } return false; } return false; }