Example #1
0
        public void Example1()
        {
            var stats = new MovingAverage();

            stats.Add(4);
            stats.Add(7);
            stats.Add(13);
            stats.Add(16);
            Assert.Equal(4, stats.Count);
            Assert.Equal(10, stats.Average, 15);
        }
Example #2
0
        public void Example4()
        {
            var stats = new MovingAverage();

            stats.Add(6);
            stats.Add(2);
            stats.Add(3);
            stats.Add(1);
            Assert.Equal(4, stats.Count);
            Assert.Equal(3, stats.Average, 15);
        }
Example #3
0
        public void Example13()
        {
            var stats = new MovingAverage(3);

            stats.Add(1000000004);
            stats.Add(1000000007);
            stats.Add(1000000013);
            stats.Add(1000000016);
            Assert.Equal(4, stats.Count);
            Assert.Equal(1000000012, stats.Average, 15);
        }
Example #4
0
        public void NoInfinity()
        {
            var stats = new MovingAverage();

            Assert.Throws <ArgumentOutOfRangeException>(delegate {
                stats.Add(double.NegativeInfinity);
            });
            Assert.Throws <ArgumentOutOfRangeException>(delegate {
                stats.Add(double.PositiveInfinity);
            });
            Assert.Throws <ArgumentOutOfRangeException>(delegate {
                stats.Add(double.NaN);
            });
        }
Example #5
0
        public void OneValue()
        {
            var stats = new MovingAverage();

            stats.Add(1);
            Assert.Equal(1, stats.Count);
            Assert.Equal(1, stats.Average);
        }
Example #6
0
        /// <summary>
        /// Performs dynamic cropping based on the largest bounding box
        /// encountered (ie smallest cropping) within the 'memory' of the
        /// autocropper
        /// </summary>
        /// <param name="topCrop">Cropping found in current frame</param>
        /// <param name="bottomCrop">Cropping found in current frame</param>
        private void MinDynamicCrop(int topCrop, int bottomCrop, int leftCrop, int rightCrop)
        {
            if (firstDynamicCrop)
            {
                if (verboseLog)
                {
                    Log.Debug("First dynamic crop, resetting to top {0}, bottom {1}, left {2}, right {3}", topCrop, bottomCrop,
                              leftCrop, rightCrop);
                }
                topCropAvg.Reset(topCrop);
                bottomCropAvg.Reset(bottomCrop);
                leftCropAvg.Reset(leftCrop);
                rightCropAvg.Reset(rightCrop);
                firstDynamicCrop = false;
            }
            else
            {
                topCropAvg.Add(topCrop);
                bottomCropAvg.Add(bottomCrop);
                leftCropAvg.Add(leftCrop);
                rightCropAvg.Add(rightCrop);
            }

            int topMin    = (int)topCropAvg.GetMin();
            int bottomMin = (int)bottomCropAvg.GetMin();
            int leftMin   = (int)leftCropAvg.GetMin();
            int rightMin  = (int)rightCropAvg.GetMin();

            if (verboseLog)
            {
                Log.Debug("Current topMin {0}, bottomMin {1}, leftMin {2}, rightMin {3}", topMin, bottomMin, leftMin, rightMin);
            }

            if (Math.Abs(topMin - lastSettings.Top) > 2 || Math.Abs(bottomMin - lastSettings.Bottom) > 2 ||
                Math.Abs(leftMin - lastSettings.Left) > 2 || Math.Abs(rightMin - lastSettings.Right) > 2)
            {
                CropSettings newSettings = new CropSettings();
                newSettings.Top    = topMin;
                newSettings.Bottom = bottomMin;
                newSettings.Left   = leftMin;
                newSettings.Right  = rightMin;
                RequestCrop(newSettings);
            }
        }
