/*Initial data are represented as a table of normalized data which was stored in order:
         * image number, filter number, fitness, parameters.*/
        //for 2 chromosome
        public Population RandomInitialise(int PopulationQuantity, double[,] initialData, int rowsNumder, int columnsNumber)
        {
            Population        popolation = new Population(PopulationQuantity);
            Random            rnd        = new Random();
            ChromosomeTypeOne ch1;
            ChromosomeTypeTwo ch2;

            Chromosome[] ch = new Chromosome[2];
            double       first;
            double       second;
            int          counter = 2;

            for (int i = 0; i < PopulationQuantity; i++)
            {
                ch2     = new ChromosomeTypeTwo(2);
                ch1     = new ChromosomeTypeOne(columnsNumber - 3);
                counter = (int)(rnd.NextDouble() * (double)(columnsNumber / 2));
                for (int k = 0; k < counter; k++)
                {
                    first  = rnd.NextDouble();
                    second = first + (1 - first) * rnd.NextDouble();
                    ch1.ChangeChromosomeGene((int)(rnd.NextDouble() * (double)ch1.GenesNumber), first, second);
                }
                ch2.ChangeChromosomeGene(0, (int)(rnd.NextDouble() * (double)324));
                ch[0] = ch1;
                ch[1] = ch2;
                Individual ind = new Individual(columnsNumber - 3, 2);
                ind.SetIndividualContent(ch);
                popolation.AddIndividual(ind);
            }
            return(popolation);
        }
        public Population MutationFromInitialData(Population population, double[,] initialData, int rowsNumber)
        {
            Random     rnd   = new Random();
            int        indx  = (int)(rnd.NextDouble() * (double)population.PopulationQuantity);
            Individual indiv = population.GetIndividualByIndex(indx);

            Chromosome[]      ch  = indiv.GetIndividualContent();
            ChromosomeTypeOne chr = (ChromosomeTypeOne)ch[0];
            int colum             = (int)(rnd.NextDouble() * (double)chr.GenesNumber);
            int firstRow          = (int)(rnd.NextDouble() * (double)rowsNumber);
            int secondRow         = (int)(rnd.NextDouble() * (double)rowsNumber);
            int k = 0;

            while (initialData[firstRow, colum + 3] == initialData[secondRow, colum + 3] && (k < 100))
            {
                secondRow = (int)(rnd.NextDouble() * (double)rowsNumber);
                k++;
            }
            double first  = initialData[firstRow, colum + 3];
            double second = initialData[secondRow, colum + 3];

            if (first < second)
            {
                chr.ChangeChromosomeGene(colum, first, second);
            }
            else
            {
                chr.ChangeChromosomeGene(colum, second, first);
            }
            indiv.SetIndividualContent(ch);
            population.SetIndividualByIndex(indx, indiv);
            return(population);
        }
 public Individual(double[,] parameters, int parametersNumber, int[] parametersTwo, int parametersNumberTwo)
 {
     chromosomesNumber = 2;
     chromosomes       = new Chromosome[chromosomesNumber];
     chromosomes[0]    = new ChromosomeTypeOne(parametersNumber, parameters);
     chromosomes[1]    = new ChromosomeTypeTwo(parametersNumberTwo, parametersTwo);
 }
        public Population InitialiseFromInitialData(int PopulationQuantity, double[,] initialData, int rowsNumber, int columnsNumber, double percentOfChangeGene)
        {
            Population        popolation = new Population(PopulationQuantity);
            Random            rnd        = new Random();
            ChromosomeTypeOne ch1;
            ChromosomeTypeTwo ch2;

            Chromosome[] ch = new Chromosome[2];
            double       first;
            double       second;
            int          counter = 2;
            int          colum;
            int          firstRow  = 0;
            int          secondRow = 0;
            int          count;

            for (int i = 0; i < PopulationQuantity; i++)
            {
                ch2 = new ChromosomeTypeTwo(1);
                ch1 = new ChromosomeTypeOne(columnsNumber - 3);
                //for half of all genes
                counter   = (int)(rnd.NextDouble() * (double)(columnsNumber * percentOfChangeGene));
                firstRow  = (int)(rnd.NextDouble() * (double)rowsNumber);
                secondRow = (int)(rnd.NextDouble() * (double)rowsNumber);
                for (int k = 0; k < counter; k++)
                {
                    colum = (int)(rnd.NextDouble() * (double)ch1.GenesNumber);
                    count = 0;

                    /*while (initialData[firstRow, colum + 3] == initialData[secondRow, colum + 3] && (count < 100))
                     * {
                     *  secondRow = (int)(rnd.NextDouble() * (double)rowsNumber);
                     *  count++;
                     * }*/
                    first  = initialData[firstRow, colum + 3];
                    second = initialData[secondRow, colum + 3];
                    if (first < second)
                    {
                        ch1.ChangeChromosomeGene(colum, first, second);
                    }
                    else
                    {
                        ch1.ChangeChromosomeGene(colum, second, first);
                    }
                }
                ch2.ChangeChromosomeGene(0, (int)initialData[firstRow, 1]);
                ch[0] = ch1;
                ch[1] = ch2;
                Individual ind = new Individual(columnsNumber - 3, 2);
                ind.SetIndividualContent(ch);
                popolation.AddIndividual(ind);
            }
            return(popolation);
        }
        public Population MutationRandom(Population population)
        {
            Random     rnd   = new Random();
            int        indx  = (int)(rnd.NextDouble() * (double)population.PopulationQuantity);
            Individual indiv = population.GetIndividualByIndex(indx);

            Chromosome[]      ch     = indiv.GetIndividualContent();
            ChromosomeTypeOne chr    = (ChromosomeTypeOne)ch[0];
            double            first  = rnd.NextDouble();
            double            second = first + (1 - first) * rnd.NextDouble();

            chr.ChangeChromosomeGene((int)(rnd.NextDouble() * (double)chr.GenesNumber), first, second);
            indiv.SetIndividualContent(ch);
            population.SetIndividualByIndex(indx, indiv);
            return(population);
        }
        public Individual(Individual ind)
        {
            int len1 = ((ChromosomeTypeOne)ind.chromosomes[0]).GenesNumber;
            int len2 = ((ChromosomeTypeTwo)ind.chromosomes[1]).GenesNumber;

            double[,] CH1 = ((ChromosomeTypeOne)ind.chromosomes[0]).ChromosomeContent;
            int[] CH2 = ((ChromosomeTypeTwo)ind.chromosomes[1]).ChromosomeContent;
            double[,] ch1 = new double[len1, 2];
            int[] ch2 = new int[len2];
            for (int i = 0; i < len1; i++)
            {
                ch1[i, 0] = CH1[i, 0];
                ch1[i, 1] = CH1[i, 1];
            }
            for (int i = 0; i < len2; i++)
            {
                ch2[i] = CH2[i];
            }
            chromosomes       = new Chromosome[2];
            chromosomes[0]    = new ChromosomeTypeOne(((ChromosomeTypeOne)ind.chromosomes[0]).GenesNumber, ch1);
            chromosomes[1]    = new ChromosomeTypeTwo(((ChromosomeTypeTwo)ind.chromosomes[1]).GenesNumber, ch2);
            chromosomesNumber = ind.ChromosomesNumber;
        }
 public Individual(double[,] parameters, int parametersNumber)
 {
     chromosomesNumber = 1;
     chromosomes       = new Chromosome[chromosomesNumber];
     chromosomes[0]    = new ChromosomeTypeOne(parametersNumber, parameters);
 }
 public Individual(int ParametersNumber, int ChromosomesNumber)
 {
     chromosomesNumber = ChromosomesNumber;
     chromosomes       = new Chromosome[chromosomesNumber];
     chromosomes[0]    = new ChromosomeTypeOne(ParametersNumber);
 }