// TODO: Try to remove rnd and options from global variables; graph must remain at least for now. public AntSystemFragment(Random rnd, Options.Options options, IGraph graph) { _options = options; _graph = graph; Treil = new List <HashSet <Vertex> >(); for (var i = 0; i < _options.NumberOfRegions; i++) { Treil.Add(new HashSet <Vertex>()); } FreeVertices = new HashSet <Vertex>(_graph.VerticesWeights); PassedVertices = new HashSet <Vertex>(); WeightOfColonies = new int[_options.NumberOfRegions]; EdgesWeightOfColonies = new int[_options.NumberOfRegions]; for (var i = 0; i < _options.NumberOfRegions; i++) { var randomFreeVertix = FreeVertices.Shuffle(rnd).First(); AddFreeVertexToTreil(i, randomFreeVertix); } }
public void UpdatePhermone(int[] weightOfColonies, List <HashSet <Vertex> > treil, Options.Options options, double sumOfOptimalityCriterions) { // Colony with heighes weight. var colonyWithHighestWeight = Array.IndexOf(weightOfColonies, weightOfColonies.Max()); // If criterions of optimality is less then 0, the minimum of pheromones will be set. if (sumOfOptimalityCriterions > 0) { for (var indexOfRegion = 0; indexOfRegion < options.NumberOfRegions; indexOfRegion++) { double pheromoneToSet; if (indexOfRegion == colonyWithHighestWeight) { pheromoneToSet = 0.01D * (sumOfOptimalityCriterions + 1.2D * weightOfColonies[colonyWithHighestWeight]); } else { pheromoneToSet = 0.01D * sumOfOptimalityCriterions; } var path = treil[indexOfRegion]; var verexCombination = path.SelectMany((value, index) => path.Skip(index + 1), (first, second) => new { first, second }); foreach (var combination in verexCombination) { PheromoneMatrix[combination.first.Index, combination.second.Index] = PheromoneMatrix[combination.first.Index, combination.second.Index] * (1 - options.Ro) + pheromoneToSet; PheromoneMatrix[combination.second.Index, combination.first.Index] = PheromoneMatrix[combination.second.Index, combination.first.Index] * (1 - options.Ro) + pheromoneToSet; } } } else { for (var i = 0; i < PheromoneMatrix.GetLength(0); i++) { for (var j = 0; j < PheromoneMatrix.GetLength(1); j++) { PheromoneMatrix[i, j] = PheromoneMatrix[i, j] * (1 - options.Ro) + Constants.MinimalVelueOfPheromoneToSet; } } } //Utility.LogDoubleMatrixAsTable(PheromoneMatrix); }