# HG changeset patch
# User Nathann Cohen <nathann.cohen@gmail.com>
# Date 1334927241 -7200
# Node ID 501d84007144e24da097156db23ce37cb499b962
# Parent 89d18ab55f9e6be9a4bdafd529f5b26f925098fc
Fixing doctests in sage/numerical/mip.pyx when using Gurobi
diff --git a/sage/numerical/backends/generic_backend.pyx b/sage/numerical/backends/generic_backend.pyx
a
|
b
|
|
854 | 854 | return default_solver |
855 | 855 | |
856 | 856 | else: |
857 | | for s in ["CPLEX", "Gurobi", "Coin", "GLPK"]: |
| 857 | for s in ["Cplex", "Gurobi", "Coin", "Glpk"]: |
858 | 858 | try: |
859 | 859 | default_mip_solver(s) |
860 | 860 | return s |
861 | 861 | except ValueError: |
862 | 862 | pass |
863 | 863 | |
864 | | elif solver == "CPLEX": |
| 864 | solver = solver.capitalize() |
| 865 | |
| 866 | if solver == "Cplex": |
865 | 867 | try: |
866 | 868 | from sage.numerical.backends.cplex_backend import CPLEXBackend |
867 | 869 | default_solver = solver |
… |
… |
|
882 | 884 | except ImportError: |
883 | 885 | raise ValueError("Gurobi is not available. Please refer to the documentation to install it.") |
884 | 886 | |
885 | | elif solver == "GLPK": |
| 887 | elif solver == "Glpk": |
886 | 888 | default_solver = solver |
887 | 889 | |
888 | 890 | else: |
… |
… |
|
935 | 937 | # We do not want to use Coin for constraint_generation. It just does not |
936 | 938 | # work |
937 | 939 | if solver == "Coin" and constraint_generation: |
938 | | solver = "GLPK" |
| 940 | solver = "Glpk" |
| 941 | |
| 942 | else: |
| 943 | solver = solver.capitalize() |
939 | 944 | |
940 | 945 | if solver == "Coin": |
941 | 946 | from sage.numerical.backends.coin_backend import CoinBackend |
942 | 947 | return CoinBackend() |
943 | 948 | |
944 | | elif solver == "GLPK": |
| 949 | elif solver == "Glpk": |
945 | 950 | from sage.numerical.backends.glpk_backend import GLPKBackend |
946 | 951 | return GLPKBackend() |
947 | 952 | |
948 | | elif solver == "CPLEX": |
| 953 | elif solver == "Cplex": |
949 | 954 | from sage.numerical.backends.cplex_backend import CPLEXBackend |
950 | 955 | return CPLEXBackend() |
951 | 956 | |
diff --git a/sage/numerical/mip.pyx b/sage/numerical/mip.pyx
a
|
b
|
|
52 | 52 | |
53 | 53 | The following example shows all these steps:: |
54 | 54 | |
55 | | sage: p = MixedIntegerLinearProgram(maximization=False) |
| 55 | sage: p = MixedIntegerLinearProgram(maximization=False, solver = "GLPK") |
56 | 56 | sage: w = p.new_variable(integer=True) |
57 | 57 | sage: p.add_constraint(w[0] + w[1] + w[2] - 14*w[3] == 0) |
58 | 58 | sage: p.add_constraint(w[1] + 2*w[2] - 8*w[3] == 0) |
… |
… |
|
96 | 96 | programming (LP) and mixed integer programming (MIP) solvers. |
97 | 97 | |
98 | 98 | See the Wikipedia article on `linear programming |
99 | | <http://en.wikipedia.org/wiki/Linear_programming>`_ for further information. |
| 99 | <http://en.wikipedia.org/wiki/Linear_programming>`_ for further information |
| 100 | on linear programming and the documentation of the :mod:`MILP module |
| 101 | <sage.numerical.mip>` for its use in Sage. |
100 | 102 | |
101 | 103 | A mixed integer program consists of variables, linear constraints on these |
102 | 104 | variables, and an objective function which is to be maximised or minimised |
… |
… |
|
105 | 107 | |
106 | 108 | INPUT: |
107 | 109 | |
108 | | - ``solver`` -- 3 solvers should be available through this class: |
| 110 | - ``solver`` -- 4 solvers should be available through this class: |
109 | 111 | |
110 | 112 | - GLPK (``solver="GLPK"``). See the `GLPK |
111 | 113 | <http://www.gnu.org/software/glpk/>`_ web site. |
… |
… |
|
116 | 118 | - CPLEX (``solver="CPLEX"``). See the `CPLEX |
117 | 119 | <http://www.ilog.com/products/cplex/>`_ web site. |
118 | 120 | |
119 | | - GUROBI (``solver="GUROBI"``). See the `GUROBI <http://www.gurobi.com/>`_ |
| 121 | - Gurobi (``solver="Gurobi"``). See the `Gurobi <http://www.gurobi.com/>`_ |
120 | 122 | web site. |
121 | 123 | |
122 | 124 | - If ``solver=None`` (default), the default solver is used (see |
… |
… |
|
158 | 160 | |
159 | 161 | INPUT: |
160 | 162 | |
161 | | - ``solver`` -- 3 solvers should be available through this class: |
| 163 | - ``solver`` -- 4 solvers should be available through this class: |
162 | 164 | |
163 | 165 | - GLPK (``solver="GLPK"``). See the `GLPK |
164 | 166 | <http://www.gnu.org/software/glpk/>`_ web site. |
… |
… |
|
170 | 172 | <http://www.ilog.com/products/cplex/>`_ web site. An interface to |
171 | 173 | CPLEX is not yet implemented. |
172 | 174 | |
173 | | - GUROBI (``solver="GUROBI"``). See the `GUROBI |
| 175 | - Gurobi (``solver="Gurobi"``). See the `Gurobi |
174 | 176 | <http://www.gurobi.com/>`_ web site. |
175 | 177 | |
176 | 178 | -If ``solver=None`` (default), the default solver is used (see |
… |
… |
|
494 | 496 | |
495 | 497 | Running the examples from above, reordering applied:: |
496 | 498 | |
497 | | sage: p = MixedIntegerLinearProgram() |
| 499 | sage: p = MixedIntegerLinearProgram(solver = "GLPK") |
498 | 500 | sage: p.add_constraint(p[0] - p[2], min = 1, max = 4) |
499 | 501 | sage: p.add_constraint(p[0] - 2*p[1], min = 1) |
500 | 502 | sage: sorted(map(reorder_constraint,p.constraints())) |
… |
… |
|
542 | 544 | |
543 | 545 | When constraints and variables have names :: |
544 | 546 | |
545 | | sage: p = MixedIntegerLinearProgram() |
| 547 | sage: p = MixedIntegerLinearProgram(solver = "GLPK") |
546 | 548 | sage: x = p.new_variable(name="Hey") |
547 | 549 | sage: p.set_objective(x[1] + x[2]) |
548 | 550 | sage: p.add_constraint(-3*x[1] + 2*x[2], max=2, name="Constraint_1") |
… |
… |
|
557 | 559 | |
558 | 560 | Without any names :: |
559 | 561 | |
560 | | sage: p = MixedIntegerLinearProgram() |
| 562 | sage: p = MixedIntegerLinearProgram(solver = "GLPK") |
561 | 563 | sage: x = p.new_variable() |
562 | 564 | sage: p.set_objective(x[1] + x[2]) |
563 | 565 | sage: p.add_constraint(-3*x[1] + 2*x[2], max=2) |
… |
… |
|
807 | 809 | |
808 | 810 | INPUT: |
809 | 811 | |
810 | | - ``obj`` -- A linear function to be optimized. |
| 812 | - ``obj`` -- A linear function to be optimized. |
811 | 813 | ( can also be set to ``None`` or ``0`` when just |
812 | 814 | looking for a feasible solution ) |
813 | 815 | |
… |
… |
|
834 | 836 | sage: round(p.solve(),5) |
835 | 837 | 6.66667 |
836 | 838 | sage: p.set_objective(None) |
837 | | sage: p.solve() |
838 | | 0.0 |
| 839 | sage: _ = p.solve() |
839 | 840 | """ |
840 | 841 | cdef list values = [] |
841 | 842 | |
… |
… |
|
850 | 851 | f = obj.dict() |
851 | 852 | else: |
852 | 853 | f = {-1 : 0} |
853 | | |
| 854 | |
854 | 855 | f.pop(-1,0) |
855 | 856 | |
856 | 857 | for i in range(self._backend.ncols()): |
… |
… |
|
858 | 859 | |
859 | 860 | |
860 | 861 | self._backend.set_objective(values) |
861 | | |
| 862 | |
862 | 863 | def add_constraint(self, linear_function, max=None, min=None, name=None): |
863 | 864 | r""" |
864 | 865 | Adds a constraint to the ``MixedIntegerLinearProgram``. |
… |
… |
|
937 | 938 | |
938 | 939 | Complex constraints:: |
939 | 940 | |
940 | | sage: p = MixedIntegerLinearProgram() |
| 941 | sage: p = MixedIntegerLinearProgram(solver = "GLPK") |
941 | 942 | sage: b = p.new_variable() |
942 | 943 | sage: p.add_constraint( b[8] - b[15] <= 3*b[8] + 9) |
943 | 944 | sage: p.show() |
… |
… |
|
967 | 968 | ValueError: min and max arguments are required to be numerical |
968 | 969 | |
969 | 970 | Do not add redundant elements (notice only one copy of each constraint is added):: |
970 | | |
971 | | sage: lp = MixedIntegerLinearProgram(check_redundant=True) |
972 | | sage: for each in xrange(10): lp.add_constraint(lp[0]-lp[1],min=1) |
973 | | sage: lp.show() |
974 | | Maximization: |
975 | | <BLANKLINE> |
976 | | Constraints: |
977 | | 1.0 <= x_0 -x_1 |
978 | | Variables: |
979 | | x_0 is a continuous variable (min=0.0, max=+oo) |
980 | | x_1 is a continuous variable (min=0.0, max=+oo) |
981 | | |
| 971 | |
| 972 | sage: lp = MixedIntegerLinearProgram(solver = "GLPK", check_redundant=True) |
| 973 | sage: for each in xrange(10): lp.add_constraint(lp[0]-lp[1],min=1) |
| 974 | sage: lp.show() |
| 975 | Maximization: |
| 976 | <BLANKLINE> |
| 977 | Constraints: |
| 978 | 1.0 <= x_0 -x_1 |
| 979 | Variables: |
| 980 | x_0 is a continuous variable (min=0.0, max=+oo) |
| 981 | x_1 is a continuous variable (min=0.0, max=+oo) |
| 982 | |
982 | 983 | We check for constant multiples of constraints as well:: |
983 | | |
984 | | sage: for each in xrange(10): lp.add_constraint(2*lp[0]-2*lp[1],min=2) |
985 | | sage: lp.show() |
986 | | Maximization: |
987 | | <BLANKLINE> |
| 984 | |
| 985 | sage: for each in xrange(10): lp.add_constraint(2*lp[0]-2*lp[1],min=2) |
| 986 | sage: lp.show() |
| 987 | Maximization: |
| 988 | <BLANKLINE> |
988 | 989 | Constraints: |
989 | 990 | 1.0 <= x_0 -x_1 |
990 | 991 | Variables: |