public override void Solve(Problem problem)
        {
            if (_initialValue == null)
            {
                throw new Exception("You must provide a starting point for the search");
            }

            _problem = problem;

            _population = new Chromosome[_noNeighbors];

            bool       searchOver  = false;
            int        generations = 0;
            Chromosome best        = new Chromosome(_initialValue);

            while (!searchOver)
            {
                _population[0] = best;
                _population[0].ComputeFitness(_problem);

                // generating population by mutation
                for (int i = 1; i < _noNeighbors; i++)
                {
                    _population[i] = new Chromosome(best);

                    double r = _rand.NextDouble();

                    if (r < Settings.pmg)
                    {
                        Mutation.PerformMutation(_population[i], _problem.Parameters); // mutatie gaussiana
                    }
                    else if (r < Settings.pmg + Settings.pmr)                          // mutatie prin resetare
                    {
                        for (int j = 0; j < _population[i].NoGenes; j++)
                        {
                            if (_rand.NextDouble() < Settings.pm)
                            {
                                _population[i].RealGenes[j] = _problem.Parameters.EncodingInfo.MinValues[j] + ExtendedRandom.NextUniform() * (_problem.Parameters.EncodingInfo.MaxValues[j] - _problem.Parameters.EncodingInfo.MinValues[j]);
                            }
                        }
                    }
                    else
                    {
                        int i1 = _rand.Next(_population[i].RealGenes.Length);
                        int i2 = _rand.Next(_population[i].RealGenes.Length);
                        if (_population[i].RealGenes[i1] >= 2 && _population[i].RealGenes[i2] >= 2)
                        {
                            _population[i].RealGenes[i1] -= 1;
                            _population[i].RealGenes[i2] += 1;
                        }
                    }

                    _population[i].ComputeFitness(_problem);
                }

                best = Selection.GetBest(_population);
                generations++;

                if (generations > _noIterations)
                {
                    _solution  = best;
                    searchOver = true;
                }
            }
        }
Exemple #2
0
        public override void Solve(Problem problem)
        {
            _problem = problem;

            // generating the initial random population
            _population = new Chromosome[_problem.Parameters.PopulationSize];
            for (int i = 0; i < _problem.Parameters.PopulationSize; i++)
            {
                _population[i] = new Chromosome(_problem.Parameters, true)
                {
                    Age = Settings.lf + _rand.Next(Settings.lf)
                };
            }

            bool searchOver  = false;
            int  generations = 0;

            while (!searchOver)
            {
                for (int i = 0; i < _population.Length; i++)
                {
                    int        age = _population[i].Age;
                    Chromosome c   = PerformHC(_population[i]);
                    _population[i]     = c;
                    _population[i].Age = age - 1;
                    _population[i].ComputeFitness(_problem);
                }

                Chromosome[] newPopulation = new Chromosome[_problem.Parameters.PopulationSize];

                if (_problem.Parameters.SelectionInfo.Elitism > 0)
                {
                    Chromosome[] elite = Selection.GetElite(_population, _problem.Parameters.SelectionInfo.Elitism);
                    for (int i = 0; i < _problem.Parameters.SelectionInfo.Elitism; i++)
                    {
                        newPopulation[i]     = elite[i];
                        newPopulation[i].Age = Settings.lf;
                    }
                }

                //if (_problem.Parameters.SelectionInfo.Type == SelectionType.Rank)
                //    Selection.SortPopulation(_population);

                for (int i = problem.Parameters.SelectionInfo.Elitism; i < _problem.Parameters.PopulationSize; i++)
                {
                    // select
                    Chromosome mother = Selection.PerformSelection(_population, _problem.Parameters.SelectionInfo);
                    Chromosome father = Selection.PerformSelection(_population, _problem.Parameters.SelectionInfo);

                    // combine
                    Chromosome child = Crossover.PerformCrossover(mother, father, _problem.Parameters);

                    // mutate
                    Mutation.PerformMutation(child, _problem.Parameters);

                    child.ComputeFitness(_problem);
                    child.Age = Settings.lf + _rand.Next(Settings.lf);

                    newPopulation[i] = child;
                }

                List <Chromosome> mergedPop = new List <Chromosome>();

                for (int i = 0; i < _population.Length; i++)
                {
                    if (_population[i].Age > 0)
                    {
                        mergedPop.Add(_population[i]);
                    }
                }

                for (int i = 0; i < newPopulation.Length; i++)
                {
                    if (newPopulation[i].Age > 0)
                    {
                        mergedPop.Add(newPopulation[i]);
                    }
                }

                for (int i = 0; i < _problem.Parameters.PopulationSize * Settings.f; i++) // indivizi noi
                {
                    Chromosome c = new Chromosome(_problem.Parameters, true)
                    {
                        Age = Settings.lf1 + _rand.Next(Settings.lf1)
                    };
                    mergedPop.Add(c);
                }

                _population = new Chromosome[mergedPop.Count];
                for (int i = 0; i < _population.Length; i++)
                {
                    _population[i] = mergedPop[i];
                }

                generations++;
                Console.Write("Generation {0} / {1}: ", generations, _problem.Parameters.StoppingInfo.MaxGenerations);
                _solution = Selection.GetBest(_population);
                Console.WriteLine("{0:F4} {1}", _solution.Fitness, _population.Length);

                if (generations > _problem.Parameters.StoppingInfo.MaxGenerations)
                {
                    //_solution = Selection.GetBest(_population);
                    searchOver = true;
                }
            }
        }