Esempio n. 1
0
 public void Effect(Ws world)
 { // apply effects wot the worldstate passed in
     foreach (Wstates W in Enum.GetValues(typeof(Wstates)))
     {
         if (world.states[W].Value)
         {
             states[W].Value = true;
         }
     }
 }
Esempio n. 2
0
 private Node GetMatchingNodeInVisited(Ws ws)
 {
     foreach (Node n in visitedNodes)
     {
         if (n.world.Equal(ws))
         {
             return(n);
         }
     }
     return(null);
 }
Esempio n. 3
0
 private Node GetMatchingNodeInConsidered(Ws ws)
 {
     foreach (Node n in considerNodes)
     {
         if (n.world.Equal(ws))
         {
             return(n);
         }
     }
     return(null);
 }
Esempio n. 4
0
 public bool Equal(Ws WS)
 {
     foreach (Wstates w in Enum.GetValues(typeof(Wstates)))
     {
         if (states[w].Value != WS.states[w].Value)// cheack that the world state value is equal to its orginal value
         {
             return(false);
         }
     }
     return(true);
 }
Esempio n. 5
0
        private List <Action> GetPossibleTransitions(Ws from, ref List <Ws> tos)
        {
            List <Action> transitions = new List <Action>();

            foreach (Action a in actions)
            {
                if (from.PreConditionsMeet(a.preConditions))
                {
                    transitions.Add(a);
                    tos.Add(from.GetWSEffected(a.effects));
                }
            }
            return(transitions);
        }
Esempio n. 6
0
 public bool EnabledEqual(Ws WS)
 {
     foreach (Wstates w in Enum.GetValues(typeof(Wstates)))
     {
         if (WS.states[w].IsEnabled)// cheak that the world state is enabled variable is equal to is orginal value
         {
             if (states[w].Value != WS.states[w].Value)
             {
                 return(false);
             }
         }
     }
     return(true);
 }
Esempio n. 7
0
        public int GetNumMisMatchStates(Ws world)
        {
            int mis = 0;

            foreach (Wstates w in Enum.GetValues(typeof(Wstates)))
            {
                if (world.states[w].IsEnabled) // gain the number of states that do not match their original value
                {
                    if (states[w].Value != world.states[w].Value)
                    {
                        mis++;
                    }
                }
            }
            return(mis);
        }
Esempio n. 8
0
        public Ws GetWSEffected(Ws effects)
        {
            Ws worldstate = new Ws();

            foreach (Wstates w in Enum.GetValues(typeof(Wstates))) // for each of the worldstates  assign the effed world states
            {
                worldstate.states[w].Value     = states[w].Value;
                worldstate.states[w].IsEnabled = states[w].IsEnabled;
            }
            foreach (Wstates w in Enum.GetValues(typeof(Wstates)))
            {
                if (effects.states[w].Value) // if the world state value is true assign the variables for the worldstate to true
                {
                    worldstate.states[w].Value     = true;
                    worldstate.states[w].IsEnabled = true;
                }
            }
            return(worldstate);
        }
Esempio n. 9
0
        public Stack <Action> GetPlan(Ws start, Goal currentGoal)
        {
            considerNodes.Clear();
            visitedNodes.Clear();
            Ws goal = currentGoal.condition;
            // Create a Node to encapsualte the start World State
            Node n0 = new Node();

            n0.world        = start;
            n0.parentWorlds = start;
            // Cost to get to this node from start
            n0.g = 0;
            // A guess as to how far we are from the goal
            n0.h = start.GetNumMisMatchStates(goal);
            // Guess of overall cost from start to goal
            n0.f = n0.g + n0.h;
            // The Action associated with the Node
            n0.action = null;
            // Add the Node to consider
            considerNodes.Add(n0);
            do
            {
                if (considerNodes.Count == 0)
                {
                    Debug.Log("Did not find a path");
                    return(null);
                }
                // Search Open List for Node with the lowest guested cost (closest to Goal).
                int  lowestVal  = 100000;
                Node lowestNode = null;
                foreach (Node n in considerNodes)
                {
                    if (n.f < lowestVal)
                    {
                        lowestVal  = n.f;
                        lowestNode = n;
                    }
                }
                // Set the lowest cost Node as the current Node
                Node currentNode = lowestNode;
                // Remove Node
                considerNodes.Remove(lowestNode);
                // If the current Node's World State match the Goal we are finished
                if (currentNode.world.HasAchived(goal))
                {
                    return(ReconstructPlan(currentNode));
                }
                // Add Current Node to visited List
                visitedNodes.Add(currentNode);
                List <Ws>     tos = new List <Ws>();
                List <Action> transitionActions = GetPossibleTransitions(currentNode.world, ref tos);
                int           index             = 0;
                // For each of currents Node's adjacent Actions
                foreach (Action a in transitionActions)
                {
                    // Get the Actions World State after effects have been applied
                    Ws to = tos[index];
                    index++;
                    // Calcuate the cost from current to the completed adjacent Action
                    int cost = currentNode.g + a.Cost;
                    // The Node may already be under consideration
                    Node openNode = GetMatchingNodeInConsidered(to);
                    // The Node may already have been processed
                    Node closedNode = GetMatchingNodeInVisited(to);
                    // If already under consideration check the cost as it may be cheaper coming via this route
                    if (openNode != null && cost < openNode.g)
                    {
                        considerNodes.Remove(openNode);
                        // Force the Node to be created
                        openNode = null;
                    }
                    // if the Node has been visited check the cost as it may be cheaper coming via this route
                    if (closedNode != null && cost < closedNode.g)
                    {
                        visitedNodes.Remove(closedNode);
                    }

                    // If adjacent Action not in visited or considered Lists
                    if (openNode == null && closedNode == null)
                    {
                        // Ecapsulate the adjacent Action in a Node
                        Node nb = new Node();
                        // The World State after Action effects are applied
                        nb.world = to;
                        nb.g     = cost;
                        // Number of mismatched Atoms between goal and current Node.
                        // A heuristic (guess) measure to how close we are to Goal.
                        nb.h      = nb.world.GetNumMisMatchStates(goal);
                        nb.f      = nb.g + nb.h;
                        nb.action = a;
                        // The World State before Action effects were applied
                        // This allows us to trace our way back to the start
                        nb.parentWorlds = currentNode.world;
                        // Add Node to List for consideration
                        considerNodes.Add(nb);
                    }
                }
            } while (true);
        }
Esempio n. 10
0
 public Goal(int priority)
 {
     m_priority = priority;
     condition = new Ws();
 }
Esempio n. 11
0
 public bool PreConditionsMeet(Ws cond)
 {
     return(EnabledEqual(cond)); //return if the world state is equal
 }
Esempio n. 12
0
 public bool HasAchived(Ws GL)
 {
     return(EnabledEqual(GL)); // return if the world state is equal
 }