public HybridACOGA(int numberOfVariables, OptimizationType opType, GA <int> .ObjectiveFunctionDelegate objectFunction, double[,] distance) : base(numberOfVariables, opType, objectFunction) { getObjectiveValue = objectFunction; numberOfCity = numberOfVariables; FromToDistanceMatrix = distance; // Allocate memory for pheromone matrix and heuristic value matrix // Heuristic values in the matrix are computed and assigned via // the specified function delegate pheromone = new double[numberOfCity, numberOfCity]; indicesOfAnts = new int[populationSize]; solutions = new int[populationSize][]; soFarTheBestSoluiton = new int[numberOfCity]; for (int r = 0; r < numberOfCity; r++) { for (int c = 0; c < numberOfCity; c++) { pheromone[r, c] = 0.01; } } for (int r = 0; r < numberOfCity; r++) { for (int c = 0; c < numberOfCity; c++) { AverageDistance += FromToDistanceMatrix[r, c]; } } AverageDistance = AverageDistance / (numberOfCity * numberOfCity); heuristicValues = new double[numberOfCity, numberOfCity]; for (int r = 0; r < numberOfCity; r++) { for (int c = 0; c < numberOfCity; c++) { heuristicValues[r, c] = DistanceInverseHeuristic(r, c); } } for (int i = 0; i < populationSize; i++) { indicesOfAnts[i] = i; solutions[i] = new int[numberOfCity]; for (int j = 0; j < numberOfCity; j++) { solutions[i][j] = j; } KnuthShuffle(solutions[i]); } // Allocate memory for the arries used in ACO, whose length is // depend on the number of variables. In contrast, those arraies // with length depending on the number of ants are allocated in // reset function. probabilities = new double[numberOfCity]; candidateSet = new int[numberOfCity]; ObjectiveValues = new double[populationSize]; // Set drop amount multiplier; which is the avarage length //dropMultiplier = dropPheromone * dropMultiplier / getObjectiveValue(objectiveValues); }
/// <summary> /// To employ a GA solver, user must provide the number of variables, the optimization type, and a function delegate /// that compute and return the objective value for a given solution. /// </summary> /// <param name="numberOfVariables"> Number of variables of the problem</param> /// <param name="opType"> The optimization problem type </param> /// <param name="objectFunction"> The function delegate that computer the objective value for a given solution </param> public GA(int numberOfVariables, OptimizationType opType, GA <T> .ObjectiveFunctionDelegate objectFunction) { numberOfGenes = numberOfVariables; optimizationType = opType; if (objectFunction == null) { throw new Exception("You must prepare an objective function."); } GetObjectiveValueFunction = objectFunction; }