Пример #1
0
        private void button1_Click_1(object sender, EventArgs e)
        {
            char test = 'a';
            StateTree<string> tree = new StateTree<string>(EqualityComparer<string>.Default);
            for (int i=0; i<26; i++)
            {
                char test2 = (char)(test + i);
                tree.AddState(test2.ToString());
            }

            MessageBox.Show(String.Join(",",tree.GetLevel0Children("a", 3)));
            MessageBox.Show(String.Join(",",tree.GetChildren("a", 3)));
        }
Пример #2
0
        public override double update(StateTransition <int[], actionType> transition)
        {
            //if (currentGoal.goalState == null)
            //{
            //    Console.WriteLine("Goal: null");
            //}
            //else
            //{
            //    Console.WriteLine("Goal: Level " + currentGoal.level + ", at " + String.Join(",", currentGoal.goalState) + ", value: " + currentGoal.value);
            //}
            //Console.WriteLine("   current state: " + String.Join(",", transition.newState) + " / " + String.Join(",", stateTree.GetParentState(transition.newState, currentGoal.level)));


            // update the stateTree
            stateTree.AddState(transition.newState);

            // perform model updates
            double returnValue = models[0].update(transition);


            if (transition.absorbingStateReached) // if this was the end of the simulation // ****** do we need this?
            {
                for (int i = 1; i < models.Length; i++)
                {
                    if (transitions[i].oldState != null)
                    {
                        //transitions[i].reward += transition.reward;
                        transitions[i].reward = Math.Max(transitions[i].reward, transition.reward);
                        models[i].update(transitions[i]);
                        transitions[i] = new StateTransition <int[], actionType>(null, default(actionType), double.NegativeInfinity, null);
                    }
                }

                currentGoal.goalState = null;
                for (int i = 0; i < subgoals.Length; i++)
                {
                    subgoals[i].Clear();
                }
                return(returnValue);
            }
            else
            {
                for (int i = 1; i < models.Length; i++)
                {
                    // get the representation of state at this level
                    int[] thisOldState = stateTree.GetParentState(transition.oldState, i);
                    int[] thisNewState = stateTree.GetParentState(transition.newState, i);

                    // check if the local transition represents a transition at this level
                    if ((!stateComparer.Equals(thisOldState, thisNewState)) || i == 0)// transition - update the model at this level, and start a new accumulation of reward
                    {
                        if (transitions[i].oldState != null)
                        {
                            models[i].update(transitions[i]);
                        }
                        transitions[i] = new StateTransition <int[], actionType>(thisOldState, transition.action, transition.reward, thisNewState); // this line might only apply to grid-worlds
                    }
                    else
                    {
                        // accumulate the reward
                        //transitions[i].reward += transition.reward;
                        transitions[i].reward = Math.Max(transitions[i].reward, transition.reward);
                    }
                }
            }


            if (currentGoal.goalState == null)
            {
                return(returnValue);
            }

            if (stateComparer.Equals(stateTree.GetParentState(transition.newState, currentGoal.level), currentGoal.goalState)) // if the current goal has been reached
            {
                for (int i = 0; i < subgoals.Length; i++)
                {
                    subgoals[i].Clear();
                }

                if (currentGoal.level == 0) // if the current goal is at level 0, select a new goal from scratch
                {
                    currentGoal.goalState = null;
                }
                else // otherwise do a contrained selection of a new goal
                {
                    currentGoal = selectGoal(transition.newState, currentGoal.level, availableActions);
                    subgoals[currentGoal.level].Add(currentGoal);
                }
            }
            else if (subgoals[0].Count == 0) // error check ****************
            {
                currentGoal.goalState = null;
                for (int i = 0; i < subgoals.Length; i++)
                {
                    subgoals[i].Clear();
                }
            }
            else if (stateComparer.Equals(subgoals[0][0].goalState, transition.newState)) // if the next step at level 0 has been reached successfully
            {
                for (int i = 0; i < currentGoal.level; i++)
                {
                    subgoals[i].RemoveAt(0);
                    if (subgoals[i].Count > 0)
                    {
                        break;
                    }
                }
            }
            else if (minLevel > 0 && !stateComparer.Equals(stateTree.GetParentState(transition.newState, currentGoal.level), currentGoal.startState)) // if we're no longer in the start state at the goal level (for dH lesions)
            {
                currentGoal.goalState = null;
                for (int i = 0; i < subgoals.Length; i++)
                {
                    subgoals[i].Clear();
                }
            }
            else if (!stateComparer.Equals(subgoals[0][0].goalState, transition.newState)) // if we're not where we expected to be at level 0
            {
                if (currentGoal.level == 0)
                {
                    currentGoal.goalState = null;
                    for (int i = 0; i < subgoals.Length; i++)
                    {
                        subgoals[i].Clear();
                    }
                }
                else
                {
                    subgoals[0].Clear();
                    //for (int i = 0; i <= currentGoal.level; i++)
                    //{
                    //    subgoals[i].Clear();
                    //}
                    //subgoals[currentGoal.level].Add(currentGoal);
                }
            }
            return(returnValue);
        }