Skip to content

Commit 0ace198

Browse files
committed
Visualizations of knapsack's results improved
1 parent b0e3797 commit 0ace198

File tree

3 files changed

+85
-5
lines changed

3 files changed

+85
-5
lines changed

OptimizationIssues/Models/KnapsackProblem.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ public KnapsackProblem(int capacity, List<int> weights, List<int> values)
1414
}
1515

1616
public override int Solve()
17+
{
18+
return 0;
19+
}
20+
21+
public (int MaxValue, List<(int Weight, int Value)> SelectedItems, int UsedCapacity) SolveWithDetails()
1722
{
1823
int n = Weights.Count;
1924
int[,] dp = new int[n + 1, KnapsackCapacity + 1];
@@ -31,7 +36,22 @@ public override int Solve()
3136
}
3237
}
3338

34-
return dp[n, KnapsackCapacity];
39+
int maxValue = dp[n, KnapsackCapacity];
40+
List<(int Weight, int Value)> selectedItems = new List<(int, int)>();
41+
int remainingCapacity = KnapsackCapacity;
42+
int usedCapacity = 0;
43+
44+
for (int i = n; i > 0 && remainingCapacity > 0; i--)
45+
{
46+
if (dp[i, remainingCapacity] != dp[i - 1, remainingCapacity])
47+
{
48+
selectedItems.Add((Weights[i - 1], Values[i - 1]));
49+
remainingCapacity -= Weights[i - 1];
50+
usedCapacity += Weights[i - 1];
51+
}
52+
}
53+
54+
return (maxValue, selectedItems, usedCapacity);
3555
}
3656
}
3757
}

OptimizationIssues/ViewModels/KnapsackViewModel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ public KnapsackViewModel()
1414
Values = new List<int>();
1515
}
1616

17-
public int SolveKnapsack()
17+
public (int MaxValue, List<(int Weight, int Value)> SelectedItems, int UsedCapacity) SolveKnapsackWithDetails()
1818
{
1919
KnapsackProblem problem = new KnapsackProblem(KnapsackCapacity, Weights, Values);
20-
return problem.Solve();
20+
return problem.SolveWithDetails();
2121
}
2222
}
2323
}

OptimizationIssues/Views/KnapsackView.xaml.cs

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Diagnostics;
44
using System.Windows;
55
using System.Windows.Controls;
6+
using System.Windows.Documents;
67
using System.Windows.Media;
78

89
namespace OptimizationIssues.Views
@@ -31,8 +32,67 @@ private void SolveButton_Click(object sender, RoutedEventArgs e)
3132
viewModel.Weights = weights;
3233
viewModel.Values = values;
3334

34-
int result = viewModel.SolveKnapsack();
35-
ResultTextBlock.Text = $"Maksymalna wartość: {result}";
35+
var (maxValue, selectedItems, usedCapacity) = viewModel.SolveKnapsackWithDetails();
36+
ResultTextBlock.Inlines.Clear();
37+
38+
ResultTextBlock.Inlines.Add(new Run("Maksymalna wartość: ")
39+
{
40+
Foreground = new SolidColorBrush(Colors.White)
41+
});
42+
43+
ResultTextBlock.Inlines.Add(new Run(maxValue.ToString())
44+
{
45+
Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFD700"))
46+
});
47+
48+
ResultTextBlock.Inlines.Add(new Run("\n\nWybrane przedmioty:\n")
49+
{
50+
Foreground = new SolidColorBrush(Colors.White)
51+
});
52+
53+
foreach (var item in selectedItems)
54+
{
55+
ResultTextBlock.Inlines.Add(new Run("Waga: ")
56+
{
57+
Foreground = new SolidColorBrush(Colors.White)
58+
});
59+
60+
ResultTextBlock.Inlines.Add(new Run(item.Weight.ToString())
61+
{
62+
Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#E6A8D7"))
63+
});
64+
65+
ResultTextBlock.Inlines.Add(new Run(", Wartość: ")
66+
{
67+
Foreground = new SolidColorBrush(Colors.White)
68+
});
69+
70+
ResultTextBlock.Inlines.Add(new Run(item.Value.ToString())
71+
{
72+
Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#ADD8E6"))
73+
});
74+
75+
ResultTextBlock.Inlines.Add(new Run("\n")
76+
{
77+
Foreground = new SolidColorBrush(Colors.White)
78+
});
79+
}
80+
81+
ResultTextBlock.Inlines.Add(new Run("\nZużyta pojemność plecaka: ")
82+
{
83+
Foreground = new SolidColorBrush(Colors.White)
84+
});
85+
86+
if (usedCapacity == capacity)
87+
ResultTextBlock.Inlines.Add(new Run($"{usedCapacity}/{capacity}")
88+
{
89+
Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#98FF98"))
90+
});
91+
else
92+
ResultTextBlock.Inlines.Add(new Run($"{usedCapacity}/{capacity}")
93+
{
94+
Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF9898"))
95+
});
3696
}
3797
else
3898
ResultTextBlock.Text = "Podano błędne dane. Upewnij się, że wszystkie pola są poprawnie wypełnione.";

0 commit comments

Comments
 (0)