Ejemplo n.º 1
0
        /// <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();
        }