- Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathNaiveKnapsackSolver.cs
37 lines (33 loc) · 1.02 KB
/
NaiveKnapsackSolver.cs
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
36
37
usingSystem;
usingSystem.Collections.Generic;
namespaceAlgorithms.Knapsack;
/// <summary>
/// Greedy heurictic solver.
/// </summary>
/// <typeparam name="T">Type of items in knapsack.</typeparam>
publicclassNaiveKnapsackSolver<T>:IHeuristicKnapsackSolver<T>
{
/// <summary>
/// TODO.
/// </summary>
/// <param name="items">TODO. 2.</param>
/// <param name="capacity">TODO. 3.</param>
/// <param name="weightSelector">TODO. 4.</param>
/// <param name="valueSelector">TODO. 5.</param>
/// <returns>TODO. 6.</returns>
publicT[]Solve(T[]items,doublecapacity,Func<T,double>weightSelector,Func<T,double>valueSelector)
{
varweight=0d;
varleft=newList<T>();
foreach(variteminitems)
{
varweightDelta=weightSelector(item);
if(weight+weightDelta<=capacity)
{
weight+=weightDelta;
left.Add(item);
}
}
returnleft.ToArray();
}
}