Example #1
0
        public void reset(Prostredie env, int t = 0, bool training = true)
        {
            // Vymaz vsetky jablka a miny
            env.NahradObjekty(Jablko.Tag, new Cesta());
            env.NahradObjekty(Mina.Tag, new Cesta());

            if (training == true)
            {
                var idx = r.Next(0, 3);
                this.currentPos = new Vector2(Prostredie.startPositionX_training[idx], Prostredie.startPositionY_training[idx]);

                apple_count = r.Next(2, 5) + 1;
                for (int i = 0; i < apple_count; i++)
                {
                    env.GenerateItem(new Jablko());
                }

                mine_count = r.Next(0, 3) + 1;
                for (int i = 0; i < mine_count; i++)
                {
                    env.GenerateItem(new Mina());
                }
            }
            else
            {
                this.currentPos = new Vector2(this.testing_startsPos[t].x, this.testing_startsPos[t].y);

                for (int i = 0; i < this.testing_applesPos[t].Length; i++)
                {
                    env.prostredie[this.testing_applesPos[t][i].y][this.testing_applesPos[t][i].x] = new Jablko();
                }

                for (int i = 0; i < this.testing_minesPos[t].Length; i++)
                {
                    env.prostredie[this.testing_minesPos[t][i].y][this.testing_minesPos[t][i].x] = new Mina();
                }
            }

            stav = new AI.QLearning.QState
            {
                PositionX  = currentPos.x,
                PositionY  = currentPos.y,
                stateRadar = Radar(env)
            };

            this.apples = 0;
            this.mines  = 0;
        }
Example #2
0
        public bool AktualizujAgenta(Prostredie env, bool ucenie, double eps, out float odmena)
        {
            bool isValid;
            int  akcia;

            // Vyber akciu
            var akcia_max = qBrain.NajdiMaxAkciu(stav);

            /****************************************************/
            /*                 Agent vykona akciu               */
            /****************************************************/
            // Ak existuje vedomost
            if (akcia_max != null)
            {
                // explore
                if (ucenie && r.NextDouble() < eps)
                {
                    isValid = sample(out akcia);
                }
                else
                {
                    akcia   = akcia_max.Value;
                    isValid = Pohyb((EAkcie)akcia, 10, 10);
                }
            }
            // Ak neexistuje este vedomost
            else
            {
                isValid = sample(out akcia);
                // ... vytvor zaznam o najdenom stave
                qBrain.Qtable.Add(stav, new float[] { 0f, 0f, 0f, 0f });
            }

            if (isValid)
            {
                odmena = env.Hodnotenie(currentPos.x, currentPos.y);
            }
            else
            {
                odmena = -1.0f;
            }

            /****************************************************/
            /*                      Feedback                    */
            /****************************************************/
            var novyStav = new AI.QLearning.QState
            {
                PositionX  = currentPos.x,
                PositionY  = currentPos.y,
                stateRadar = Radar(env)
            };

            if (ucenie)
            {
                var buducaAkcia    = qBrain.NajdiMaxAkciu(novyStav);
                var buducaQhodnota = 0f;

                // Aktualizuj Qtable hodnotu pre [s;a]
                if (buducaAkcia != null)
                {
                    buducaQhodnota = qBrain.Qtable[novyStav][buducaAkcia.Value];
                }

                qBrain.Aktualizuj(stav, akcia, odmena, buducaQhodnota);
            }

            this.stav = novyStav;

            // Agent zobral jablko
            if (env.prostredie[currentPos.y][currentPos.x].id == Jablko.Tag)
            {
                env.prostredie[currentPos.y][currentPos.x] = new Cesta();
                this.apples += 1;
            }

            // Agent aktivoval minu
            if (env.prostredie[currentPos.y][currentPos.x].id == Mina.Tag)
            {
                env.prostredie[currentPos.y][currentPos.x] = new Cesta();
                this.mines += 1;
            }

            return(isValid);
        }