Skip to content

Commit 4aa2495

Browse files
committed
Item numberring in knapsack problem fixed
1 parent 2f58751 commit 4aa2495

File tree

3 files changed

+36
-18
lines changed

3 files changed

+36
-18
lines changed

OptimizationIssues/Models/KnapsackProblem.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,24 @@
33
public class KnapsackProblem : ProblemBase
44
{
55
public int KnapsackCapacity { get; set; }
6+
public List<int> Indexes { get; set; }
67
public List<int> Weights { get; set; }
78
public List<int> Values { get; set; }
89

9-
public KnapsackProblem(int capacity, List<int> weights, List<int> values)
10+
public KnapsackProblem(int capacity, List<int> indexes, List<int> weights, List<int> values)
1011
{
1112
KnapsackCapacity = capacity;
1213
Weights = weights;
1314
Values = values;
15+
Indexes = Enumerable.Range(0, weights.Count).ToList();
1416
}
1517

1618
public override int Solve()
1719
{
1820
return 0;
1921
}
2022

21-
public (int MaxValue, List<(int Weight, int Value)> SelectedItems, int UsedCapacity) SolveWithDetails()
23+
public (int MaxValue, List<(int Index, int Weight, int Value)> SelectedItems, int UsedCapacity) SolveWithDetails()
2224
{
2325
int n = Weights.Count;
2426
int[,] dp = new int[n + 1, KnapsackCapacity + 1];
@@ -37,15 +39,15 @@ public override int Solve()
3739
}
3840

3941
int maxValue = dp[n, KnapsackCapacity];
40-
List<(int Weight, int Value)> selectedItems = new List<(int, int)>();
42+
List<(int Index, int Weight, int Value)> selectedItems = new List<(int, int, int)>();
4143
int remainingCapacity = KnapsackCapacity;
4244
int usedCapacity = 0;
4345

4446
for (int i = n; i > 0 && remainingCapacity > 0; i--)
4547
{
4648
if (dp[i, remainingCapacity] != dp[i - 1, remainingCapacity])
4749
{
48-
selectedItems.Add((Weights[i - 1], Values[i - 1]));
50+
selectedItems.Add((Indexes[i - 1], Weights[i - 1], Values[i - 1]));
4951
remainingCapacity -= Weights[i - 1];
5052
usedCapacity += Weights[i - 1];
5153
}

OptimizationIssues/ViewModels/KnapsackViewModel.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,20 @@ namespace OptimizationIssues.ViewModels
55
public class KnapsackViewModel
66
{
77
public int KnapsackCapacity { get; set; }
8+
public List<int> Indexes { get; set; }
89
public List<int> Weights { get; set; }
910
public List<int> Values { get; set; }
1011

1112
public KnapsackViewModel()
1213
{
14+
Indexes = new List<int>();
1315
Weights = new List<int>();
1416
Values = new List<int>();
1517
}
1618

17-
public (int MaxValue, List<(int Weight, int Value)> SelectedItems, int UsedCapacity) SolveKnapsackWithDetails()
19+
public (int MaxValue, List<(int Index, int Weight, int Value)> SelectedItems, int UsedCapacity) SolveKnapsackWithDetails()
1820
{
19-
KnapsackProblem problem = new KnapsackProblem(KnapsackCapacity, Weights, Values);
21+
KnapsackProblem problem = new KnapsackProblem(KnapsackCapacity, Indexes, Weights, Values);
2022
return problem.SolveWithDetails();
2123
}
2224
}

OptimizationIssues/Views/KnapsackView.xaml.cs

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ namespace OptimizationIssues.Views
1313
/// </summary>
1414
public partial class KnapsackView : UserControl
1515
{
16+
public class KnapsackItem
17+
{
18+
public int Index { get; set; }
19+
public int Weight { get; set; }
20+
public int Value { get; set; }
21+
}
1622
public KnapsackView()
1723
{
1824
InitializeComponent();
@@ -42,7 +48,7 @@ private void SolveButton_Click(object sender, RoutedEventArgs e)
4248

4349
ResultTextBlock.Inlines.Add(new Run(maxValue.ToString())
4450
{
45-
Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFD700"))
51+
Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#98FF98"))
4652
});
4753

4854
ResultTextBlock.Inlines.Add(new Run("\nZużyta pojemność plecaka: ")
@@ -61,24 +67,32 @@ private void SolveButton_Click(object sender, RoutedEventArgs e)
6167

6268
ResultTextBlock.Inlines.Add(new Run("\n\nWybrane przedmioty:\n")
6369
{
64-
Foreground = new SolidColorBrush(Colors.White)
70+
Foreground = new SolidColorBrush(Colors.White),
71+
FontWeight = FontWeights.Bold
6572
});
6673

67-
var sortedItems = selectedItems
68-
.Select((value, index) => new { value, originalIndex = index + 1 })
69-
.OrderBy(item => weights.IndexOf(item.value.Weight))
70-
.ToList();
74+
selectedItems.Reverse();
7175

72-
foreach (var item in sortedItems)
73-
{
74-
int itemNumber = weights.IndexOf(item.value.Weight) + 1;
76+
foreach(var item in selectedItems)
77+
{
78+
int itemNumber = item.Index + 1;
79+
80+
ResultTextBlock.Inlines.Add(new Run($"Przedmiot ")
81+
{
82+
Foreground = new SolidColorBrush(Colors.White)
83+
});
84+
85+
ResultTextBlock.Inlines.Add(new Run($"{itemNumber}")
86+
{
87+
Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFD700"))
88+
});
7589

76-
ResultTextBlock.Inlines.Add(new Run($"Przedmiot {itemNumber} - Waga: ")
90+
ResultTextBlock.Inlines.Add(new Run($" - Waga: ")
7791
{
7892
Foreground = new SolidColorBrush(Colors.White)
7993
});
8094

81-
ResultTextBlock.Inlines.Add(new Run(item.value.Weight.ToString())
95+
ResultTextBlock.Inlines.Add(new Run(item.Weight.ToString())
8296
{
8397
Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#E6A8D7"))
8498
});
@@ -88,7 +102,7 @@ private void SolveButton_Click(object sender, RoutedEventArgs e)
88102
Foreground = new SolidColorBrush(Colors.White)
89103
});
90104

91-
ResultTextBlock.Inlines.Add(new Run(item.value.Value.ToString())
105+
ResultTextBlock.Inlines.Add(new Run(item.Value.ToString())
92106
{
93107
Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#ADD8E6"))
94108
});

0 commit comments

Comments
 (0)