/// <summary> /// Creates new algorithm instance /// </summary> /// <param name="input">The input</param> public KnapsackDynamicAlgorithm(KnapsackInput <int, int> input) { this.Input = input; this.Capacity = input.Capacity; this.Variables = input.Variables; this.memorization = new IntegerTableMemorization(input.Capacity + 1, input.Variables + 1); }
public static void PrintResults(KnapsackInput input) { IList <KnapsackSolver> solvers = new List <KnapsackSolver>() { new BranchAndBoundSolver(input.Items, input.Capacity), new GreedySolver(input.Items, input.Capacity), new DynamicProgrammingSolver(input.Items, input.Capacity) }; Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine("Max Capacity is {0}", input.Capacity); Console.ForegroundColor = ConsoleColor.White; foreach (var solver in solvers) { Stopwatch sw = new Stopwatch(); sw.Start(); var solution = solver.Solve(); sw.Stop(); Console.WriteLine(solution); Console.WriteLine("Elapsed = {0}\n", sw.Elapsed); Console.ReadKey(); } }
private void UseSet2_Click(object sender, RoutedEventArgs e) { input = new KnapsackInput() { Capacity = 16, Items = new List <Item>() { new Item() { Name = "1", Value = 50, Weight = 5 }, new Item() { Name = "2", Value = 99, Weight = 9 }, new Item() { Name = "3", Value = 18, Weight = 2 }, new Item() { Name = "4", Value = 15, Weight = 3 }, new Item() { Name = "5", Value = 42, Weight = 7 } } }; allItemsGrid.DataContext = input.Items; itemsGrid.DataContext = null; resultTextBlock.Text = "Result of the solution will be displayed here"; }
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 UseSet1_Click(object sender, RoutedEventArgs e) { input = new KnapsackInput() { Capacity = 10, Items = new List <Item>() { new Item { Name = "1", Weight = 4, Value = 40 }, new Item { Name = "2", Weight = 7, Value = 42 }, new Item { Name = "3", Weight = 5, Value = 25 }, new Item { Name = "4", Weight = 3, Value = 12 } } }; allItemsGrid.DataContext = input.Items; itemsGrid.DataContext = null; resultTextBlock.Text = "Result of the solution will be displayed here"; }
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"; }
private void GenerateCoeff_Click(object sender, RoutedEventArgs e) { var coeffWindow = new DialogWindow(); if (coeffWindow.ShowDialog() == true) { input = new KnapsackInput(coeffWindow.Capacity, coeffWindow.AmountOfItems, coeffWindow.I, coeffWindow.J); allItemsGrid.DataContext = input.Items; itemsGrid.DataContext = null; resultTextBlock.Text = "Result of the solution will be displayed here"; } }
private void InputFile_Click(object sender, RoutedEventArgs e) { var inputFileWindow = new InputFileWindow(); if (inputFileWindow.ShowDialog() == true) { input = new KnapsackInput(); input.ReadInput(inputFileWindow.FileName); } allItemsGrid.DataContext = input.Items; itemsGrid.DataContext = null; resultTextBlock.Text = "Result of the solution will be displayed here"; }
/// <summary> /// Прочитать входные данные из файла, обработать их и сохранить для последующих действий. /// </summary> /// <param name="file">Ссылка на выбранный файл.</param> /// <exception cref="InvalidInputFileException">Входные данные в файле не в поддерживаемом формате.</exception> public async Task SetFromFile(IFileReference file) { await using var stream = await file.CreateMemoryStreamAsync(4096); using var reader = new StreamReader(stream); var content = reader.ReadToEnd(); var decerialized = JsonConvert.DeserializeObject <KnapsackInput>(content); if (decerialized?.Items == null || decerialized.Knapsack == null) { throw new InvalidInputFileException(); } Input = decerialized; InputUpdated?.Invoke(); }
private void InputManually_Click(object sender, RoutedEventArgs e) { var inputManuallyWindow = new InputManuallyWindow(); if (inputManuallyWindow.ShowDialog() == true) { input = new KnapsackInput() { Capacity = inputManuallyWindow.CapacityToAdd, Items = new List <Item>(inputManuallyWindow.ItemsToAdd) }; allItemsGrid.DataContext = input.Items; itemsGrid.DataContext = null; resultTextBlock.Text = "Result of the solution will be displayed here"; } }
private void UseSet3_Click(object sender, RoutedEventArgs e) { input = new KnapsackInput() { Capacity = 41, Items = new List <Item>() { new Item { Name = "1", Weight = 12, Value = 14 }, new Item { Name = "2", Weight = 40, Value = 34 }, new Item { Name = "3", Weight = 10, Value = 12 }, new Item { Name = "4", Weight = 18, 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 } } }; 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); }
public static void Main() { /* var input1 = ReadInput("./../../SampleInputs/easy20.txt"); * PrintResults(input1);*/ var input2 = new KnapsackInput() { Capacity = 18, ExpectedResult = 44, Items = new List <Item>() { new Item { Name = "fourth", Weight = 4, Value = 12 }, new Item { Name = "third", Weight = 6, Value = 10 }, new Item { Name = "second", Weight = 5, Value = 8 }, new Item { Name = "cheese", Weight = 7, Value = 11 }, new Item { Name = "first", Weight = 3, Value = 14 }, new Item { Name = "potatos", Weight = 1, Value = 7 }, new Item { Name = "bear", Weight = 6, Value = 9 } } }; PrintResults(input2); var input3 = new KnapsackInput() { Capacity = 4, ExpectedResult = 6, Items = new List <Item>() { new Item() { Name = "first", Value = 2, Weight = 1 }, new Item() { Name = "Second", Value = 3, Weight = 2 }, new Item() { Name = "Third", Value = 4, Weight = 3 }, new Item() { Name = "Fourth", Value = 5, Weight = 4 }, new Item() { Name = "Second", Value = 6, Weight = 5 } } }; 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); }