Skip to content

Commit 179574e

Browse files
authored
Add knapsack problem (#74)
* Add knapsack problem * Add knapsack problem tests * Add CRLF
1 parent d34d5c2 commit 179574e

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using Algorithms.Knapsack;
2+
using NUnit.Framework;
3+
using NUnit.Framework.Internal;
4+
using System;
5+
using System.Linq;
6+
7+
namespace Algorithms.Tests.Knapsack
8+
{
9+
public class NaiveKnapsackSolverTests
10+
{
11+
[Test]
12+
[Parallelizable]
13+
public void TakesHalf([Random(0, 1000, 1000)]int length)
14+
{
15+
//Arrange
16+
var solver = new NaiveKnapsackSolver<int>();
17+
var items = Enumerable.Repeat(42, 2 * length).ToArray();
18+
var expectedResult = Enumerable.Repeat(42, length);
19+
20+
//Act
21+
var result = solver.Solve(items, length, x => 1, x => 1);
22+
23+
//Assert
24+
Assert.AreEqual(expectedResult, result);
25+
}
26+
}
27+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
3+
namespace Algorithms.Knapsack
4+
{
5+
/// <summary>
6+
/// Solves knapsack problem using some heuristics
7+
/// Sum of values of taken items -> max
8+
/// Sum of weights of taken items <= capacity
9+
/// </summary>
10+
/// <typeparam name="T">Type of items in knapsack</typeparam>
11+
public interface IHeuristicKnapsackSolver<T>
12+
{
13+
/// <summary>
14+
/// Solves knapsack problem using some heuristics
15+
/// Sum of values of taken items -> max
16+
/// Sum of weights of taken items <= capacity
17+
/// </summary>
18+
/// <param name="items">All items to choose from</param>
19+
/// <param name="capacity">How much weight we can take</param>
20+
/// <param name="weightSelector">Maps item to its weight</param>
21+
/// <param name="valueSelector">Maps item to its value</param>
22+
/// <returns>Items that were chosen</returns>
23+
T[] Solve(T[] items, double capacity, Func<T, double> weightSelector, Func<T, double> valueSelector);
24+
}
25+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace Algorithms.Knapsack
2+
{
3+
/// <summary>
4+
/// Solves knapsack problem
5+
/// Sum of values of taken items -> max
6+
/// Sum of weights of taken items <= capacity
7+
/// </summary>
8+
/// <typeparam name="T">Type of items in knapsack</typeparam>
9+
public interface IKnapsackSolver<T> : IHeuristicKnapsackSolver<T>
10+
{
11+
}
12+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace Algorithms.Knapsack
5+
{
6+
/// <summary>
7+
/// Greedy heurictic solver
8+
/// </summary>
9+
/// <typeparam name="T">Type of items in knapsack</typeparam>
10+
public class NaiveKnapsackSolver<T> : IHeuristicKnapsackSolver<T>
11+
{
12+
public T[] Solve(T[] items, double capacity, Func<T, double> weightSelector, Func<T, double> valueSelector)
13+
{
14+
var weight = 0d;
15+
var left = new List<T>();
16+
17+
foreach (var item in items)
18+
{
19+
var weightDelta = weightSelector(item);
20+
if (weight + weightDelta <= capacity)
21+
{
22+
weight += weightDelta;
23+
left.Add(item);
24+
}
25+
}
26+
27+
return left.ToArray();
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)