/// <summary>
        /// Construct a neural genetic algorithm.
        /// </summary>
        ///
        /// <param name="network">The network to base this on.</param>
        /// <param name="randomizer">The randomizer used to create this initial population.</param>
        /// <param name="calculateScore">The score calculation object.</param>
        /// <param name="populationSize">The population size.</param>
        /// <param name="mutationPercent">The percent of offspring to mutate.</param>
        /// <param name="percentToMate">The percent of the population allowed to mate.</param>
        public NeuralGeneticAlgorithm(BasicNetwork network,
                                      IRandomizer randomizer, ICalculateScore calculateScore,
                                      int populationSize, double mutationPercent,
                                      double percentToMate) : base(TrainingImplementationType.Iterative)
        {
            Genetic = new NeuralGeneticAlgorithmHelper
            {
                CalculateScore = new GeneticScoreAdapter(calculateScore)
            };
            IPopulation population = new BasicPopulation(populationSize);

            Genetic.MutationPercent  = mutationPercent;
            Genetic.MatingPopulation = percentToMate * 2;
            Genetic.PercentToMate    = percentToMate;
            Genetic.Crossover        = new Splice(network.Structure.CalculateSize() / 3);
            Genetic.Mutate           = new MutatePerturb(4.0d);
            Genetic.Population       = population;
            for (int i = 0; i < population.PopulationSize; i++)
            {
                var chromosomeNetwork = (BasicNetwork)(network
                                                       .Clone());
                randomizer.Randomize(chromosomeNetwork);

                var genome = new NeuralGenome(chromosomeNetwork)
                {
                    GA = Genetic
                };
                Genetic.PerformCalculateScore(genome);
                Genetic.Population.Add(genome);
            }
            population.Sort();
        }
        /// <summary>
        /// Construct a neural genetic algorithm.
        /// </summary>
        ///
        /// <param name="network">The network to base this on.</param>
        /// <param name="randomizer">The randomizer used to create this initial population.</param>
        /// <param name="calculateScore">The score calculation object.</param>
        /// <param name="populationSize">The population size.</param>
        /// <param name="mutationPercent">The percent of offspring to mutate.</param>
        /// <param name="percentToMate">The percent of the population allowed to mate.</param>
        public NeuralGeneticAlgorithm(BasicNetwork network,
                                      IRandomizer randomizer, ICalculateScore calculateScore,
                                      int populationSize, double mutationPercent,
                                      double percentToMate) : base(TrainingImplementationType.Iterative)
        {
            Genetic = new NeuralGeneticAlgorithmHelper
                           {
                               CalculateScore = new GeneticScoreAdapter(calculateScore)
                           };
            IPopulation population = new BasicPopulation(populationSize);
            Genetic.MutationPercent = mutationPercent;
            Genetic.MatingPopulation = percentToMate*2;
            Genetic.PercentToMate = percentToMate;
            Genetic.Crossover = new Splice(network.Structure.CalculateSize()/3);
            Genetic.Mutate = new MutatePerturb(4.0d);
            Genetic.Population = population;
            for (int i = 0; i < population.PopulationSize; i++)
            {
                var chromosomeNetwork = (BasicNetwork) (network
                                                           .Clone());
                randomizer.Randomize(chromosomeNetwork);

                var genome = new NeuralGenome(chromosomeNetwork) {GA = Genetic};
                Genetic.PerformCalculateScore(genome);
                Genetic.Population.Add(genome);
            }
            population.Sort();
        }
