public static void RunGreedyAlgorithm(double maxVehicleDistance, DataSet set, OutputOptions options = OutputOptions.Console, string outPutFileName = null) { List <string> problems = ReadSet(set); List <FileLine> lines = new List <FileLine>(); foreach (string problem in problems) { Console.WriteLine(set + " " + problem); GreedyAlgorithm greedyAlgorithm = new GreedyAlgorithm(maxVehicleDistance); Problem problemInstance = Reader.ReadProblem(set, problem); greedyAlgorithm.LoadProblemInstance(problemInstance); TimeSpan executionTime = greedyAlgorithm.Solve(); Solution sol = greedyAlgorithm.GetSolution(); string result; switch (options) { case OutputOptions.Console: result = sol != null?sol.Cost.ToString() : "no solution found"; Console.WriteLine(problemInstance.Name + " Perfect Solution: " + problemInstance.Solution.Cost + " GreedyAlgorithm cost: " + result); break; case OutputOptions.File: lines.Add(new FileLine(problemInstance, sol, executionTime, 1)); break; default: break; } } if (options == OutputOptions.File) { WriteToFile(lines, outPutFileName); } }
public BinaryGreedyRandomGeneration(IEvaluation <bool> evaluation, int?greedyIterations = null, int?seed = null) : base(evaluation.pcConstraint) { _greedyIterations = greedyIterations; brg = new BinaryRandomGeneration(evaluation.pcConstraint, seed); ga = new GreedyAlgorithm(evaluation, seed); }
public IActionResult GreedyPost([FromBody] Data data) { if (data.Count <= 0) { return(BadRequest("Count <= 0")); } else if (data.Time.Count != data.Count) { return(BadRequest("Time.Count != Count")); } else if (data.Matrix.Count != data.Count) { return(BadRequest("Matrix.Count != Count")); } for (int i = 0; i < data.Matrix.Count; i++) { var list = data.Matrix[i]; if (list.Count != data.Count) { return(BadRequest($"Matrix[{i}].Count != Count")); } } var algorithm = new GreedyAlgorithm(); var result = algorithm.RunAlgorithm(data); return(new JsonResult(result)); }
private async void ComputeGreedy(IEnumerable <int> digits, float valueToCalculate) { GreedyAlgorithm algorithm = new GreedyAlgorithm(digits); var watch = System.Diagnostics.Stopwatch.StartNew(); var result = await Task.Run(() => { return(algorithm.CalculateResult(valueToCalculate)); }); watch.Stop(); if (viewModel.Time) { viewModel.ExeTimeGreedy = $"{watch.Elapsed.TotalMilliseconds.ToString()}ms"; } listGreedy.Items.Clear(); if (result != null) { foreach (var r in result) { var item = new ListBoxItem(); item.Content = $"{r.Value} x {r.Key}"; listGreedy.Items.Add(item); } } else { var item = new ListBoxItem(); item.Content = "Cannot solve"; listGreedy.Items.Add(item); } }
public void TestMethod1() { Problem problem = Reader.ReadProblem(filepath); GreedyAlgorithm greedyAlgorithm = new GreedyAlgorithm(1000.0); greedyAlgorithm.LoadProblemInstance(problem); greedyAlgorithm.Solve(); Solution solution = greedyAlgorithm.GetSolution(); Assert.IsNotNull(solution); }
static void Main(string[] args) { var inf = int.MaxValue; var arr = new int[, ] { { inf, 21, 53, 11, 72, 13, 54, 25, 66, 57 }, { 21, inf, 32, 28, 97, 16, 95, 54, 73, 23 }, { 53, 32, inf, 86, 65, 24, 83, 12, 31, 40 }, { 11, 28, 86, inf, 31, 42, 13, 24, 45, 16 }, { 72, 97, 65, 31, inf, 78, 57, 76, 45, 65 }, { 13, 16, 24, 42, 78, inf, 4, 95, 15, 35 }, { 54, 95, 83, 13, 57, 4, inf, 11, 72, 83 }, { 25, 54, 12, 24, 76, 95, 11, inf, 44, 44 }, { 66, 73, 31, 45, 45, 15, 72, 44, inf, 59 }, { 57, 23, 40, 16, 65, 35, 83, 44, 95, inf } }; var graph = new Graph(arr); Console.WriteLine($"{"algorithm name",40} {"iterations",15} {"result",10} {"time (ms)",15}"); ReturnData bruteForceSearch = BruteForceSearch.Search(graph); Console.WriteLine($"{bruteForceSearch.AlgorithmName,40} {bruteForceSearch.Iterations,15} " + $"{bruteForceSearch.Result,10} {bruteForceSearch.TimeInMs,15}"); ReturnData greedyAlgorithm = GreedyAlgorithm.Search(graph); Console.WriteLine($"{greedyAlgorithm.AlgorithmName,40} {greedyAlgorithm.Iterations,15} " + $"{greedyAlgorithm.Result,10} {greedyAlgorithm.TimeInMs,15}"); ReturnData branchAndBoundAlgorithm = BranchAndBoundAlgorithm.Search(graph); Console.WriteLine($"{branchAndBoundAlgorithm.AlgorithmName,40} {branchAndBoundAlgorithm.Iterations,15} " + $"{branchAndBoundAlgorithm.Result,10} {branchAndBoundAlgorithm.TimeInMs,15}"); ReturnData localSearch2Algorithm = LocalSearch2.Search(graph); Console.WriteLine($"{localSearch2Algorithm.AlgorithmName,40} {localSearch2Algorithm.Iterations,15} " + $"{localSearch2Algorithm.Result,10} {localSearch2Algorithm.TimeInMs,15}"); ReturnData simulatedAnnealingAlgorithm_lin = SimulatedAnnealing.Search(graph, SimulatedAnnealing.GetNewTemperature_Linear); Console.WriteLine($"{simulatedAnnealingAlgorithm_lin.AlgorithmName + "(lin)",40} {simulatedAnnealingAlgorithm_lin.Iterations,15} " + $"{simulatedAnnealingAlgorithm_lin.Result,10} {simulatedAnnealingAlgorithm_lin.TimeInMs,15}"); ReturnData simulatedAnnealingAlgorithm_pow = SimulatedAnnealing.Search(graph, SimulatedAnnealing.GetNewTemperature_Power); Console.WriteLine($"{simulatedAnnealingAlgorithm_pow.AlgorithmName + "(pow)",40} {simulatedAnnealingAlgorithm_pow.Iterations,15} " + $"{simulatedAnnealingAlgorithm_pow.Result,10} {simulatedAnnealingAlgorithm_pow.TimeInMs,15}"); }
private IAlgorithm <ChessRepresentation, BaseMove> GetAlgorithm(Type algorithmType, Type evaluatorType) { var generator = new MoveGenerator(_mechanism); var applier = new MoveApplier(_mechanism); var evaluator = (IEvaluator <ChessRepresentation>)Activator.CreateInstance(evaluatorType, new[] { _mechanism }); // TODO : A bit hacky, refactor later! if (algorithmType == typeof(MinimaxAlgorithm <ChessRepresentation, BaseMove>)) { var minimax = new MinimaxAlgorithm <ChessRepresentation, BaseMove>(evaluator, generator, applier) { MaxDepth = (int)numericUpDown1.Value }; return(minimax); } if (algorithmType == typeof(AlphaBetaAlgorithm <ChessRepresentation, BaseMove>)) { var ab = new AlphaBetaAlgorithm <ChessRepresentation, BaseMove>(evaluator, generator, applier) { MaxDepth = (int)numericUpDown1.Value }; return(ab); } if (algorithmType == typeof(MinimaxAverageAlgorithm <ChessRepresentation, BaseMove>)) { var minimaxAvg = new MinimaxAverageAlgorithm <ChessRepresentation, BaseMove>(evaluator, generator, applier) { MaxDepth = (int)numericUpDown1.Value }; return(minimaxAvg); } if (algorithmType == typeof(GreedyAlgorithm <ChessRepresentation, BaseMove>)) { var greedy = new GreedyAlgorithm <ChessRepresentation, BaseMove>(evaluator, generator, applier); return(greedy); } if (algorithmType == typeof(RandomAlgorithm <ChessRepresentation, BaseMove>)) { var randomAlgorithm = new RandomAlgorithm <ChessRepresentation, BaseMove>(generator); return(randomAlgorithm); } throw new ArgumentOutOfRangeException(nameof(algorithmType)); }
public void Test1() { var evaluator = new TestCase1.Evaluator(); var generator = new TestCase1.Generator(); var applier = new TestCase1.Applier(); var algorithm = new GreedyAlgorithm <TestCase1.State, TestCase1.Move>(evaluator, generator, applier); var initState = new TestCase1.State(1, int.MinValue); var move1 = algorithm.Calculate(initState); var state2 = applier.Apply(initState, move1); var move2 = algorithm.Calculate(state2); Assert.Equal('a', move1.Label); Assert.Equal('c', move2.Label); }
static void Main(string[] args) { List <Point> points = TspFileReader.ReadTspFile(@"C:\Users\QuocVinh\Downloads\Compressed\asc\asc\test\demo.tsp"); DoThi dothi = new DoThi(points, true); GreedyAlgorithm greedyAlgorithm = new GreedyAlgorithm(dothi); double greedyShortestTourDistance = greedyAlgorithm.Run(); Parameters parameters = new Parameters() { T0 = (1.0 / (dothi.Dimensions * greedyShortestTourDistance)) }; parameters.Show(); Solver solver = new Solver(parameters, dothi); List <double> results = solver.RunACS(); Console.ReadLine(); }
void Button_Click(object sender, RoutedEventArgs e) { if (!CheckInputData()) { return; } PrepareSetofItems(); var sack = new Sack(_W, SetOfItems); var brutalForce = new BrutalForceAlgorithm(sack); var greedy = new GreedyAlgorithm(sack); var dynamic = new DynamicAlgorithm(sack); ClearItems(); if (cbBF.IsChecked != null && cbBF.IsChecked.Value) { Compute(brutalForce); } Compute(greedy); Compute(dynamic); }
private static void LaunchGreedy() { int production_size = default; string[] production_names = default; float[] ecologies = default; int[] costs = default; int[] profits = default; int a = default; Console.WriteLine("Choose input data:\n" + "1 - from console;\n" + "2 - from file;\n"); int choose = int.Parse(Console.ReadLine()); if (choose == 1) { ReadStartInfoFromConsole(ref production_size, ref production_names, ref ecologies, ref costs, ref profits, ref a); } else if (choose == 2) { ReadStartInfoFromFile(ref production_size, ref production_names, ref ecologies, ref costs, ref profits, ref a); } GreedyAlgorithm algorithm = new GreedyAlgorithm(production_size, ecologies.ToList(), costs.ToList(), profits.ToList(), a); var rez = algorithm.Execute(); var rez_prod = ConvertNames(rez.Item1, production_names); var rez_cf = rez.Item2; Console.WriteLine("Rezult:"); rez_prod.ForEach(x => Console.WriteLine(x)); Console.WriteLine($"CF: {rez_cf}"); WriteResultToFile(rez_prod.ToArray(), rez_cf); }
private void ProcessAlgorithm(Algorithm algorithm) { try { var data = new List <List <int> >(); for (int i = 0; i < WorkersCount; i++) { var list = new List <int>(); for (int j = 0; j < TasksCount; j++) { list.Add(Int32.Parse(Controls[i.ToString() + " " + j.ToString()].Text)); } data.Add(list); } var dataCopy = data.Select(x => x.ToList()).ToList(); IAlgorithm algorithmHandler = new ElMansouryAlgorithm();// null; var dataCopy2 = data.Select(x => x.ToList()).ToList(); Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); var result = algorithmHandler.Handle(data); stopwatch.Stop(); var z = result.OrderBy(e => e[1]).Aggregate("", (current, ints) => current + $"Работник {ints[0] + 1} - работа {ints[1] + 1}" + Environment.NewLine); Controls.Add(new TextBox { Location = new Point(395, 587), Size = new Size(350, 350), Text = z, Multiline = true }); Stopwatch stopwatch2 = new Stopwatch(); stopwatch2.Start(); var result2 = new GreedyAlgorithm().Handle(dataCopy2); stopwatch2.Stop(); int cf = 0; for (int i = 0; i < result.Count; i++) { Controls[result[i][0].ToString() + " " + result[i][1].ToString()].BackColor = Color.LimeGreen; cf += dataCopy[result[i][0]][result[i][1]]; } int cf2 = 0; for (int i = 0; i < result2.Count; i++) { cf2 += dataCopy[result2[i][0]][result2[i][1]]; } CreateCfControls(cf, cf2); DisableOutputButtons(); CreateTimeControl(stopwatch.Elapsed, stopwatch2.Elapsed); ActiveControl = null; } catch (FormatException) { MessageBox.Show("Ефективність виконання задачі повинна бути числом."); } }
static void Main(string[] args) { QAPAlgorithm test = null; string command = ""; while (command != "exit") { command = Console.ReadLine(); switch (command) { case "gen": //Console.WriteLine("nOfGenerations: "); //int nOfGenerations = Int32.Parse(Console.ReadLine()); //Console.WriteLine("p_crossover: "); //double p_crossover = Double.Parse(Console.ReadLine()); //Console.WriteLine("p_mutation: "); //double p_mutation = Double.Parse(Console.ReadLine()); //Console.WriteLine("tourn_size: "); //int tourn_size = Int32.Parse(Console.ReadLine()); //Console.WriteLine("pop_size: "); //int pop_size = Int32.Parse(Console.ReadLine()); //Console.WriteLine("test_case: "); //int gen_test_case = Int32.Parse(Console.ReadLine()); //base test = new GeneticAlgorithm(100, 100, 0.7, 0.01, SelectionMethod.Tournament, 5, $"input{12}.txt"); test.Run(); //test selection test = new GeneticAlgorithm(100, 100, 0.7, 0.01, SelectionMethod.Roulette, 1, $"input{12}.txt"); test.Run(); test = new GeneticAlgorithm(100, 100, 0.7, 0.01, SelectionMethod.Tournament, 1, $"input{12}.txt"); test.Run(); test = new GeneticAlgorithm(100, 100, 0.7, 0.01, SelectionMethod.Tournament, 2, $"input{12}.txt"); test.Run(); test = new GeneticAlgorithm(100, 100, 0.7, 0.01, SelectionMethod.Tournament, 10, $"input{12}.txt"); test.Run(); test = new GeneticAlgorithm(100, 100, 0.7, 0.01, SelectionMethod.Tournament, 25, $"input{12}.txt"); test.Run(); test = new GeneticAlgorithm(100, 100, 0.7, 0.01, SelectionMethod.Tournament, 100, $"input{12}.txt"); test.Run(); //test mutation test = new GeneticAlgorithm(100, 100, 0.7, 0.00, SelectionMethod.Tournament, 5, $"input{12}.txt"); test.Run(); test = new GeneticAlgorithm(100, 100, 0.7, 0.02, SelectionMethod.Tournament, 5, $"input{12}.txt"); test.Run(); test = new GeneticAlgorithm(100, 100, 0.7, 0.05, SelectionMethod.Tournament, 5, $"input{12}.txt"); test.Run(); test = new GeneticAlgorithm(100, 100, 0.7, 0.10, SelectionMethod.Tournament, 5, $"input{12}.txt"); test.Run(); test = new GeneticAlgorithm(100, 100, 0.7, 0.20, SelectionMethod.Tournament, 5, $"input{12}.txt"); test.Run(); test = new GeneticAlgorithm(100, 100, 0.7, 0.50, SelectionMethod.Tournament, 5, $"input{12}.txt"); test.Run(); test = new GeneticAlgorithm(100, 100, 0.7, 1.00, SelectionMethod.Tournament, 5, $"input{12}.txt"); test.Run(); //test crossover test = new GeneticAlgorithm(100, 100, 0.0, 0.01, SelectionMethod.Tournament, 5, $"input{12}.txt"); test.Run(); test = new GeneticAlgorithm(100, 100, 0.2, 0.01, SelectionMethod.Tournament, 5, $"input{12}.txt"); test.Run(); test = new GeneticAlgorithm(100, 100, 0.4, 0.01, SelectionMethod.Tournament, 5, $"input{12}.txt"); test.Run(); test = new GeneticAlgorithm(100, 100, 0.5, 0.01, SelectionMethod.Tournament, 5, $"input{12}.txt"); test.Run(); test = new GeneticAlgorithm(100, 100, 0.6, 0.01, SelectionMethod.Tournament, 5, $"input{12}.txt"); test.Run(); test = new GeneticAlgorithm(100, 100, 0.8, 0.01, SelectionMethod.Tournament, 5, $"input{12}.txt"); test.Run(); test = new GeneticAlgorithm(100, 100, 1.0, 0.01, SelectionMethod.Tournament, 5, $"input{12}.txt"); test.Run(); //test pop-size test = new GeneticAlgorithm(100, 10, 0.7, 0.01, SelectionMethod.Tournament, 5, $"input{12}.txt"); test.Run(); test = new GeneticAlgorithm(100, 20, 0.7, 0.01, SelectionMethod.Tournament, 5, $"input{12}.txt"); test.Run(); test = new GeneticAlgorithm(100, 50, 0.7, 0.01, SelectionMethod.Tournament, 5, $"input{12}.txt"); test.Run(); test = new GeneticAlgorithm(100, 100, 0.7, 0.01, SelectionMethod.Tournament, 5, $"input{12}.txt"); test.Run(); test = new GeneticAlgorithm(100, 200, 0.7, 0.01, SelectionMethod.Tournament, 5, $"input{12}.txt"); test.Run(); test = new GeneticAlgorithm(100, 500, 0.7, 0.01, SelectionMethod.Tournament, 5, $"input{12}.txt"); test.Run(); //test gen-count test = new GeneticAlgorithm(10, 100, 0.7, 0.01, SelectionMethod.Tournament, 5, $"input{12}.txt"); test.Run(); test = new GeneticAlgorithm(20, 100, 0.7, 0.01, SelectionMethod.Tournament, 5, $"input{12}.txt"); test.Run(); test = new GeneticAlgorithm(50, 100, 0.7, 0.01, SelectionMethod.Tournament, 5, $"input{12}.txt"); test.Run(); test = new GeneticAlgorithm(100, 100, 0.7, 0.01, SelectionMethod.Tournament, 5, $"input{12}.txt"); test.Run(); test = new GeneticAlgorithm(200, 100, 0.7, 0.01, SelectionMethod.Tournament, 5, $"input{12}.txt"); test.Run(); test = new GeneticAlgorithm(500, 100, 0.7, 0.01, SelectionMethod.Tournament, 5, $"input{12}.txt"); test.Run(); //test both test = new GeneticAlgorithm(500, 500, 0.7, 0.01, SelectionMethod.Tournament, 5, $"input{12}.txt"); test.Run(); Console.WriteLine("done!"); break; case "grd": Console.WriteLine("starting_point: "); int starting_point = Int32.Parse(Console.ReadLine()); Console.WriteLine("test_case: "); int grd_test_case = Int32.Parse(Console.ReadLine()); test = new GreedyAlgorithm(starting_point, $"input{grd_test_case}.txt"); break; case "rnd": Console.WriteLine("best_of: "); int best_of = Int32.Parse(Console.ReadLine()); Console.WriteLine("test_case: "); int rnd_test_case = Int32.Parse(Console.ReadLine()); test = new RandomAlgorithm(best_of, $"input{rnd_test_case}.txt"); break; default: Console.WriteLine("wut"); break; } if (test != null) { test.Input.ReadFile(); Console.WriteLine(test.Evaluate(test.Run())); } } }