Python script to solve Knapsack problem (heuristic). Dynamic programming implementation to check solution precision (exact).
numpy
git clone https://github.com/vvkin/knapsack-genetic/
cd knapsack-genetic
pipenv install
pipenv shell
pytnon generate_data.py 100 100 int 50 50 data
python knapsack_genetic.py data 10000
Script to generate random data for knapsack problem.
It accepts 6 parameters.
python generate_data.py [items_number] [knapsack_capacity] [items_type] [max_weight] [max_cost] [file_name]
items_number
— integer value. Number of items for problemknapsack_capacity
— float value. Maximal weight that knapsack can containmax_weight
— float value. Maximal weight that can be among items, weight ∈ [1, max_weight]max_cost
— float value. Maximal cost than can be among items, cost ∈ [2, max_cost]file_name
— string value. File name to write generated data
Dynamic algorithm to solve Knapsack problem. Can solve problem only with integer weights and costs.
It accepts 1 paratemer.
python knapsack_dp.py [file_name]
file_name
— string value. File name with input data
Python script to solve Knapsack problem using genetic algorithm. The algorithm does not guarantee an exact solution, it's heuristic.
It accepts 2 parameters.
python knapsack_genetic.py [file_name] [iterations_number]
file_name
— string value. File name with input dataiterations_number
— integer value. Number of iterations in genetic algorithm
Vadym Kinchur, vvkin