Skip to content

Commit 1e5b76a

Browse files
committed
Enhance knapsack visualization, add sample data button
1 parent 23f6466 commit 1e5b76a

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

OptimizationIssues/Views/KnapsackView.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
</StackPanel>
8484

8585
<StackPanel Grid.Column="1" Margin="10">
86+
<Button Content="Generuj przykładowe dane" VerticalAlignment="Top" Width="200" Click="GenerateSampleDataButton_Click"/>
8687
<TextBlock Text="Wyniki:" FontWeight="Bold" FontSize="16" VerticalAlignment="Top"/>
8788
<TextBlock x:Name="ResultTextBlock" Text="Tutaj pojawią się wyniki..." VerticalAlignment="Top" FontSize="14"/>
8889
</StackPanel>

OptimizationIssues/Views/KnapsackView.xaml.cs

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,21 @@ private void SolveButton_Click(object sender, RoutedEventArgs e)
6464
Foreground = new SolidColorBrush(Colors.White)
6565
});
6666

67-
foreach (var item in selectedItems)
67+
var sortedItems = selectedItems
68+
.Select((value, index) => new { value, originalIndex = index + 1 })
69+
.OrderBy(item => weights.IndexOf(item.value.Weight))
70+
.ToList();
71+
72+
foreach (var item in sortedItems)
6873
{
69-
ResultTextBlock.Inlines.Add(new Run("Waga: ")
74+
int itemNumber = weights.IndexOf(item.value.Weight) + 1;
75+
76+
ResultTextBlock.Inlines.Add(new Run($"Przedmiot {itemNumber} - Waga: ")
7077
{
7178
Foreground = new SolidColorBrush(Colors.White)
7279
});
7380

74-
ResultTextBlock.Inlines.Add(new Run(item.Weight.ToString())
81+
ResultTextBlock.Inlines.Add(new Run(item.value.Weight.ToString())
7582
{
7683
Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#E6A8D7"))
7784
});
@@ -81,7 +88,7 @@ private void SolveButton_Click(object sender, RoutedEventArgs e)
8188
Foreground = new SolidColorBrush(Colors.White)
8289
});
8390

84-
ResultTextBlock.Inlines.Add(new Run(item.Value.ToString())
91+
ResultTextBlock.Inlines.Add(new Run(item.value.Value.ToString())
8592
{
8693
Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#ADD8E6"))
8794
});
@@ -175,5 +182,36 @@ private void InputsChanged(object sender, TextChangedEventArgs e)
175182
{
176183
SolveButton.IsEnabled = ValidateInputs(out _, out _, out _);
177184
}
185+
private void GenerateSampleDataButton_Click(object sender, RoutedEventArgs e)
186+
{
187+
Random random = new Random();
188+
189+
int capacity = random.Next(10, 101);
190+
CapacityTextBox.Text = capacity.ToString();
191+
192+
int itemCount = random.Next(3, 8);
193+
List<int> weights = new List<int>();
194+
List<int> values = new List<int>();
195+
196+
for (int i = 0; i < itemCount; i++)
197+
{
198+
weights.Add(random.Next(1, capacity / 2));
199+
values.Add(random.Next(10, 101));
200+
}
201+
202+
WeightsTextBox.Text = string.Join(", ", weights);
203+
ValuesTextBox.Text = string.Join(", ", values);
204+
205+
CapacityTextBox.BorderBrush = Brushes.Black;
206+
CapacityTextBox.BorderThickness = new Thickness(1);
207+
208+
WeightsTextBox.BorderBrush = Brushes.Black;
209+
WeightsTextBox.BorderThickness = new Thickness(1);
210+
211+
ValuesTextBox.BorderBrush = Brushes.Black;
212+
ValuesTextBox.BorderThickness = new Thickness(1);
213+
214+
SolveButton.IsEnabled = ValidateInputs(out _, out _, out _);
215+
}
178216
}
179217
}

0 commit comments

Comments
 (0)