Sage Reference Manual: Numerical Optimization - Mirrors
Sage Reference Manual: Numerical Optimization - Mirrors
Sage Reference Manual: Numerical Optimization - Mirrors
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
<strong>Sage</strong> <strong>Reference</strong> <strong>Manual</strong>: <strong>Numerical</strong> <strong>Optimization</strong>, Release 6.1.1<br />
sage: L = [-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1]<br />
sage: Superincreasing(L).subset_sum(1)<br />
Traceback (most recent call last):<br />
...<br />
TypeError: Element e (= -10) of seq must be a non-negative integer.<br />
REFERENCES:<br />
sage.numerical.knapsack.knapsack(seq, binary=True, max=1, value_only=False, solver=None,<br />
verbose=0)<br />
Solves the knapsack problem<br />
For more information on the knapsack problem, see the documentation of the knapsack module or the<br />
Wikipedia article Knapsack_problem.<br />
INPUT:<br />
•seq – Two different possible types:<br />
–A sequence of tuples (weight, value, something1, something2, ...). Note that<br />
only the first two coordinates (weight and values) will be taken into account. The rest (if any)<br />
will be ignored. This can be useful if you need to attach some information to the items.<br />
–A sequence of reals (a value of 1 is assumed).<br />
•binary – When set to True, an item can be taken 0 or 1 time. When set to False, an item can be taken<br />
any amount of times (while staying integer and positive).<br />
•max – Maximum admissible weight.<br />
•value_only – When set to True, only the maximum useful value is returned. When set to False,<br />
both the maximum useful value and an assignment are returned.<br />
•solver – (default: None) Specify a Linear Program (LP) solver to be used. If set to None, the default<br />
one is used. For more information on LP solvers and which default solver is used, see the documentation<br />
of class MixedIntegerLinearProgram.<br />
•verbose – integer (default: 0). Sets the level of verbosity. Set to 0 by default, which means quiet.<br />
OUTPUT:<br />
If value_only is set to True, only the maximum useful value is returned. Else (the default), the function<br />
returns a pair [value,list], where list can be of two types according to the type of seq:<br />
•The list of tuples (w i , u i , ...) occurring in the solution.<br />
•A list of reals where each real is repeated the number of times it is taken into the solution.<br />
EXAMPLES:<br />
If your knapsack problem is composed of three items (weight, value) defined by (1,2), (1.5,1),<br />
(0.5,3), and a bag of maximum weight 2, you can easily solve it this way:<br />
sage: from sage.numerical.knapsack import knapsack<br />
sage: knapsack( [(1,2), (1.5,1), (0.5,3)], max=2)<br />
[5.0, [(1, 2), (0.500000000000000, 3)]]<br />
sage: knapsack( [(1,2), (1.5,1), (0.5,3)], max=2, value_only=True)<br />
5.0<br />
Besides weight and value, you may attach any data to the items:<br />
6 Chapter 1. Knapsack Problems