Esempio n. 1
0
        /// <summary>
        /// Fitness Function
        /// </summary>
        /// <returns>Between 0 and 1 with 1 being being most fit</returns>
        private double CalculateFitness(Chromosome chromosome)
        {
            int[] arr = new int[25];
            int   i   = 0;

            foreach (Gene gene in chromosome)
            {
                arr[i] = (int)gene.ObjectValue;
                i++;
            }

            double fitnessValue = -1;

            if (chromosome != null)
            {
                int fitness = ClickOMania.Game(arr, gameBoard.GetBoardAsArray());

                double adjustedFitness = (50 - fitness);        // fitness is how many squares are empty, not how many are left
                adjustedFitness = adjustedFitness * (1F / 50F); // fitness needs to be between 0 and one, so multiply by min/max

                fitnessValue = adjustedFitness;
            }
            else
            {
                //chromosome is null
                throw new ArgumentNullException("chromosome", "The specified Chromosome is null.");
            }

            return(fitnessValue);
        }
        private void genListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (genListBox.SelectedItem != null)
            {
                // Retrieve the data relating to the selected generation
                int genIndex = genListBox.SelectedIndex;
                Log.GenerationData selectedGen = log.ReadFull(genIndex);

                // Update the general info section
                generationValueLabel.Content = selectedGen.GenNum;
                wocFitnessValueLabel.Content = String.Format("{0:0.000}", selectedGen.WocFitness);
                maxFitnessValueLabel.Content = String.Format("{0:0.000}", selectedGen.MaxFitness);
                minFitnessValueLabel.Content = String.Format("{0:0.000}", selectedGen.MinFitness);

                // ---- Update the board viewer ----
                // Clear the image viewers
                WoCSlnImage.Source   = null;
                BestFitImage.Source  = null;
                LeastFitImage.Source = null;

                // Generate the images
                wocImgList = ClickOMania.GenerateImageList(selectedGen.WocSlnSolution, log.OriginalBoard.GetBoardAsArray());
                bstImgList = ClickOMania.GenerateImageList(selectedGen.MostFitSolution, log.OriginalBoard.GetBoardAsArray());
                lstImgList = ClickOMania.GenerateImageList(selectedGen.LeastFitSolution, log.OriginalBoard.GetBoardAsArray());

                // Reset the sliders to 0
                WoCSlnSlider.Value   = 0;
                BestFitSlider.Value  = 0;
                LeastFitSlider.Value = 0;

                // Update the slider max values
                WoCSlnSlider.Maximum   = wocImgList.Count - 1;
                BestFitSlider.Maximum  = bstImgList.Count - 1;
                LeastFitSlider.Maximum = lstImgList.Count - 1;

                // Set all sliders to their max value
                WoCSlnSlider.Value   = WoCSlnSlider.Maximum;
                BestFitSlider.Value  = BestFitSlider.Maximum;
                LeastFitSlider.Value = LeastFitSlider.Maximum;
            }
        }