-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathNaiveKnapsackSolver.cs
More file actions
35 lines (32 loc) · 1.24 KB
/
NaiveKnapsackSolver.cs
File metadata and controls
35 lines (32 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
namespace Algorithms.Knapsack;
/// <summary>
/// Greedy heurictic solver.
/// </summary>
/// <typeparam name="T">Type of items in knapsack.</typeparam>
public class NaiveKnapsackSolver<T> : IHeuristicKnapsackSolver<T>
{
/// <summary>
/// Solves the knapsack problem using a naive greedy approach.
/// Items are added in order until capacity is reached.
/// </summary>
/// <param name="items">Array of items to consider for the knapsack.</param>
/// <param name="capacity">Maximum weight capacity of the knapsack.</param>
/// <param name="weightSelector">Function to get the weight of an item.</param>
/// <param name="valueSelector">Function to get the value of an item.</param>
/// <returns>Array of items that fit in the knapsack.</returns>
public T[] Solve(T[] items, double capacity, Func<T, double> weightSelector, Func<T, double> valueSelector)
{
var weight = 0d;
var left = new List<T>();
foreach (var item in items)
{
var weightDelta = weightSelector(item);
if (weight + weightDelta <= capacity)
{
weight += weightDelta;
left.Add(item);
}
}
return left.ToArray();
}
}