Exemplo n.º 1
0
        public static void AddAverageStats(List <IterationStats> newStats, string path)
        {
            List <IterationStats> previousStats = new List <IterationStats>();

            if (File.Exists(path))
            {
                StreamReader reader = new StreamReader(path);
                var          previousStatsReader          = new CsvReader(reader, CultureInfo.InvariantCulture);
                IEnumerable <IterationStats> statsRecords = previousStatsReader.GetRecords <IterationStats>();
                foreach (IterationStats addStat in statsRecords)
                {
                    previousStats.Add(addStat);
                }
                reader.Close();

                if (newStats.Count == previousStats.Count)
                {
                    for (int i = 0; i < previousStats.Count; i++)
                    {
                        IterationStats adjustStat = previousStats[i];
                        IterationStats addStat    = newStats[i];
                        if (addStat.Iteration != adjustStat.Iteration)
                        {
                            Console.WriteLine("Different iteration number");
                        }
                        adjustStat.PositiveHeat += addStat.PositiveHeat;
                        adjustStat.NegativeHeat += addStat.NegativeHeat;
                        adjustStat.CellCount    += addStat.CellCount;
                        adjustStat.IterationAverageNeighbours += addStat.IterationAverageNeighbours;
                        adjustStat.IterationDensity           += addStat.IterationDensity;
                    }
                    WriteStats(previousStats, path);
                }
                else
                {
                    Console.WriteLine("Not same size of iteration stats");
                }
            }
            else
            {
                WriteStats(newStats, path);
            }
        }
Exemplo n.º 2
0
        public void Run(InputCsvFile input, bool predefinedPosition)
        {
            int fieldSize = input.FieldSize;
            int probability = input.ProbabilityForLife;
            int numberOfIterations = input.NumberOfIterations;
            int numberOfSimulations = input.NumberOfSimulations;
            bool writeStats = input.SaveStatistics;
            string statsName = input.NameStatisticFile;
            string endStateName = input.NameEndStateFile;
            if (fieldSize == -1) fieldSize = _defaultStarterSize;
            if (probability == -1) probability = _defaultProbability;
            if (numberOfIterations == -1) numberOfIterations = _defaultIterations;
            if (numberOfSimulations == -1) numberOfSimulations = _defaultNumOfSimulations;
            if (predefinedPosition) numberOfSimulations = 1; //only the predefined game has to be run
            for (int simulation = 0; simulation < numberOfSimulations; simulation++)
            {
                Console.WriteLine("--------------------------");
                if (!predefinedPosition)
                {
                    _cells.Clear();
                }
                _repetitionList.Clear();
                _stats.Clear();
                if (!predefinedPosition)
                {
                    GenerateCellsWithProbability(probability, fieldSize);
                    Console.WriteLine($"Generated Cells: {_cells.Count}");
                    Console.WriteLine($"Field Size: {fieldSize} x {fieldSize}");
                }
                else
                {
                    Console.WriteLine($"Predefined Generated Cells: {_cells.Count}");
                }
                FillChangedPositionsOnStart();
                int checkStarted = 0;
                bool iterationRepeated = false;
                bool iterationDead = false;
                for (int i = 0; i < numberOfIterations; i++)
                {
                    _currentIteration = i + 1;
                    IterateSimulation();

                    //For average stats, simulation must add the dead iterations
                    if(_cells.Count == 0 && !input.AverageStats){
                        iterationDead = true;
                        break;
                    }

                    //For average stats, repetition is not enabled
                    if(!input.AverageStats){
                        if (i % _checkAfterIterations == 0)
                        {
                            checkStarted = i;
                        }
                        if (checkStarted != 0)
                        {

                            if (i - checkStarted <= _checkForIterations)
                            {
                                if (IsCurrentIterationRepetition())
                                {
                                    iterationRepeated = true;
                                    Console.WriteLine($"Iterations: {i + 1}");
                                    break;
                                }
                                AddDictionaryToCheckList();
                            }
                            else if (i - checkStarted == _checkForIterations + 1)
                            {
                                _repetitionList.Clear();
                            }
                        }
                    }
                }
                if (iterationRepeated)
                {
                    Console.WriteLine("Field repeated - simulation terminated");
                }
                else if(iterationDead)
                {
                    Console.WriteLine("Field dead - simulation terminated");
                }
                else
                {
                    Console.WriteLine($"Alive after 1000 iterations: {_cells.Count}");
                }

                if (writeStats)
                {

                    IterationStats endStats = new IterationStats();
                    endStats.CellCount = _cells.Count;
                    _stats.Add(endStats);
                    if (iterationRepeated)
                    {
                        IterationStats.WriteStats(_stats, $"./{ statsName }-{ simulation }-terminated.csv");
                    }
                    else
                    {
                        IterationStats.WriteStats(_stats, $"./{ statsName }-{ simulation }.csv");
                        
                        if(input.AverageStats){
                            IterationStats.AddAverageStats(_stats, $"./{ statsName }-cache.csv");    
                        }
                        
                    }

                }
                if (input.SaveEndState)
                {
                    IterationEndPosition.WriteEndPosition(_cells, $"./{ endStateName }-{ simulation }.csv");
                }
                
            }
            if(input.AverageStats){
                IterationStats.CalculateAndWriteAverageStats(input.NumberOfSimulations, $"./{ statsName }-cache.csv", $"./{ statsName }-average.csv");
            }
            Console.WriteLine("--------------------------");
        }
