public override void Improve(Route route, bool printProgress = false)
        {
            _genePool = new GeneticRoute[GenePoolCount];
            _selected = new GeneticRoute[SelectionCount];

            if (printProgress) {
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Starting a new {0} search", GetType().Name);
                Console.WriteLine("Creating initial population...");
                Console.Write("Progress: 0/{0}", GenePoolCount);
                Console.ForegroundColor = ConsoleColor.White;
            }

            ReversingSearcher improver = new ReversingSearcher();

            _genePool[0] = new GeneticRoute(route);
            _genePool[0].Fitness = route.Length;
            for (int i = 1; i < GenePoolCount; ++i) {
                _genePool[i] = new GeneticRoute(route.Graph, _rand);
                _genePool[i].Fitness = _genePool[i].Length;
                //Route clone = new Route(_genePool[i]);
                //improver.Improve(clone, false);
                //_genePool[i].Fitness = clone.Length;
                if (printProgress) {
                    Console.CursorLeft = 10;
                    Console.Write("{0}/{1}", i + 1, GenePoolCount);
                }
            }

            if (printProgress) {
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine();
                Console.WriteLine("Initial population created");
                Console.WriteLine("Running generations...");
                Console.Write("Progress: 0/{0} - 0", GenerationLimit);
                Console.ForegroundColor = ConsoleColor.White;
            }

            for (int g = 0; g < GenerationLimit; ++g) {
                if (printProgress) {
                    Console.CursorLeft = 10;
                    Console.Write("{0}/{1} - {2}   ", g + 1, GenerationLimit, _genePool[0].Fitness);
                }

                for (int i = 0; i < SelectionCount; ++i) {
                    int j = 0;
                    while (IsSelected(i, j) || _rand.NextDouble() >= SelectionQuality) {
                        j = (j + 1) % GenePoolCount;
                    }
                    _selected[i] = _genePool[j];
                }

                for (int i = 0; i < OffspringCount; ++i) {
                    GeneticRoute parentA = _selected[0]; // TODO: fix for different
                    GeneticRoute parentB = _selected[1]; //       selection counts
                    ushort[] genes = new ushort[route.Count];
                    Crossover(genes, parentA.Genes, parentB.Genes);
                    Mutate(genes);
                    GeneticRoute child = new GeneticRoute(route.Graph, genes);
                    //Route clone = new Route(child);
                    //improver.Improve(clone, false);
                    //child.Fitness = clone.Length;
                    child.Fitness = child.Length;

                    int l = -1;
                    for (int j = GenePoolCount - 1; j >= 0; --j) {
                        GeneticRoute other = _genePool[j];
                        if (other.Fitness > child.Fitness) {
                            _genePool[j] = child;

                            if (l > -1)
                                _genePool[l] = other;

                            if (j == 0) {
                                BetterRouteFound(this, new BetterRouteFoundEventArgs(child));
                                g = 0;
                            }

                            l = j;
                        } else break;
                    }
                }
            }

            if (printProgress) {
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("\nGenetic search complete");
                Console.ForegroundColor = ConsoleColor.White;
            }

            Route best = new Route(_genePool[0]);
            improver.Improve(best);

            route.Clear();
            for (int i = 0; i < best.Count; ++i)
                route.Insert(route.VIndexOf(best[i]), i);
        }
Esempio n. 2
0
        public override void Improve(Route route, bool printProgress = false)
        {
            _genePool = new GeneticRoute[GenePoolCount];
            _selected = new GeneticRoute[SelectionCount];

            if (printProgress)
            {
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Starting a new {0} search", GetType().Name);
                Console.WriteLine("Creating initial population...");
                Console.Write("Progress: 0/{0}", GenePoolCount);
                Console.ForegroundColor = ConsoleColor.White;
            }

            ReversingSearcher improver = new ReversingSearcher();

            _genePool[0]         = new GeneticRoute(route);
            _genePool[0].Fitness = route.Length;
            for (int i = 1; i < GenePoolCount; ++i)
            {
                _genePool[i]         = new GeneticRoute(route.Graph, _rand);
                _genePool[i].Fitness = _genePool[i].Length;
                //Route clone = new Route(_genePool[i]);
                //improver.Improve(clone, false);
                //_genePool[i].Fitness = clone.Length;
                if (printProgress)
                {
                    Console.CursorLeft = 10;
                    Console.Write("{0}/{1}", i + 1, GenePoolCount);
                }
            }

            if (printProgress)
            {
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine();
                Console.WriteLine("Initial population created");
                Console.WriteLine("Running generations...");
                Console.Write("Progress: 0/{0} - 0", GenerationLimit);
                Console.ForegroundColor = ConsoleColor.White;
            }

            for (int g = 0; g < GenerationLimit; ++g)
            {
                if (printProgress)
                {
                    Console.CursorLeft = 10;
                    Console.Write("{0}/{1} - {2}   ", g + 1, GenerationLimit, _genePool[0].Fitness);
                }

                for (int i = 0; i < SelectionCount; ++i)
                {
                    int j = 0;
                    while (IsSelected(i, j) || _rand.NextDouble() >= SelectionQuality)
                    {
                        j = (j + 1) % GenePoolCount;
                    }
                    _selected[i] = _genePool[j];
                }

                for (int i = 0; i < OffspringCount; ++i)
                {
                    GeneticRoute parentA = _selected[0]; // TODO: fix for different
                    GeneticRoute parentB = _selected[1]; //       selection counts
                    ushort[]     genes   = new ushort[route.Count];
                    Crossover(genes, parentA.Genes, parentB.Genes);
                    Mutate(genes);
                    GeneticRoute child = new GeneticRoute(route.Graph, genes);
                    //Route clone = new Route(child);
                    //improver.Improve(clone, false);
                    //child.Fitness = clone.Length;
                    child.Fitness = child.Length;

                    int l = -1;
                    for (int j = GenePoolCount - 1; j >= 0; --j)
                    {
                        GeneticRoute other = _genePool[j];
                        if (other.Fitness > child.Fitness)
                        {
                            _genePool[j] = child;

                            if (l > -1)
                            {
                                _genePool[l] = other;
                            }

                            if (j == 0)
                            {
                                BetterRouteFound(this, new BetterRouteFoundEventArgs(child));
                                g = 0;
                            }

                            l = j;
                        }
                        else
                        {
                            break;
                        }
                    }
                }
            }

            if (printProgress)
            {
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("\nGenetic search complete");
                Console.ForegroundColor = ConsoleColor.White;
            }

            Route best = new Route(_genePool[0]);

            improver.Improve(best);

            route.Clear();
            for (int i = 0; i < best.Count; ++i)
            {
                route.Insert(route.VIndexOf(best[i]), i);
            }
        }