Beispiel #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);
        }
Beispiel #2
0
        public static UInt32[] SortNodes(Graph graph, UInt32 actualNode, Ant ant)
        {
            //UInt32[] nodes = new UInt32[graph.dimension - (UInt32)ant.visited.Count];
            List <double> weights = new List <double>();

            for (UInt32 i = 0; i < graph.edgeWeight.Length; i++)
            {
                if (i == actualNode)
                {
                    continue;
                }

                if (ant.IsVisited(i))
                {
                    continue;
                }

                weights.Add(graph.edgeWeight[actualNode, i]);
            }

            weights.Sort();

            UInt32 [] sortedNodes = new UInt32[weights.Count];

            for (UInt32 j = 0; j < weights.Count; j++)
            {
                for (UInt32 k = 0; k < (UInt32)graph.dimension; k++)
                {
                    if (weights[(int)j] == graph.edgeWeight[actualNode, (int)k])
                    {
                        sortedNodes[j] = k;
                    }
                }
            }

            return(sortedNodes);
        }
Beispiel #3
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;
                }
            }

            //candList = SortNodes(graph, currentNode, ant);

            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);
        }