Example #1
0
        /// <summary>
        /// Construct with default set of parameters.
        /// </summary>
        public NeatGenomeParameters()
        {
            // Previous default option:
            // _normalNeuronActivFn                     = SteepenedSigmoid.__DefaultInstance;
            _normalNeuronActivFn                     = InverseAbsoluteSteepSigmoid.__DefaultInstance;
            _regulatoryActivFn                       = StepFunctionOffset.__DefaultInstance;
            _outputNeuronActivFn                     = Proportional.__DefaultInstance;
            _connectionWeightRange                   = DefaultConnectionWeightRange;
            _initialInterconnectionsProportion       = DefaultInitialInterconnectionsProportion;
            _disjointExcessGenesRecombineProbability = DefaultDisjointExcessGenesRecombineProbability;
            _connectionWeightMutationProbability     = DefaultConnectionWeightMutationProbability;
            _addNodeMutationProbability              = DefaultAddNodeMutationProbability;
            _addConnectionMutationProbability        = DefaultAddConnectionMutationProbability;
            _nodeAuxStateMutationProbability         = DefaultNodeAuxStateMutationProbability;
            _deleteConnectionMutationProbability     = DefaultDeleteConnectionMutationProbability;

            _rouletteWheelLayout = CreateRouletteWheelLayout();
            _rouletteWheelLayoutNonDestructive = CreateRouletteWheelLayout_NonDestructive();

            // Create a connection weight mutation scheme.
            _connectionMutationInfoList = CreateConnectionWeightMutationScheme_Default();

            // No fitness history.
            _fitnessHistoryLength = 0;
        }
Example #2
0
        static Dictionary <string, PasswordInfo> morphEnglish(Dictionary <string, PasswordInfo> english, double[] digitProbs, double[] posProbs, int minLength = 0)
        {
            Dictionary <string, PasswordInfo> results = new Dictionary <string, PasswordInfo>();

            FastRandom random = new FastRandom();

            Console.WriteLine("Probs sum: {0}", digitProbs.Sum());
            RouletteWheelLayout digitLayout = new RouletteWheelLayout(digitProbs);
            RouletteWheelLayout posLayout   = new RouletteWheelLayout(posProbs);
            int alreadyNumbered             = 0;

            foreach (string s in english.Keys)
            {
                bool numbered = false;
                for (int i = 0; i < s.Length; i++)
                {
                    if (s[i] >= '0' && s[i] <= '9')
                    {
                        alreadyNumbered++;
                        numbered = true;
                        break;
                    }
                }
                string morphedPassword = s;
                while (!numbered || morphedPassword.Length < minLength)
                {
                    int toAdd = RouletteWheel.SingleThrow(digitLayout, random);
                    int pos   = RouletteWheel.SingleThrow(posLayout, random);

                    if (pos == 0)
                    {
                        break;
                    }
                    else if (pos == 1)
                    {
                        morphedPassword = toAdd + morphedPassword;
                    }
                    else if (pos == 2)
                    {
                        morphedPassword = morphedPassword + toAdd;
                    }
                    else
                    {
                        pos             = random.Next(morphedPassword.Length);
                        morphedPassword = morphedPassword.Substring(0, pos) + toAdd + morphedPassword.Substring(pos, morphedPassword.Length - pos);
                    }
                    numbered = true;
                }
                PasswordInfo val;
                if (!results.TryGetValue(morphedPassword, out val))
                {
                    results.Add(morphedPassword, new PasswordInfo(1, 1));
                }
            }
            Console.WriteLine("Had numbers already: {0}", alreadyNumbered);
            return(results);
        }
        /// <summary>
        /// Constructs an activation function library with a default set of activation functions.
        /// </summary>
        public DefaultCppnActivationFunctionLibrary()
        {
            _functionList = new List <ActivationFunctionInfo>(4);
            double[] probabilities = { 0.25, 0.25, 0.25, 0.25 };
            _functionList.Add(new ActivationFunctionInfo(0, probabilities[0], Linear.__DefaultInstance));
            _functionList.Add(new ActivationFunctionInfo(1, probabilities[1], BipolarSigmoid.__DefaultInstance));
            _functionList.Add(new ActivationFunctionInfo(2, probabilities[2], Gaussian.__DefaultInstance));
            _functionList.Add(new ActivationFunctionInfo(3, probabilities[3], Sine.__DefaultInstance));
            _rwl = new RouletteWheelLayout(probabilities);

            _functionDict = CreateFunctionDictionary(_functionList);
        }
