Jordan
Jordan

Reputation: 4502

Knapsack Problem Variant - Maximizing Weight and Value Up To Limit

I'm trying to work out an algorithm (likely using OR Tools in Python) for solving a problem that seems to be related to the Knapsack problem.

If I'm trying to plan my first trip from Location A to Location B, how can I select the items such that:

A contrived example:

The "easy" solution is to make 4 trips:

But the smarter solution is to make only 3 trips:

I've worked with the OR Tools linear solver, but only with maximizing one value while having multiple constraints. How can I maximize multiple values (loaded weight and loaded value) with multiple constraints?

Upvotes: 1

Views: 578

Answers (1)

Jordan
Jordan

Reputation: 4502

I believe I found a solution to this. What I did was try to maximize a composite variable that accounts for both weight and value. In Python with OR Tools:

objective = solver.Objective()

for i, item in enumerate(item_list):
    objective.SetCoefficient(x[i], item['mass']/max_volume + item['value']/max_value)

objective.SetMaximization()

This sets the coefficient to account for both mass and value. The important part is that each is normalized against its associated limit. This solution is consistently giving me sets of items that fully use both the weight and value "space" to their full extent.

Upvotes: 1

Related Questions