diff --git a/OptimizationIssues/Models/KnapsackProblem.cs b/OptimizationIssues/Models/KnapsackProblem.cs index 31127dc..3beb26e 100644 --- a/OptimizationIssues/Models/KnapsackProblem.cs +++ b/OptimizationIssues/Models/KnapsackProblem.cs @@ -14,6 +14,11 @@ public KnapsackProblem(int capacity, List weights, List values) } public override int Solve() + { + return 0; + } + + public (int MaxValue, List<(int Weight, int Value)> SelectedItems, int UsedCapacity) SolveWithDetails() { int n = Weights.Count; int[,] dp = new int[n + 1, KnapsackCapacity + 1]; @@ -31,7 +36,22 @@ public override int Solve() } } - return dp[n, KnapsackCapacity]; + int maxValue = dp[n, KnapsackCapacity]; + List<(int Weight, int Value)> selectedItems = new List<(int, int)>(); + int remainingCapacity = KnapsackCapacity; + int usedCapacity = 0; + + for (int i = n; i > 0 && remainingCapacity > 0; i--) + { + if (dp[i, remainingCapacity] != dp[i - 1, remainingCapacity]) + { + selectedItems.Add((Weights[i - 1], Values[i - 1])); + remainingCapacity -= Weights[i - 1]; + usedCapacity += Weights[i - 1]; + } + } + + return (maxValue, selectedItems, usedCapacity); } } } \ No newline at end of file diff --git a/OptimizationIssues/ViewModels/KnapsackViewModel.cs b/OptimizationIssues/ViewModels/KnapsackViewModel.cs index 1de9235..eb1e2bf 100644 --- a/OptimizationIssues/ViewModels/KnapsackViewModel.cs +++ b/OptimizationIssues/ViewModels/KnapsackViewModel.cs @@ -14,10 +14,10 @@ public KnapsackViewModel() Values = new List(); } - public int SolveKnapsack() + public (int MaxValue, List<(int Weight, int Value)> SelectedItems, int UsedCapacity) SolveKnapsackWithDetails() { KnapsackProblem problem = new KnapsackProblem(KnapsackCapacity, Weights, Values); - return problem.Solve(); + return problem.SolveWithDetails(); } } } \ No newline at end of file diff --git a/OptimizationIssues/Views/KnapsackView.xaml.cs b/OptimizationIssues/Views/KnapsackView.xaml.cs index ccddb0d..7de7978 100644 --- a/OptimizationIssues/Views/KnapsackView.xaml.cs +++ b/OptimizationIssues/Views/KnapsackView.xaml.cs @@ -3,6 +3,7 @@ using System.Diagnostics; using System.Windows; using System.Windows.Controls; +using System.Windows.Documents; using System.Windows.Media; namespace OptimizationIssues.Views @@ -31,8 +32,67 @@ private void SolveButton_Click(object sender, RoutedEventArgs e) viewModel.Weights = weights; viewModel.Values = values; - int result = viewModel.SolveKnapsack(); - ResultTextBlock.Text = $"Maksymalna wartość: {result}"; + var (maxValue, selectedItems, usedCapacity) = viewModel.SolveKnapsackWithDetails(); + ResultTextBlock.Inlines.Clear(); + + ResultTextBlock.Inlines.Add(new Run("Maksymalna wartość: ") + { + Foreground = new SolidColorBrush(Colors.White) + }); + + ResultTextBlock.Inlines.Add(new Run(maxValue.ToString()) + { + Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFD700")) + }); + + ResultTextBlock.Inlines.Add(new Run("\n\nWybrane przedmioty:\n") + { + Foreground = new SolidColorBrush(Colors.White) + }); + + foreach (var item in selectedItems) + { + ResultTextBlock.Inlines.Add(new Run("Waga: ") + { + Foreground = new SolidColorBrush(Colors.White) + }); + + ResultTextBlock.Inlines.Add(new Run(item.Weight.ToString()) + { + Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#E6A8D7")) + }); + + ResultTextBlock.Inlines.Add(new Run(", Wartość: ") + { + Foreground = new SolidColorBrush(Colors.White) + }); + + ResultTextBlock.Inlines.Add(new Run(item.Value.ToString()) + { + Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#ADD8E6")) + }); + + ResultTextBlock.Inlines.Add(new Run("\n") + { + Foreground = new SolidColorBrush(Colors.White) + }); + } + + ResultTextBlock.Inlines.Add(new Run("\nZużyta pojemność plecaka: ") + { + Foreground = new SolidColorBrush(Colors.White) + }); + + if (usedCapacity == capacity) + ResultTextBlock.Inlines.Add(new Run($"{usedCapacity}/{capacity}") + { + Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#98FF98")) + }); + else + ResultTextBlock.Inlines.Add(new Run($"{usedCapacity}/{capacity}") + { + Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF9898")) + }); } else ResultTextBlock.Text = "Podano błędne dane. Upewnij się, że wszystkie pola są poprawnie wypełnione.";