Example #4
0
        public MarkovChain(MarkovChainNode[] nodes, int stepsPerActivation, FastRandom random)
        {
            _nodes = nodes;
            _stepsPerActivation = stepsPerActivation;
            _random             = random;

            _rouletteWheels = new RouletteWheelLayout[nodes.Length];
            for (int i = 0; i < nodes.Length; i++)
            {
                _rouletteWheels[i] = new RouletteWheelLayout(nodes[i].TransitionProbabilities);
            }
        }
Example #5
0
        /// <summary>
        /// Constructs an activation function library with the provided list of activation functions.
        /// </summary>
        public DefaultActivationFunctionLibrary(IList <ActivationFunctionInfo> fnList)
        {
            // Build a RouletteWheelLayout based on the selection probability on each item.
            int count = fnList.Count;

            double[] probabilities = new double[count];
            for (int i = 0; i < count; i++)
            {
                probabilities[i] = fnList[i].SelectionProbability;
            }
            _rwl          = new RouletteWheelLayout(probabilities);
            _functionList = fnList;

            // Build a dictionary of functions keyed on integer ID.
            _functionDict = CreateFunctionDictionary(_functionList);
        }
Example #6
0
        /// <summary>
        /// Move the prey. The prey moves by a simple set of stochastic rules that make it more likely to move away from
        /// the agent, and moreso when it is close.
        /// </summary>
        public void MovePrey()
        {
            // Determine if prey will move in this timestep. (Speed is simulated stochastically)
            if (_rng.NextDouble() > _preySpeed)
            {
                return;
            }

            // Determine position of agent relative to prey.
            PolarPoint relPolarPos = PolarPoint.FromCartesian(_agentPos - _preyPos);

            // Calculate probabilities of moving in each of the four directions. This stochastic strategy is taken from:
            // Incremental Evolution Of Complex General Behavior, Faustino Gomez and Risto Miikkulainen (1997)
            // (http://nn.cs.utexas.edu/downloads/papers/gomez.adaptive-behavior.pdf)
            // Essentially the prey moves randomply but we bias the movements so the prey moves away from the agent, and thus
            // generally avoids getting eaten through stupidity.
            double T = MovePrey_T(relPolarPos.Radial);

            double[] probs = new double[4];
            probs[0] = Math.Exp((CalcAngleDelta(relPolarPos.Theta, Math.PI / 2.0) / Math.PI) * T * 0.33);  // North.
            probs[1] = Math.Exp((CalcAngleDelta(relPolarPos.Theta, 0) / Math.PI) * T * 0.33);              // East.
            probs[2] = Math.Exp((CalcAngleDelta(relPolarPos.Theta, Math.PI * 1.5) / Math.PI) * T * 0.33);  // South.
            probs[3] = Math.Exp((CalcAngleDelta(relPolarPos.Theta, Math.PI) / Math.PI) * T * 0.33);        // West.

            RouletteWheelLayout rwl = new RouletteWheelLayout(probs);
            int action = RouletteWheel.SingleThrow(rwl, _rng);

            switch (action)
            {
            case 0:      // Move north.
                _preyPos._y = Math.Min(_preyPos._y + 1, _gridSize - 1);
                break;

            case 1:     // Move east.
                _preyPos._x = Math.Min(_preyPos._x + 1, _gridSize - 1);
                break;

            case 2:     // Move south.
                _preyPos._y = Math.Max(_preyPos._y - 1, 0);
                break;

            case 3:     // Move west (is the best?)
                _preyPos._x = Math.Max(_preyPos._x - 1, 0);
                break;
            }
        }
