Esempio n. 1
0
 /// <summary>
 /// Executa uma 'volta' de uma formiga pelo grafo.
 /// </summary>
 public static void  executeTour()
 {
     // laço para fazer as formigas percorrerem todos os nós.
     for (int i = 0; i < nodeCount - 2; i++)
     {
         // laço para fazer cada formigar dar 'um passo'.
         for (int j = 0; j < antNumber; j++)
         {
             // obtém a formiga do array.
             AntAgent ant = (AntAgent)ants[j];
             // move a formiga para um próximo nó.
             ant.moveTo(i);
             matchesTable[i][j] = ant.ActualNode;
         }
     }
 }
Esempio n. 2
0
 /// <summary>
 /// Método que realiza a fase de inicialização da meta-heurística.
 /// Cria as formigas e as espalha pelo grafo. Além disso atribui os valores
 /// iniciais de rastro e calcula a visibilidade.
 /// </summary>
 private static void initialize()
 {
     // Laço para criar as formigas.
     for (int i = 0; i < antNumber; i++)
     {
         // Cria a formiga
         AntAgent ant = new AntAgent();
         // Adiciona no ArrayList
         ants.Add(ant);
     }
     // Laço para inicializar os valores das matrizes.
     for (int i = 0; i < nodeCount; i++)
     {
         for (int j = 0; j < nodeCount; j++)
         {
             //Calcula o valor da visibilidade.
             edgeVisibility[i][j] = 1.0 / graph[i][j];
             // Atribui valor inicial de rastro.
             edgeTrail[i][j] = initialTrail;
         }
     }
 }
Esempio n. 3
0
        private static void updateTrail()
        {
            double totalCost = 0.0;

            #region init
            int[][] table = new int[nodeCount][];
            double  cost2 = 0.0;
            table = bTable;
            for (int i = 0; i < bTable[i].Length; i++)
            {
                int iNode = 0;
                int jNode = 0;
                for (int j = 0; j < bTable.Length - 1; j++)
                {
                    // Obtêm o valor do nó i.
                    iNode = (bTable[j][i]);
                    // Obtêm o valor do nó j.
                    jNode = (bTable[j + 1][i]);
                    if (!((iNode < antNumber) && (jNode < antNumber)))
                    {
                        // Adiciona ao valor da rota o custo de ir do nó i ao j.
                        cost2 += AntCycle.Graph[iNode][jNode];
                    }
                }
                // Obtêm o valor do nó i.
                iNode = (bTable[table.Length - 1][i]);
                // Obtêm o valor do nó j.
                jNode = (bTable[0][i]);
                if (!((iNode < antNumber) && (jNode < antNumber)))
                {
                    // Adiciona ao valor da rota o custo de ir do nó i ao j.
                    cost2 += AntCycle.Graph[iNode][jNode];
                }
                // Retorna o valor da rota obtido.
            }

            #endregion


            for (int i = 0; i < antNumber; i++)
            {
                AntAgent ant = (AntAgent)ants[i];
                ant.calculateTourLength();
                for (int j = 0; j < ant.TabuList.Count - 1; j++)
                {
                    int iNode = ((System.Int32)ant.TabuList[j]);
                    int jNode = ((System.Int32)ant.TabuList[j + 1]);
                    edgeDeltaTrail[iNode][jNode] += trailQuantity / ant.TripCosts;
                    table[j][i]        = (int)matchesTable[j][i];
                    matchesTable[j][i] = -1;
                }
                int endNode   = ((System.Int32)ant.TabuList[ant.TabuList.Count - 1]);
                int beginNode = ((System.Int32)ant.TabuList[0]);
                edgeDeltaTrail[endNode][beginNode] += trailQuantity / ant.TripCosts;
                totalCost += ant.TripCosts;
            }
            if (totalCost < cost)
            {
                cost = cost2;
            }
            else if (totalCost == cost)
            {
                stagnationCyclesNumberCounter++;
            }
            for (int i = 0; i < nodeCount; i++)
            {
                for (int j = 0; j < nodeCount; j++)
                {
                    edgeTrail[i][j]      = evaporationCoeficient * edgeTrail[i][j] + edgeDeltaTrail[i][j];
                    edgeDeltaTrail[i][j] = 0;
                }
            }
        }