Example #7
0
        public void Analyze()
        {
            float power = 0;

            for (int i = 0; i < bandCount; ++i)
            {
                signalAverages[i].Add(signalPowers[i]);
                float v = signalPowers[i] / signalAverages[i].Value;
                //v = Math.Max(0, v);
                v      = v * v;
                power += v > 1.3f ? 1 : 0;
            }
            power += 1e-5f;
            powerBuffer.Push(power);

            powerProcessor.Process(powerBuffer);


            for (int i = 0; i < binCount; ++i)
            {
                MovingAverage avg = bpmAverages[i];
                avg.Add(powerProcessor.Magnitudes[lowBinIndex + i] + powerProcessor.Magnitudes[(lowBinIndex + i) / 2]);
                peakiness[i] = avg.Value;
            }

            if (beatIndex != -1)
            {
                float phase = powerProcessor.Phases[beatIndex + lowBinIndex];

                if (Math.Abs(phase) < 3.14f * 0.5f && phase * lastPhase < 0)
                {
                    HasBeat          = true;
                    BPS              = 1.0f / (periodsSinceBeat * period);
                    periodsSinceBeat = 0;
                    Console.WriteLine(BPS);
                    FindBeat();
                }
                else
                {
                    periodsSinceBeat++;
                    HasBeat = false;
                }
                lastPhase = phase;
            }
            else
            {
                FindBeat();
            }
        }
Example #8
0
        public float Test(int episodes, int maxStepsPerEpisode, int renderInterval)
        {
            var rewardAvg = new MovingAverage(episodes);

            for (int ep = 0; ep < episodes; ++ep)
            {
                bool render = renderInterval > 0 && (ep % renderInterval) == 0;

                LastObservation = Env.Reset();
                float totalReward = 0;
                int   step        = 0;
                for (; step < maxStepsPerEpisode; ++step)
                {
                    Tensor action = GetOptimalAction();

                    bool done = Env.Step(action, out var observation, out var reward);

                    LastObservation = observation;
                    totalReward    += reward;

                    if (render)
                    {
                        RenderEnv();
                    }

                    if (done)
                    {
                        break;
                    }
                }

                rewardAvg.Add(totalReward);

                if (Verbose)
                {
                    LogLine($"Test# {ep} reward(avg): {Math.Round(totalReward, 2)}({Math.Round(rewardAvg.Avg, 2)}) steps: {step}");
                }
            }

            SaveLog();
            return(rewardAvg.Avg);
        }
Example #9
0
        public static void Grow(NodeHandle start, uint maxMs, bool debug = false)
        {
            if (start == NodeHandle.Invalid)
            {
                return;
            }
            var sw = new Stopwatch();

            sw.Start();
            int count = 0;
            var queue = new Queue <NodeHandle>();

            queue.Enqueue(start);
            var doors = new HashSet <EntHandle>(NearbyObjects().Where(e => Pathfinder.checkDoorModels.Contains(GetModel(e))));

            while (sw.ElapsedMilliseconds < maxMs)
            {
                NodeHandle next;
                if (queue.Count > 0)
                {
                    next = queue.Dequeue();
                }
                else if (!Ungrown.TryDequeue(out next))
                {
                    break;
                }
                if (IsGrown(next))
                {
                    continue;
                }
                GrowOne(next, doors, debug).Each(queue.Enqueue);
                count    += 1;
                LastGrown = next;
            }
            if (queue.Count > 0)
            {
                queue.Each(Ungrown.Enqueue);
            }
            double seconds = (float)(sw.ElapsedTicks + 1) / (float)TimeSpan.TicksPerSecond;

            grownPerSecond.Add((float)count / (float)seconds);
        }
Example #10
0
        public static void OnTick()
        {
            var w = new Stopwatch();

            w.Start();
            FrameCount += 1;
            try {
                GameTime = Call <uint>(GET_GAME_TIMER);

                CurrentPlayer = Call <PlayerHandle>(GET_PLAYER_INDEX);
                Self          = Call <PedHandle>(GET_PLAYER_PED, CurrentPlayer);
                var sw = new Stopwatch();
                sw.Start();
                CameraMatrix   = Matrix(GameplayCam.Handle);
                PlayerMatrix   = Matrix(Self);
                PlayerPosition = Position(PlayerMatrix);
                PlayerVehicle  = CurrentVehicle(Self);
                sw.Stop();
                int dt = (int)GameTime - (int)LastGameTime;
                if (dt != 0)
                {
                    fps.Add(1000 / dt);
                }
                CurrentFPS = fps.Value;

                // consume key strokes, dispatch them to scripts and key bindings
                Controls.OnTick();

                // run all Script instances in order
                Script.Order.Visit(s => s.OnTick());

                LastGameTime = GameTime;
            } catch (Exception err) {
                Log($"Uncaught error in Main.OnTick: {err.Message} {err.StackTrace}");
                OnAbort();
            } finally {
                w.Stop();
            }
        }
Example #11
0
 public void SetMulti(int count) => _parseMultiAvg.Add(count);