Example #7
0
        /// <summary>
        /// Copy constructor.
        /// </summary>
        public NeatGenomeParameters(NeatGenomeParameters copyFrom)
        {
            _feedforwardOnly                         = copyFrom._feedforwardOnly;
            _activationFn                            = copyFrom._activationFn;
            _connectionWeightRange                   = copyFrom._connectionWeightRange;
            _initialInterconnectionsProportion       = copyFrom._initialInterconnectionsProportion;
            _disjointExcessGenesRecombineProbability = copyFrom._disjointExcessGenesRecombineProbability;
            _connectionWeightMutationProbability     = copyFrom._connectionWeightMutationProbability;
            _addNodeMutationProbability              = copyFrom._addNodeMutationProbability;
            _addConnectionMutationProbability        = copyFrom._addConnectionMutationProbability;
            _nodeAuxStateMutationProbability         = copyFrom._nodeAuxStateMutationProbability;
            _deleteConnectionMutationProbability     = copyFrom._deleteConnectionMutationProbability;

            _rouletteWheelLayout = new RouletteWheelLayout(copyFrom._rouletteWheelLayout);
            _rouletteWheelLayoutNonDestructive = new RouletteWheelLayout(copyFrom._rouletteWheelLayoutNonDestructive);

            _connectionMutationInfoList = new ConnectionMutationInfoList(copyFrom._connectionMutationInfoList);
            _connectionMutationInfoList.Initialize();
            _fitnessHistoryLength = copyFrom._fitnessHistoryLength;
        }
