Knapsack#

class Knapsack(values, weights, max_weight)[fuente]#

Bases: OptimizationApplication

Optimization application for the «knapsack problem» [1].

Referencias

[1]: «Knapsack problem», https://en.wikipedia.org/wiki/Knapsack_problem

Parámetros:
  • values (List[int]) – A list of the values of items

  • weights (List[int]) – A list of the weights of items

  • max_weight (int) – The maximum weight capacity

Attributes

max_weight#

Getter of max_weight

Devuelve:

The maximal weight for the knapsack problem

Methods

interpret(result)[fuente]#

Interpret a result as item indices

Parámetros:

result (OptimizationResult | ndarray) – The calculated result of the problem

Devuelve:

A list of items whose corresponding variable is 1

Tipo del valor devuelto:

List[int]

static sample_most_likely(state_vector)#

Compute the most likely binary string from state vector.

Parámetros:

state_vector (QuasiDistribution | Statevector | ndarray | Dict) – state vector or counts or quasi-probabilities.

Devuelve:

binary string as numpy.ndarray of ints.

Muestra:

ValueError – if state_vector is not QuasiDistribution, Statevector, np.ndarray, or dict.

Tipo del valor devuelto:

ndarray

to_quadratic_program()[fuente]#

Convert a knapsack problem instance into a QuadraticProgram

Devuelve:

The QuadraticProgram created from the knapsack problem instance.

Tipo del valor devuelto:

QuadraticProgram