public Route Search( Graph graph, bool printProgress = false )
        {
            if ( printProgress )
            {
                Console.ForegroundColor = ConsoleColor.Cyan;
                Console.WriteLine( "Selecting the best result from {0} different searchers...", Searchers.Length );
                Console.ForegroundColor = ConsoleColor.White;
            }

            ISearcher bestSearcher = Searchers[0];
            Route best = Searchers[0].Search( graph, printProgress );

            for ( int i = 1; i < Searchers.Length; ++i )
            {
                Route next = Searchers[i].Search( graph, printProgress );
                if ( next.Length < best.Length )
                {
                    bestSearcher = Searchers[i];
                    best = next;
                }
            }

            if ( printProgress )
            {
                Console.ForegroundColor = ConsoleColor.Cyan;
                Console.WriteLine( "And the winner is: {0} ({1})!", bestSearcher.GetType().Name, best.Length );
                Console.ForegroundColor = ConsoleColor.White;
            }

            return best;
        }
Пример #2
0
        public GeneticRoute(Graph graph, ushort[] genes)
            : base(graph)
        {
            if (genes.Length != graph.Count)
                throw new Exception("Incorrect gene count");

            Genes = genes;
            UpdateIndicesFromGenes();
        }
Пример #3
0
        public GeneticRoute(Graph graph, Random rand)
            : base(graph)
        {
            Genes = new ushort[graph.Count];
            for (int i = 0; i < Genes.Length; ++i) {
                Genes[i] = (ushort) rand.Next(0, (Genes.Length - i));
            }

            UpdateIndicesFromGenes();
        }
Пример #4
0
        public Ant(Graph graph, int startVertex)
        {
            Graph = graph;
            CurrentVertex = startVertex;
            History = new int[graph.Count];

            _stepNo = 0;
            _cost = 0;
            _visited = new bool[graph.Count];

            _rand = new Random(_sRand.Next());
        }
        public Route Search( Graph graph, bool printProgress = false )
        {
            Route route = new Route( graph );

            if ( printProgress )
            {
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine( "Starting a new {0} search", GetType().Name );
                if ( Improver != null )
                {
                    Console.ForegroundColor = ConsoleColor.DarkGreen;
                    Console.WriteLine( "Search will use {0} to "
                    + "improve each iteration", Improver.GetType().Name );
                }
                Console.ForegroundColor = ConsoleColor.Green;
                Console.Write( "Progress: 0/{0} - 0", graph.Count );
                Console.ForegroundColor = ConsoleColor.White;
            }

            int lastCount = route.Count;
            while ( route.Count < graph.Count )
            {
                int vIndex = ChooseNext( route );
                route.Insert( vIndex, ChooseIndex( route, vIndex ) );

                lastCount = route.Count;

                if ( Improver != null )
                    Improver.Improve( route, false );

                if ( printProgress )
                {
                    Console.CursorLeft = 10;
                    Console.Write( "{0}/{1} - {2}    ",
                        route.Count, graph.Count, route.Length );
                }
            }

            if ( printProgress )
            {
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine( "\n{0} search complete", GetType().Name );
                Console.ForegroundColor = ConsoleColor.White;
            }

            return route;
        }
Пример #6
0
 private static Route RunSearch( Graph graph, ISearcher searcher, Route route = null, bool quiet = false )
 {
     if ( searcher is HillClimbSearcher && route != null )
     {
         route = new Route( route );
         _stopwatch.Restart();
         ( (HillClimbSearcher) searcher ).Improve( route, !quiet );
         _stopwatch.Stop();
     }
     else
     {
         _stopwatch.Restart();
         route = searcher.Search( graph, !quiet );
         _stopwatch.Stop();
     }
     Console.ForegroundColor = ConsoleColor.Green;
     Console.WriteLine( "Search time: {0}ms", _stopwatch.ElapsedMilliseconds );
     Console.ForegroundColor = ConsoleColor.White;
     Console.WriteLine( route.ToString() );
     return route;
 }
        public Route Search(Graph graph, bool printProgress = false)
        {
            if (printProgress) {
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Starting a new {0} search with {1} thread{2}", GetType().Name,
                    Threads, Threads != 1 ? "s" : "");

                Console.ForegroundColor = ConsoleColor.DarkGreen;
                Console.WriteLine("Search will use {0} for each iteration", Searcher.GetType().Name);

                Console.ForegroundColor = ConsoleColor.Green;
                Console.Write("Progress: 0/{0} - 0", Attempts);
                Console.ForegroundColor = ConsoleColor.White;
            }

            Route best = null;
            int attempt = 0;
            int exited = 0;

            Func<bool> next = () => {
                lock (this) {
                    if (attempt < Attempts) {
                        ++attempt;
                        return true;
                    }

                    return false;
                }
            };

            Func<bool> hasNext = () => {
                lock (this)
                    return attempt < Attempts;
            };

            Func<bool> ended = () => {
                lock (this)
                    return exited == Threads;
            };

            Action exit = () => {
                lock (this)
                    ++exited;
            };

            Action rejoin = () => {
                lock (this)
                    --exited;
            };

            Func<bool> waitForEnd = () => {
                exit();

                while (!hasNext() && !ended())
                    Thread.Yield();

                if (!ended()) {
                    rejoin();
                    return false;
                } else
                    return true;
            };

            Action<Route> compare = (route) => {
                lock (this) {
                    if (best == null || route.Length < best.Length) {
                        best = new Route(route);
                        attempt = 0;

                        if (BetterRouteFound != null)
                            BetterRouteFound(this, new BetterRouteFoundEventArgs(route));
                    } else if (attempt % (Attempts >> 4) == 0)
                        _rand = new Random();

                    if (printProgress) {
                        Console.CursorLeft = 10;
                        Console.Write("{0}/{1} - {2}    ", attempt, Attempts, best.Length);
                    }
                }
            };

            ThreadStart loop = () => {
                Route route = new Route(graph);

                for (; ; ) {
                    while (next()) {
                        route.Clear();
                        for (int i = 0; i < graph.Count; ++i) {
                            lock (_rand) route.AddEnd(_rand.Next(i, graph.Count));
                            Searcher.Improve(route);
                        }
                        compare(route);
                    }

                    if (waitForEnd())
                        break;
                }
            };

            Thread[] workers = new Thread[Threads - 1];
            for (int i = 0; i < workers.Length; ++i) {
                workers[i] = new Thread(loop);
                workers[i].Start();
            }

            loop();

            if (printProgress) {
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("\n{0} search complete", GetType().Name);
                Console.ForegroundColor = ConsoleColor.White;
            }

            return best;
        }
 public virtual Route Search( Graph graph, bool printProgress = false )
 {
     Route route = Route.CreateDefault( graph );
     Improve( route, printProgress );
     return route;
 }