All Projects → dwave-examples → knapsack

dwave-examples / knapsack

Licence: Apache-2.0 License
Implementation of knapsack problem, set up for scaling to large problem size

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to knapsack

nurse-scheduling
A demo of a nurse scheduling model
Stars: ✭ 24 (+41.18%)
Mutual labels:  constraint-satisfaction-problem, hybrid-solution
sudoku
Solve a Sudoku puzzle with a quantum computer
Stars: ✭ 20 (+17.65%)
Mutual labels:  constraint-satisfaction-problem, hybrid-solution
satellite-placement
Group satellites into constellations such that their average observation coverage is maximized
Stars: ✭ 20 (+17.65%)
Mutual labels:  constraint-satisfaction-problem, hybrid-solution
Decider
An Open Source .Net Constraint Programming Solver
Stars: ✭ 112 (+558.82%)
Mutual labels:  constraint-satisfaction-problem
circuit-fault-diagnosis
Find possible failing components on a circuit.
Stars: ✭ 18 (+5.88%)
Mutual labels:  constraint-satisfaction-problem
GHOST
General meta-Heuristic Optimization Solving Toolkit
Stars: ✭ 28 (+64.71%)
Mutual labels:  constraint-satisfaction-problem
facile
Python constraint programming library
Stars: ✭ 21 (+23.53%)
Mutual labels:  constraint-satisfaction-problem
clustering
Using a quantum computer to cluster data points
Stars: ✭ 20 (+17.65%)
Mutual labels:  constraint-satisfaction-problem
job-shop-scheduling
Determine a schedule for running a set of jobs.
Stars: ✭ 34 (+100%)
Mutual labels:  constraint-satisfaction-problem
ordered
Entropy-controlled contexts in Python
Stars: ✭ 36 (+111.76%)
Mutual labels:  constraint-satisfaction-problem
pycsp3
A Python Library for modeling combinatorial constrained problems
Stars: ✭ 39 (+129.41%)
Mutual labels:  constraint-satisfaction-problem
Optaplanner
AI constraint solver in Java to optimize the vehicle routing problem, employee rostering, task assignment, maintenance scheduling, conference scheduling and other planning problems.
Stars: ✭ 2,454 (+14335.29%)
Mutual labels:  constraint-satisfaction-problem
Stanford Cs 221 Artificial Intelligence
VIP cheatsheets for Stanford's CS 221 Artificial Intelligence
Stars: ✭ 1,923 (+11211.76%)
Mutual labels:  constraint-satisfaction-problem
factoring
Factor numbers using a quantum computer
Stars: ✭ 30 (+76.47%)
Mutual labels:  constraint-satisfaction-problem

Linux/Mac/Windows build status

Knapsack

The knapsack problem is a well-known optimization problem. It is encountered, for example, in packing shipping containers. A shipping container has a weight capacity which it can hold. Given a collection of items to be shipped, where each item has a value and a weight, the problem is to select the optimal items to pack in the shipping container. This optimization problem can be defined as an objective with a constraint:

  • Objective: Maximize freight value (sum of values of the selected items).
  • Constraint: Total freight weight (sum of weights of the selected items) must be less than or equal to the container's capacity.

This example solves such a knapsack problem by reformulating it as a constrained quadratic model (CQM) and submitting it to a Leap hybrid CQM solver.

Usage

To run the default demo, enter the command:

python knapsack.py

To view available options, enter the command:

python knapsack.py --help

Command-line arguments let you select one of several data sets (under the /data folder) and set the freight capacity. The data files are formulated as rows of items, each defined as a pair of weight and value.

Code Overview

The code in knapsack.py includes three main functions:

  • build_knapsack_cqm() creates a CQM by setting an objective and constraint as follows:

    • Objective: Binary variables are created for each item, and assigned a linear bias equal to the negative value of the item's value. To minimize this objective, by selecting an optimal set of items, is equivalent to maximizing the total value of the freight. Solutions set a value of 1 to selected items and 0 to unselected items.
    • Constraint: A quadratic model with the previously created binary variables, where the linear biases are set equal to the weight of each item, is created with the requirement that the total weight must not exceed the container's capacity.
  • parse_inputs() is a utility function that reads data from the example files.

  • parse_solution() parses and displays the results returned from the solver.

License

Released under the Apache License 2.0. See LICENSE file.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].