Skip to content

Commit

Permalink
PTUtils.Knapsack: Added overloadKnapsack flag, made the behaviour mor…
Browse files Browse the repository at this point in the history
…e consistent, added tests

Fixes Syomus#72
  • Loading branch information
BasmanovDaniil committed Sep 10, 2022
1 parent a401ea2 commit 48c93f9
Show file tree
Hide file tree
Showing 3 changed files with 280 additions and 11 deletions.
48 changes: 37 additions & 11 deletions Runtime/PTUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,17 @@ public static void Swap<T>(ref T left, ref T right)
/// Knapsack problem solver for items with equal value
/// </summary>
/// <typeparam name="T">Item identificator</typeparam>
/// <param name="set">
/// Set of items where key <typeparamref name="T"/> is item identificator and value is item weight</param>
/// <param name="set">Set of items where key <typeparamref name="T"/> is item identificator and value is item weight</param>
/// <param name="capacity">Maximum weight</param>
/// <param name="knapsack">Pre-filled knapsack</param>
/// <param name="overloadKnapsack">Set to true if you want to fill the remaining free space with the smallest item</param>
/// <returns>
/// Filled knapsack where values are number of items of type key.
/// Tends to overload knapsack: fills remainder with one smallest item.</returns>
/// <remarks>
/// https://en.wikipedia.org/wiki/Knapsack_problem
/// </remarks>
public static Dictionary<T, int> Knapsack<T>(Dictionary<T, float> set, float capacity,
Dictionary<T, int> knapsack = null)
Dictionary<T, int> knapsack = null, bool overloadKnapsack = false)
{
var keys = new List<T>(set.Keys);
// Sort keys by their weights in descending order
Expand All @@ -87,16 +86,19 @@ public static Dictionary<T, int> Knapsack<T>(Dictionary<T, float> set, float cap
knapsack[key] = 0;
}
}
return Knapsack(set, keys, capacity, knapsack, 0);
return Knapsack(set, keys, capacity, knapsack, 0, overloadKnapsack);
}

private static Dictionary<T, int> Knapsack<T>(Dictionary<T, float> set, List<T> keys, float remainder,
Dictionary<T, int> knapsack, int startIndex)
Dictionary<T, int> knapsack, int startIndex, bool overloadKnapsack)
{
T smallestKey = keys[keys.Count - 1];
if (remainder < set[smallestKey])
{
knapsack[smallestKey] = 1;
if (overloadKnapsack)
{
knapsack[smallestKey] = 1;
}
return knapsack;
}
// Cycle through items and try to put them in knapsack
Expand All @@ -110,28 +112,52 @@ private static Dictionary<T, int> Knapsack<T>(Dictionary<T, float> set, List<T>
}
if (remainder > 0)
{
if (overloadKnapsack)
{
knapsack[smallestKey] += 1;
remainder -= set[smallestKey];
}

// Fix for https://github.com/Syomus/ProceduralToolkit/issues/72
var repackedCopy = new Dictionary<T, int>(knapsack);

// Throw out largest item and try again
for (var i = 0; i < keys.Count; i++)
{
T key = keys[i];
if (knapsack[key] != 0)
if (repackedCopy[key] != 0)
{
// Already tried every combination, return as is
if (key.Equals(smallestKey))
{
return knapsack;
return repackedCopy;
}
knapsack[key]--;
repackedCopy[key]--;
remainder += set[key];
startIndex = i + 1;
break;
}
}
knapsack = Knapsack(set, keys, remainder, knapsack, startIndex);

repackedCopy = Knapsack(set, keys, remainder, repackedCopy, startIndex, overloadKnapsack);
if (TotalWeight(set, repackedCopy) > TotalWeight(set, knapsack))
{
knapsack = repackedCopy;
}
}
return knapsack;
}

private static float TotalWeight<T>(Dictionary<T, float> set, Dictionary<T, int> knapsack)
{
float weight = 0;
foreach(var item in knapsack)
{
weight += set[item.Key] * item.Value;
}
return weight;
}

