09.02.2015 Views

Sage Reference Manual: Numerical Optimization - Mirrors

Sage Reference Manual: Numerical Optimization - Mirrors

Sage Reference Manual: Numerical Optimization - Mirrors

SHOW MORE
SHOW LESS

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 />

The sequence that self represents must also be non-empty:<br />

sage: Superincreasing([]).largest_less_than(2)<br />

Traceback (most recent call last):<br />

...<br />

ValueError: seq must be a super-increasing sequence<br />

sage: Superincreasing(list()).largest_less_than(2)<br />

Traceback (most recent call last):<br />

...<br />

ValueError: seq must be a super-increasing sequence<br />

subset_sum(N)<br />

Solving the subset sum problem for a super-increasing sequence.<br />

Let S = (s 1 , s 2 , s 3 , . . . , s n ) be a non-empty sequence of non-negative integers, and let N ∈ Z be nonnegative.<br />

The subset sum problem asks for a subset A ⊆ S all of whose elements sum to N. This method<br />

specializes the subset sum problem to the case of super-increasing sequences. If a solution exists, then it<br />

is also a super-increasing sequence.<br />

Note: This method only solves the subset sum problem for super-increasing sequences. In general,<br />

solving the subset sum problem for an arbitrary sequence is known to be computationally hard.<br />

INPUT:<br />

•N – a non-negative integer.<br />

OUTPUT:<br />

•A non-empty subset of self whose elements sum to N. This subset is also a super-increasing sequence.<br />

If no such subset exists, then return the empty list.<br />

ALGORITHMS:<br />

The algorithm used is adapted from page 355 of [HPS08].<br />

EXAMPLES:<br />

Solving the subset sum problem for a super-increasing sequence and target sum:<br />

sage: from sage.numerical.knapsack import Superincreasing<br />

sage: L = [1, 2, 5, 21, 69, 189, 376, 919]<br />

sage: Superincreasing(L).subset_sum(98)<br />

[69, 21, 5, 2, 1]<br />

TESTS:<br />

The target N must be a non-negative integer:<br />

sage: from sage.numerical.knapsack import Superincreasing<br />

sage: L = [0, 1, 2, 4]<br />

sage: Superincreasing(L).subset_sum(-6)<br />

Traceback (most recent call last):<br />

...<br />

TypeError: N (= -6) must be a non-negative integer.<br />

sage: Superincreasing(L).subset_sum(-6.2)<br />

Traceback (most recent call last):<br />

...<br />

TypeError: N (= -6.20000000000000) must be a non-negative integer.<br />

The sequence that self represents must only contain non-negative integers:<br />

1.3. Super-increasing sequences 5

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!