Exemplo n.º 1
0
        private BestSoFar LocalSearch(Ant bestAnt)
        {
            var iterationBest = new BestSoFar(this.startingNode.NodeId, this.endNodeId)
            {
                Route         = bestAnt.Route,
                RouteDistance = bestAnt.RouteDistance
            };

            //if (iterationBest.Route.Count > 4)
            //{
            //    for (var i = 0; i < this.iterations; i++)
            //    {
            //        var children = FindChildren(iterationBest);
            //        var bestChild = children.OrderBy(x => x.RouteDistance).FirstOrDefault();
            //        if (bestChild.RouteDistance > iterationBest.RouteDistance)
            //        {
            //            break;
            //        }

            //        iterationBest = bestChild;
            //    }
            //}

            return(iterationBest);
        }
Exemplo n.º 2
0
        private List <BestSoFar> FindChildren(BestSoFar bestRoute)
        {
            var children = new List <BestSoFar>();
            int ceiling  = this.endNodeId == null ? (bestRoute.Route.Count - 2) / 2 : (bestRoute.Route.Count - 3) / 2;

            for (var i = 0; i < ceiling; i++)
            {
                var index    = 1 + i * 2;
                var route    = bestRoute.Route.ToList();
                var distance = bestRoute.RouteDistance;

                distance -= this.arcsInfo.Find(x => x.InitNodeId == route[index - 1] && x.EndNodeId == route[index]).Distance;
                distance -= this.arcsInfo.Find(x => x.InitNodeId == route[index + 1] && x.EndNodeId == route[index + 2]).Distance;

                var tmp = route[index];
                route[index]     = route[index + 1];
                route[index + 1] = tmp;

                distance += this.arcsInfo.Find(x => x.InitNodeId == route[index - 1] && x.EndNodeId == route[index]).Distance;
                distance += this.arcsInfo.Find(x => x.InitNodeId == route[index + 1] && x.EndNodeId == route[index + 2]).Distance;

                var child = new BestSoFar(this.startingNode.NodeId, this.endNodeId)
                {
                    Route         = route,
                    RouteDistance = distance,
                    LocalSearch   = true
                };

                children.Add(child);
            }

            return(children);
        }
Exemplo n.º 3
0
        //starts the search
        public double[][] FindRoute()
        {
            for (var i = 0; i < this.iterations; i++)
            {
                //initializes pheromone ammount in all arcs
                this.InitializePheromone();

                var bestAnt       = this.ConstructSolutions();
                var iterationBest = this.LocalSearch(bestAnt);
                iterationBest.Iteration = i;

                if (this.bestSoFar == null || this.bestSoFar.Route.Count != this.nodes.Count)
                {
                    this.bestSoFar = iterationBest;
                }
                else
                {
                    this.bestSoFar = this.bestSoFar.RouteDistance < iterationBest.RouteDistance ? this.bestSoFar : iterationBest;
                }
                //this.UpdateStats(i);
                this.UpdatePheromone();
            }

            var bestRoute      = this.bestSoFar.GetOrderedRoute();
            var bestRouteArray = this.RouteToArray(bestRoute);

            this.bestSoFar = null; //if I run the method w/o reinstancing the Problem object best so far must be reinitialized or it will carry to the next run

            return(bestRouteArray);
        }