# HG changeset patch
# User Nathann Cohen <nathann.cohen@gmail.com>
# Date 1278401239 -7200
# Node ID e3fdb7b2857bbd527295111eca0dc67b9ad701a5
# Parent f2be508f32363b770369110bed056b7222bcbc34
trac 9432 -- fixing a docstring
diff -r f2be508f3236 -r e3fdb7b2857b sage/numerical/knapsack.py
a
|
b
|
|
50 | 50 | you can easily solve it this way:: |
51 | 51 | |
52 | 52 | sage: from sage.numerical.knapsack import knapsack |
53 | | sage: knapsack( [(1,2), (1.5,1), (0.5,3)], max=2) # optional - requires Glpk or COIN-OR/CBC |
| 53 | sage: knapsack( [(1,2), (1.5,1), (0.5,3)], max=2) |
54 | 54 | [5.0, [(1, 2), (0.500000000000000, 3)]] |
55 | 55 | |
56 | 56 | Super-increasing sequences |
… |
… |
|
593 | 593 | and a bag of maximum weight 2, you can easily solve it this way:: |
594 | 594 | |
595 | 595 | sage: from sage.numerical.knapsack import knapsack |
596 | | sage: knapsack( [(1,2), (1.5,1), (0.5,3)], max=2) # optional - requires Glpk or COIN-OR/CBC |
| 596 | sage: knapsack( [(1,2), (1.5,1), (0.5,3)], max=2) |
597 | 597 | [5.0, [(1, 2), (0.500000000000000, 3)]] |
598 | 598 | |
599 | | sage: knapsack( [(1,2), (1.5,1), (0.5,3)], max=2, value_only=True) # optional - requires Glpk or COIN-OR/CBC |
| 599 | sage: knapsack( [(1,2), (1.5,1), (0.5,3)], max=2, value_only=True) |
600 | 600 | 5.0 |
601 | 601 | |
602 | 602 | In the case where all the values (usefulness) of the items |
… |
… |
|
605 | 605 | `(1,1), (1.5,1), (0.5,1)` the command:: |
606 | 606 | |
607 | 607 | sage: from sage.numerical.knapsack import knapsack |
608 | | sage: knapsack([1,1.5,0.5], max=2, value_only=True) # optional - requires Glpk or COIN-OR/CBC |
| 608 | sage: knapsack([1,1.5,0.5], max=2, value_only=True) |
609 | 609 | 2.0 |
610 | 610 | |
611 | 611 | INPUT: |
diff -r f2be508f3236 -r e3fdb7b2857b sage/numerical/mip.pyx
a
|
b
|
|
63 | 63 | sage: p = MixedIntegerLinearProgram(maximization=True) |
64 | 64 | """ |
65 | 65 | |
66 | | self._default_solver = None |
| 66 | self._default_solver = "GLPK" |
67 | 67 | |
68 | 68 | try: |
69 | 69 | if self._default_solver == None: |
… |
… |
|
79 | 79 | except ImportError: |
80 | 80 | pass |
81 | 81 | |
82 | | try: |
83 | | if self._default_solver == None: |
84 | | from sage.numerical.mip_glpk import solve_glpk |
85 | | self._default_solver = "GLPK" |
86 | | except ImportError: |
87 | | pass |
88 | | |
89 | | |
90 | 82 | # List of all the MIPVariables linked to this instance of |
91 | 83 | # MixedIntegerLinearProgram |
92 | 84 | self._mipvariables = [] |
… |
… |
|
152 | 144 | self._constraints_matrix_j = [] |
153 | 145 | self._constraints_matrix_values = [] |
154 | 146 | self._constraints_bounds_max = [] |
155 | | self._constraints_bounds_min = [] |
156 | | |
157 | | |
| 147 | self._constraints_bounds_min = [] |
158 | 148 | |
159 | 149 | def __repr__(self): |
160 | 150 | r""" |
… |
… |
|
492 | 482 | http://en.wikipedia.org/wiki/MPS_%28format%29 |
493 | 483 | """ |
494 | 484 | |
495 | | try: |
496 | | from sage.numerical.mip_glpk import write_mps |
497 | | except ImportError: |
498 | | from sage.misc.exceptions import OptionalPackageNotFoundError |
499 | | raise OptionalPackageNotFoundError("You need GLPK installed to use this function. To install it, you can type in Sage: install_package('glpk')") |
| 485 | from sage.numerical.mip_glpk import write_mps |
500 | 486 | |
501 | 487 | self._update_variables_name() |
502 | 488 | write_mps(self, filename, modern) |
… |
… |
|
524 | 510 | For more information about the LP file format : |
525 | 511 | http://lpsolve.sourceforge.net/5.5/lp-format.htm |
526 | 512 | """ |
527 | | try: |
528 | | from sage.numerical.mip_glpk import write_lp |
529 | | except: |
530 | | from sage.misc.exceptions import OptionalPackageNotFoundError |
531 | | raise OptionalPackageNotFoundError("You need GLPK installed to use this function. To install it, you can type in Sage: install_package('glpk')") |
| 513 | from sage.numerical.mip_glpk import write_lp |
532 | 514 | |
533 | 515 | self._update_variables_name() |
534 | 516 | write_lp(self, filename) |
… |
… |
|
1052 | 1034 | sage: p.add_constraint(1.5*x[1] + 3*x[2], max=4) |
1053 | 1035 | sage: round(p.solve(),6) |
1054 | 1036 | 6.666667 |
1055 | | sage: p.get_values(x) |
1056 | | {0: 0.0, 1: 1.3333333333333333} |
| 1037 | sage: x = p.get_values(x) |
| 1038 | sage: round(x[1],6) |
| 1039 | 0.0 |
| 1040 | sage: round(x[2],6) |
| 1041 | 1.333333 |
1057 | 1042 | |
1058 | 1043 | Computation of a maximum stable set in Petersen's graph:: |
1059 | 1044 | |
… |
… |
|
1075 | 1060 | if solver == None: |
1076 | 1061 | solver = self._default_solver |
1077 | 1062 | |
1078 | | |
1079 | | if solver == None: |
1080 | | from sage.misc.exceptions import OptionalPackageNotFoundError |
1081 | | raise OptionalPackageNotFoundError("There does not seem to be any (Mixed) Integer Linear Program solver installed. Please visit http://www.sagemath.org/doc/constructions/linear_programming.html to learn more about the solvers available.") |
1082 | | |
1083 | 1063 | try: |
1084 | 1064 | if solver == "Coin": |
1085 | 1065 | from sage.numerical.mip_coin import solve_coin as solve |
diff -r f2be508f3236 -r e3fdb7b2857b sage/numerical/optimize.py
a
|
b
|
|
698 | 698 | |
699 | 699 | sage: from sage.numerical.optimize import binpacking |
700 | 700 | sage: values = [1/5, 1/3, 2/3, 3/4, 5/7] |
701 | | sage: bins = binpacking(values) # optional - GLPK, CBC |
702 | | sage: len(bins) # optional - GLPK, CBC |
| 701 | sage: bins = binpacking(values) |
| 702 | sage: len(bins) |
703 | 703 | 3 |
704 | 704 | |
705 | 705 | Checking the bins are of correct size :: |
706 | 706 | |
707 | | sage: all([ sum(b)<= 1 for b in bins ]) # optional - GLPK, CBC |
| 707 | sage: all([ sum(b)<= 1 for b in bins ]) |
708 | 708 | True |
709 | 709 | |
710 | 710 | Checking every item is in a bin :: |
711 | 711 | |
712 | | sage: b1, b2, b3 = bins # optional - GLPK, CBC |
713 | | sage: all([ (v in b1 or v in b2 or v in b3) for v in values ]) # optional - GLPK, CBC |
| 712 | sage: b1, b2, b3 = bins |
| 713 | sage: all([ (v in b1 or v in b2 or v in b3) for v in values ]) |
714 | 714 | True |
715 | 715 | |
716 | 716 | One way to use only three boxes (which is best possible) is to put |
… |
… |
|
720 | 720 | Of course, we can also check that there is no solution using only two boxes :: |
721 | 721 | |
722 | 722 | sage: from sage.numerical.optimize import binpacking |
723 | | sage: binpacking([0.2,0.3,0.8,0.9], k=2) # optional - GLPK, CBC |
| 723 | sage: binpacking([0.2,0.3,0.8,0.9], k=2) |
724 | 724 | Traceback (most recent call last): |
725 | 725 | ... |
726 | 726 | ValueError: This problem has no solution ! |