Example #12
0
        public void Should_compute_average()
        {
            var sut = new MovingAverage(4);

            sut.ComputeAverage().Should().Be(0);
            sut.Add(2);
            sut.ComputeAverage().Should().Be(2);
            sut.Add(4);
            sut.ComputeAverage().Should().Be(3);
            sut.Add(0);
            sut.ComputeAverage().Should().Be(2);
            sut.Add(6);
            sut.ComputeAverage().Should().Be(3);
            sut.Add(6);
            sut.ComputeAverage().Should().Be(4);
            sut.Add(0);
            sut.Add(0);
            sut.Add(0);
            sut.Add(0);
            sut.ComputeAverage().Should().Be(0);
            sut.Add(10);
            sut.Add(10);
            sut.Add(10);
            sut.Add(10);
            sut.ComputeAverage().Should().Be(10);
        }
Example #13
0
        public void Train(int episodes, int maxStepsPerEpisode)
        {
            Epsilon = MaxEpsilon;

            var rewardChart = new Neuro.ChartGenerator($"{Name}_reward", $"{Name}\n{GetParametersDescription()}", "Episode");

            rewardChart.AddSeries(0, "Reward", System.Drawing.Color.LightGray);
            rewardChart.AddSeries(1, $"Avg({RewardAverageN}) reward", System.Drawing.Color.Blue);
            rewardChart.AddSeries(2, $"Avg({StepsAverageN}) steps per episode\n(right axis)", System.Drawing.Color.CornflowerBlue, true);
            rewardChart.AddSeries(3, "Reward high score", System.Drawing.Color.DarkOrchid);
            var rewardAvg = new MovingAverage(RewardAverageN);
            var stepsAvg  = new MovingAverage(StepsAverageN);

            int globalStep = 0;

            for (int ep = 1; ep <= episodes; ++ep)
            {
                LastObservation = Env.Reset();
                OnReset(LastObservation);

                float totalReward = 0;
                int   step        = 0;

                for (; step < maxStepsPerEpisode; ++step, ++globalStep)
                {
                    Tensor action;

                    if (globalStep < WarmupSteps || GlobalRandom.Rng.NextDouble() < Epsilon)
                    {
                        action = Env.ActionSpace.Sample(); // explore
                    }
                    else
                    {
                        action = GetOptimalAction(); // exploit
                    }
                    if (globalStep >= WarmupSteps && EpsilonDecayMode == EEpsilonDecayMode.EveryStep)
                    {
                        DecayEpsilon();
                    }

                    bool done = Env.Step(action, out var observation, out var reward);

                    if (done && !float.IsNaN(RewardOnDone))
                    {
                        reward = RewardOnDone;
                    }

                    if (RewardClipping)
                    {
                        reward = reward > 0 ? 1 : (reward < 0 ? -1 : 0);
                    }

                    totalReward += reward;

                    if (TrainRenderInterval > 0 && (ep % TrainRenderInterval == 0))
                    {
                        RenderEnv();
                    }

                    OnStep(step, globalStep, action, reward, observation, done);

                    LastObservation = observation;

                    if ((globalStep % TrainInterval == 0) && (globalStep >= WarmupSteps))
                    {
                        OnTrain();
                    }

                    if (done)
                    {
                        break;
                    }
                }

                OnEpisodeEnd(ep);

                rewardAvg.Add(totalReward);
                stepsAvg.Add(step);
                rewardChart.AddData(ep, totalReward, 0);
                rewardChart.AddData(ep, rewardAvg.Avg, 1);
                rewardChart.AddData(ep, stepsAvg.Avg, 2);

                if (totalReward > RewardHighScore)
                {
                    rewardChart.AddData(ep, totalReward, 3);
                    RewardHighScore = totalReward;
                }

                if (SaveFreq > 0 && (ep % SaveFreq == 0))
                {
                    Save($"{Name}_{ep}");
                }

                if (Verbose)
                {
                    LogLine($"Ep# {ep} reward(avg): {Math.Round(totalReward, 2)}({Math.Round(rewardAvg.Avg, 2)}) steps: {step} ε: {Math.Round(Epsilon, 4)} total_steps: {globalStep}");
                }

                if (globalStep >= WarmupSteps && EpsilonDecayMode == EEpsilonDecayMode.EveryEpisode)
                {
                    DecayEpsilon();
                }

                if (ep % 10 == 0)
                {
                    rewardChart.Save();
                }
            }

            rewardChart.Save();
            SaveLog();
        }