예제 #1
0
        public static Ant CreateSolutionNN(Graph graph, UInt32 startNode = 0)
        {
            Ant ant = new Ant();

            ant.Initialize(graph.dimension);
            UInt32 currentNode = startNode;

            ant.Visit(currentNode);

            for (int i = 1; i < graph.dimension; i++)
            {
                UInt32 nextNode = NextNode(graph, (UInt32)i, ant);
                if (nextNode == currentNode)
                {
                    double minDistance = double.MaxValue;
                    for (UInt32 node = 0; node < graph.dimension; ++node)
                    {
                        if (!ant.IsVisited(node))
                        {
                            var distance = graph.edgeWeight[currentNode, node];
                            if (distance < minDistance)
                            {
                                minDistance = distance;
                                nextNode    = node;
                            }
                        }
                    }
                }

                ant.Visit(nextNode);
                currentNode = nextNode;
            }
            return(ant);
        }
예제 #2
0
        public static UInt32 moveAntMMAS(Graph graph, PheromoneMemory pheromone, double [] heuristic, Ant ant)
        {
            var    dimension   = graph.dimension;
            var    currentNode = ant.visited[ant.visited.Count - 1];
            UInt32 offset      = currentNode * dimension;

            UInt32 [] candList     = new UInt32 [MaxCandListSize];
            UInt32    candListSize = 0;

            for (int i = 0; i < graph.dimension; i++)
            {
                if (!ant.IsVisited((UInt32)i))
                {
                    candList[candListSize] = (UInt32)i;
                    ++candListSize;
                }
            }

            UInt32 chosenNode = currentNode;

            if (candListSize > 0)
            {
                double[] productsPrefixSum = new double[MaxCandListSize];
                double   total             = 0;
                for (UInt32 j = 0; j < candListSize; ++j)
                {
                    var node    = candList[j];
                    var product = pheromone.Get(currentNode, node) * heuristic[offset + node];
                    total += product;
                    productsPrefixSum[j] = total;
                }

                chosenNode = candList[candListSize - 1];

                var r = GetRandomDouble() * total;
                for (UInt32 i = 0; i < candListSize; ++i)
                {
                    if (r < productsPrefixSum[i])
                    {
                        chosenNode = candList[i];
                        break;
                    }
                }
            }
            else
            {
                double maxProduct = 0;
                for (UInt32 node = 0; node < dimension; ++node)
                {
                    if (!ant.IsVisited(node))
                    {
                        var product = pheromone.Get(currentNode, node) * heuristic[offset + node];
                        if (product > maxProduct)
                        {
                            maxProduct = product;
                            chosenNode = node;
                        }
                    }
                }
            }

            ant.Visit(chosenNode);
            return(chosenNode);
        }