public static ResultData Run(BaseGraph graph, Options options, Random rnd, IExportGraph graphExport) { var stopwatch = new Stopwatch(); stopwatch.Start(); graph.InitializeGraph(); graph.InitializeAnts(options.NumberOfAnts); graph.ColorVerticesRandomly(options.NumberOfPartitions); graph.CalculateLocalCostFunction(); var bestCost = graph.GetGlobalCostFunction(); var bestCostIteration = 0; var bestDistribution = (Vertex[])graph.Vertices.Clone(); var iteration = 0; while (bestCost > 0 && iteration < options.NumberOfIterations) { // At a given iteration each ant moves from the current position // to the adjacent vertex with the lowest local cost, // i.e. the vertex with the greatest number of constraints (neighbors of a different color). // which increases the local cost. foreach (var ant in graph.Ants.Keys.ToArray()) { // The agent or ant moves to the worst adjacent vertex with a // probability pm (it moves randomly to any other adjacent vertex with probability 1 − pm) Vertex vertexWithAnt; if (rnd.NextDouble() < options.MovingProbability) { // Move the ant to the worst adjacent vertex. vertexWithAnt = graph.MoveAntToVertexWithLowestCost(ant); } else { // Move randomly to any adjacent vertex. vertexWithAnt = graph.MoveAntToAnyAdjacentVertex(ant); } // Save the data of the vertex on witch ant moved. int oldColor = vertexWithAnt.Color; int vertexWithAntID = vertexWithAnt.ID; Vertex vertexWithNewColor; // Replaces the color which belong to ant with a new color. if (rnd.NextDouble() < options.ColoringProbability) { // Change vertex color to the best possible color. vertexWithNewColor = graph.ColorVertexWithBestColor(ant); } else { // Change to a randomly chosen color. vertexWithNewColor = graph.ColorVertexWithRandomColor(ant, options.NumberOfPartitions); } // Keep balance (Change a randomly chosen vertex with low local cost // from the new to the old color). Vertex vertexWhichKeepBalance = graph.KeepBalance(options.NumberOfVerticesForBalance, vertexWithAntID, oldColor, vertexWithNewColor.Color); // For the chosen vertices and all adjacent vertices update local cost function. graph.UpdateLocalCostFunction(vertexWhichKeepBalance, vertexWithNewColor); // Get best global cost. var globalCost = graph.GetGlobalCostFunction(); if (globalCost < bestCost) { bestCost = globalCost; bestCostIteration = iteration; bestDistribution = (Vertex[])graph.Vertices.Clone(); } } iteration++; } stopwatch.Stop(); graphExport.ExportGraph(bestDistribution); var result = new ResultData(bestCost, bestCostIteration, stopwatch.ElapsedMilliseconds); return(result); }
static int Main(string[] args) { XmlConfigurator.Configure(); Action <Dictionary <Vertex, List <Edge> >, OptionsBase> exportAct = (graph, options) => { IExportGraph exportFile = null; var dataWriter = new FileWriter(options.ExportGraphFileName); if (options.ExportGraphFileType == GraphFileType.Dimacs) { exportFile = new DimacsFileExport(dataWriter); } else if (options.ExportGraphFileType == GraphFileType.FullWeightedMetis) { exportFile = new FullWeightedMetisFileExport(dataWriter); } else if (options.ExportGraphFileType == GraphFileType.UnweightedMetis) { exportFile = new UnweightedMetisFileExport(dataWriter); } else if (options.ExportGraphFileType == GraphFileType.D3Json) { exportFile = new D3JsonFileExport(dataWriter); } if (exportFile == null) { throw new ApplicationException("The application does not support file export type."); } exportFile.ExportGraph(graph); }; Func <RandomGraphOptions, Dictionary <Vertex, List <Edge> > > randomGraphFunc = options => { IRandomGraph randomGraph = null; if (options.GraphAlgortiham == RandomGraphType.ErdosRenyiEdges) { randomGraph = new ErdosRenyiModel(options.NumberOfVertices, options.NumberOfEdges); } //else if (options.GraphAlgortiham == RandomGraphType.ErdosRenyiPercent) //{ // randomGraph = new ErdosRenyiModel(int.Parse(args[1]), double.Parse(args[2])); //} if (randomGraph == null) { throw new ApplicationException("The application does not support random graph model."); } var graph = randomGraph.GenerateGraph(); return(graph); }; Func <ConvertGraphOptions, Dictionary <Vertex, List <Edge> > > parseGraphFromFileFunc = options => { var fileLoader = new FileLoader(options.InputGraphFileName); ParseGraphBase graphParser = null; if (options.ImportGraphFileType == GraphFileType.Dimacs) { graphParser = new ParseDimacsGraph(fileLoader); } if (graphParser == null) { throw new ApplicationException("The application does not support input graph model."); } var graph = graphParser.ParseGraph(); return(graph); }; var result = Parser.Default.ParseArguments <RandomGraphOptions, ConvertGraphOptions>(args); var exitCode = result .MapResult( (RandomGraphOptions options) => { if (options.Verbose) { System.Console.WriteLine("Filenames: {0}", 1); } else { System.Console.WriteLine("Processing..."); var graph = randomGraphFunc(options); exportAct(graph, options); } return(0); }, (ConvertGraphOptions options) => { var graph = parseGraphFromFileFunc(options); exportAct(graph, options); return(0); }, errors => 1); return(exitCode); }