Esempio n. 1
0
        public SimulationResult Run()
        {
            UniformDistribution uniform          = new UniformDistribution(0, (2 * Math.PI));
            SimulationResult    simulationResult = new SimulationResult();
            double x = 0;
            double y = 0;

            simulationResult.DistanceFinal = 0;
            for (int i = 0; i < this.Steps; i++)
            {
                int    l        = 1;
                double angleRad = uniform.GetRandomValue();
                x = (x + (l * Math.Cos(angleRad)));
                y = (y + (l * Math.Sin(angleRad)));
                double distance = Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2));
                if (i == 0)
                {
                    simulationResult.MinX = simulationResult.MaxX = x;
                    simulationResult.MinY = simulationResult.MaxY = y;
                }
                else
                {
                    simulationResult.MinX = (x < simulationResult.MinX) ? x : simulationResult.MinX;
                    simulationResult.MaxX = (x > simulationResult.MaxX) ? x : simulationResult.MaxX;
                    simulationResult.MinY = (y < simulationResult.MinY) ? y : simulationResult.MinY;
                    simulationResult.MaxY = (y > simulationResult.MaxY) ? y : simulationResult.MaxY;
                }
                simulationResult.CoordinatesX.Add(x);
                simulationResult.CoordinatesY.Add(y);
                simulationResult.Distances.Add(distance);
                simulationResult.DistanceFinal += distance;
            }
            simulationResult.DistanceDiff = Math.Abs((Steps * Math.Sqrt(Steps)) - simulationResult.DistanceFinal);
            return(simulationResult);
        }
Esempio n. 2
0
        private void PlotChartHistogram(SimulationResult simResult)
        {
            ChartArea chartArea = new ChartArea("Histogram");

            chartArea.AxisX = new Axis()
            {
                MajorGrid = new Grid()
                {
                    LineColor = Color.FromArgb(0x78b6b6b6)
                }
            };
            chartArea.AxisY = new Axis()
            {
                MajorGrid = new Grid()
                {
                    LineColor = Color.FromArgb(0x78b6b6b6)
                }
            };
            chartHistogram.ChartAreas.Add(chartArea);

            Series series = new Series("Distâncias")
            {
                ChartArea   = "Histogram",
                Color       = Color.DarkBlue,
                BorderWidth = 3,
                ChartType   = SeriesChartType.Column
            };

            for (int i = 0; i < simResult.CoordinatesX.Count; i++)
            {
                series.Points.AddXY(simResult.CoordinatesX[i], simResult.CoordinatesY[i]);
            }
            chartHistogram.Series.Add(series);
        }
Esempio n. 3
0
        public SimulationResult Run(int repetitions)
        {
            List <double>           distancesDiffs     = new List <double>();
            SimulationResult        simulationResult   = new SimulationResult();
            List <SimulationResult> repetitionsResults = new List <SimulationResult>();

            for (int i = 0; i < repetitions; i++)
            {
                repetitionsResults.Add(Run());
            }
            foreach (SimulationResult simResult in repetitionsResults)
            {
                distancesDiffs.Add(simResult.DistanceDiff);
            }
            distancesDiffs.Sort();

            int classesCount = Int32.Parse(Math.Truncate(Math.Sqrt(repetitions)).ToString());

            if (classesCount > 30)
            {
                classesCount = 30;
            }
            double classValue = (distancesDiffs[distancesDiffs.Count - 1] / classesCount);
            Dictionary <double, int> classesFrequencies = new Dictionary <double, int>();

            for (int i = 1; i <= classesCount; i++)
            {
                classesFrequencies.Add((i * classValue), 0);
            }
            foreach (double distanceDiff in distancesDiffs)
            {
                for (int i = 1; i <= classesCount; i++)
                {
                    if (i == 1)
                    {
                        if (distanceDiff > 0 && distanceDiff <= classValue)
                        {
                            classesFrequencies[classValue] += 1;
                        }
                    }
                    else
                    {
                        double lower = (i - 1) * classValue;
                        double upper = i * classValue;
                        if ((distanceDiff > lower) && (distanceDiff <= upper))
                        {
                            classesFrequencies[upper] += 1;
                        }
                    }
                }
            }
            foreach (KeyValuePair <double, int> classFrequency in classesFrequencies)
            {
                simulationResult.CoordinatesX.Add(classFrequency.Key);
                simulationResult.CoordinatesY.Add(Math.Truncate((double)(100 * (classFrequency.Value)) / repetitions));
            }

            return(simulationResult);
        }