Пример #3
0
        /// <summary>
        /// Construct a neural genetic algorithm.
        /// </summary>
        /// <param name="network">The network to base this on.</param>
        /// <param name="randomizer">The randomizer used to create this initial population.</param>
        /// <param name="calculateScore">The score calculation object.</param>
        /// <param name="populationSize">The population size.</param>
        /// <param name="mutationPercent">The percent of offspring to mutate.</param>
        /// <param name="percentToMate">The percent of the population allowed to mate.</param>
        public NeuralGeneticAlgorithm(BasicNetwork network,
                                      IRandomizer randomizer,
                                      ICalculateScore calculateScore,
                                      int populationSize, double mutationPercent,
                                      double percentToMate)
        {
            this.Helper = new NeuralGeneticAlgorithmHelper();
            this.Helper.CalculateScore = new GeneticScoreAdapter(calculateScore);
            IPopulation population = new BasicPopulation(populationSize);

            Helper.MutationPercent  = mutationPercent;
            Helper.MatingPopulation = (percentToMate * 2);
            Helper.PercentToMate    = (percentToMate);
            Helper.Crossover        = (new Splice(network.Structure.CalculateSize() / 3));
            Helper.Mutate           = (new MutatePerturb(4.0));
            Helper.Population       = (population);
            for (int i = 0; i < population.PopulationSize; i++)
            {
                BasicNetwork chromosomeNetwork = (BasicNetwork)network
                                                 .Clone();
                randomizer.Randomize(chromosomeNetwork);

                NeuralGenome genome =
                    new NeuralGenome(this, chromosomeNetwork);
                Helper.PerformScoreCalculation(genome);
                Helper.Population.Add(genome);
            }
            population.Sort();
        }
        /// <summary>
        /// Create a new NNThreadSafeNetworkPool
        /// </summary>
        /// <param name="baseNetwork">The network to use for this object</param>
        /// <param name="listStartLength">The default length to start the network list at.</param>
        public NNThreadSafeNetworkPool(BasicNetwork baseNetwork, string networkConfigStr, int listStartLength)
        {
            this.baseNetwork      = (BasicNetwork)baseNetwork.Clone();
            this.networkConfigStr = networkConfigStr;

            for (int i = 0; i < listStartLength; i++)
            {
                threadSafeNetworksList.Add(new ThreadSafeNetwork((BasicNetwork)baseNetwork.Clone()));
            }

            //Initialise idleNetworksBitFlags
            for (int i = 0; i < listStartLength; i++)
            {
                idleNetworksBitFlags = idleNetworksBitFlags | (1 << i);
            }
        }
Пример #5
0
        /// <summary>
        /// Update the velocity, position and personal
        /// best position of a particle.
        /// </summary>
        /// <param name="particleIndex">index of the particle in the swarm</param>
        /// <param name="init">if true, the position and velocity
        ///                          will be initialised. </param>
        public void UpdateParticle(int particleIndex, bool init)
        {
            int i = particleIndex;

            double[] particlePosition = null;
            if (init)
            {
                // Create a new particle with random values.
                // Except the first particle which has the same values
                // as the network passed to the algorithm.
                if (m_networks[i] == null)
                {
                    m_networks[i] = (BasicNetwork)m_bestNetwork.Clone();
                    if (i > 0)
                    {
                        m_randomizer.Randomize(m_networks[i]);
                    }
                }
                particlePosition = GetNetworkState(i);
                m_bestVectors[i] = particlePosition;

                // randomise the velocity
                m_va.Randomise(m_velocities[i], m_maxVelocity);
            }
            else
            {
                particlePosition = GetNetworkState(i);
                UpdateVelocity(i, particlePosition);

                // velocity clamping
                m_va.ClampComponents(m_velocities[i], m_maxVelocity);

                // new position (Xt = Xt-1 + Vt)
                m_va.Add(particlePosition, m_velocities[i]);

                // pin the particle against the boundary of the search space.
                // (only for the components exceeding maxPosition)
                m_va.ClampComponents(particlePosition, m_maxPosition);

                SetNetworkState(i, particlePosition);
            }
            UpdatePersonalBestPosition(i, particlePosition);
        }
        /// <summary>
        /// Get a prediction from the network pool using the provided inputs
        /// </summary>
        /// <param name="inputs"></param>
        /// <returns></returns>
        public double[] getNetworkPrediction(double[] inputs)
        {
            int networkInstance = DetermineAvailableInstanceIndex();

            if (networkInstance == -1)
            {
                lock (baseLocker)
                {
                    threadSafeNetworksList.Add(new ThreadSafeNetwork((BasicNetwork)baseNetwork.Clone()));
                    networkInstance = threadSafeNetworksList.Count - 1;
                }
            }

            double[] outputs = threadSafeNetworksList[networkInstance].getNetworkPrediction(inputs);

            //Now set instance available again
            lock (baseLocker)
                idleNetworksBitFlags = idleNetworksBitFlags | (1 << networkInstance);

            //Get next available network in list
            //If one is not avaialble add one to the list and then use that
            return(outputs);
        }