Example #1
0
        /// <summary>
        /// Advance a population to another generation by applying a generation rule.
        /// </summary>
        /// <param name="population">The input population.</param>
        /// <param name="rule">The generation rule.</param>
        /// <param name="nhbdSize">The size of the neighborhood.</param>
        /// <param name="popIndex"> </param>
        /// <returns>The result of applying the generation rule to the input population.</returns>
        public Population AdvancePopulation(Population population, ulong rule, int nhbdSize, int popIndex)
        {
            Contract.Requires(population != null);
            Contract.Requires(rule >= 0);
            Contract.Requires(nhbdSize > 0);

            Population retPop = new Population(population.Size);

            // The index into the population to select the cells around the current cell by the nhbd size.
            int indexMag = (int)Math.Floor((double)nhbdSize / 2);

            // Iterate through every cell in the population, and for each one determine if the new cell at
            // that index in the new population will be alive or dead.
            for (int i = 0; i < retPop.Size; i++)
            {
                int ruleIndex = 0;

                TokenPile contributingTokens = new TokenPile();

                // Iterate through the cells around the current cell, dependant on the nhbd size.
                for (int n = -indexMag+i; n <= indexMag+i; n++)
                {
                    // If the cell is alive (and thus should contribute), then add the power of two of the current
                    // index to the rule index.
                    if (population[n].Value == 0) continue;

                    ruleIndex += (int)Math.Pow(2, n + indexMag - i);

                    if (UseTokens)
                    {
                        contributingTokens.AddTokensFromPile(population[n].Tokens);
                    }
                }

                // Index into the rule by all of the cells that contributed to this new cell.
                int cellValue = (rule & (1UL << ruleIndex)) > 0 ? 1 : 0;

                // Make the new cell, carrying over the tokens of those cells that contributed.
                Cell newCell = new Cell(cellValue) {Tokens = contributingTokens.Normalize()};

                // Add the cell to the return population.
                retPop.Cells.Add(newCell);
            }

            return retPop;
        }
Example #2
0
        public void AssignInitialTokens(Population population)
        {
            int tokenIndex = 0;

            foreach (Cell cell in population.Cells.Where(cell => cell.Value >= 1))
            {
                cell.Tokens.AddToken(tokenIndex);
                tokenIndex++;
            }
        }
Example #3
0
        public Population GenSimpleStartPopulation(int populationSize)
        {
            Population retPop = new Population(populationSize);

            Random rand = new Random();

            for (int i = 0; i < populationSize; i++)
            {
                int cellValue = i == populationSize/2 ? 1 : 0;

                retPop.Cells.Add(new Cell(cellValue));
            }

            return retPop;
        }
Example #4
0
        /// <summary>
        /// Generates and returns a population whose cells have an equal chance of being dead or alive, randomly.
        /// </summary>
        /// <param name="populationSize">The size of the population to generate.</param>
        /// <returns>The randomized population.</returns>
        public Population GenRandomPopulation(int populationSize)
        {
            Population retPop = new Population(populationSize);

            Random rand = new Random();

            for (int i = 0; i < populationSize; i++)
            {
                retPop.Cells.Add(new Cell(rand.Next(2)));
            }

            return retPop;
        }