protected int[] AntActivity(double[,] pheromone, double[,] heuristic) { int[] tour = new int[tourLength]; bool[] visited = new bool[tourLength]; int selectedNeighbor; double[] probabilities; List <int> neighbors; double denominator; // Select the initial vertex randomly. tour[0] = Statistics.RandomDiscreteUniform(0, tourLength - 1); visited[tour[0]] = true; // Complete the rest of the tour. for (int i = 1; i < tourLength; i++) { neighbors = FactibleNeighbors(tour[i - 1], visited); denominator = 0; for (int j = 0; j < neighbors.Count; j++) { denominator += (Math.Pow(pheromone[tour[i - 1], neighbors[j]], alpha) * Math.Pow(heuristic[tour[i - 1], neighbors[j]], beta)); } probabilities = new double[neighbors.Count]; for (int j = 0; j < neighbors.Count; j++) { probabilities[j] = (Math.Pow(pheromone[tour[i - 1], neighbors[j]], alpha) * Math.Pow(heuristic[tour[i - 1], neighbors[j]], beta)) / denominator; } selectedNeighbor = neighbors[Statistics.SampleRoulette(probabilities)]; visited[selectedNeighbor] = true; tour[i] = selectedNeighbor; } return(tour); }
public void Run(int timeLimit) { int startTime = Environment.TickCount; int iterationStartTime = 0; int iterationTime = 0; int maxIterationTime = 0; int numVariables = LowerBounds.Length; int selectedSize = Math.Max(1, (int)Math.Round(TruncationFactor * PopulationSize)); int[][] population = new int[PopulationSize][]; double[] evaluation = new double[PopulationSize]; double[][] model = new double[numVariables][]; for (int i = 0; i < numVariables; i++) { model[i] = new double[(UpperBounds[i] - LowerBounds[i]) + 1]; } // Generate the initial random population. for (int k = 0; k < PopulationSize; k++) { population[k] = new int[numVariables]; for (int i = 0; i < numVariables; i++) { population[k][i] = Statistics.RandomDiscreteUniform(LowerBounds[i], UpperBounds[i]); } } BestIndividual = null; maxIterationTime = Environment.TickCount - startTime; while (Environment.TickCount - startTime < timeLimit - maxIterationTime) { iterationStartTime = Environment.TickCount; // Handle constraints using a repairing method. if (RepairEnabled) { for (int k = 0; k < PopulationSize; k++) { Repair(population[k]); } } // Run a local search method for each individual in the population. if (LocalSearchEnabled) { for (int k = 0; k < PopulationSize; k++) { LocalSearch(population[k]); } } // Evaluate the population. for (int k = 0; k < PopulationSize; k++) { evaluation[k] = Fitness(population[k]); } // Apply the selection method. Array.Sort(evaluation, population); if (BestIndividual == null || evaluation[0] < BestFitness) { BestIndividual = population[0]; BestFitness = evaluation[0]; } // Learn the probabilistic model from the selected population. for (int i = 0; i < numVariables; i++) { // Set the counters to zero. for (int k = 0; k <= UpperBounds[i] - LowerBounds[i]; k++) { model[i][k] = 0; } // Count the values of the variable. for (int k = 0; k < selectedSize; k++) { model[i][population[k][i] - LowerBounds[i]] += 1; } // Calculate the frequency of each value of the variable. for (int k = 0; k <= UpperBounds[i] - LowerBounds[i]; k++) { model[i][k] /= selectedSize; } } // Sample the population for the next generation. for (int k = 0; k < PopulationSize; k++) { population[k] = new int[numVariables]; for (int i = 0; i < numVariables; i++) { population[k][i] = LowerBounds[i] + Statistics.SampleRoulette(model[i]); } } iterationTime = Environment.TickCount - iterationStartTime; maxIterationTime = (maxIterationTime < iterationTime) ? iterationTime : maxIterationTime; } }