View Source Backfish.Examples.Knapsack (Backfish v0.1.0)
An implementation of the knapsack problem using the Backfish backtracking library.
The knapsack problem involves selecting a subset of items, each with a weight and a value, to include in a knapsack so that the total weight does not exceed the capacity of the knapsack and the total value is maximized.
Summary
Functions
Returns the initial state of the knapsack problem.
Checks if the given state is a goal state.
Generates the next possible states from the given state.
Functions
Returns the initial state of the knapsack problem.
Parameters
args
: A keyword list of arguments. Expects:items
, which is a list of tuples representing the items (each tuple contains a value and a weight), and:capacity
, which is the capacity of the knapsack.
Returns
- A map representing the initial state, including the items, capacity, current weight, current value, and a list indicating which items are included in the knapsack.
Checks if the given state is a goal state.
Parameters
state
: The current state of the problem, which includes the items and the list of included items.
Returns
true
if the number of included items equals the total number of items,false
otherwise.
Generates the next possible states from the given state.
Parameters
state
: The current state of the problem, which includes the items, capacity, current weight, current value, and the list of included items.
Returns
- A list of the next possible states, each including a new item either included or excluded.