コード例 #1
0
        public void InitializeEvolution(Color[,] sourceIndividual)
        {
            if (AlgorithmInformation.Population < 4)
            {
                AlgorithmInformation.Population = 4;
            }

            if (AlgorithmInformation.Elite < 2)
            {
                AlgorithmInformation.Elite = 2;
            }

            _threads             = new List <Thread>();
            _populationForThread = new int[AlgorithmInformation.GetOptimalThreadAmount()];

            _populationIndividuals = new Individual[AlgorithmInformation.Population];
            _populationToPass      = new Individual[AlgorithmInformation.Population];
            _eliteIndividuals      = new Individual[AlgorithmInformation.Elite];

            _destinationIndividual = sourceIndividual;

            _evolutionFitness = new EvolutionFitness(AlgorithmInformation.ImageWidth, AlgorithmInformation.ImageHeight);

            Individual individual;

            for (int i = 0; i < AlgorithmInformation.Population; i++)
            {
                individual = new Individual();
                individual.Initialize();

                _populationIndividuals[i] = individual;
            }
        }
コード例 #2
0
        public void SoftMutation()
        {
            var point = RandomMutation.RandomIntervalIntegerInclusive(0, 1);

            switch (point)
            {
            case 0:
                PositionX = RandomMutation.RandomPosition(PositionX - AlgorithmInformation.SmallDeltaValue(), PositionX + AlgorithmInformation.SmallDeltaValue());
                break;

            case 1:
                PositionY = RandomMutation.RandomPosition(PositionY - AlgorithmInformation.SmallDeltaValue(), PositionY + AlgorithmInformation.SmallDeltaValue());
                break;
            }

            FixPosition();
        }
コード例 #3
0
        public void SoftMutation()
        {
            var colourMutation = RandomMutation.RandomIntervalIntegerInclusive(0, 4);

            switch (colourMutation)
            {
            case 0:
                RedColour = RandomMutation.RandomColour((byte)(RedColour - AlgorithmInformation.SmallDeltaValue()), (byte)(RedColour + AlgorithmInformation.SmallDeltaValue()));
                break;

            case 1:
                GreenColour = RandomMutation.RandomColour((byte)(GreenColour - AlgorithmInformation.SmallDeltaValue()), (byte)(GreenColour + AlgorithmInformation.SmallDeltaValue()));
                break;

            case 2:
                BlueColour = RandomMutation.RandomColour((byte)(BlueColour - AlgorithmInformation.SmallDeltaValue()), (byte)(BlueColour + AlgorithmInformation.SmallDeltaValue()));
                break;

            case 3:
                AlphaColour = RandomMutation.RandomColour((byte)(BlueColour - AlgorithmInformation.SmallDeltaValue()), (byte)(BlueColour + AlgorithmInformation.SmallDeltaValue()));
                break;
            }
        }
コード例 #4
0
        override public Individual Generate()
        {
            _generation++;

            if (AlgorithmInformation.DynamicMutation && (_generation % 100 == 0))
            {
                AlgorithmInformation.MutationChance -= 1;
            }

            var oneThread = AlgorithmInformation.Population / AlgorithmInformation.GetOptimalThreadAmount();

            if (oneThread * AlgorithmInformation.GetOptimalThreadAmount() != AlgorithmInformation.Population)
            {
                int delta = AlgorithmInformation.Population - (oneThread * (AlgorithmInformation.GetOptimalThreadAmount() - 1));

                for (int j = 0; j < AlgorithmInformation.GetOptimalThreadAmount() - 1; j++)
                {
                    _populationForThread[j] = oneThread;
                }

                _populationForThread[AlgorithmInformation.GetOptimalThreadAmount() - 1] = delta;
            }
            else
            {
                for (int j = 0; j < AlgorithmInformation.GetOptimalThreadAmount(); j++)
                {
                    _populationForThread[j] = oneThread;
                }
            }

            int from = 0;
            int to   = _populationForThread[0];

            for (int j = 0; j < AlgorithmInformation.GetOptimalThreadAmount(); j++)
            {
                int[] coords = { from, to };

                var th = new Thread(() =>
                {
                    ComparePopulation(coords[0], coords[1]);
                });
                this._threads.Add(th);

                if ((j + 1) < _populationForThread.Length)
                {
                    from += _populationForThread[j];
                    to   += _populationForThread[j + 1];
                }
            }

            foreach (Thread th in _threads)
            {
                th.Start();
            }

            foreach (Thread th in _threads)
            {
                th.Join();
            }



            _populationIndividuals = _populationIndividuals.OrderByDescending(i => i.Adaptation).ToArray();

            for (int j = 0; j < AlgorithmInformation.Population; j++)
            {
                _populationToPass[j] = _populationIndividuals[j].CloneIndividual();
            }

            for (int j = 0; j < AlgorithmInformation.Elite; j++)
            {
                _eliteIndividuals[j] = _populationIndividuals[j].CloneIndividual();
            }

            int mother = 0;
            int father = 1;

            for (int i = 0; i < AlgorithmInformation.Population; i++)
            {
                _populationIndividuals[i] = Reproduct(_eliteIndividuals[mother], _eliteIndividuals[father]);

                mother++;
                if (mother >= AlgorithmInformation.Elite)
                {
                    father++;
                    mother = 0;
                }

                if (father >= AlgorithmInformation.Elite)
                {
                    father = 0;
                }
            }

            AlgorithmInformation.KilledChilds += AlgorithmInformation.Population - AlgorithmInformation.Elite;

            OnIndividualCreated(_eliteIndividuals[0]);

            _threads.Clear();

            return(_eliteIndividuals[0]);
        }