Exemplo n.º 3
0
        private void IterateSimulation()
        {
            
            this._previouslyChangedCoords = new List<ChangedCellCoords>(this._newChangedCoords);
            this._newChangedCoords.Clear();
            Dictionary<CellCoords, Cell> checkedCells = new Dictionary<CellCoords, Cell>(new CellCoordsComparer());
            CellAverageNeighbours averageNeighbours = new CellAverageNeighbours();
            CellDensity iterationDensity = new CellDensity(_cells.Count);
            //var newCells = new Dictionary<CellCoords, Cell>(new CellCoordsComparer());
            var deadCells = new Dictionary<CellCoords, Cell>(new CellCoordsComparer());
            var stat = new IterationStats();
            stat.Iteration = _currentIteration;
            foreach(KeyValuePair<CellCoords, Cell> cell in _cells){
                iterationDensity.AddCellCoordToCheck(cell.Key);
            }
            iterationDensity.CalculateDensity();
            stat.IterationDensity = iterationDensity.cellDensity;
            stat.CellCount = _cells.Count;
            stat.PositiveHeat = 0;
            stat.NegativeHeat = 0;
            foreach (ChangedCellCoords changedCellCoords in this._previouslyChangedCoords)
            {
                for (int y = changedCellCoords.cellCoords.y - 1; y <= (changedCellCoords.cellCoords.y + 1); y++)
                {
                    for (int x = changedCellCoords.cellCoords.x - 1; x <= (changedCellCoords.cellCoords.x + 1); x++)
                    {
                        if (!checkedCells.TryAdd(new CellCoords(x, y), new Cell()))
                        {
                            continue;
                        }
                        int neighbours = GetAliveNeighbours(x, y);
                        Cell currentCell;
                        bool exists = _cells.TryGetValue(new CellCoords(x, y), out currentCell);

                        switch (neighbours)
                        {
                            case 2:
                                //dead cells remain dead, living cells remain living (no actions)
                                break;
                            case 3:
                                //if living cell, remains living
                                if (!exists)
                                {
                                    this._newChangedCoords.Add(new ChangedCellCoords(new CellCoords(x,y),true));
                                    stat.PositiveHeat++;
                                }
                                break;
                            default:
                                if (exists)
                                {
                                    this._newChangedCoords.Add(new ChangedCellCoords(new CellCoords(x,y),false));
                                    stat.NegativeHeat++;
                                }
                                break;
                        }
                    }
                }
            }

            int addedCells = 0;
            int removedCells = 0;
            foreach(ChangedCellCoords changedCoords in this._newChangedCoords){
                if(changedCoords.newLife){
                    _cells.Add(changedCoords.cellCoords, new Cell());
                    addedCells++;
                }else{
                    _cells.Remove(changedCoords.cellCoords);
                    removedCells++;
                }
            }

            stat.IterationAverageNeighbours = averageNeighbours.averageNeighbourNumber;
            checkedCells.Clear();
            checkedCells = null;
            _stats.Add(stat);
        }