aboutsummaryrefslogtreecommitdiffstats
path: root/src/Program/Analysis.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Program/Analysis.cs')
-rw-r--r--src/Program/Analysis.cs76
1 files changed, 59 insertions, 17 deletions
diff --git a/src/Program/Analysis.cs b/src/Program/Analysis.cs
index d0a49cb..d6c6082 100644
--- a/src/Program/Analysis.cs
+++ b/src/Program/Analysis.cs
@@ -2,20 +2,32 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
+using System.Linq;
+using System.Threading.Tasks;
using NetworkFlow;
namespace Program
{
public struct Result
{
+ string Filename;
+ string ResultType;
+ int Iterations;
public double MinCut;
public TimeSpan Runtime;
- public Result(double minCut, TimeSpan runtime)
+ public Result(string filename, String type, int interations, 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})";
+
}
class Analysis
@@ -24,17 +36,22 @@ namespace Program
Dictionary<int, List<Result>> ApproxResults = new();
Func<int, int>[] IterFuncs = new Func<int, int>[] { n => 10, n => n, n => (int)(Math.Pow(n, 2) * Math.Log(n, Math.E)) };
Graph _graph;
+ public string Filename;
- public Analysis(Graph graph)
+ public Analysis(Graph graph, string filename)
{
_graph = graph;
+ Filename = filename;
}
public void Analize()
{
+ Console.WriteLine($"\nFilename: {Filename}");
Stopwatch stopwatch = new();
- double result = CalculateExact((Graph)_graph.Clone());
- ExactResult = new(result, stopwatch.Elapsed);
+ double result = CalculateExact();
+ ExactResult = new(Filename, "Exact", 1, result, stopwatch.Elapsed);
+
+ Console.WriteLine(ExactResult);
foreach (var iterFunc in IterFuncs)
{
@@ -44,34 +61,59 @@ namespace Program
for (int i = 0; i < 5; i++)
{
stopwatch.Restart();
- result = CalculateApprox(_graph, iterations);
- ApproxResults[iterations].Add(new(result, stopwatch.Elapsed));
+ result = CalculateApprox(iterations);
+ Result approxResult = new(Filename, "Apprx", iterations, result, stopwatch.Elapsed);
+ ApproxResults[iterations].Add(approxResult);
+ Console.WriteLine(approxResult);
}
}
}
- double CalculateExact(Graph graph) => graph.MaxFlow(0, graph.Nodes.Count - 1);
+ double CalculateExact()
+ {
+ 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;
+ }
- double CalculateApprox(Graph graph, int iterations)
+ double CalculateApprox(int iterations)
{
double result = double.MaxValue;
- for (int j = 0; j < iterations; j++){
+ #region Parallel_Loop
+ Parallel.For(0, iterations, j =>
+ {
var graphClone = (Graph)_graph.Clone();
- result = Math.Min(result, graphClone.Contraction());
- }
+ double currentResult = graphClone.Contraction();
+ if (currentResult != 0)
+ result = Math.Min(result, currentResult);
+ });
+ #endregion
return result;
}
- public override string ToString()
+ public List<string> ToCSV()
{
- string outString = $"Exact Value: {ExactResult.MinCut}, Exact Runtime: {ExactResult.Runtime}\n";
-
- foreach (var results in ApproxResults)
- foreach (var result in results.Value)
- outString += $"Approx Value: {result.MinCut}, Approx Runtime: {result.Runtime}";
+ List<string> 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;
}
}