private List <Individual> CloneList(List <Individual> template)
        {
            List <Individual> clonedList = new List <Individual>();

            foreach (Individual individual in template)
            {
                clonedList.Add(Individual.CloneIndividual(individual));
            }

            return(clonedList);
        }
示例#2
0
        private Individual MutateIndividual(Individual individual)
        {
            Individual clone  = Individual.CloneIndividual(individual);
            double     chance = random.NextDouble();

            for (int i = 0; i < individual.Movements.Count; i++)
            {
                double value = random.NextDouble();
                if (value > chance)
                {
                    clone.Movements[i] = clone.Movements[i] == 0 ? 1 : 0;
                }
            }

            return(clone);
        }
        public List <Individual> GenerateDescendants(Individual mother, Individual father)
        {
            Individual motherClone = Individual.CloneIndividual(mother);
            Individual fatherClone = Individual.CloneIndividual(father);

            double     value = random.NextDouble();
            Individual mutatedMother = null, mutatedFather = null;
            int        counter = 0;

            for (int i = 0; i < 5; i++)
            {
                mutatedMother = MutateIndividual(motherClone);
                if (StaticOperations.ValidateIndividual(mutatedMother))
                {
                    break;
                }
                counter++;
            }
            if (counter == 5)
            {
                mutatedMother = mother;
            }

            counter = 0;
            for (int i = 0; i < 5; i++)
            {
                mutatedFather = MutateIndividual(fatherClone);
                if (StaticOperations.ValidateIndividual(mutatedFather))
                {
                    break;
                }
                counter++;
            }
            if (counter == 5)
            {
                mutatedFather = father;
            }

            List <Individual> crossedOvers = CrossOverIndividuals(mutatedMother, mutatedFather);

            return(crossedOvers);
        }
        private void ShowDataImages(object sender, IndividualEventArgs e)
        {
            Dispatcher.Invoke(delegate
            {
                if (_tmpIndividual == null || _originalBitmap == null)
                {
                    return;
                }

                lock (_tmpIndividual)
                {
                    if (_bestOfAllIndividual == null)
                    {
                        _bestOfAllIndividual = _tmpIndividual.CloneIndividual();
                        _newBestIndividual   = true;
                    }

                    if (_bestOfAllIndividual.Adaptation < _tmpIndividual.Adaptation)
                    {
                        _bestOfAllIndividual = _tmpIndividual.CloneIndividual();
                        _newBestIndividual   = true;

                        dataChartList.Add(new ChartKeyValue()
                        {
                            Key = _bestOfAllIndividual.Generation, Value = _bestOfAllIndividual.Adaptation
                        });
                    }

                    _topGenerationIndividual = _tmpIndividual.CloneIndividual();
                }

                using (var bit = new Bitmap(_originalBitmap.Width, _originalBitmap.Height))
                {
                    this.actualGeneticImage.Source = null;
                    Graphics graphics      = Graphics.FromImage(bit);
                    graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                    ImageRenderer.DrawImage(_topGenerationIndividual, graphics);
                    bit.Save("topGenerationIndividual.png", ImageFormat.Png);

                    if (_newBestIndividual == true)
                    {
                        this.bestGeneticImage.Source = null;
                        bit.Save("topIndividual.png", ImageFormat.Png);
                    }
                }

                var bitmap = new BitmapImage();
                var stream = File.OpenRead("topGenerationIndividual.png");

                bitmap.BeginInit();
                bitmap.CacheOption  = BitmapCacheOption.OnLoad;
                bitmap.StreamSource = stream;
                bitmap.EndInit();
                stream.Close();
                stream.Dispose();

                _generation          = _topGenerationIndividual.Generation;
                _killedChilds        = AlgorithmInformation.KilledChilds;
                this._bestFitness    = Math.Round(_bestOfAllIndividual.Adaptation, 2);
                this._currentFitness = Math.Round(_topGenerationIndividual.Adaptation, 2);
                this._mutationChance = AlgorithmInformation.MutationChance;
                this._mutationType   = (int)AlgorithmInformation.MutationType;


                this.actualGeneticImage.Source   = bitmap;
                this.generation.Content          = "Generation: " + _topGenerationIndividual.Generation.ToString();
                this.currentFitness.Content      = "Current fitness: " + Math.Round(_topGenerationIndividual.Adaptation, 2).ToString() + "%";
                this.populationLabel.Content     = "Population: " + AlgorithmInformation.Population;
                this.eliteLabel.Content          = "Elite: " + AlgorithmInformation.Elite;
                this.mutationTypeLabel.Content   = "Mutation type: " + AlgorithmInformation.MutationType.ToString();
                this.mutationChanceLabel.Content = "Mutation chance: " + AlgorithmInformation.MutationChance.ToString() + "%";
                this.killedChildsLabel.Content   = "Killed childs: " + AlgorithmInformation.KilledChilds;

                _shapesAmount = (int)this.ShapesAmountSlider.Value;

                if (this.mutationDynamicRadio.IsChecked ?? false)
                {
                    this.dinamicallyMutationLabel.Content = "Dynamic mutation: YES";
                    this._dunamicMutation = 1;
                }
                else
                {
                    this.dinamicallyMutationLabel.Content = "Dynamic mutation: NO";
                    this._dunamicMutation = 0;
                }

                if (this.twoParentRadio.IsChecked ?? false)
                {
                    this.reproductionTypeLabel.Content = "Reproduction: Two-parents";
                }
                else
                {
                    this.reproductionTypeLabel.Content = "Reproduction: Single-parent";
                }


                if (_newBestIndividual == true)
                {
                    var bitmap1 = new BitmapImage();
                    var stream1 = File.OpenRead("topIndividual.png");

                    bitmap1.BeginInit();
                    bitmap1.CacheOption  = BitmapCacheOption.OnLoad;
                    bitmap1.StreamSource = stream1;
                    bitmap1.EndInit();
                    stream1.Close();
                    stream1.Dispose();

                    this.bestGeneticImage.Source = bitmap1;
                    this.bestFitness.Content     = "Best fitness: " + Math.Round(_bestOfAllIndividual.Adaptation, 2).ToString() + "%";



                    _newBestIndividual = false;
                }
            });
        }