Skip to content

Commit

Permalink
Item numberring in knapsack problem fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
enviGit committed Nov 9, 2024
1 parent 2f58751 commit 4aa2495
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 18 deletions.
10 changes: 6 additions & 4 deletions OptimizationIssues/Models/KnapsackProblem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,24 @@
public class KnapsackProblem : ProblemBase
{
public int KnapsackCapacity { get; set; }
public List<int> Indexes { get; set; }
public List<int> Weights { get; set; }
public List<int> Values { get; set; }

public KnapsackProblem(int capacity, List<int> weights, List<int> values)
public KnapsackProblem(int capacity, List<int> indexes, List<int> weights, List<int> values)
{
KnapsackCapacity = capacity;
Weights = weights;
Values = values;
Indexes = Enumerable.Range(0, weights.Count).ToList();
}

public override int Solve()
{
return 0;
}

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

int maxValue = dp[n, KnapsackCapacity];
List<(int Weight, int Value)> selectedItems = new List<(int, int)>();
List<(int Index, int Weight, int Value)> selectedItems = new List<(int, 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]));
selectedItems.Add((Indexes[i - 1], Weights[i - 1], Values[i - 1]));
remainingCapacity -= Weights[i - 1];
usedCapacity += Weights[i - 1];
}
Expand Down
6 changes: 4 additions & 2 deletions OptimizationIssues/ViewModels/KnapsackViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@ namespace OptimizationIssues.ViewModels
public class KnapsackViewModel
{
public int KnapsackCapacity { get; set; }
public List<int> Indexes { get; set; }
public List<int> Weights { get; set; }
public List<int> Values { get; set; }

public KnapsackViewModel()
{
Indexes = new List<int>();
Weights = new List<int>();
Values = new List<int>();
}

public (int MaxValue, List<(int Weight, int Value)> SelectedItems, int UsedCapacity) SolveKnapsackWithDetails()
public (int MaxValue, List<(int Index, int Weight, int Value)> SelectedItems, int UsedCapacity) SolveKnapsackWithDetails()
{
KnapsackProblem problem = new KnapsackProblem(KnapsackCapacity, Weights, Values);
KnapsackProblem problem = new KnapsackProblem(KnapsackCapacity, Indexes, Weights, Values);
return problem.SolveWithDetails();
}
}
Expand Down
38 changes: 26 additions & 12 deletions OptimizationIssues/Views/KnapsackView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ namespace OptimizationIssues.Views
/// </summary>
public partial class KnapsackView : UserControl
{
public class KnapsackItem
{
public int Index { get; set; }
public int Weight { get; set; }
public int Value { get; set; }
}
public KnapsackView()
{
InitializeComponent();
Expand Down Expand Up @@ -42,7 +48,7 @@ private void SolveButton_Click(object sender, RoutedEventArgs e)

ResultTextBlock.Inlines.Add(new Run(maxValue.ToString())
{
Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFD700"))
Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#98FF98"))
});

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

ResultTextBlock.Inlines.Add(new Run("\n\nWybrane przedmioty:\n")
{
Foreground = new SolidColorBrush(Colors.White)
Foreground = new SolidColorBrush(Colors.White),
FontWeight = FontWeights.Bold
});

var sortedItems = selectedItems
.Select((value, index) => new { value, originalIndex = index + 1 })
.OrderBy(item => weights.IndexOf(item.value.Weight))
.ToList();
selectedItems.Reverse();

foreach (var item in sortedItems)
{
int itemNumber = weights.IndexOf(item.value.Weight) + 1;
foreach(var item in selectedItems)
{
int itemNumber = item.Index + 1;

ResultTextBlock.Inlines.Add(new Run($"Przedmiot ")
{
Foreground = new SolidColorBrush(Colors.White)
});

ResultTextBlock.Inlines.Add(new Run($"{itemNumber}")
{
Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFD700"))
});

ResultTextBlock.Inlines.Add(new Run($"Przedmiot {itemNumber} - Waga: ")
ResultTextBlock.Inlines.Add(new Run($" - Waga: ")
{
Foreground = new SolidColorBrush(Colors.White)
});

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

ResultTextBlock.Inlines.Add(new Run(item.value.Value.ToString())
ResultTextBlock.Inlines.Add(new Run(item.Value.ToString())
{
Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#ADD8E6"))
});
Expand Down

0 comments on commit 4aa2495

Please sign in to comment.