public int BruteForce() { var solution = new BruteForce(); return(solution.RomanToInt(Input)); }
// Measure algorithms time private static TestResult RunAlgorithms(Dataset dataSet, int measurementCount) { Console.WriteLine("{0}: starting calculating dateset {1}", DateTime.Now, dataSet.GetFilePath()); var csvBuilder = new StringBuilder(); csvBuilder.AppendLine(TestResultHelper.GetInstanceResultHeader()); var stopwatch = new Stopwatch(); double BFTotal = 0; double BandBTotal = 0; double dpTotal = 0; double reduxTotal = 0; double reduxErrSum = 0; double reduxMaxErr = 0; double greedyTotal = 0; double greedyErrSum = 0; double greedyMaxErr = 0; double ftpasTotal = 0; for (int i = 1; i <= dataSet.LastInstanceId; i++) { Console.WriteLine($"{DateTime.Now}::{dataSet.Name}::{dataSet.InstanceItemsCount}::{i}"); // Load instance and solution var instance = InstanceLoader.LoadInstanceAsync(dataSet, i); if (instance == null) { continue; } int optimalResult = InstanceLoader.LoadSolution(dataSet, i); double bruteForceTimeAvg = 0; double branchAndBoundTimeAvg = 0; double dynamicProgrammingAvg = 0; double greedyAvg = 0; double reduxAvg = 0; int[] greedyHeuristicResult = null; int[] reduxHeuristicResult = null; for (int j = 0; j < measurementCount; j++) { stopwatch.Reset(); // Start Brute Force stopwatch.Start(); var bruteForceResult = BruteForce.Solve(0, instance.Weights, instance.Prices, 0, 0, instance.MaxWeight, instance.MinPrice, new int[instance.N + 1], new int[instance.N + 1]); stopwatch.Stop(); CheckResult(instance, bruteForceResult[instance.N], optimalResult, dataSet.Name, nameof(BruteForce)); bruteForceTimeAvg += stopwatch.Elapsed.TotalMilliseconds; stopwatch.Reset(); // Start Branch and Bound stopwatch.Start(); var branchAndBoundResult = BranchAndBound.Solve(0, instance.Weights, instance.Prices, 0, 0, instance.MaxWeight, new int[instance.N + 1], new int[instance.N + 1]); //BranchAndBound.Decide(0, instance.Weights, instance.Prices, 0, 0, instance.MaxWeight, 324); stopwatch.Stop(); CheckResult(instance, branchAndBoundResult[instance.N], optimalResult, dataSet.Name, nameof(BranchAndBound)); branchAndBoundTimeAvg += stopwatch.Elapsed.TotalMilliseconds; stopwatch.Reset(); // Start Dynamic programming stopwatch.Start(); var dynamicProgrammingResult = DynamicProgramming.Solve(instance.Items, instance.MaxWeight); stopwatch.Stop(); CheckResult(instance, dynamicProgrammingResult[instance.N], optimalResult, dataSet.Name, nameof(DynamicProgramming)); dynamicProgrammingAvg += stopwatch.Elapsed.TotalMilliseconds; stopwatch.Reset(); // Start Greedy heuristic stopwatch.Start(); greedyHeuristicResult = GreedyHeuristic.Solve(instance.Items.ToList(), instance.MaxWeight); stopwatch.Stop(); //CheckResult(instance, greedyHeuristicResult[instance.N], optimalPrice, dataSet.Name, nameof(GreedyHeuristic)); greedyAvg += stopwatch.Elapsed.TotalMilliseconds; stopwatch.Reset(); // Start Redux heuristic stopwatch.Start(); reduxHeuristicResult = ReduxHeuristic.Solve(instance.Items.ToList(), instance.MaxWeight); stopwatch.Stop(); reduxAvg += stopwatch.Elapsed.TotalMilliseconds; stopwatch.Reset(); } BFTotal += Avg(bruteForceTimeAvg, measurementCount); BandBTotal += Avg(branchAndBoundTimeAvg, measurementCount); dpTotal += Avg(dynamicProgrammingAvg, measurementCount); greedyTotal += Avg(greedyAvg, measurementCount); reduxTotal += Avg(reduxAvg, measurementCount); csvBuilder.AppendLine(TestResultHelper.FormatInstanceResultRow( instance.Id, instance.N, Avg(bruteForceTimeAvg, measurementCount), Avg(branchAndBoundTimeAvg, measurementCount), Avg(dynamicProgrammingAvg, measurementCount), Avg(greedyAvg, measurementCount), Avg(reduxAvg, measurementCount), 0 )); } Console.WriteLine("{0}: calculating dataset {1} ended", DateTime.Now, dataSet.GetFilePath()); return(new TestResult { InstanceSize = dataSet.InstanceItemsCount, InstancesResults = csvBuilder.ToString(), BFTime = Avg(BFTotal, dataSet.InstancesCount), BandBTime = Avg(BandBTotal, dataSet.InstancesCount), GreedyHeuristicTime = Avg(greedyTotal, dataSet.InstancesCount), ReduxHeuristicTime = Avg(reduxTotal, dataSet.InstancesCount), DPTime = Avg(dpTotal, dataSet.InstancesCount), FTPASTime = Avg(ftpasTotal, dataSet.InstancesCount), GreedyRelErr = Avg(greedyErrSum, dataSet.InstancesCount), GreedyMaxErr = greedyMaxErr, ReduxRelErr = Avg(reduxErrSum, dataSet.InstancesCount), ReduxMaxErr = reduxMaxErr }); }
static void Main(string[] args) { Logger.WriteLine("String Pattern Matching"); // Variable to hold whether or not to continue the program after each termination string keepPlaying; // Loop while the user wants to continue do { // Get user input of text, pattern, and which algorithm to run var(text, pattern, algorithm) = GetUserInput(); // Run one algorithm if user did not specify to run all if (algorithm != "all" && algorithm != "ALL") { // Get the index and comparisons based on which algorithm the user chose var(index, comparisons) = algorithm switch { var x when x == "BF" || x == "bf" => BruteForce.Run(text, pattern), var y when y == "BMH" || y == "bmh" => BMH.Run(text, pattern), var z when z == "BM" || z == "bm" => BM.Run(text, pattern), _ => throw new Exception("Invalid algorithm string"), }; // If the pattern was not found if (index == -1) { Logger.Write("\nPattern was not found in the given text. "); } // Else the pattern was found else { Logger.Write($"\nPattern was found at index {index}. "); } Logger.WriteLine($"There were {comparisons} comparisons made."); } else { // Get the index and comparisons from all algorithms var(indexBF, comparisonsBF) = BruteForce.Run(text, pattern); var(indexBMH, comparisonsBMH) = BMH.Run(text, pattern); var(indexBM, comparisonsBM) = BM.Run(text, pattern); // All algorithms should return the same index so we only need to use indexBF // If pattern was not found if (indexBF == -1) { Logger.WriteLine("\nPattern was not found in the given text. "); } // Else pattern was found else { Logger.WriteLine($"\nPattern was found at index {indexBF}. "); } Logger.WriteLine($"There were {comparisonsBF} comparisons made by the Brute Force Algorithm."); Logger.WriteLine($"There were {comparisonsBMH} comparisons made by the Boyer-Moore-Horspool Algorithm."); Logger.WriteLine($"There were {comparisonsBM} comparisons made by the Boyer-Moore Algorithm."); } // Loop until valid input of y/n do { Logger.WriteLine("\nWould you like to repeat program? y/n "); keepPlaying = Logger.ReadLine(); }while (keepPlaying != "y" && keepPlaying != "Y" && keepPlaying != "n" && keepPlaying != "N"); Logger.WriteLine(); }while (keepPlaying == "y" || keepPlaying == "Y"); Logger.LogToFile(); Console.WriteLine("\nLog outputted to logs/ directory."); Console.WriteLine("\nPress enter to exit..."); Console.ReadLine(); }
/** * Frecuency of occurrence of an itemset: Counts in how many transactions a given itemset occurs. * itemset : Array of codes of a itemset. **/ public int SupportCount(int[] itemset) { List <List <int> > dataBase = context.Transactions.Select(t => t.Value.Items).ToList(); return(BruteForce.SupportCount(itemset, dataBase)); }