Skip to content

Knapsack with floating point capacity uses only small items #72

@Swahhillie

Description

@Swahhillie

Description

The knapsack algorithm breaks if it can't fill the whole bag.
It will fill the entire bag with the smallest weight item instead.

Version

  • Unity: 2019.4.35f
  • ProceduralToolkit: 0.2.3

Steps to reproduce

public void TestKnapsack()
{
    var set = new Dictionary<int, float>()
    {
        { 1, 1f }, { 2, 2f }, { 4, 4f }
    };
    
    string Result(Dictionary<int, int> result)
    {
        var str = "";
        foreach (var p in result)
        {
            str += $"{p.Key} * {p.Value}\n";
        }

        return str;
    }

    var ten = PTUtils.Knapsack(set, 10.0f);
    var tenPointFive = PTUtils.Knapsack(set, 10.5f);
    
    //as expected
    Debug.Log(Result(ten));
    //4 * 2
    //2 * 1
    //1 * 0
    
    //wrong
    Debug.Log(Result(tenPointFive));
    //4 * 0
    //2 * 0
    //1 * 10
}

Run the provided code and see that the result is not as expected.

Metadata

Metadata

Labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions