Skip to content

Commit

Permalink
Visualizations of knapsack's results improved
Browse files Browse the repository at this point in the history
  • Loading branch information
enviGit committed Nov 9, 2024
1 parent b0e3797 commit 0ace198
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 5 deletions.
22 changes: 21 additions & 1 deletion OptimizationIssues/Models/KnapsackProblem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ public KnapsackProblem(int capacity, List<int> weights, List<int> 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];
Expand All @@ -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);
}
}
}
4 changes: 2 additions & 2 deletions OptimizationIssues/ViewModels/KnapsackViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ public KnapsackViewModel()
Values = new List<int>();
}

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();
}
}
}
64 changes: 62 additions & 2 deletions OptimizationIssues/Views/KnapsackView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.";
Expand Down

0 comments on commit 0ace198

Please sign in to comment.