Example #8
0
        /// <summary>
        /// Construct with default set of parameters.
        /// </summary>
        public NeatGenomeParameters()
        {
            _activationFn                            = SteepenedSigmoid.__DefaultInstance;
            _connectionWeightRange                   = DefaultConnectionWeightRange;
            _initialInterconnectionsProportion       = DefaultInitialInterconnectionsProportion;
            _disjointExcessGenesRecombineProbability = DefaultDisjointExcessGenesRecombineProbability;
            _connectionWeightMutationProbability     = DefaultConnectionWeightMutationProbability;
            _addNodeMutationProbability              = DefaultAddNodeMutationProbability;
            _addConnectionMutationProbability        = DefaultAddConnectionMutationProbability;
            _nodeAuxStateMutationProbability         = DefaultNodeAuxStateMutationProbability;
            _deleteConnectionMutationProbability     = DefaultDeleteConnectionMutationProbability;

            _rouletteWheelLayout = CreateRouletteWheelLayout();
            _rouletteWheelLayoutNonDestructive = CreateRouletteWheelLayout_NonDestructive();

            // Create a connection weight mutation scheme.
            _connectionMutationInfoList = CreateConnectionWeightMutationScheme_Default();

            // No fitness history.
            _fitnessHistoryLength = 0;
        }
        /// <summary>
        /// Cross specie mating.
        /// </summary>
        /// <param name="rwl">RouletteWheelLayout for selectign genomes in teh current specie.</param>
        /// <param name="rwlArr">Array of RouletteWheelLayout objects for genome selection. One for each specie.</param>
        /// <param name="rwlSpecies">RouletteWheelLayout for selecting species. Based on relative fitness of species.</param>
        /// <param name="currentSpecieIdx">Current specie's index in _specieList</param>
        /// <param name="genomeList">Current specie's genome list.</param>
        private TGenome CreateOffspring_CrossSpecieMating(RouletteWheelLayout rwl,
                                                          RouletteWheelLayout[] rwlArr,
                                                          RouletteWheelLayout rwlSpecies,
                                                          int currentSpecieIdx,
                                                          IList <TGenome> genomeList)
        {
            // Select parent from current specie.
            int parent1Idx = RouletteWheel.SingleThrow(rwl, _rng);

            // Select specie other than current one for 2nd parent genome.
            RouletteWheelLayout rwlSpeciesTmp = rwlSpecies.RemoveOutcome(currentSpecieIdx);
            int specie2Idx = RouletteWheel.SingleThrow(rwlSpeciesTmp, _rng);

            // Select a parent genome from the second specie.
            int parent2Idx = RouletteWheel.SingleThrow(rwlArr[specie2Idx], _rng);

            // Get the two parents to mate.
            TGenome parent1 = genomeList[parent1Idx];
            TGenome parent2 = _specieList[specie2Idx].GenomeList[parent2Idx];

            return(parent1.CreateOffspring(parent2, _currentGeneration));
        }
        /// <summary>
        /// Create the required number of offspring genomes, using specieStatsArr as the basis for selecting how
        /// many offspring are produced from each species.
        /// </summary>
        private List <TGenome> CreateOffspring(SpecieStats[] specieStatsArr, int offspringCount)
        {
            // Build a RouletteWheelLayout for selecting species for cross-species reproduction.
            // While we're in the loop we also pre-build a RouletteWheelLayout for each specie;
            // Doing this before the main loop means we have RouletteWheelLayouts available for
            // all species when performing cross-specie matings.
            int specieCount = specieStatsArr.Length;

            double[] specieFitnessArr    = new double[specieCount];
            RouletteWheelLayout[] rwlArr = new RouletteWheelLayout[specieCount];

            // Count of species with non-zero selection size.
            // If this is exactly 1 then we skip inter-species mating. One is a special case because for 0 the
            // species all get an even chance of selection, and for >1 we can just select normally.
            int nonZeroSpecieCount = 0;

            for (int i = 0; i < specieCount; i++)
            {
                // Array of probabilities for specie selection. Note that some of these probabilites can be zero, but at least one of them won't be.
                SpecieStats inst = specieStatsArr[i];
                specieFitnessArr[i] = inst._selectionSizeInt;
                if (0 != inst._selectionSizeInt)
                {
                    nonZeroSpecieCount++;
                }

                // For each specie we build a RouletteWheelLayout for genome selection within
                // that specie. Fitter genomes have higher probability of selection.
                List <TGenome> genomeList    = _specieList[i].GenomeList;
                double[]       probabilities = new double[inst._selectionSizeInt];
                for (int j = 0; j < inst._selectionSizeInt; j++)
                {
                    probabilities[j] = genomeList[j].EvaluationInfo.Fitness;
                }
                rwlArr[i] = new RouletteWheelLayout(probabilities);
            }

            // Complete construction of RouletteWheelLayout for specie selection.
            RouletteWheelLayout rwlSpecies = new RouletteWheelLayout(specieFitnessArr);

            // Produce offspring from each specie in turn and store them in offspringList.
            List <TGenome> offspringList = new List <TGenome>(offspringCount);

            for (int specieIdx = 0; specieIdx < specieCount; specieIdx++)
            {
                SpecieStats    inst       = specieStatsArr[specieIdx];
                List <TGenome> genomeList = _specieList[specieIdx].GenomeList;

                // Get RouletteWheelLayout for genome selection.
                RouletteWheelLayout rwl = rwlArr[specieIdx];

                // --- Produce the required number of offspring from asexual reproduction.
                for (int i = 0; i < inst._offspringAsexualCount; i++)
                {
                    int     genomeIdx = RouletteWheel.SingleThrow(rwl, _rng);
                    TGenome offspring = genomeList[genomeIdx].CreateOffspring(_currentGeneration);
                    offspringList.Add(offspring);
                }
                _stats._asexualOffspringCount += (ulong)inst._offspringAsexualCount;

                // --- Produce the required number of offspring from sexual reproduction.
                // Cross-specie mating.
                // If nonZeroSpecieCount is exactly 1 then we skip inter-species mating. One is a special case because
                // for 0 the  species all get an even chance of selection, and for >1 we can just select species normally.
                int crossSpecieMatings = nonZeroSpecieCount == 1 ? 0 :
                                         (int)Utilities.ProbabilisticRound(_eaParams.InterspeciesMatingProportion
                                                                           * inst._offspringSexualCount, _rng);
                _stats._sexualOffspringCount       += (ulong)(inst._offspringSexualCount - crossSpecieMatings);
                _stats._interspeciesOffspringCount += (ulong)crossSpecieMatings;

                // An index that keeps track of how many offspring have been produced in total.
                int matingsCount = 0;
                for (; matingsCount < crossSpecieMatings; matingsCount++)
                {
                    TGenome offspring = CreateOffspring_CrossSpecieMating(rwl, rwlArr, rwlSpecies, specieIdx, genomeList);
                    offspringList.Add(offspring);
                }

                // For the remainder we use normal intra-specie mating.
                // Test for special case - we only have one genome to select from in the current specie.
                if (1 == inst._selectionSizeInt)
                {
                    // Fall-back to asexual reproduction.
                    for (; matingsCount < inst._offspringSexualCount; matingsCount++)
                    {
                        int     genomeIdx = RouletteWheel.SingleThrow(rwl, _rng);
                        TGenome offspring = genomeList[genomeIdx].CreateOffspring(_currentGeneration);
                        offspringList.Add(offspring);
                    }
                }
                else
                {
                    // Remainder of matings are normal within-specie.
                    for (; matingsCount < inst._offspringSexualCount; matingsCount++)
                    {
                        // Select parents. SelectRouletteWheelItem() guarantees parent2Idx!=parent1Idx
                        int     parent1Idx = RouletteWheel.SingleThrow(rwl, _rng);
                        TGenome parent1    = genomeList[parent1Idx];

                        // Remove selected parent from set of possible outcomes.
                        RouletteWheelLayout rwlTmp = rwl.RemoveOutcome(parent1Idx);
                        if (0.0 != rwlTmp.ProbabilitiesTotal)
                        {   // Get the two parents to mate.
                            int     parent2Idx = RouletteWheel.SingleThrow(rwlTmp, _rng);
                            TGenome parent2    = genomeList[parent2Idx];
                            TGenome offspring  = parent1.CreateOffspring(parent2, _currentGeneration);
                            offspringList.Add(offspring);
                        }
                        else
                        {   // No other parent has a non-zero selection probability (they all have zero fitness).
                            // Fall back to asexual reproduction of the single genome with a non-zero fitness.
                            TGenome offspring = parent1.CreateOffspring(_currentGeneration);
                            offspringList.Add(offspring);
                        }
                    }
                }
            }

            _stats._totalOffspringCount += (ulong)offspringCount;
            return(offspringList);
        }
        /// <summary>
        /// Calculate statistics for each specie. This method is at the heart of the evolutionary algorithm,
        /// the key things that are achieved in this method are - for each specie we calculate:
        ///  1) The target size based on fitness of the specie's member genomes.
        ///  2) The elite size based on the current size. Potentially this could be higher than the target
        ///     size, so a target size is taken to be a hard limit.
        ///  3) Following (1) and (2) we can calculate the total number offspring that need to be generated
        ///     for the current generation.
        /// </summary>
        private SpecieStats[] CalcSpecieStats(out int offspringCount)
        {
            double totalMeanFitness = 0.0;

            // Build stats array and get the mean fitness of each specie.
            int specieCount = _specieList.Count;

            SpecieStats[] specieStatsArr = new SpecieStats[specieCount];
            for (int i = 0; i < specieCount; i++)
            {
                SpecieStats inst = new SpecieStats();
                specieStatsArr[i] = inst;
                inst._meanFitness = _specieList[i].CalcMeanFitness();
                totalMeanFitness += inst._meanFitness;
            }

            // Calculate the new target size of each specie using fitness sharing.
            // Keep a total of all allocated target sizes, typically this will vary slightly from the
            // overall target population size due to rounding of each real/fractional target size.
            int totalTargetSizeInt = 0;

            if (0.0 == totalMeanFitness)
            {   // Handle specific case where all genomes/species have a zero fitness.
                // Assign all species an equal targetSize.
                double targetSizeReal = (double)_populationSize / (double)specieCount;

                for (int i = 0; i < specieCount; i++)
                {
                    SpecieStats inst = specieStatsArr[i];
                    inst._targetSizeReal = targetSizeReal;

                    // Stochastic rounding will result in equal allocation if targetSizeReal is a whole
                    // number, otherwise it will help to distribute allocations evenly.
                    inst._targetSizeInt = (int)Utilities.ProbabilisticRound(targetSizeReal, _rng);

                    // Total up discretized target sizes.
                    totalTargetSizeInt += inst._targetSizeInt;
                }
            }
            else
            {
                // The size of each specie is based on its fitness relative to the other species.
                for (int i = 0; i < specieCount; i++)
                {
                    SpecieStats inst = specieStatsArr[i];
                    inst._targetSizeReal = (inst._meanFitness / totalMeanFitness) * (double)_populationSize;

                    // Discretize targetSize (stochastic rounding).
                    inst._targetSizeInt = (int)Utilities.ProbabilisticRound(inst._targetSizeReal, _rng);

                    // Total up discretized target sizes.
                    totalTargetSizeInt += inst._targetSizeInt;
                }
            }

            // Discretized target sizes may total up to a value that is not equal to the required overall population
            // size. Here we check this and if there is a difference then we adjust the specie's targetSizeInt values
            // to compensate for the difference.
            //
            // E.g. If we are short of the required populationSize then we add the required additional allocation to
            // selected species based on the difference between each specie's targetSizeReal and targetSizeInt values.
            // What we're effectively doing here is assigning the additional required target allocation to species based
            // on their real target size in relation to their actual (integer) target size.
            // Those species that have an actual allocation below there real allocation (the difference will often
            // be a fractional amount) will be assigned extra allocation probabilistically, where the probability is
            // based on the differences between real and actual target values.
            //
            // Where the actual target allocation is higher than the required target (due to rounding up), we use the same
            // method but we adjust specie target sizes down rather than up.
            int targetSizeDeltaInt = totalTargetSizeInt - _populationSize;

            if (targetSizeDeltaInt < 0)
            {
                // Check for special case. If we are short by just 1 then increment targetSizeInt for the specie containing
                // the best genome. We always ensure that this specie has a minimum target size of 1 with a final test (below),
                // by incrementing here we avoid the probabilistic allocation below followed by a further correction if
                // the champ specie ended up with a zero target size.
                if (-1 == targetSizeDeltaInt)
                {
                    specieStatsArr[_bestSpecieIdx]._targetSizeInt++;
                }
                else
                {
                    // We are short of the required populationSize. Add the required additional allocations.
                    // Determine each specie's relative probability of receiving additional allocation.
                    double[] probabilities = new double[specieCount];
                    for (int i = 0; i < specieCount; i++)
                    {
                        SpecieStats inst = specieStatsArr[i];
                        probabilities[i] = Math.Max(0.0, inst._targetSizeReal - (double)inst._targetSizeInt);
                    }

                    // Use a built in class for choosing an item based on a list of relative probabilities.
                    RouletteWheelLayout rwl = new RouletteWheelLayout(probabilities);

                    // Probabilistically assign the required number of additional allocations.
                    // ENHANCEMENT: We can improve the allocation fairness by updating the RouletteWheelLayout
                    // after each allocation (to reflect that allocation).
                    // targetSizeDeltaInt is negative, so flip the sign for code clarity.
                    targetSizeDeltaInt *= -1;
                    for (int i = 0; i < targetSizeDeltaInt; i++)
                    {
                        int specieIdx = RouletteWheel.SingleThrow(rwl, _rng);
                        specieStatsArr[specieIdx]._targetSizeInt++;
                    }
                }
            }
            else if (targetSizeDeltaInt > 0)
            {
                // We have overshot the required populationSize. Adjust target sizes down to compensate.
                // Determine each specie's relative probability of target size downward adjustment.
                double[] probabilities = new double[specieCount];
                for (int i = 0; i < specieCount; i++)
                {
                    SpecieStats inst = specieStatsArr[i];
                    probabilities[i] = Math.Max(0.0, (double)inst._targetSizeInt - inst._targetSizeReal);
                }

                // Use a built in class for choosing an item based on a list of relative probabilities.
                RouletteWheelLayout rwl = new RouletteWheelLayout(probabilities);

                // Probabilistically decrement specie target sizes.
                // ENHANCEMENT: We can improve the selection fairness by updating the RouletteWheelLayout
                // after each decrement (to reflect that decrement).
                for (int i = 0; i < targetSizeDeltaInt;)
                {
                    int specieIdx = RouletteWheel.SingleThrow(rwl, _rng);

                    // Skip empty species. This can happen because the same species can be selected more than once.
                    if (0 != specieStatsArr[specieIdx]._targetSizeInt)
                    {
                        specieStatsArr[specieIdx]._targetSizeInt--;
                        i++;
                    }
                }
            }

            // We now have Sum(_targetSizeInt) == _populationSize.
            Debug.Assert(SumTargetSizeInt(specieStatsArr) == _populationSize);

            // TODO: Better way of ensuring champ species has non-zero target size?
            // However we need to check that the specie with the best genome has a non-zero targetSizeInt in order
            // to ensure that the best genome is preserved. A zero size may have been allocated in some pathological cases.
            if (0 == specieStatsArr[_bestSpecieIdx]._targetSizeInt)
            {
                specieStatsArr[_bestSpecieIdx]._targetSizeInt++;

                // Adjust down the target size of one of the other species to compensate.
                // Pick a specie at random (but not the champ specie). Note that this may result in a specie with a zero
                // target size, this is OK at this stage. We handle allocations of zero in PerformOneGeneration().
                int idx = RouletteWheel.SingleThrowEven(specieCount - 1, _rng);
                idx = idx == _bestSpecieIdx ? idx + 1 : idx;

                if (specieStatsArr[idx]._targetSizeInt > 0)
                {
                    specieStatsArr[idx]._targetSizeInt--;
                }
                else
                {   // Scan forward from this specie to find a suitable one.
                    bool done = false;
                    idx++;
                    for (; idx < specieCount; idx++)
                    {
                        if (idx != _bestSpecieIdx && specieStatsArr[idx]._targetSizeInt > 0)
                        {
                            specieStatsArr[idx]._targetSizeInt--;
                            done = true;
                            break;
                        }
                    }

                    // Scan forward from start of species list.
                    if (!done)
                    {
                        for (int i = 0; i < specieCount; i++)
                        {
                            if (i != _bestSpecieIdx && specieStatsArr[i]._targetSizeInt > 0)
                            {
                                specieStatsArr[i]._targetSizeInt--;
                                done = true;
                                break;
                            }
                        }
                        if (!done)
                        {
                            throw new SharpNeatException("CalcSpecieStats(). Error adjusting target population size down. Is the population size less than or equal to the number of species?");
                        }
                    }
                }
            }

            // Now determine the eliteSize for each specie. This is the number of genomes that will remain in a
            // specie from the current generation and is a proportion of the specie's current size.
            // Also here we calculate the total number of offspring that will need to be generated.
            offspringCount = 0;
            for (int i = 0; i < specieCount; i++)
            {
                // Special case - zero target size.
                if (0 == specieStatsArr[i]._targetSizeInt)
                {
                    specieStatsArr[i]._eliteSizeInt = 0;
                    continue;
                }

                // Discretize the real size with a probabilistic handling of the fractional part.
                double eliteSizeReal = _specieList[i].GenomeList.Count * _eaParams.ElitismProportion;
                int    eliteSizeInt  = (int)Utilities.ProbabilisticRound(eliteSizeReal, _rng);

                // Ensure eliteSizeInt is no larger than the current target size (remember it was calculated
                // against the current size of the specie not its new target size).
                SpecieStats inst = specieStatsArr[i];
                inst._eliteSizeInt = Math.Min(eliteSizeInt, inst._targetSizeInt);

                // Ensure the champ specie preserves the champ genome. We do this even if the targetsize is just 1
                // - which means the champ genome will remain and no offspring will be produced from it, apart from
                // the (usually small) chance of a cross-species mating.
                if (i == _bestSpecieIdx && inst._eliteSizeInt == 0)
                {
                    Debug.Assert(inst._targetSizeInt != 0, "Zero target size assigned to champ specie.");
                    inst._eliteSizeInt = 1;
                }

                // Now we can determine how many offspring to produce for the specie.
                inst._offspringCount = inst._targetSizeInt - inst._eliteSizeInt;
                offspringCount      += inst._offspringCount;

                // While we're here we determine the split between asexual and sexual reproduction. Again using
                // some probabilistic logic to compensate for any rounding bias.
                double offspringAsexualCountReal = (double)inst._offspringCount * _eaParams.OffspringAsexualProportion;
                inst._offspringAsexualCount = (int)Utilities.ProbabilisticRound(offspringAsexualCountReal, _rng);
                inst._offspringSexualCount  = inst._offspringCount - inst._offspringAsexualCount;

                // Also while we're here we calculate the selectionSize. The number of the specie's fittest genomes
                // that are selected from to create offspring. This should always be at least 1.
                double selectionSizeReal = _specieList[i].GenomeList.Count * _eaParams.SelectionProportion;
                inst._selectionSizeInt = Math.Max(1, (int)Utilities.ProbabilisticRound(selectionSizeReal, _rng));
            }

            return(specieStatsArr);
        }
