private void ChangeVector(VariablesVector variablesVector)
        {
            variablesVector.IsActualF = false;

            int[] variables = variablesVector.Variables;

            for (int i = 0; i < m_dimentionsNumber; i++)
            {
                ChangeVariable(ref variables[i]);
            }
        }
Beispiel #2
0
        VariablesVector[] createFirtsPopulation()
        {
            Random random = new Random();

            VariablesVector[] result = new VariablesVector[m_populationLength];

            for (int i = 0; i < m_populationLength; i++)
            {
                VariablesVector person = new VariablesVector(m_variablesVectorLength);
                person.RandomizeVariables(random, m_maxValue);
                result[i] = person;
            }

            return(result);
        }
Beispiel #3
0
        public static VariablesVector[] Twist(List <RouletteElement> rouletteElements)
        {
            Random random = new Random();
            int    neededVariablesVectorLength = 2; //amount of persons

            VariablesVector[] result = new VariablesVector[neededVariablesVectorLength];

            for (int i = 0; i < neededVariablesVectorLength; i++)
            {
                double r = random.NextDouble();
                result[i] = rouletteElements.Find(el => r >= el.BottomProbability && r < el.TopProbability).VariablesVector;
            }

            return(result);
        }
Beispiel #4
0
        public VariablesVector[] TwoPointCrossing(VariablesVector[] variablesVectors)
        {
            VariablesVector a = variablesVectors[0];
            VariablesVector b = variablesVectors[1];

            VariablesVector[] result = new[] { new VariablesVector(m_dimentionsNumber), new VariablesVector(m_dimentionsNumber) };

            for (int dimNumber = 0; dimNumber < m_dimentionsNumber; dimNumber++)
            {
                //получение результата скрещивания по одному измерению
                int[] crossingInDimentionResult = CrossTwoVariables(a.Variables[dimNumber], b.Variables[dimNumber]);

                //переносим это в массив результатов
                AssingCrossingResultToDimention(result, crossingInDimentionResult, dimNumber);
            }

            return(result);
        }
Beispiel #5
0
        public static RouletteElement[] formRouletteElements(List <VariablesVector> population)
        {
            RouletteElement[] result = new RouletteElement[population.Count];

            double sumOfFitnessFunctionOfAllPersnons = population.Sum(el => el.F);
            double accumulativeSumOfProbability      = 0;

            for (int i = 0; i < population.Count; i++)
            {
                VariablesVector currentPerson = population[i];

                double probability    = currentPerson.F / sumOfFitnessFunctionOfAllPersnons;
                double topProbability = accumulativeSumOfProbability + probability;

                result[i] = new RouletteElement(currentPerson,
                                                accumulativeSumOfProbability,
                                                topProbability);

                accumulativeSumOfProbability = topProbability;
            }

            return(result);
        }
 public RouletteElement(VariablesVector variablesVector, double bottomProbability, double topProbability)
 {
     this.VariablesVector   = variablesVector;
     this.BottomProbability = bottomProbability;
     this.TopProbability    = topProbability;
 }
Beispiel #7
0
        public double Maximize()
        {
            int positionOfMaxBit = Service.getPositionOfMaxBit(m_maxValue);

            Cross        crossObj        = new Cross(positionOfMaxBit, m_variablesVectorLength);
            Mutation     mutationObj     = new Mutation(positionOfMaxBit, m_variablesVectorLength, m_mutationPercent);
            HardMutation hardMutationObj = new HardMutation(positionOfMaxBit, m_variablesVectorLength, m_hardMutationPercent);

            List <VariablesVector> population = createFirtsPopulation().ToList();

            CalcFValueForPopulation(population);

            population = population.OrderByDescending(el => el.F).ToList();

            processing?.Invoke(this, new GeneticAlgorithmEventArgs(-1, population[0].F, population.Average(el => el.F)));

            int bestPersonsLength = Math.Max(1, (int)(m_populationLength * m_bestPersonsPart));

            for (int iterations = 0; iterations < m_iterationsLimit; iterations++)
            {
                //form the roulette
                RouletteElement[] rouletteElements = Roulette.formRouletteElements(population);

                //crossing
                for (int i = 0; i < m_populationLength / 8; i++)
                {
                    VariablesVector[] selectedByRoulette = Roulette.Twist(rouletteElements.ToList());

                    VariablesVector[] descendants = crossObj.TwoPointCrossing(selectedByRoulette);
                    population.AddRange(descendants);
                }

                population = population.OrderByDescending(el => el.F).ToList();

                List <VariablesVector> populationWithoutBestPersons = population.Skip(bestPersonsLength).ToList();

                //and mutation
                mutationObj.DoMutation(populationWithoutBestPersons);

                //hard Mutation
                if (iterations > 0 && iterations % m_hardMutaionFriquency == 0)
                {
                    hardMutationObj.DoMutation(populationWithoutBestPersons);
                }

                CalcFValueForPopulation(population.Where(el => !el.IsActualF).ToList());

                //leaving the best variants
                population = population.OrderByDescending(el => el.F).Take(m_populationLength).ToList();

                //debug
                double F = population.Max(el => el.F);

                processing?.Invoke(this, new GeneticAlgorithmEventArgs(iterations, F, population.Average(el => el.F)));
            }

            double max = population.Max(el => el.F);

            VariablesVector withMax = population.Find(el => el.F == max);

            return(max);
        }