Esempio n. 1
0
        public void GetQValues_ForReplay(SarsaElement elem,
                                         out List <Double> q_values, out Boolean randomaction)
        {
            Int32   index = 0;
            Boolean found = false;

            q_values = new List <Double>();

            while (!found && index < table.Count)
            {
                if (table[index].state.Equal(elem.state))
                {
                    found = true;
                }
                else
                {
                    index++;
                }
            }

            if (found)
            {
                randomaction = false;
                q_values.Add(table[index].q_turnleft);
                q_values.Add(table[index].q_turnright);
                q_values.Add(table[index].q_movefw);
                q_values.Add(table[index].q_movebw);
                q_values.Add(table[index].q_item);
                q_values.Add(table[index].q_shoot);
            }
            else
            {
                randomaction = true;
            }
        }
Esempio n. 2
0
        public Int32 SearchIndex_OrAddElem(SarsaElement elem)
        {
            Int32   index = 0;
            Boolean found = false;

            while (!found && index < table.Count)
            {
                if (table[index].state.Equal(elem.state))
                {
                    found = true;
                }
                else
                {
                    index++;
                }
            }

            if (found)
            {
                return(index);
            }
            else
            {
                table.Add(elem);
                return(table.Count - 1);
            }
        }
Esempio n. 3
0
        public void GetBestAction_ForReplay(SarsaElement elem, out Action bestaction, out Double max_q_value, out Boolean randomaction)
        {
            Int32   index = 0;
            Boolean found = false;

            while (!found && index < table.Count)
            {
                if (table[index].state.Equal(elem.state))
                {
                    found = true;
                }
                else
                {
                    index++;
                }
            }

            if (found)
            {
                // Get best Q-value and action according to policy
                max_q_value = table[index].q_turnleft;
                bestaction  = Action.turnleft;
                if (table[index].q_turnright > max_q_value)
                {
                    max_q_value = table[index].q_turnright;
                    bestaction  = Action.turnright;
                }
                if (table[index].q_movefw > max_q_value)
                {
                    max_q_value = table[index].q_movefw;
                    bestaction  = Action.movefw;
                }
                if (table[index].q_movebw > max_q_value)
                {
                    max_q_value = table[index].q_movebw;
                    bestaction  = Action.movebw;
                }
                if (table[index].q_item > max_q_value)
                {
                    max_q_value = table[index].q_item;
                    bestaction  = Action.item;
                }
                if (table[index].q_shoot > max_q_value)
                {
                    max_q_value = table[index].q_shoot;
                    bestaction  = Action.shoot;
                }

                randomaction = false;
            }
            else
            {
                // If state was not found, take a random action
                Random rand = new Random();
                bestaction   = (Action)(rand.Next(0, 6));
                max_q_value  = 0;
                randomaction = true;
            }
        }
Esempio n. 4
0
        public Double GetMaxQValue_AllActions(SarsaElement elem)
        {
            Int32 index = SearchIndex_OrAddElem(elem);

            if (index < 0)
            {
                return(0.0); // error
            }
            Double q_max = table[index].q_turnleft;

            if (table[index].q_turnright > q_max)
            {
                q_max = table[index].q_turnright;
            }

            if (table[index].q_movefw > q_max)
            {
                q_max = table[index].q_movefw;
            }

            if (table[index].q_movebw > q_max)
            {
                q_max = table[index].q_movebw;
            }

            if (table[index].q_item > q_max)
            {
                q_max = table[index].q_item;
            }

            if (table[index].q_shoot > q_max)
            {
                q_max = table[index].q_shoot;
            }

            return(q_max);
        }