Example #12
0
        private void produceOffspring()
        {
            double[] offspringCount = new double[LEEAParams.SPECIESCOUNT];

            // generate species-specific genome lists
            List <QEAGenome>[] speciesGenomes = new List <QEAGenome> [LEEAParams.SPECIESCOUNT];
            for (int i = 0; i < LEEAParams.SPECIESCOUNT; i++)
            {
                speciesGenomes[i] = new List <QEAGenome>();
            }

            for (int i = 0; i < genomeList.Count; i++)
            {
                speciesGenomes[genomeList[i].Species].Add(genomeList[i]);
            }

            // determine offspring count for each species
            if (LEEAParams.SPECIESCOUNT == 1)
            {
                offspringCount[0] = LEEAParams.POPSIZE;
            }
            else
            {
                double[] specieFitness = new double[LEEAParams.SPECIESCOUNT];
                // calculate species stats to determine how many offspring each species is granted

                for (int i = 0; i < LEEAParams.SPECIESCOUNT; i++)
                {
                    for (int j = 0; j < speciesGenomes[i].Count; j++)
                    {
                        specieFitness[i] += speciesGenomes[i][j].Fitness;
                    }

                    if (speciesGenomes[i].Count != 0)
                    {
                        specieFitness[i] /= speciesGenomes[i].Count;
                    }
                }


                for (int i = 0; i < LEEAParams.SPECIESCOUNT; i++)
                {
                    offspringCount[i] = Math.Round(LEEAParams.POPSIZE * specieFitness[i] / specieFitness.Sum());
                }

                // rounding error could leave us a few short or long of the pop size, trim or fill to reach population size
                RouletteWheelLayout rwl = new RouletteWheelLayout(offspringCount);
                while (offspringCount.Sum() > LEEAParams.POPSIZE)
                {
                    int index = RouletteWheel.SingleThrow(rwl, r);
                    if (offspringCount[index] > 1)
                    {
                        offspringCount[index]--;
                    }
                }

                while (offspringCount.Sum() < LEEAParams.POPSIZE)
                {
                    int index = RouletteWheel.SingleThrow(rwl, r);
                    offspringCount[index]++;
                }
            }

            List <QEAGenome> newGeneration = new List <QEAGenome>();

            // generate offspring for each species
            // parallelism doesn't work if speciescount = 1 here!

            for (int i = 0; i < LEEAParams.SPECIESCOUNT; i++)
            {
                if (offspringCount[i] > 0)
                {
                    // sort the genome list by fitness
                    Comparison <QEAGenome> comparison = (x, y) => y.Fitness.CompareTo(x.Fitness);
                    speciesGenomes[i].Sort(comparison);

                    // determine the top X individuals that we will select from
                    int selectionNumber = (int)(speciesGenomes[i].Count * LEEAParams.SELECTIONPROPORTION);
                    if (selectionNumber == 0)
                    {
                        selectionNumber = 1;
                    }

                    // build list of probabilities based on fitness
                    double[] probabilities = new double[selectionNumber];

                    for (int j = 0; j < probabilities.Length; j++)
                    {
                        probabilities[j] = speciesGenomes[i][j].Fitness;
                    }

                    RouletteWheelLayout rw = new RouletteWheelLayout(probabilities);

                    // build a list of matings to be performed.  This must be done outside of the parallelized section.
                    int[][] matings = new int[(int)offspringCount[i]][];
                    for (int j = 0; j < matings.Length; j++)
                    {
                        matings[j] = new int[2];

                        // select main parent
                        int index = RouletteWheel.SingleThrow(rw, r);

                        if (r.NextDouble() < LEEAParams.SEXPROPORTION && probabilities.Length > 1) // can't have sexual reproduction if this species only has a single member
                        {
                            matings[j][0] = index;

                            int parent2 = index;
                            while (parent2 == index)
                            {
                                parent2 = RouletteWheel.SingleThrow(rw, r);
                            }
                            matings[j][1] = parent2;
                        }
                        else
                        {
                            matings[j][0] = index;
                            matings[j][1] = int.MinValue;
                        }
                    }

                    Parallel.For(0, matings.Length, po, j =>
                                 //for (int j = 0; j < matings.Length; j++)
                    {
                        // mutate
                        QEAGenome child;
                        if (matings[j][1] > int.MinValue)
                        {
                            // sexual reproduction
                            child         = speciesGenomes[i][matings[j][0]].createOffspring(speciesGenomes[i][matings[j][1]]);
                            child.Fitness = (speciesGenomes[i][matings[j][0]].Fitness + speciesGenomes[i][matings[j][1]].Fitness) / 2;
                        }
                        else
                        {
                            child         = speciesGenomes[i][matings[j][0]].createOffspring();
                            child.Fitness = speciesGenomes[i][matings[j][0]].Fitness;
                        }

                        lock (newGeneration)
                        {
                            newGeneration.Add(child);
                        }
                    });
                }
            }

            // encourage the garbage collector to free up some memory
            foreach (QEAGenome g in genomeList)
            {
                g.weights = null;
            }
            genomeList = null;


            genomeList = newGeneration;
        }
 /// <summary>
 /// Initialize the list. Call this after all items have been aded to the list. This
 /// creates a RouletteWheelLayout based upon the activation probability of each item
 /// in the list.
 /// </summary>
 public void Initialize()
 {
     _rouletteWheelLayout = CreateRouletteWheelLayout();
 }