/// <summary> /// The constructor. /// </summary> /// <param name="theGraph">The graph that we are seeking a minimal path through.</param> /// <param name="theAntCount">The number of ants to use.</param> public DiscreteACO(ICostGraph theGraph, int theAntCount) { Alpha = 1; Beta = 5; Evaporation = 0.5; Q = 500; PR = 0.01; Random = new MersenneTwisterGenerateRandom(); int len = theGraph.Count; _graph = theGraph; _pheromone = new double[len][]; _bestPath = new int[len]; _bestCost = double.PositiveInfinity; for (int i = 0; i < len; i++) { _pheromone[i] = new double[len]; for (int j = 0; j < len; j++) { _pheromone[i][j] = INITIAL_PHEROMONE; } } for (int i = 0; i < theAntCount; i++) { _ants.Add(new DiscreteAnt(len)); } }
/// <summary> /// Calculate the cost, up to the current point. /// </summary> /// <param name="currentIndex">The current point.</param> /// <param name="graph">The cost graph.</param> /// <returns>The current cost.</returns> public double CalculateCost(int currentIndex, ICostGraph graph) { double length = graph.Cost(_path[currentIndex - 1], _path[0]); for (int i = 0; i < currentIndex - 1; i++) { length += graph.Cost(_path[i], _path[i + 1]); } return(length); }
/// <summary> /// Calculate the cost, up to the current point. /// </summary> /// <param name="currentIndex">The current point.</param> /// <param name="graph">The cost graph.</param> /// <returns>The current cost.</returns> public double CalculateCost(int currentIndex, ICostGraph graph) { double length = graph.Cost(_path[currentIndex - 1], _path[0]); for (int i = 0; i < currentIndex - 1; i++) { length += graph.Cost(_path[i], _path[i + 1]); } return length; }