From c28168978b2f64162171e8a51d6330ae48784234 Mon Sep 17 00:00:00 2001 From: Toby Vincent Date: Sun, 5 Dec 2021 20:02:33 -0600 Subject: fixed csv output format and bug in stopwatch Co-authored-by: Neil Kollack --- src/Program/Analysis.cs | 81 +++++++++++++++---------------------------------- 1 file changed, 25 insertions(+), 56 deletions(-) (limited to 'src/Program/Analysis.cs') diff --git a/src/Program/Analysis.cs b/src/Program/Analysis.cs index d6c6082..f817200 100644 --- a/src/Program/Analysis.cs +++ b/src/Program/Analysis.cs @@ -10,83 +10,67 @@ namespace Program { public struct Result { - string Filename; - string ResultType; - int Iterations; public double MinCut; public TimeSpan Runtime; - public Result(string filename, String type, int interations, double minCut, TimeSpan runtime) + public Result(double minCut, TimeSpan runtime) { - Filename = filename; - ResultType = type; - Iterations = interations; MinCut = minCut; Runtime = runtime; } - public string ToCSV() => $"{Filename},{ResultType},{Iterations},{MinCut},{Runtime}"; - public override string ToString() => $"{ResultType} Value: {MinCut}, Runtime: {Runtime} (i: {Iterations})"; - + public override string ToString() => $"{MinCut} @ {Runtime.TotalMilliseconds}"; } class Analysis { - Result ExactResult; - Dictionary> ApproxResults = new(); - Func[] IterFuncs = new Func[] { n => 10, n => n, n => (int)(Math.Pow(n, 2) * Math.Log(n, Math.E)) }; Graph _graph; - public string Filename; + Result _exact = new(); + Result Exact { get => _exact.Equals(default(Result)) ? (_exact = CalculateExact()) : _exact; } + int Repetitions; - public Analysis(Graph graph, string filename) + public Analysis(Graph graph, int repetitions) { _graph = graph; - Filename = filename; + Repetitions = repetitions; } - public void Analize() + public List Analize(int iterations) { - Console.WriteLine($"\nFilename: {Filename}"); - Stopwatch stopwatch = new(); - double result = CalculateExact(); - ExactResult = new(Filename, "Exact", 1, result, stopwatch.Elapsed); - - Console.WriteLine(ExactResult); - - foreach (var iterFunc in IterFuncs) + List results = new(); + results.Add(Exact); + Console.WriteLine($"Exact {Exact}"); + for (int i = 0; i < Repetitions; i++) { - int iterations = iterFunc(_graph.Nodes.Count); - ApproxResults.Add(iterations, new()); - - for (int i = 0; i < 5; i++) - { - stopwatch.Restart(); - result = CalculateApprox(iterations); - Result approxResult = new(Filename, "Apprx", iterations, result, stopwatch.Elapsed); - ApproxResults[iterations].Add(approxResult); - Console.WriteLine(approxResult); - } + results.Add(CalculateApprox(iterations)); + Console.WriteLine($"Contract_{i + 1} {results.Last()}"); } + + return results; } - double CalculateExact() + Result CalculateExact() { + Stopwatch stopwatch = Stopwatch.StartNew(); double result = double.MaxValue; + // loop t for all nodes foreach (var node in _graph.Nodes.Keys.ToList().Skip(1)) { var graphClone = (Graph)_graph.Clone(); + // take min of current result and new result double currentResult = graphClone.MaxFlow(0, node); if (currentResult != 0) result = Math.Min(result, currentResult); } - Console.WriteLine(); - return result; + + return new(result, stopwatch.Elapsed); } - double CalculateApprox(int iterations) + Result CalculateApprox(int iterations) { + Stopwatch stopwatch = Stopwatch.StartNew(); double result = double.MaxValue; #region Parallel_Loop @@ -99,22 +83,7 @@ namespace Program }); #endregion - return result; - } - - public List ToCSV() - { - List lines = new(); - lines.Add(ExactResult.ToCSV()); - ApproxResults.Values.ToList().ForEach(approx => approx.ForEach(r => lines.Add(r.ToCSV()))); - return lines; - } - - public override string ToString() - { - string outString = ExactResult.ToString(); - ApproxResults.Values.ToList().ForEach(r => outString += String.Join("", r)); - return outString; + return new(result, stopwatch.Elapsed); } } } \ No newline at end of file -- cgit v1.2.3-70-g09d2