Ticket #9432: trac_9432.patch

File trac_9432.patch, 6.4 KB (added by ncohen, 9 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 b  
    5050you can easily solve it this way::
    5151
    5252    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)
    5454    [5.0, [(1, 2), (0.500000000000000, 3)]]
    5555
    5656Super-increasing sequences
     
    593593    and a bag of maximum weight 2, you can easily solve it this way::
    594594
    595595        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)
    597597        [5.0, [(1, 2), (0.500000000000000, 3)]]
    598598
    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)
    600600        5.0
    601601
    602602    In the case where all the values (usefulness) of the items
     
    605605    `(1,1), (1.5,1), (0.5,1)` the command::
    606606
    607607        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)
    609609        2.0
    610610
    611611    INPUT:
  • sage/numerical/mip.pyx

    diff -r f2be508f3236 -r e3fdb7b2857b sage/numerical/mip.pyx
    a b  
    6363            sage: p = MixedIntegerLinearProgram(maximization=True)
    6464        """
    6565       
    66         self._default_solver = None
     66        self._default_solver = "GLPK"
    6767       
    6868        try:
    6969            if self._default_solver == None:
     
    7979        except ImportError:
    8080            pass
    8181
    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 
    9082        # List of all the MIPVariables linked to this instance of
    9183        # MixedIntegerLinearProgram
    9284        self._mipvariables = []
     
    152144        self._constraints_matrix_j = []
    153145        self._constraints_matrix_values = []
    154146        self._constraints_bounds_max = []
    155         self._constraints_bounds_min = []       
    156 
    157 
     147        self._constraints_bounds_min = []
    158148
    159149    def __repr__(self):
    160150         r"""
     
    492482        http://en.wikipedia.org/wiki/MPS_%28format%29
    493483        """
    494484       
    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
    500486
    501487        self._update_variables_name()
    502488        write_mps(self, filename, modern)
     
    524510        For more information about the LP file format :
    525511        http://lpsolve.sourceforge.net/5.5/lp-format.htm
    526512        """
    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
    532514
    533515        self._update_variables_name()
    534516        write_lp(self, filename)
     
    10521034            sage: p.add_constraint(1.5*x[1] + 3*x[2], max=4)
    10531035            sage: round(p.solve(),6)
    10541036            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
    10571042
    10581043         Computation of a maximum stable set in Petersen's graph::
    10591044
     
    10751060        if solver == None:
    10761061            solver = self._default_solver
    10771062
    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 
    10831063        try:
    10841064            if solver == "Coin":
    10851065                from sage.numerical.mip_coin import solve_coin as solve
  • sage/numerical/optimize.py

    diff -r f2be508f3236 -r e3fdb7b2857b sage/numerical/optimize.py
    a b  
    698698
    699699        sage: from sage.numerical.optimize import binpacking
    700700        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)
    703703        3
    704704
    705705    Checking the bins are of correct size ::
    706706
    707         sage: all([ sum(b)<= 1 for b in bins ]) # optional - GLPK, CBC
     707        sage: all([ sum(b)<= 1 for b in bins ])
    708708        True
    709709
    710710    Checking every item is in a bin ::
    711711
    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 ])
    714714        True
    715715
    716716    One way to use only three boxes (which is best possible) is to put
     
    720720    Of course, we can also check that there is no solution using only two boxes ::
    721721
    722722        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)
    724724        Traceback (most recent call last):
    725725        ...
    726726        ValueError: This problem has no solution !