Example #1
0
        /// <summary>
        /// 随机从数组中取出数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <returns></returns>
        ///
        public static T RandomTake <T>(this List <T> list)
        {
            int index = NP.Random(list.Count);
            T   item  = list[index];

            return(item);
        }
Example #2
0
 public static void RandomRemove <T>(this List <T> list, int capacity)
 {
     if (list.Count >= capacity)
     {
         int index = NP.Random(list.Count);
         list.RemoveAt(index);
     }
 }
Example #3
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        private (int x, int y, double[] classIndex) RandomAccessMemory()
        {
            //use actionNumber represent real types
            int   rawValueIndex = NP.Random(_randomSeedKeys);
            Point p             = _memory[rawValueIndex].RandomTake();

            //current one-hot action
            double[] classIndex = NP.ToOneHot(Array.IndexOf(_randomSeedKeys, rawValueIndex), ActionNum);
            return(p.X, p.Y, classIndex);
        }
Example #4
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <param name="bacthSize"></param>
        /// <returns></returns>
        public static List <T> RandomTakeBatch <T>(this List <T> list, int limitSize = 200)
        {
            int      num  = list.Count;
            List <T> dist = new List <T>();

            for (int i = 0; i < limitSize; i++)
            {
                int key = NP.Random(num);
                dist.Add(list[key]);
            }
            return(dist);
        }
Example #5
0
        /// <summary>
        /// 获取当前actor下的action和reward
        /// </summary>
        /// <param name="step"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public (double[] action, double q) EpsilonGreedy(int step, double[] state)
        {
            int totalEpochs = Convert.ToInt32(_epoches * 0.9);
            var epsion      = EpsilonCalcute(step, eps_total: totalEpochs);

            if (NP.Random() < epsion)
            {
                return(_env.RandomAction(), 0);
            }
            else
            {
                var(action, q) = ChooseAction(state);
                return(action, q);
            }
        }
        /// <summary>
        /// -----------------------------------------------------
        /// *    0  |  1  |  2
        /// * -----------------------
        /// *    7  |  8  |  3
        /// * -----------------------
        /// *    6  |  5  |  4
        /// </summary>
        /// <returns></returns>
        private (int x, int y, double[] actions) RandomAccessMemory()
        {
            //
            int   rawValueIndex = NP.Random(_randomSeedKeys);
            Point pt            = _memory[rawValueIndex].RandomTake();

            //
            double[] actions = new double[ActionNum];
            //快速搜索x++方向点
            List <Point> points = new List <Point>()
            {
                new Point(pt.X - 1, pt.Y - 1), //(-1,-1)
                new Point(pt.X, pt.Y - 1),     //(0,-1)
                new Point(pt.X + 1, pt.Y - 1), //(1,-1)
                new Point(pt.X + 1, pt.Y),     //(1,0)
                new Point(pt.X + 1, pt.Y + 1), //(1,1)
                new Point(pt.X, pt.Y + 1),     //(0,1)
                new Point(pt.X - 1, pt.Y + 1), //(-1,1)
                new Point(pt.X - 1, pt.Y),     //(-1,0)
            };

            //search next point
            for (int pointIndex = 0; pointIndex < ActionNum; pointIndex++)
            {
                Point p = points[pointIndex];
                //if reach to the end, use original point
                if (p.X >= _limit_x || p.X < 0 || p.Y >= _limit_y || p.Y < 0)
                {
                    continue;
                }
                //store right action(one-hot)
                if (_queryTable[p.X, p.Y] == rawValueIndex)
                {
                    actions.CombineOneHot(NP.ToOneHot(pointIndex, ActionNum));
                }
            }
            //
            if (!_existActions.Exists(p => NP.Equal(p, actions)))
            {
                _existActions.Add(actions);
            }
            //
            return(pt.X, pt.Y, actions);
        }
Example #7
0
        /// <summary>
        /// random数据集
        /// </summary>
        public double[] RandomAction()
        {
            int action = NP.Random(ActionNum);

            return(NP.ToOneHot(action, ActionNum));
        }