private void SolveMultiple100() { KnapsackInput input; for (int i = 0; i < 100; i++) { input = new KnapsackInput(); input.GenerateRandomItems(); var stopwatch = new Stopwatch(); var solverBaB = new BranchAndBoundSolver(input.Items, input.Capacity); var solverRand = new RandomSolver(input.Items, input.Capacity); var solverGreedy = new GreedySolver(input.Items, input.Capacity); var solverDynamic = new DynamicProgrammingSolver(input.Items, input.Capacity); stopwatch.Start(); solverBaB.Solve(); stopwatch.Stop(); timeResultsBaB.Add(new DataPoint(i, stopwatch.Elapsed.TotalMilliseconds)); stopwatch.Restart(); solverRand.Solve(); stopwatch.Stop(); timeResultsRand.Add(new DataPoint(i, stopwatch.Elapsed.TotalMilliseconds)); stopwatch.Restart(); solverGreedy.Solve(); stopwatch.Stop(); timeResultsGreedy.Add(new DataPoint(i, stopwatch.Elapsed.TotalMilliseconds)); stopwatch.Restart(); solverDynamic.Solve(); stopwatch.Stop(); timeResultsDynamic.Add(new DataPoint(i, stopwatch.Elapsed.TotalMilliseconds)); } timeAverageGreedy = Math.Round((timeResultsGreedy.Sum(x => x.Y) / 100), 4); timeAverageRand = Math.Round((timeResultsRand.Sum(x => x.Y) / 100), 4); timeAverageBaB = Math.Round((timeResultsBaB.Sum(x => x.Y) / 100), 4); timeAverageDynamic = Math.Round((timeResultsDynamic.Sum(x => x.Y) / 100), 4); }
private void GenerateRandom_Click(object sender, RoutedEventArgs e) { input = new KnapsackInput(); input.GenerateRandomItems(); allItemsGrid.DataContext = input.Items; itemsGrid.DataContext = null; resultTextBlock.Text = "Result of the solution will be displayed here"; }
public static void Main() { /* var input1 = ReadInput("./../../SampleInputs/easy20.txt"); * PrintResults(input1);*/ var input2 = new KnapsackInput() { Capacity = 25, ExpectedResult = 44, Items = new List <Item>() { new Item { Name = "1", Weight = 2, Value = 4 }, new Item { Name = "2", Weight = 4, Value = 7 }, new Item { Name = "3", Weight = 6, Value = 10 }, new Item { Name = "4", Weight = 8, Value = 13 }, new Item { Name = "5", Weight = 10, Value = 16 }, new Item { Name = "6", Weight = 12, Value = 19 }, new Item { Name = "7", Weight = 14, Value = 22 }, new Item { Name = "8", Weight = 16, Value = 25 } } }; // PrintResults(input2); var input3 = new KnapsackInput() { Capacity = 10, ExpectedResult = 6, Items = new List <Item>() { new Item() { Name = "1", Value = 40, Weight = 4 }, new Item() { Name = "2", Value = 42, Weight = 7 }, new Item() { Name = "3", Value = 25, Weight = 5 }, new Item() { Name = "4", Value = 12, Weight = 3 } } }; // PrintResults(input3); var input4 = new KnapsackInput() { Capacity = 16, ExpectedResult = 90, Items = new List <Item>() { new Item { Name = "1", Value = 40, Weight = 2 }, new Item { Name = "2", Value = 30, Weight = 5 }, new Item { Name = "3", Value = 50, Weight = 10 }, new Item { Name = "4", Value = 10, Weight = 5 } } }; // PrintResults(input4); var inputManually = new KnapsackInput(29, 9, 2, 3); // PrintResults(inputManually); var inputRandom = new KnapsackInput(); inputRandom.GenerateRandomItems(); inputRandom.PrintAllItems(); PrintResults(inputRandom); }