Beispiel #1
0
        public Person[] GeneratePop(AMatrixWrapper wrapper, int capacity)
        {
            Person[] retPop = new Person[capacity];

            int dimension = wrapper.Matrix.GetLength(1);

            double[,] matrix = wrapper.Matrix;

            for (int iter = 0; iter < capacity; iter++)
            {
                int[]      retCode    = new int[dimension];
                List <int> LeftCities = new List <int>();
                for (int i = 0; i < dimension; i++)
                {
                    LeftCities.Add(i);
                }

                int startCity = mRand.Next(dimension);

                retCode[0] = startCity;
                LeftCities.Remove(startCity);

                for (int i = 0; i < dimension - 1; i++)   // Array-completing cycle.
                {
                    double sum = 0;
                    foreach (int j in LeftCities)
                    {
                        sum += matrix[retCode[i], j];
                    }

                    sum = sum * sum;
                    double[] roulette = new double[dimension - i];
                    roulette[0] = 0;
                    for (int j = 0; j < dimension - i - 1; j++)
                    {
                        roulette[j + 1] = roulette[j] + sum / (matrix[retCode[i], LeftCities[j]] * matrix[retCode[i], LeftCities[j]]);
                    }

                    int randPoint = mRand.Next((int)roulette[dimension - i - 1]);
                    for (int j = 1; j < dimension - i; j++)
                    {
                        if (randPoint <= roulette[j])
                        {
                            retCode[i + 1] = LeftCities[j - 1];
                            LeftCities.RemoveAt(j - 1);
                            break;
                        }
                    }
                }
                retPop[iter] = new Person(retCode);
            }
            return(retPop);
        }
Beispiel #2
0
        public Person[] Selection(Person[] persons, AMatrixWrapper mWrapper)
        {
            int resultPersonsSize = persons.Length / 2;

            Person[] resultPersons = new Person[resultPersonsSize];

            for (int i = 0; i < resultPersonsSize; i++)
            {
                if (mWrapper.FitnessFunction(persons[i]) < mWrapper.FitnessFunction(persons[i + resultPersonsSize]))
                {
                    resultPersons[i] = persons[i];
                }
                else
                {
                    resultPersons[i] = persons[i + resultPersonsSize];
                }
            }

            return(resultPersons);
        }
Beispiel #3
0
        public Person[] Selection(Person[] persons, AMatrixWrapper mWrapper)
        {
            Random rand = new Random();
            int    resultPersonsSize = persons.Length / 2;

            Person[] resultPersons = new Person[resultPersonsSize];

            for (int i = 0; i < resultPersonsSize; i++)
            {
                double[] strit = new double[persons.Length];
                double   sum   = 0;

                for (int j = 0; j < persons.Length; j++)
                {
                    strit[j] = mWrapper.FitnessFunction(persons[j]);
                    sum     += strit[j];
                }

                double[] revStrit = new double[persons.Length + 1];
                revStrit[0] = 0;
                for (int j = 0; j < persons.Length; j++)
                {
                    revStrit[j + 1] = (sum / strit[j]) * (sum / strit[j]) + revStrit[j];
                }

                int randPoint = rand.Next((int)revStrit[persons.Length]);
                for (int j = 0; j < persons.Length; j++)
                {
                    if (randPoint <= revStrit[j + 1])
                    {
                        resultPersons[i] = persons[j];
                        break;
                    }
                }
            }

            return(resultPersons);
        }
Beispiel #4
0
        public Person[] Selection(Person[] persons, AMatrixWrapper mWrapper)
        {
            Person[] retPersons = new Person[persons.Length / 2];
            double[] fitArray   = new double[persons.Length];

            for (int i = 0; i < persons.Length; i++)
            {
                fitArray[i] = mWrapper.FitnessFunction(persons[i]);
            }

            double buf = 0;
            Person persBuf;

            for (int i = 0; i < fitArray.Length; i++)
            {
                for (int j = i + 1; j < fitArray.Length; j++)
                {
                    if (fitArray[i] > fitArray[j])
                    {
                        buf         = fitArray[i];
                        fitArray[i] = fitArray[j];
                        fitArray[j] = buf;
                        persBuf     = persons[i];
                        persons[i]  = persons[j];
                        persons[j]  = persBuf;
                    }
                }
            }

            double avg = 0;

            for (int i = 0; i < fitArray.Length / 2; i++)
            {
                avg += fitArray[i];
            }
            avg /= (fitArray.Length / 2);

            // Now fitArray is range array:
            for (int i = 0; i < fitArray.Length / 2; i++)
            {
                fitArray[i] = avg / fitArray[i];
            }

            avg = 0;
            // Calc new avg:
            for (int i = 0; i < fitArray.Length / 2; i++)
            {
                avg += fitArray[i];
            }
            avg /= fitArray.Length / 2;

            // Scaling range array:
            for (int i = 0; i < fitArray.Length / 2; i++)
            {
                fitArray[i] *= avg;
            }

            int counter = 0;

            for (int i = 0; i < fitArray.Length / 2; i++)
            {
                for (int j = 0; j < (int)fitArray[i]; j++)
                {
                    retPersons[counter] = persons[i];
                    counter++;
                }
            }

            for (int i = 0; i < fitArray.Length / 2 - counter; i++)
            {
                retPersons[counter + i] = persons[i];
            }
            return(retPersons);
        }