public static string ToString(this Vector3 vector, string format, IFormatProvider formatProvider)
{
return string.Format("({0}, {1}, {2})", vector.x.ToString(format, formatProvider), vector.y.ToString(format, formatProvider),
Expand Down
232 changes: 232 additions & 0 deletions Tests/Editor/PTUtilsTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
using System.Collections.Generic;
using NUnit.Framework;

namespace ProceduralToolkit.Tests.PTUtilsTest
{
public class PTUtilsTest
{
public class Knapsack_NoOverload
{
private readonly Dictionary<int, float> set = new()
{
{ 1, 1 },
{ 2, 2 },
{ 4, 4 },
};
private readonly Dictionary<int, int> knapsack0 = new()
{
{ 4, 0 },
{ 2, 0 },
{ 1, 0 },
};
private readonly Dictionary<int, int> knapsack1 = new()
{
{ 4, 0 },
{ 2, 0 },
{ 1, 1 },
};
private readonly Dictionary<int, int> knapsack2 = new()
{
{ 4, 0 },
{ 2, 1 },
{ 1, 0 },
};
private readonly Dictionary<int, int> knapsack3 = new()
{
{ 4, 0 },
{ 2, 1 },
{ 1, 1 },
};
private readonly Dictionary<int, int> knapsack10 = new()
{
{ 4, 2 },
{ 2, 1 },
{ 1, 0 },
};

[Test]
public void MinusOne()
{
Assert.AreEqual(knapsack0, PTUtils.Knapsack(set, -1, overloadKnapsack: false));
}

[Test]
public void Zero()
{
Assert.AreEqual(knapsack0, PTUtils.Knapsack(set, 0, overloadKnapsack: false));
}

[Test]
public void PointFive()
{
Assert.AreEqual(knapsack0, PTUtils.Knapsack(set, 0.5f, overloadKnapsack: false));
}

[Test]
public void One()
{
Assert.AreEqual(knapsack1, PTUtils.Knapsack(set, 1, overloadKnapsack: false));
}

[Test]
public void OnePointFive()
{
Assert.AreEqual(knapsack1, PTUtils.Knapsack(set, 1.5f, overloadKnapsack: false));
}

[Test]
public void Two()
{
Assert.AreEqual(knapsack2, PTUtils.Knapsack(set, 2, overloadKnapsack: false));
}

[Test]
public void TwoPointFive()
{
Assert.AreEqual(knapsack2, PTUtils.Knapsack(set, 2.5f, overloadKnapsack: false));
}

[Test]
public void Three()
{
Assert.AreEqual(knapsack3, PTUtils.Knapsack(set, 3, overloadKnapsack: false));
}

[Test]
public void ThreePointFive()
{
Assert.AreEqual(knapsack3, PTUtils.Knapsack(set, 3.5f, overloadKnapsack: false));
}

[Test]
public void Ten()
{
Assert.AreEqual(knapsack10, PTUtils.Knapsack(set, 10, overloadKnapsack: false));
}

[Test]
public void TenPointFive()
{
Assert.AreEqual(knapsack10, PTUtils.Knapsack(set, 10.5f, overloadKnapsack: false));
}
}

public class Knapsack_Overload
{
private readonly Dictionary<int, float> set = new()
{
{ 1, 1 },
{ 2, 2 },
{ 4, 4 },
};
private readonly Dictionary<int, int> knapsack0 = new()
{
{ 4, 0 },
{ 2, 0 },
{ 1, 1 },
};
private readonly Dictionary<int, int> knapsack1_5 = new()
{
{ 4, 0 },
{ 2, 0 },
{ 1, 2 },
};
private readonly Dictionary<int, int> knapsack2 = new()
{
{ 4, 0 },
{ 2, 1 },
{ 1, 0 },
};
private readonly Dictionary<int, int> knapsack3 = new()
{
{ 4, 0 },
{ 2, 1 },
{ 1, 1 },
};
private readonly Dictionary<int, int> knapsack3_5 = new()
{
{ 4, 0 },
{ 2, 1 },
{ 1, 2 },
};
private readonly Dictionary<int, int> knapsack10 = new()
{
{ 4, 2 },
{ 2, 1 },
{ 1, 0 },
};
private readonly Dictionary<int, int> knapsack10_5 = new()
{
{ 4, 2 },
{ 2, 1 },
{ 1, 1 },
};

[Test]
public void MinusOne()
{
Assert.AreEqual(knapsack0, PTUtils.Knapsack(set, -1, overloadKnapsack: true));
}

[Test]
public void Zero()
{
Assert.AreEqual(knapsack0, PTUtils.Knapsack(set, 0, overloadKnapsack: true));
}

[Test]
public void PointFive()
{
Assert.AreEqual(knapsack0, PTUtils.Knapsack(set, 0.5f, overloadKnapsack: true));
}

[Test]
public void One()
{
Assert.AreEqual(knapsack0, PTUtils.Knapsack(set, 1, overloadKnapsack: true));
}

[Test]
public void OnePointFive()
{
Assert.AreEqual(knapsack1_5, PTUtils.Knapsack(set, 1.5f, overloadKnapsack: true));
}

[Test]
public void Two()
{
Assert.AreEqual(knapsack2, PTUtils.Knapsack(set, 2, overloadKnapsack: true));
}

[Test]
public void TwoPointFive()
{
Assert.AreEqual(knapsack3, PTUtils.Knapsack(set, 2.5f, overloadKnapsack: true));
}

[Test]
public void Three()
{
Assert.AreEqual(knapsack3, PTUtils.Knapsack(set, 3, overloadKnapsack: true));
}

[Test]
public void ThreePointFive()
{
Assert.AreEqual(knapsack3_5, PTUtils.Knapsack(set, 3.5f, overloadKnapsack: true));
}

[Test]
public void Ten()
{
Assert.AreEqual(knapsack10, PTUtils.Knapsack(set, 10, overloadKnapsack: true));
}

[Test]
public void TenPointFive()
{
Assert.AreEqual(knapsack10_5, PTUtils.Knapsack(set, 10.5f, overloadKnapsack: true));
}
}
}
}
11 changes: 11 additions & 0 deletions Tests/Editor/PTUtilsTest.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 48c93f9

Please sign in to comment.