Sage Reference Manual: Numerical Optimization - Mirrors
Sage Reference Manual: Numerical Optimization - Mirrors
Sage Reference Manual: Numerical Optimization - Mirrors
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
<strong>Sage</strong> <strong>Reference</strong> <strong>Manual</strong>: <strong>Numerical</strong> <strong>Optimization</strong>, Release 6.1.1<br />
1.2 Examples<br />
If your knapsack problem is composed of three items (weight, value) defined by (1,2), (1.5,1), (0.5,3), and a bag of<br />
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 />
1.3 Super-increasing sequences<br />
We can test for whether or not a sequence is super-increasing:<br />
sage: from sage.numerical.knapsack import Superincreasing<br />
sage: L = [1, 2, 5, 21, 69, 189, 376, 919]<br />
sage: seq = Superincreasing(L)<br />
sage: seq<br />
Super-increasing sequence of length 8<br />
sage: seq.is_superincreasing()<br />
True<br />
sage: Superincreasing().is_superincreasing([1,3,5,7])<br />
False<br />
Solving the subset sum problem for a super-increasing sequence and target sum:<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 />
class sage.numerical.knapsack.Superincreasing(seq=None)<br />
Bases: sage.structure.sage_object.<strong>Sage</strong>Object<br />
A class for super-increasing sequences.<br />
Let L = (a 1 , a 2 , a 3 , . . . , a n ) be a non-empty sequence of non-negative integers. Then L is said to be superincreasing<br />
if each a i is strictly greater than the sum of all previous values. That is, for each a i ∈ L the sequence<br />
L must satisfy the property<br />
∑i−1<br />
a i ><br />
k=1<br />
in order to be called a super-increasing sequence, where |L| ≥ 2. If L has only one element, it is also defined to<br />
be a super-increasing sequence.<br />
If seq is None, then construct an empty sequence. By definition, this empty sequence is not super-increasing.<br />
INPUT:<br />
•seq – (default: None) a non-empty sequence.<br />
EXAMPLES:<br />
sage: from sage.numerical.knapsack import Superincreasing<br />
sage: L = [1, 2, 5, 21, 69, 189, 376, 919]<br />
sage: Superincreasing(L).is_superincreasing()<br />
True<br />
sage: Superincreasing().is_superincreasing([1,3,5,7])<br />
False<br />
a k<br />
2 Chapter 1. Knapsack Problems