Esempio n. 4
0
        private void PlotChartDistances(SimulationResult simResult, int steps)
        {
            ChartArea chartArea = new ChartArea("Distance");

            chartArea.AxisX = new Axis()
            {
                Minimum   = 0,
                Maximum   = Math.Truncate(simResult.DistanceFinal),
                Interval  = Math.Truncate((double)steps / 5),
                MajorGrid = new Grid()
                {
                    LineColor = Color.FromArgb(0x78b6b6b6)
                }
            };
            chartArea.AxisY = new Axis()
            {
                MajorGrid = new Grid()
                {
                    LineColor = Color.FromArgb(0x78b6b6b6)
                }
            };
            chartDistance.ChartAreas.Add(chartArea);

            Series seriesDistances = new Series("Distâncias")
            {
                ChartArea   = "Distance",
                Color       = Color.Blue,
                BorderWidth = 3,
                ChartType   = SeriesChartType.Line
            };

            Series seriesSquareRoot = new Series("Raiz n")
            {
                ChartArea   = "Distance",
                Color       = Color.Red,
                BorderWidth = 3,
                ChartType   = SeriesChartType.Spline
            };

            double sumDistances = 0;

            seriesDistances.Points.AddXY(0, 0);
            seriesSquareRoot.Points.AddXY(0, 0);
            for (int i = 0; i < steps; i++)
            {
                sumDistances += simResult.Distances[i];
                double truncateX = Math.Truncate(sumDistances);
                seriesDistances.Points.AddXY(truncateX, simResult.Distances[i]);
                seriesSquareRoot.Points.AddXY(truncateX, Math.Sqrt(truncateX));
            }
            chartDistance.Series.Add(seriesDistances);
            chartDistance.Series.Add(seriesSquareRoot);
        }
Esempio n. 5
0
        private void PlotChartPath(SimulationResult simResult, int steps)
        {
            ChartArea chartArea = new ChartArea("Path");

            chartArea.AxisX = new Axis()
            {
                Minimum           = Math.Truncate(simResult.MinX) - 1,
                Maximum           = Math.Truncate(simResult.MaxX) + 1,
                Interval          = Math.Truncate((simResult.MaxX - simResult.MinX) / 5),
                IsStartedFromZero = false,
                MajorGrid         = new Grid()
                {
                    LineColor = Color.FromArgb(0x78b6b6b6)
                }
            };
            chartArea.AxisY = new Axis()
            {
                Minimum           = Math.Truncate(simResult.MinY) - 1,
                Maximum           = Math.Truncate(simResult.MaxY) + 1,
                Interval          = Math.Truncate((simResult.MaxY - simResult.MinY) / 5),
                IsStartedFromZero = false,
                MajorGrid         = new Grid()
                {
                    LineColor = Color.FromArgb(0x78b6b6b6)
                }
            };
            chartPath.ChartAreas.Add(chartArea);

            Series series = new Series("Caminho")
            {
                ChartArea   = "Path",
                Color       = Color.FromArgb(0x78CCCC00),
                BorderWidth = 3,
                MarkerStyle = MarkerStyle.Diamond,
                MarkerColor = Color.FromArgb(0x79000080),
                MarkerSize  = 10,
                ChartType   = SeriesChartType.Line
            };

            for (int i = 0; i < steps; i++)
            {
                series.Points.AddXY(simResult.CoordinatesX[i], simResult.CoordinatesY[i]);
            }
            chartPath.Series.Add(series);
        }
Esempio n. 6
0
        private void RunSimulation(int steps, int repetitions)
        {
            Simulator        simulator = new Simulator(steps);
            SimulationResult simResult = new SimulationResult();

            if (repetitions == 0)
            {
                simResult = simulator.Run();
                PlotChartPath(simResult, steps);
                PlotChartDistances(simResult, steps);
                labelDistanceWalkedResult.Text    = Math.Truncate(simResult.DistanceFinal).ToString();
                labelDistanceEstimatedResult.Text = Math.Truncate(simResult.DistanceDiff).ToString();
            }
            else
            {
                simResult = simulator.Run(repetitions);
                PlotChartHistogram(simResult);
            }
        }