# Ticket #9432: trac_9432.patch

File trac_9432.patch, 6.4 KB (added by ncohen, 11 years ago)
• ## sage/numerical/knapsack.py

```# 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 you can easily solve it this way:: sage: from sage.numerical.knapsack import knapsack sage: knapsack( [(1,2), (1.5,1), (0.5,3)], max=2) # optional - requires Glpk or COIN-OR/CBC sage: knapsack( [(1,2), (1.5,1), (0.5,3)], max=2) [5.0, [(1, 2), (0.500000000000000, 3)]] Super-increasing sequences and a bag of maximum weight 2, you can easily solve it this way:: sage: from sage.numerical.knapsack import knapsack sage: knapsack( [(1,2), (1.5,1), (0.5,3)], max=2) # optional - requires Glpk or COIN-OR/CBC sage: knapsack( [(1,2), (1.5,1), (0.5,3)], max=2) [5.0, [(1, 2), (0.500000000000000, 3)]] sage: knapsack( [(1,2), (1.5,1), (0.5,3)], max=2, value_only=True) # optional - requires Glpk or COIN-OR/CBC sage: knapsack( [(1,2), (1.5,1), (0.5,3)], max=2, value_only=True) 5.0 In the case where all the values (usefulness) of the items `(1,1), (1.5,1), (0.5,1)` the command:: sage: from sage.numerical.knapsack import knapsack sage: knapsack([1,1.5,0.5], max=2, value_only=True) # optional - requires Glpk or COIN-OR/CBC sage: knapsack([1,1.5,0.5], max=2, value_only=True) 2.0 INPUT:
• ## sage/numerical/mip.pyx

`diff -r f2be508f3236 -r e3fdb7b2857b sage/numerical/mip.pyx`
 a sage: p = MixedIntegerLinearProgram(maximization=True) """ self._default_solver = None self._default_solver = "GLPK" try: if self._default_solver == None: except ImportError: pass try: if self._default_solver == None: from sage.numerical.mip_glpk import solve_glpk self._default_solver = "GLPK" except ImportError: pass # List of all the MIPVariables linked to this instance of # MixedIntegerLinearProgram self._mipvariables = [] self._constraints_matrix_j = [] self._constraints_matrix_values = [] self._constraints_bounds_max = [] self._constraints_bounds_min = [] self._constraints_bounds_min = [] def __repr__(self): r""" http://en.wikipedia.org/wiki/MPS_%28format%29 """ try: from sage.numerical.mip_glpk import write_mps except ImportError: from sage.misc.exceptions import OptionalPackageNotFoundError raise OptionalPackageNotFoundError("You need GLPK installed to use this function. To install it, you can type in Sage: install_package('glpk')") from sage.numerical.mip_glpk import write_mps self._update_variables_name() write_mps(self, filename, modern) For more information about the LP file format : http://lpsolve.sourceforge.net/5.5/lp-format.htm """ try: from sage.numerical.mip_glpk import write_lp except: from sage.misc.exceptions import OptionalPackageNotFoundError raise OptionalPackageNotFoundError("You need GLPK installed to use this function. To install it, you can type in Sage: install_package('glpk')") from sage.numerical.mip_glpk import write_lp self._update_variables_name() write_lp(self, filename) sage: p.add_constraint(1.5*x[1] + 3*x[2], max=4) sage: round(p.solve(),6) 6.666667 sage: p.get_values(x) {0: 0.0, 1: 1.3333333333333333} sage: x = p.get_values(x) sage: round(x[1],6) 0.0 sage: round(x[2],6) 1.333333 Computation of a maximum stable set in Petersen's graph:: if solver == None: solver = self._default_solver if solver == None: from sage.misc.exceptions import OptionalPackageNotFoundError 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.") try: if solver == "Coin": from sage.numerical.mip_coin import solve_coin as solve
• ## sage/numerical/optimize.py

`diff -r f2be508f3236 -r e3fdb7b2857b sage/numerical/optimize.py`
 a sage: from sage.numerical.optimize import binpacking sage: values = [1/5, 1/3, 2/3, 3/4, 5/7] sage: bins = binpacking(values) # optional - GLPK, CBC sage: len(bins) # optional - GLPK, CBC sage: bins = binpacking(values) sage: len(bins) 3 Checking the bins are of correct size :: sage: all([ sum(b)<= 1 for b in bins ]) # optional - GLPK, CBC sage: all([ sum(b)<= 1 for b in bins ]) True Checking every item is in a bin :: sage: b1, b2, b3 = bins # optional - GLPK, CBC sage: all([ (v in b1 or v in b2 or v in b3) for v in values ]) # optional - GLPK, CBC sage: b1, b2, b3 = bins sage: all([ (v in b1 or v in b2 or v in b3) for v in values ]) True One way to use only three boxes (which is best possible) is to put Of course, we can also check that there is no solution using only two boxes :: sage: from sage.numerical.optimize import binpacking sage: binpacking([0.2,0.3,0.8,0.9], k=2)              # optional - GLPK, CBC sage: binpacking([0.2,0.3,0.8,0.9], k=2) Traceback (most recent call last): ... ValueError: This problem has no solution !