# HG changeset patch
# User Nathann Cohen <nathann.cohen@gmail.com>
# Date 1334234028 -7200
# Node ID 89d18ab55f9e6be9a4bdafd529f5b26f925098fc
# Parent 7d8ac0ee679de0e5eeba4966fe280f5f65295472
Crashes and broken doctests with Gurobi
diff --git a/sage/graphs/digraph.py b/sage/graphs/digraph.py
a
|
b
|
|
1314 | 1314 | - ``verbose`` -- integer (default: ``0``). Sets the level of |
1315 | 1315 | verbosity. Set to 0 by default, which means quiet. |
1316 | 1316 | |
1317 | | ALGORITHM: |
| 1317 | ALGORITHM: |
1318 | 1318 | |
1319 | 1319 | This problem is solved using Linear Programming, in two different |
1320 | 1320 | ways. The first one is to solve the following formulation: |
1321 | 1321 | |
1322 | 1322 | .. MATH:: |
1323 | 1323 | |
1324 | | \mbox{Minimize : }&\sum_{(u,v)\in G} b_{(u,v)}\\ |
1325 | | \mbox{Such that : }&\\ |
1326 | | &\forall (u,v)\in G, d_u-d_v+nb_{(u,v)}\geq 0\\ |
| 1324 | \mbox{Minimize : }&\sum_{(u,v)\in G} b_{(u,v)}\\ |
| 1325 | \mbox{Such that : }&\\ |
| 1326 | &\forall (u,v)\in G, d_u-d_v+ n \cdot b_{(u,v)}\geq 0\\ |
1327 | 1327 | &\forall u\in G, 0\leq d_u\leq |G|\\ |
1328 | | |
| 1328 | |
1329 | 1329 | An explanation: |
1330 | 1330 | |
1331 | 1331 | An acyclic digraph can be seen as a poset, and every poset has a linear |
… |
… |
|
1339 | 1339 | |
1340 | 1340 | The number of edges removed is then minimized, which is the objective. |
1341 | 1341 | |
1342 | | (Constraint Generation) |
1343 | | |
| 1342 | (Constraint Generation) |
| 1343 | |
1344 | 1344 | If the parameter ``constraint_generation`` is enabled, a more efficient |
1345 | 1345 | formulation is used : |
1346 | | |
1347 | | .. MATH:: |
1348 | | |
1349 | | \mbox{Minimize : }&\sum_{(u,v)\in G} b_{(u,v)}\\ |
1350 | | \mbox{Such that : }&\\ |
1351 | | &\forall C\text{ circuits }\subseteq G, \sum_{uv\in C}b_{(u,v)}\geq 1\\ |
1352 | | |
| 1346 | |
| 1347 | .. MATH:: |
| 1348 | |
| 1349 | \mbox{Minimize : }&\sum_{(u,v)\in G} b_{(u,v)}\\ |
| 1350 | \mbox{Such that : }&\\ |
| 1351 | &\forall C\text{ circuits }\subseteq G, \sum_{uv\in C}b_{(u,v)}\geq 1\\ |
| 1352 | |
1353 | 1353 | As the number of circuits contained in a graph is exponential, this LP |
1354 | 1354 | is solved through constraint generation. This means that the solver is |
1355 | 1355 | sequentially asked to solved the problem, knowing only a portion of the |
… |
… |
|
1364 | 1364 | |
1365 | 1365 | sage: cycle=graphs.CycleGraph(5) |
1366 | 1366 | sage: dcycle=DiGraph(cycle) |
1367 | | sage: cycle.size() |
| 1367 | sage: cycle.size() |
1368 | 1368 | 5 |
1369 | 1369 | sage: dcycle.feedback_edge_set(value_only=True) |
1370 | 1370 | 5 |
1371 | | |
| 1371 | |
1372 | 1372 | And in this situation, for any edge `uv` of the first graph, `uv` of |
1373 | 1373 | `vu` is in the returned feedback arc set:: |
1374 | 1374 | |
… |
… |
|
1379 | 1379 | sage: (u,v) in feedback or (v,u) in feedback |
1380 | 1380 | True |
1381 | 1381 | |
1382 | | TESTS: |
1383 | | |
1384 | | Comparing with/without constraint generation:: |
1385 | | |
1386 | | sage: g = digraphs.RandomDirectedGNP(10,.3) |
1387 | | sage: x = g.feedback_edge_set(value_only = True) |
1388 | | sage: y = g.feedback_edge_set(value_only = True, |
1389 | | ... constraint_generation = False) |
1390 | | sage: x == y |
1391 | | True |
| 1382 | TESTS: |
| 1383 | |
| 1384 | Comparing with/without constraint generation. Also double-checks ticket :trac:`12833`:: |
| 1385 | |
| 1386 | sage: for i in range(20): |
| 1387 | ... g = digraphs.RandomDirectedGNP(10,.3) |
| 1388 | ... x = g.feedback_edge_set(value_only = True) |
| 1389 | ... y = g.feedback_edge_set(value_only = True, |
| 1390 | ... constraint_generation = False) |
| 1391 | ... if x != y: |
| 1392 | ... print "Oh my, oh my !" |
| 1393 | ... break |
1392 | 1394 | """ |
1393 | 1395 | # It would be a pity to start a LP if the digraph is already acyclic |
1394 | 1396 | if self.is_directed_acyclic(): |
… |
… |
|
1409 | 1411 | |
1410 | 1412 | # Variables are binary, and their coefficient in the objective is 1 |
1411 | 1413 | |
1412 | | p.set_objective( Sum( b[u][v] |
| 1414 | p.set_objective( Sum( b[u][v] |
1413 | 1415 | for u,v in self.edges(labels = False))) |
1414 | 1416 | |
1415 | 1417 | p.solve(log = verbose) |
1416 | 1418 | |
1417 | 1419 | # For as long as we do not break because the digraph is |
1418 | 1420 | # acyclic.... |
1419 | | while (1): |
| 1421 | while True: |
1420 | 1422 | |
1421 | 1423 | # Building the graph without the edges removed by the LP |
1422 | 1424 | h = DiGraph() |
… |
… |
|
1426 | 1428 | |
1427 | 1429 | # Is the digraph acyclic ? |
1428 | 1430 | isok, certificate = h.is_directed_acyclic(certificate = True) |
1429 | | |
| 1431 | |
1430 | 1432 | # If so, we are done ! |
1431 | 1433 | if isok: |
1432 | 1434 | break |
… |
… |
|
1437 | 1439 | # There is a circuit left. Let's add the corresponding |
1438 | 1440 | # constraint ! |
1439 | 1441 | |
1440 | | p.add_constraint( |
1441 | | Sum( b[u][v] for u,v in |
| 1442 | p.add_constraint( |
| 1443 | Sum( b[u][v] for u,v in |
1442 | 1444 | zip(certificate, certificate[1:] + [certificate[0]])), |
1443 | 1445 | min = 1) |
1444 | | |
| 1446 | |
1445 | 1447 | obj = p.solve(log = verbose) |
1446 | 1448 | |
1447 | 1449 | if value_only: |
1448 | 1450 | return Integer(round(obj)) |
1449 | | |
| 1451 | |
1450 | 1452 | else: |
1451 | | |
| 1453 | |
1452 | 1454 | # listing the edges contained in the MFAS |
1453 | 1455 | return [(u,v) for u,v in self.edges(labels = False) |
1454 | 1456 | if p.get_values(b[u][v]) > .5] |
… |
… |
|
1458 | 1460 | ###################################### |
1459 | 1461 | else: |
1460 | 1462 | p=MixedIntegerLinearProgram(maximization=False, solver=solver) |
1461 | | |
| 1463 | |
1462 | 1464 | b=p.new_variable(binary = True) |
1463 | 1465 | d=p.new_variable(integer = True) |
1464 | 1466 | |
… |
… |
|
1468 | 1470 | p.add_constraint(d[u]-d[v]+n*(b[(u,v)]),min=1) |
1469 | 1471 | |
1470 | 1472 | for v in self: |
1471 | | p.add_constraint(d[v],min=n) |
1472 | | |
| 1473 | p.add_constraint(d[v] <= n) |
1473 | 1474 | |
1474 | 1475 | p.set_objective(Sum([b[(u,v)] for (u,v) in self.edges(labels=None)])) |
1475 | 1476 | |
… |
… |
|
1477 | 1478 | return Integer(round(p.solve(objective_only=True, log=verbose))) |
1478 | 1479 | else: |
1479 | 1480 | p.solve(log=verbose) |
1480 | | |
| 1481 | |
1481 | 1482 | b_sol=p.get_values(b) |
1482 | | |
| 1483 | |
1483 | 1484 | return [(u,v) for (u,v) in self.edges(labels=None) if b_sol[(u,v)]==1] |
1484 | 1485 | |
1485 | 1486 | def feedback_vertex_set(self, value_only=False, solver=None, verbose=0, constraint_generation = True): |
diff --git a/sage/numerical/backends/generic_backend.pyx b/sage/numerical/backends/generic_backend.pyx
a
|
b
|
|
818 | 818 | - CPLEX (``solver="CPLEX"``). See the |
819 | 819 | `CPLEX <http://www.ilog.com/products/cplex/>`_ web site. |
820 | 820 | |
821 | | - GUROBI (``solver="GUROBI"``). See the `GUROBI |
| 821 | - Gurobi (``solver="Gurobi"``). See the `Gurobi |
822 | 822 | <http://www.gurobi.com/>`_ web site. |
823 | 823 | |
824 | 824 | ``solver`` should then be equal to one of ``"GLPK"``, |
825 | | ``"Coin"``, ``"CPLEX"``, or ``"GUROBI"``. |
| 825 | ``"Coin"``, ``"CPLEX"``, or ``"Gurobi"``. |
826 | 826 | |
827 | 827 | - If ``solver=None`` (default), the current default solver's name is |
828 | 828 | returned. |
… |
… |
|
843 | 843 | sage: default_mip_solver("Yeahhhhhhhhhhh") |
844 | 844 | Traceback (most recent call last): |
845 | 845 | ... |
846 | | ValueError: 'solver' should be set to 'GLPK', 'Coin', 'CPLEX', 'GUROBI' or None. |
| 846 | ValueError: 'solver' should be set to 'GLPK', 'Coin', 'CPLEX', 'Gurobi' or None. |
847 | 847 | sage: default_mip_solver(former_solver) |
848 | 848 | """ |
849 | 849 | global default_solver |
… |
… |
|
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 |
… |
… |
|
875 | 875 | except ImportError: |
876 | 876 | raise ValueError("COIN is not available. Please refer to the documentation to install it.") |
877 | 877 | |
878 | | elif solver == "GUROBI": |
| 878 | elif solver == "Gurobi": |
879 | 879 | try: |
880 | 880 | from sage.numerical.backends.gurobi_backend import GurobiBackend |
881 | 881 | default_solver = solver |
… |
… |
|
886 | 886 | default_solver = solver |
887 | 887 | |
888 | 888 | else: |
889 | | raise ValueError("'solver' should be set to 'GLPK', 'Coin', 'CPLEX', 'GUROBI' or None.") |
| 889 | raise ValueError("'solver' should be set to 'GLPK', 'Coin', 'CPLEX', 'Gurobi' or None.") |
890 | 890 | |
891 | 891 | cpdef GenericBackend get_solver(constraint_generation = False, solver = None): |
892 | 892 | """ |
… |
… |
|
905 | 905 | - CPLEX (``solver="CPLEX"``). See the |
906 | 906 | `CPLEX <http://www.ilog.com/products/cplex/>`_ web site. |
907 | 907 | |
908 | | - GUROBI (``solver="GUROBI"``). See the `GUROBI |
| 908 | - Gurobi (``solver="Gurobi"``). See the `Gurobi |
909 | 909 | <http://www.gurobi.com/>`_ web site. |
910 | 910 | |
911 | 911 | ``solver`` should then be equal to one of ``"GLPK"``, ``"Coin"``, |
912 | | ``"CPLEX"``, ``"GUROBI"``, or ``None``. If ``solver=None`` (default), |
| 912 | ``"CPLEX"``, ``"Gurobi"``, or ``None``. If ``solver=None`` (default), |
913 | 913 | the default solver is used (see ``default_mip_solver`` method. |
914 | 914 | |
915 | 915 | - ``constraint_generation`` (boolean) -- whether the solver |
… |
… |
|
949 | 949 | from sage.numerical.backends.cplex_backend import CPLEXBackend |
950 | 950 | return CPLEXBackend() |
951 | 951 | |
952 | | elif solver == "GUROBI": |
| 952 | elif solver == "Gurobi": |
953 | 953 | from sage.numerical.backends.gurobi_backend import GurobiBackend |
954 | 954 | return GurobiBackend() |
955 | 955 | |
956 | 956 | else: |
957 | | raise ValueError("'solver' should be set to 'GLPK', 'Coin', 'CPLEX', 'GUROBI' or None (in which case the default one is used).") |
| 957 | raise ValueError("'solver' should be set to 'GLPK', 'Coin', 'CPLEX', 'Gurobi' or None (in which case the default one is used).") |
958 | 958 | |
959 | 959 | |
diff --git a/sage/numerical/backends/gurobi_backend.pxd b/sage/numerical/backends/gurobi_backend.pxd
a
|
b
|
|
61 | 61 | int GRBsetintparam(GRBenv *env, char * attrname, int value) |
62 | 62 | |
63 | 63 | GRBenv * GRBgetenv (GRBmodel * model ) |
64 | | |
| 64 | |
65 | 65 | |
66 | 66 | int GRBgetconstrs (GRBmodel * model, int * numnzP, int * cbeg, int * cind, double * cval, int start, int len ) |
67 | 67 | |
… |
… |
|
95 | 95 | |
96 | 96 | cdef GRBenv * env |
97 | 97 | cdef GRBmodel ** model |
98 | | |
| 98 | |
99 | 99 | cdef int num_vars |
100 | 100 | |
101 | 101 | |
diff --git a/sage/numerical/backends/gurobi_backend.pyx b/sage/numerical/backends/gurobi_backend.pyx
a
|
b
|
|
1 | 1 | """ |
2 | 2 | Gurobi Backend |
3 | 3 | |
4 | | AUTHORS: |
| 4 | AUTHORS: |
5 | 5 | |
6 | 6 | - Nathann Cohen (2011-10): initial implementation |
| 7 | |
| 8 | TESTS: |
| 9 | |
| 10 | Bug from :trac:`12833`:: |
| 11 | |
| 12 | sage: g = DiGraph('IESK@XgAbCgH??KG??') |
| 13 | sage: g.feedback_edge_set(value_only = True, constraint_generation = False) |
| 14 | 7 |
| 15 | sage: g.feedback_edge_set(value_only = True, constraint_generation = False, solver = "Gurobi") # optional - Gurobi |
| 16 | 7 |
| 17 | |
| 18 | Methods |
| 19 | ------- |
7 | 20 | """ |
8 | 21 | |
9 | 22 | ############################################################################## |
… |
… |
|
16 | 29 | from sage.numerical.mip import MIPSolverException |
17 | 30 | |
18 | 31 | cdef class GurobiBackend(GenericBackend): |
19 | | def __cinit__(self, maximization = True): |
| 32 | def __init__(self, maximization = True): |
20 | 33 | """ |
21 | 34 | Constructor |
22 | 35 | |
23 | 36 | EXAMPLE:: |
24 | 37 | |
25 | | sage: p = MixedIntegerLinearProgram(solver="Gurobi") # optional - GUROBI |
| 38 | sage: p = MixedIntegerLinearProgram(solver="Gurobi") # optional - Gurobi |
26 | 39 | """ |
27 | | cdef int error |
| 40 | cdef int error |
28 | 41 | |
29 | 42 | cdef GRBenv ** env |
30 | 43 | env = <GRBenv **> sage_malloc(sizeof(GRBenv *)) |
31 | | |
32 | 44 | |
33 | 45 | error = GRBloadenv(env, NULL) |
34 | 46 | |
35 | | if error or (env[0] == NULL): |
| 47 | check(self.env, error) |
| 48 | |
| 49 | if env[0] == NULL: |
36 | 50 | raise Exception("Could not initialize Gurobi environment") |
37 | 51 | |
38 | 52 | self.model = <GRBmodel **> sage_malloc(sizeof(GRBmodel *)) |
| 53 | |
39 | 54 | error = GRBnewmodel(env[0], self.model, NULL, 0, NULL, NULL, NULL, NULL, NULL) |
40 | 55 | |
41 | | |
42 | 56 | self.env = GRBgetenv (self.model[0]) |
43 | 57 | |
44 | | |
45 | 58 | if error: |
46 | 59 | raise Exception("Could not initialize Gurobi model") |
47 | 60 | |
… |
… |
|
82 | 95 | |
83 | 96 | - ``name`` - an optional name for the newly added variable (default: ``None``). |
84 | 97 | |
85 | | OUTPUT: The index of the newly created variable |
| 98 | OUTPUT: The index of the newly created variable |
86 | 99 | |
87 | 100 | EXAMPLE:: |
88 | 101 | |
89 | | sage: from sage.numerical.backends.generic_backend import get_solver # optional - GUROBI |
90 | | sage: p = get_solver(solver = "Gurobi") # optional - GUROBI |
91 | | sage: p.ncols() # optional - GUROBI |
| 102 | sage: from sage.numerical.backends.generic_backend import get_solver # optional - Gurobi |
| 103 | sage: p = get_solver(solver = "Gurobi") # optional - Gurobi |
| 104 | sage: p.ncols() # optional - Gurobi |
92 | 105 | 0 |
93 | | sage: p.add_variable() # optional - GUROBI |
| 106 | sage: p.add_variable() # optional - Gurobi |
94 | 107 | 0 |
95 | | sage: p.ncols() # optional - GUROBI |
| 108 | sage: p.ncols() # optional - Gurobi |
96 | 109 | 1 |
97 | | sage: p.add_variable(binary=True) # optional - GUROBI |
| 110 | sage: p.add_variable(binary=True) # optional - Gurobi |
98 | 111 | 1 |
99 | | sage: p.add_variable(lower_bound=-2.0, integer=True) # optional - GUROBI |
| 112 | sage: p.add_variable(lower_bound=-2.0, integer=True) # optional - Gurobi |
100 | 113 | 2 |
101 | | sage: p.add_variable(continuous=True, integer=True) # optional - GUROBI |
| 114 | sage: p.add_variable(continuous=True, integer=True) # optional - Gurobi |
102 | 115 | Traceback (most recent call last): |
103 | 116 | ... |
104 | | ValueError: ... |
105 | | sage: p.add_variable(name='x',obj=1.0) # optional - GUROBI |
| 117 | ValueError: ... |
| 118 | sage: p.add_variable(name='x',obj=1.0) # optional - Gurobi |
106 | 119 | 3 |
107 | | sage: p.col_name(3) # optional - GUROBI |
| 120 | sage: p.col_name(3) # optional - Gurobi |
108 | 121 | 'x' |
109 | | sage: p.objective_coefficient(3) # optional - GUROBI |
| 122 | sage: p.objective_coefficient(3) # optional - Gurobi |
110 | 123 | 1.0 |
111 | 124 | """ |
112 | 125 | # Checking the input |
113 | 126 | cdef char vtype = int(bool(binary)) + int(bool(continuous)) + int(bool(integer)) |
114 | 127 | if vtype == 0: |
115 | | continuous = True |
| 128 | continuous = True |
116 | 129 | elif vtype != 1: |
117 | 130 | raise ValueError("Exactly one parameter of 'binary', 'integer' and 'continuous' must be 'True'.") |
118 | 131 | |
… |
… |
|
125 | 138 | elif integer: |
126 | 139 | vtype = GRB_INTEGER |
127 | 140 | |
128 | | cdef char * c_name = "" |
| 141 | cdef char * c_name = "" |
129 | 142 | |
130 | 143 | if name is None: |
131 | 144 | name = "x_"+str(self.ncols()) |
… |
… |
|
136 | 149 | upper_bound = GRB_INFINITY |
137 | 150 | if lower_bound is None: |
138 | 151 | lower_bound = -GRB_INFINITY |
139 | | |
140 | | |
| 152 | |
| 153 | |
141 | 154 | error = GRBaddvar(self.model[0], 0, NULL, NULL, obj, <double> lower_bound, <double> upper_bound, vtype, c_name) |
142 | 155 | |
143 | 156 | check(self.env,error) |
… |
… |
|
176 | 189 | |
177 | 190 | EXAMPLE:: |
178 | 191 | |
179 | | sage: from sage.numerical.backends.generic_backend import get_solver # optional - GUROBI |
180 | | sage: p = get_solver(solver = "Gurobi") # optional - GUROBI |
181 | | sage: p.ncols() # optional - GUROBI |
| 192 | sage: from sage.numerical.backends.generic_backend import get_solver # optional - Gurobi |
| 193 | sage: p = get_solver(solver = "Gurobi") # optional - Gurobi |
| 194 | sage: p.ncols() # optional - Gurobi |
182 | 195 | 0 |
183 | | sage: p.add_variables(5) # optional - GUROBI |
| 196 | sage: p.add_variables(5) # optional - Gurobi |
184 | 197 | 4 |
185 | | sage: p.ncols() # optional - GUROBI |
| 198 | sage: p.ncols() # optional - Gurobi |
186 | 199 | 5 |
187 | | sage: p.add_variables(2, lower_bound=-2.0, integer=True, names=['a','b']) # optional - GUROBI |
| 200 | sage: p.add_variables(2, lower_bound=-2.0, integer=True, names=['a','b']) # optional - Gurobi |
188 | 201 | 6 |
189 | 202 | """ |
190 | 203 | cdef int i |
191 | 204 | cdef int value |
192 | 205 | for i in range(number): |
193 | | value = self.add_variable(lower_bound = lower_bound, |
194 | | upper_bound = upper_bound, |
| 206 | value = self.add_variable(lower_bound = lower_bound, |
| 207 | upper_bound = upper_bound, |
195 | 208 | binary = binary, |
196 | 209 | continuous = continuous, |
197 | 210 | integer = integer, |
… |
… |
|
233 | 246 | # p_types[0] = GRB_BINARY |
234 | 247 | # else: |
235 | 248 | # p_types[0] = GRB_INTEGER |
236 | | # |
| 249 | # |
237 | 250 | # for i in range(2, number): |
238 | 251 | # p_types[i] = p_types[i-1] |
239 | 252 | # |
… |
… |
|
244 | 257 | # for i, name in enumerate(names): |
245 | 258 | # p_names[i+1] = p_names[i] + NAME_MAX_LEN |
246 | 259 | # p_names[i][0] = str(name) |
247 | | # |
| 260 | # |
248 | 261 | # error = GRBaddvars (self.model[0], number, 0, NULL, NULL, NULL, p_obj, p_lb, p_ub, p_types, p_names) |
249 | 262 | # |
250 | 263 | # # Freeing the memory |
… |
… |
|
279 | 292 | |
280 | 293 | EXAMPLE:: |
281 | 294 | |
282 | | sage: from sage.numerical.backends.generic_backend import get_solver # optional - GUROBI |
283 | | sage: p = get_solver(solver = "Gurobi") # optional - GUROBI |
284 | | sage: p.ncols() # optional - GUROBI |
| 295 | sage: from sage.numerical.backends.generic_backend import get_solver # optional - Gurobi |
| 296 | sage: p = get_solver(solver = "Gurobi") # optional - Gurobi |
| 297 | sage: p.ncols() # optional - Gurobi |
285 | 298 | 0 |
286 | | sage: p.add_variable() # optional - GUROBI |
| 299 | sage: p.add_variable() # optional - Gurobi |
287 | 300 | 0 |
288 | | sage: p.set_variable_type(0,1) # optional - GUROBI |
289 | | sage: p.is_variable_integer(0) # optional - GUROBI |
| 301 | sage: p.set_variable_type(0,1) # optional - Gurobi |
| 302 | sage: p.is_variable_integer(0) # optional - Gurobi |
290 | 303 | True |
291 | 304 | """ |
292 | 305 | cdef int error |
… |
… |
|
314 | 327 | |
315 | 328 | EXAMPLE:: |
316 | 329 | |
317 | | sage: from sage.numerical.backends.generic_backend import get_solver # optional - GUROBI |
318 | | sage: p = get_solver(solver = "Gurobi") # optional - GUROBI |
319 | | sage: p.is_maximization() # optional - GUROBI |
| 330 | sage: from sage.numerical.backends.generic_backend import get_solver # optional - Gurobi |
| 331 | sage: p = get_solver(solver = "Gurobi") # optional - Gurobi |
| 332 | sage: p.is_maximization() # optional - Gurobi |
320 | 333 | True |
321 | | sage: p.set_sense(-1) # optional - GUROBI |
322 | | sage: p.is_maximization() # optional - GUROBI |
| 334 | sage: p.set_sense(-1) # optional - Gurobi |
| 335 | sage: p.is_maximization() # optional - Gurobi |
323 | 336 | False |
324 | 337 | """ |
325 | 338 | cdef int error |
… |
… |
|
345 | 358 | |
346 | 359 | EXAMPLE:: |
347 | 360 | |
348 | | sage: from sage.numerical.backends.generic_backend import get_solver # optional - GUROBI |
349 | | sage: p = get_solver(solver = "Gurobi") # optional - GUROBI |
350 | | sage: p.add_variable() # optional - GUROBI |
| 361 | sage: from sage.numerical.backends.generic_backend import get_solver # optional - Gurobi |
| 362 | sage: p = get_solver(solver = "Gurobi") # optional - Gurobi |
| 363 | sage: p.add_variable() # optional - Gurobi |
351 | 364 | 0 |
352 | | sage: p.objective_coefficient(0) == 0 # optional - GUROBI |
| 365 | sage: p.objective_coefficient(0) == 0 # optional - Gurobi |
353 | 366 | True |
354 | | sage: p.objective_coefficient(0,2) # optional - GUROBI |
355 | | sage: p.objective_coefficient(0) # optional - GUROBI |
| 367 | sage: p.objective_coefficient(0,2) # optional - Gurobi |
| 368 | sage: p.objective_coefficient(0) # optional - Gurobi |
356 | 369 | 2.0 |
357 | 370 | """ |
358 | 371 | cdef int error |
… |
… |
|
378 | 391 | |
379 | 392 | EXAMPLE:: |
380 | 393 | |
381 | | sage: from sage.numerical.backends.generic_backend import get_solver # optional - GUROBI |
382 | | sage: p = get_solver(solver = "Gurobi") # optional - GUROBI |
383 | | sage: p.problem_name("There once was a french fry") # optional - GUROBI |
384 | | sage: print p.problem_name() # optional - GUROBI |
| 394 | sage: from sage.numerical.backends.generic_backend import get_solver # optional - Gurobi |
| 395 | sage: p = get_solver(solver = "Gurobi") # optional - Gurobi |
| 396 | sage: p.problem_name("There once was a french fry") # optional - Gurobi |
| 397 | sage: print p.problem_name() # optional - Gurobi |
385 | 398 | There once was a french fry |
386 | 399 | |
387 | 400 | TESTS:: |
388 | 401 | |
389 | | sage: from sage.numerical.backends.generic_backend import get_solver # optional - GUROBI |
390 | | sage: p = get_solver(solver = "Gurobi") # optional - GUROBI |
391 | | sage: print p.problem_name() # optional - GUROBI |
| 402 | sage: from sage.numerical.backends.generic_backend import get_solver # optional - Gurobi |
| 403 | sage: p = get_solver(solver = "Gurobi") # optional - Gurobi |
| 404 | sage: print p.problem_name() # optional - Gurobi |
392 | 405 | """ |
393 | 406 | cdef int error |
394 | 407 | cdef char * pp_name[1] |
… |
… |
|
397 | 410 | error = GRBsetstrattr(self.model[0], "ModelName", name) |
398 | 411 | check(self.env, error) |
399 | 412 | check(self.env,GRBupdatemodel(self.model[0])) |
400 | | |
| 413 | |
401 | 414 | else: |
402 | 415 | check(self.env,GRBgetstrattr(self.model[0], "ModelName", <char **> pp_name)) |
403 | 416 | if pp_name[0] == NULL: |
… |
… |
|
405 | 418 | else: |
406 | 419 | value = str(pp_name[0]) |
407 | 420 | |
408 | | return value |
| 421 | return value |
409 | 422 | |
410 | 423 | cpdef set_objective(self, list coeff): |
411 | 424 | """ |
… |
… |
|
418 | 431 | |
419 | 432 | EXAMPLE:: |
420 | 433 | |
421 | | sage: from sage.numerical.backends.generic_backend import get_solver # optional - GUROBI |
422 | | sage: p = get_solver(solver = "Gurobi") # optional - GUROBI |
423 | | sage: p.add_variables(5) # optional - GUROBI |
| 434 | sage: from sage.numerical.backends.generic_backend import get_solver # optional - Gurobi |
| 435 | sage: p = get_solver(solver = "Gurobi") # optional - Gurobi |
| 436 | sage: p.add_variables(5) # optional - Gurobi |
424 | 437 | 4 |
425 | | sage: p.set_objective([1, 1, 2, 1, 3]) # optional - GUROBI |
426 | | sage: map(lambda x :p.objective_coefficient(x), range(5)) # optional - GUROBI |
| 438 | sage: p.set_objective([1, 1, 2, 1, 3]) # optional - Gurobi |
| 439 | sage: map(lambda x :p.objective_coefficient(x), range(5)) # optional - Gurobi |
427 | 440 | [1.0, 1.0, 2.0, 1.0, 3.0] |
428 | 441 | """ |
429 | 442 | cdef int i = 0 |
430 | 443 | cdef double value |
431 | 444 | cdef int error |
432 | | |
| 445 | |
433 | 446 | for value in coeff: |
434 | 447 | error = GRBsetdblattrelement (self.model[0], "Obj", i, value) |
435 | 448 | check(self.env,error) |
… |
… |
|
447 | 460 | |
448 | 461 | EXAMPLE:: |
449 | 462 | |
450 | | sage: from sage.numerical.backends.generic_backend import get_solver # optional - GUROBI |
451 | | sage: p = get_solver(solver = "Gurobi") # optional - GUROBI |
452 | | sage: p.set_verbosity(2) # optional - GUROBI |
| 463 | sage: from sage.numerical.backends.generic_backend import get_solver # optional - Gurobi |
| 464 | sage: p = get_solver(solver = "Gurobi") # optional - Gurobi |
| 465 | sage: p.set_verbosity(2) # optional - Gurobi |
453 | 466 | |
454 | 467 | """ |
455 | 468 | cdef int error |
… |
… |
|
475 | 488 | - ``upper_bound`` - an upper bound, either a real value or ``None`` |
476 | 489 | |
477 | 490 | - ``name`` - an optional name for this row (default: ``None``) |
478 | | |
| 491 | |
479 | 492 | EXAMPLE:: |
480 | 493 | |
481 | | sage: from sage.numerical.backends.generic_backend import get_solver # optional - GUROBI |
482 | | sage: p = get_solver(solver = "Gurobi") # optional - GUROBI |
483 | | sage: p.add_variables(5) # optional - GUROBI |
| 494 | sage: from sage.numerical.backends.generic_backend import get_solver # optional - Gurobi |
| 495 | sage: p = get_solver(solver = "Gurobi") # optional - Gurobi |
| 496 | sage: p.add_variables(5) # optional - Gurobi |
484 | 497 | 4 |
485 | | sage: p.add_linear_constraint( zip(range(5), range(5)), 2.0, 2.0) # optional - GUROBI |
486 | | sage: p.row(0) # optional - GUROBI |
| 498 | sage: p.add_linear_constraint( zip(range(5), range(5)), 2.0, 2.0) # optional - Gurobi |
| 499 | sage: p.row(0) # optional - Gurobi |
487 | 500 | ([0, 1, 2, 3, 4], [0.0, 1.0, 2.0, 3.0, 4.0]) |
488 | | sage: p.row_bounds(0) # optional - GUROBI |
| 501 | sage: p.row_bounds(0) # optional - Gurobi |
489 | 502 | (2.0, 2.0) |
490 | | sage: p.add_linear_constraint( zip(range(5), range(5)), 1.0, 1.0, name='foo') # optional - GUROBI |
491 | | sage: p.row_name(1) # optional - GUROBI |
| 503 | sage: p.add_linear_constraint( zip(range(5), range(5)), 1.0, 1.0, name='foo') # optional - Gurobi |
| 504 | sage: p.row_name(1) # optional - Gurobi |
492 | 505 | 'foo' |
493 | 506 | """ |
494 | 507 | |
… |
… |
|
500 | 513 | |
501 | 514 | row_i = <int *> sage_malloc((len(coefficients)) * sizeof(int)) |
502 | 515 | row_values = <double *> sage_malloc((len(coefficients)) * sizeof(double)) |
503 | | |
| 516 | |
504 | 517 | |
505 | 518 | for i,(c,v) in enumerate(coefficients): |
506 | 519 | row_i[i] = c |
… |
… |
|
523 | 536 | error = GRBaddrangeconstr(self.model[0], len(coefficients), row_i, row_values, <double> lower_bound, <double> upper_bound, name) |
524 | 537 | |
525 | 538 | check(self.env,error) |
526 | | |
| 539 | |
527 | 540 | error = GRBupdatemodel(self.model[0]) |
528 | 541 | |
529 | 542 | check(self.env,error) |
… |
… |
|
548 | 561 | |
549 | 562 | EXAMPLE:: |
550 | 563 | |
551 | | sage: from sage.numerical.backends.generic_backend import get_solver # optional - GUROBI |
552 | | sage: p = get_solver(solver = "Gurobi") # optional - GUROBI |
553 | | sage: p.add_variables(5) # optional - GUROBI |
| 564 | sage: from sage.numerical.backends.generic_backend import get_solver # optional - Gurobi |
| 565 | sage: p = get_solver(solver = "Gurobi") # optional - Gurobi |
| 566 | sage: p.add_variables(5) # optional - Gurobi |
554 | 567 | 4 |
555 | | sage: p.add_linear_constraint(zip(range(5), range(5)), 2, 2) # optional - GUROBI |
556 | | sage: p.row(0) # optional - GUROBI |
| 568 | sage: p.add_linear_constraint(zip(range(5), range(5)), 2, 2) # optional - Gurobi |
| 569 | sage: p.row(0) # optional - Gurobi |
557 | 570 | ([0, 1, 2, 3, 4], [0.0, 1.0, 2.0, 3.0, 4.0]) |
558 | | sage: p.row_bounds(0) # optional - GUROBI |
| 571 | sage: p.row_bounds(0) # optional - Gurobi |
559 | 572 | (2.0, 2.0) |
560 | 573 | """ |
561 | 574 | cdef int error |
… |
… |
|
564 | 577 | cdef int length[1] |
565 | 578 | error = GRBgetconstrs(self.model[0], length, NULL, NULL, NULL, index, 1 ) |
566 | 579 | check(self.env,error) |
567 | | |
| 580 | |
568 | 581 | cdef int * p_indices = <int *> sage_malloc(length[0] * sizeof(int)) |
569 | 582 | cdef double * p_values = <double *> sage_malloc(length[0] * sizeof(double)) |
570 | | |
| 583 | |
571 | 584 | error = GRBgetconstrs(self.model[0], length, <int *> fake, p_indices, p_values, index, 1 ) |
572 | 585 | check(self.env,error) |
573 | 586 | |
… |
… |
|
581 | 594 | |
582 | 595 | sage_free(p_indices) |
583 | 596 | sage_free(p_values) |
584 | | |
| 597 | |
585 | 598 | return indices, values |
586 | 599 | |
587 | 600 | |
… |
… |
|
601 | 614 | |
602 | 615 | EXAMPLE:: |
603 | 616 | |
604 | | sage: from sage.numerical.backends.generic_backend import get_solver # optional - GUROBI |
605 | | sage: p = get_solver(solver = "Gurobi") # optional - GUROBI |
606 | | sage: p.add_variables(5) # optional - GUROBI |
| 617 | sage: from sage.numerical.backends.generic_backend import get_solver # optional - Gurobi |
| 618 | sage: p = get_solver(solver = "Gurobi") # optional - Gurobi |
| 619 | sage: p.add_variables(5) # optional - Gurobi |
607 | 620 | 4 |
608 | | sage: p.add_linear_constraint(zip(range(5), range(5)), 2, 2) # optional - GUROBI |
609 | | sage: p.row(0) # optional - GUROBI |
| 621 | sage: p.add_linear_constraint(zip(range(5), range(5)), 2, 2) # optional - Gurobi |
| 622 | sage: p.row(0) # optional - Gurobi |
610 | 623 | ([0, 1, 2, 3, 4], [0.0, 1.0, 2.0, 3.0, 4.0]) |
611 | | sage: p.row_bounds(0) # optional - GUROBI |
| 624 | sage: p.row_bounds(0) # optional - Gurobi |
612 | 625 | (2.0, 2.0) |
613 | 626 | """ |
614 | 627 | cdef double d[1] |
… |
… |
|
644 | 657 | |
645 | 658 | EXAMPLE:: |
646 | 659 | |
647 | | sage: from sage.numerical.backends.generic_backend import get_solver # optional - GUROBI |
648 | | sage: p = get_solver(solver = "Gurobi") # optional - GUROBI |
649 | | sage: p.add_variable() # optional - GUROBI |
| 660 | sage: from sage.numerical.backends.generic_backend import get_solver # optional - Gurobi |
| 661 | sage: p = get_solver(solver = "Gurobi") # optional - Gurobi |
| 662 | sage: p.add_variable() # optional - Gurobi |
650 | 663 | 0 |
651 | | sage: p.col_bounds(0) # optional - GUROBI |
| 664 | sage: p.col_bounds(0) # optional - Gurobi |
652 | 665 | (0.0, None) |
653 | | sage: p.variable_upper_bound(0, 5) # optional - GUROBI |
654 | | sage: p.col_bounds(0) # optional - GUROBI |
| 666 | sage: p.variable_upper_bound(0, 5) # optional - Gurobi |
| 667 | sage: p.col_bounds(0) # optional - Gurobi |
655 | 668 | (0.0, 5.0) |
656 | 669 | """ |
657 | 670 | |
… |
… |
|
660 | 673 | error = GRBgetdblattrelement(self.model[0], "LB", index, <double *> lb) |
661 | 674 | check(self.env, error) |
662 | 675 | |
663 | | |
664 | 676 | error = GRBgetdblattrelement(self.model[0], "UB", index, <double *> ub) |
665 | 677 | check(self.env, error) |
666 | 678 | |
667 | 679 | return (None if lb[0] <= -2147483647 else lb[0], |
668 | | None if (<int> ub[0]) >= 2147483647 else ub[0]) |
| 680 | None if ub[0] >= 2147483647 else ub[0]) |
669 | 681 | |
670 | 682 | cpdef int solve(self) except -1: |
671 | 683 | """ |
… |
… |
|
679 | 691 | |
680 | 692 | EXAMPLE:: |
681 | 693 | |
682 | | sage: from sage.numerical.backends.generic_backend import get_solver # optional - GUROBI |
683 | | sage: p = get_solver(solver = "Gurobi") # optional - GUROBI |
684 | | sage: p.add_variables(5) # optional - GUROBI |
| 694 | sage: from sage.numerical.backends.generic_backend import get_solver # optional - Gurobi |
| 695 | sage: p = get_solver(solver = "Gurobi") # optional - Gurobi |
| 696 | sage: p.add_variables(5) # optional - Gurobi |
685 | 697 | 4 |
686 | | sage: p.add_linear_constraint([(0,1), (1, 1)], 1.2, 1.7) # optional - GUROBI |
687 | | sage: p.set_variable_type(0, 1) # optional - GUROBI |
688 | | sage: p.set_variable_type(1, 1) # optional - GUROBI |
689 | | sage: p.solve() # optional - GUROBI |
| 698 | sage: p.add_linear_constraint([(0,1), (1, 1)], 1.2, 1.7) # optional - Gurobi |
| 699 | sage: p.set_variable_type(0, 1) # optional - Gurobi |
| 700 | sage: p.set_variable_type(1, 1) # optional - Gurobi |
| 701 | sage: p.solve() # optional - Gurobi |
690 | 702 | Traceback (most recent call last): |
691 | 703 | ... |
692 | 704 | MIPSolverException: 'Gurobi: The problem is infeasible' |
693 | 705 | """ |
694 | 706 | cdef int error |
695 | 707 | global mip_status |
696 | | |
| 708 | |
697 | 709 | check(self.env, GRBoptimize(self.model[0])) |
698 | 710 | |
699 | 711 | cdef int status[1] |
… |
… |
|
714 | 726 | |
715 | 727 | EXAMPLE:: |
716 | 728 | |
717 | | sage: from sage.numerical.backends.generic_backend import get_solver # optional - GUROBI |
718 | | sage: p = get_solver(solver = "Gurobi") # optional - GUROBI |
719 | | sage: p.add_variables(2) # optional - GUROBI |
| 729 | sage: from sage.numerical.backends.generic_backend import get_solver # optional - Gurobi |
| 730 | sage: p = get_solver(solver = "Gurobi") # optional - Gurobi |
| 731 | sage: p.add_variables(2) # optional - Gurobi |
720 | 732 | 1 |
721 | | sage: p.add_linear_constraint([[0, 1], [1, 2]], None, 3) # optional - GUROBI |
722 | | sage: p.set_objective([2, 5]) # optional - GUROBI |
723 | | sage: p.solve() # optional - GUROBI |
| 733 | sage: p.add_linear_constraint([[0, 1], [1, 2]], None, 3) # optional - Gurobi |
| 734 | sage: p.set_objective([2, 5]) # optional - Gurobi |
| 735 | sage: p.solve() # optional - Gurobi |
724 | 736 | 0 |
725 | | sage: p.get_objective_value() # optional - GUROBI |
| 737 | sage: p.get_objective_value() # optional - Gurobi |
726 | 738 | 7.5 |
727 | | sage: p.get_variable_value(0) # optional - GUROBI |
| 739 | sage: p.get_variable_value(0) # optional - Gurobi |
728 | 740 | 0.0 |
729 | | sage: p.get_variable_value(1) # optional - GUROBI |
| 741 | sage: p.get_variable_value(1) # optional - Gurobi |
730 | 742 | 1.5 |
731 | 743 | """ |
732 | 744 | cdef double p_value[1] |
733 | | |
| 745 | |
734 | 746 | check(self.env,GRBgetdblattr(self.model[0], "ObjVal", <double* >p_value)) |
735 | 747 | |
736 | 748 | return p_value[0] |
… |
… |
|
745 | 757 | |
746 | 758 | EXAMPLE:: |
747 | 759 | |
748 | | sage: from sage.numerical.backends.generic_backend import get_solver # optional - GUROBI |
749 | | sage: p = get_solver(solver = "Gurobi") # optional - GUROBI |
750 | | sage: p.add_variables(2) # optional - GUROBI |
| 760 | sage: from sage.numerical.backends.generic_backend import get_solver # optional - Gurobi |
| 761 | sage: p = get_solver(solver = "Gurobi") # optional - Gurobi |
| 762 | sage: p.add_variables(2) # optional - Gurobi |
751 | 763 | 1 |
752 | | sage: p.add_linear_constraint([[0, 1], [1, 2]], None, 3) # optional - GUROBI |
753 | | sage: p.set_objective([2, 5]) # optional - GUROBI |
754 | | sage: p.solve() # optional - GUROBI |
| 764 | sage: p.add_linear_constraint([[0, 1], [1, 2]], None, 3) # optional - Gurobi |
| 765 | sage: p.set_objective([2, 5]) # optional - Gurobi |
| 766 | sage: p.solve() # optional - Gurobi |
755 | 767 | 0 |
756 | | sage: p.get_objective_value() # optional - GUROBI |
| 768 | sage: p.get_objective_value() # optional - Gurobi |
757 | 769 | 7.5 |
758 | | sage: p.get_variable_value(0) # optional - GUROBI |
| 770 | sage: p.get_variable_value(0) # optional - Gurobi |
759 | 771 | 0.0 |
760 | | sage: p.get_variable_value(1) # optional - GUROBI |
| 772 | sage: p.get_variable_value(1) # optional - Gurobi |
761 | 773 | 1.5 |
762 | 774 | """ |
763 | 775 | |
… |
… |
|
771 | 783 | |
772 | 784 | EXAMPLE:: |
773 | 785 | |
774 | | sage: from sage.numerical.backends.generic_backend import get_solver # optional - GUROBI |
775 | | sage: p = get_solver(solver = "Gurobi") # optional - GUROBI |
776 | | sage: p.ncols() # optional - GUROBI |
| 786 | sage: from sage.numerical.backends.generic_backend import get_solver # optional - Gurobi |
| 787 | sage: p = get_solver(solver = "Gurobi") # optional - Gurobi |
| 788 | sage: p.ncols() # optional - Gurobi |
777 | 789 | 0 |
778 | | sage: p.add_variables(2) # optional - GUROBI |
| 790 | sage: p.add_variables(2) # optional - Gurobi |
779 | 791 | 1 |
780 | | sage: p.ncols() # optional - GUROBI |
| 792 | sage: p.ncols() # optional - Gurobi |
781 | 793 | 2 |
782 | 794 | """ |
783 | 795 | cdef int i[1] |
… |
… |
|
790 | 802 | |
791 | 803 | EXAMPLE:: |
792 | 804 | |
793 | | sage: from sage.numerical.backends.generic_backend import get_solver # optional - GUROBI |
794 | | sage: p = get_solver(solver = "Gurobi") # optional - GUROBI |
795 | | sage: p.nrows() # optional - GUROBI |
| 805 | sage: from sage.numerical.backends.generic_backend import get_solver # optional - Gurobi |
| 806 | sage: p = get_solver(solver = "Gurobi") # optional - Gurobi |
| 807 | sage: p.nrows() # optional - Gurobi |
796 | 808 | 0 |
797 | | sage: p.add_linear_constraint([], 2, None) # optional - GUROBI |
798 | | sage: p.add_linear_constraint([], 2, None) # optional - GUROBI |
799 | | sage: p.nrows() # optional - GUROBI |
| 809 | sage: p.add_linear_constraint([], 2, None) # optional - Gurobi |
| 810 | sage: p.add_linear_constraint([], 2, None) # optional - Gurobi |
| 811 | sage: p.nrows() # optional - Gurobi |
800 | 812 | 2 |
801 | 813 | """ |
802 | 814 | cdef int i[1] |
… |
… |
|
813 | 825 | |
814 | 826 | EXAMPLE:: |
815 | 827 | |
816 | | sage: from sage.numerical.backends.generic_backend import get_solver # optional - GUROBI |
817 | | sage: p = get_solver(solver = "Gurobi") # optional - GUROBI |
818 | | sage: p.add_variable(name='I am a variable') # optional - GUROBI |
| 828 | sage: from sage.numerical.backends.generic_backend import get_solver # optional - Gurobi |
| 829 | sage: p = get_solver(solver = "Gurobi") # optional - Gurobi |
| 830 | sage: p.add_variable(name='I am a variable') # optional - Gurobi |
819 | 831 | 0 |
820 | | sage: p.col_name(0) # optional - GUROBI |
| 832 | sage: p.col_name(0) # optional - Gurobi |
821 | 833 | 'I am a variable' |
822 | 834 | """ |
823 | 835 | cdef char * name[1] |
… |
… |
|
838 | 850 | |
839 | 851 | EXAMPLE:: |
840 | 852 | |
841 | | sage: from sage.numerical.backends.generic_backend import get_solver # optional - GUROBI |
842 | | sage: p = get_solver(solver = "Gurobi") # optional - GUROBI |
843 | | sage: p.add_linear_constraint([], 2, None, name='Empty constraint 1') # optional - GUROBI |
844 | | sage: p.row_name(0) # optional - GUROBI |
| 853 | sage: from sage.numerical.backends.generic_backend import get_solver # optional - Gurobi |
| 854 | sage: p = get_solver(solver = "Gurobi") # optional - Gurobi |
| 855 | sage: p.add_linear_constraint([], 2, None, name='Empty constraint 1') # optional - Gurobi |
| 856 | sage: p.row_name(0) # optional - Gurobi |
845 | 857 | 'Empty constraint 1' |
846 | 858 | """ |
847 | 859 | cdef char * name[1] |
… |
… |
|
862 | 874 | |
863 | 875 | EXAMPLE:: |
864 | 876 | |
865 | | sage: from sage.numerical.backends.generic_backend import get_solver # optional - GUROBI |
866 | | sage: p = get_solver(solver = "Gurobi") # optional - GUROBI |
867 | | sage: p.ncols() # optional - GUROBI |
| 877 | sage: from sage.numerical.backends.generic_backend import get_solver # optional - Gurobi |
| 878 | sage: p = get_solver(solver = "Gurobi") # optional - Gurobi |
| 879 | sage: p.ncols() # optional - Gurobi |
868 | 880 | 0 |
869 | | sage: p.add_variable() # optional - GUROBI |
| 881 | sage: p.add_variable() # optional - Gurobi |
870 | 882 | 0 |
871 | | sage: p.set_variable_type(0,0) # optional - GUROBI |
872 | | sage: p.is_variable_binary(0) # optional - GUROBI |
| 883 | sage: p.set_variable_type(0,0) # optional - Gurobi |
| 884 | sage: p.is_variable_binary(0) # optional - Gurobi |
873 | 885 | True |
874 | 886 | """ |
875 | 887 | cdef char vtype[1] |
… |
… |
|
887 | 899 | |
888 | 900 | EXAMPLE:: |
889 | 901 | |
890 | | sage: from sage.numerical.backends.generic_backend import get_solver # optional - GUROBI |
891 | | sage: p = get_solver(solver = "Gurobi") # optional - GUROBI |
892 | | sage: p.ncols() # optional - GUROBI |
| 902 | sage: from sage.numerical.backends.generic_backend import get_solver # optional - Gurobi |
| 903 | sage: p = get_solver(solver = "Gurobi") # optional - Gurobi |
| 904 | sage: p.ncols() # optional - Gurobi |
893 | 905 | 0 |
894 | | sage: p.add_variable() # optional - GUROBI |
| 906 | sage: p.add_variable() # optional - Gurobi |
895 | 907 | 0 |
896 | | sage: p.set_variable_type(0,1) # optional - GUROBI |
897 | | sage: p.is_variable_integer(0) # optional - GUROBI |
| 908 | sage: p.set_variable_type(0,1) # optional - Gurobi |
| 909 | sage: p.is_variable_integer(0) # optional - Gurobi |
898 | 910 | True |
899 | 911 | """ |
900 | 912 | cdef char vtype[1] |
… |
… |
|
911 | 923 | |
912 | 924 | EXAMPLE:: |
913 | 925 | |
914 | | sage: from sage.numerical.backends.generic_backend import get_solver # optional - GUROBI |
915 | | sage: p = get_solver(solver = "Gurobi") # optional - GUROBI |
916 | | sage: p.ncols() # optional - GUROBI |
| 926 | sage: from sage.numerical.backends.generic_backend import get_solver # optional - Gurobi |
| 927 | sage: p = get_solver(solver = "Gurobi") # optional - Gurobi |
| 928 | sage: p.ncols() # optional - Gurobi |
917 | 929 | 0 |
918 | | sage: p.add_variable() # optional - GUROBI |
| 930 | sage: p.add_variable() # optional - Gurobi |
919 | 931 | 0 |
920 | | sage: p.is_variable_continuous(0) # optional - GUROBI |
| 932 | sage: p.is_variable_continuous(0) # optional - Gurobi |
921 | 933 | True |
922 | | sage: p.set_variable_type(0,1) # optional - GUROBI |
923 | | sage: p.is_variable_continuous(0) # optional - GUROBI |
| 934 | sage: p.set_variable_type(0,1) # optional - Gurobi |
| 935 | sage: p.is_variable_continuous(0) # optional - Gurobi |
924 | 936 | False |
925 | 937 | |
926 | 938 | """ |
… |
… |
|
934 | 946 | |
935 | 947 | EXAMPLE:: |
936 | 948 | |
937 | | sage: from sage.numerical.backends.generic_backend import get_solver # optional - GUROBI |
938 | | sage: p = get_solver(solver = "Gurobi") # optional - GUROBI |
939 | | sage: p.is_maximization() # optional - GUROBI |
| 949 | sage: from sage.numerical.backends.generic_backend import get_solver # optional - Gurobi |
| 950 | sage: p = get_solver(solver = "Gurobi") # optional - Gurobi |
| 951 | sage: p.is_maximization() # optional - Gurobi |
940 | 952 | True |
941 | | sage: p.set_sense(-1) # optional - GUROBI |
942 | | sage: p.is_maximization() # optional - GUROBI |
| 953 | sage: p.set_sense(-1) # optional - Gurobi |
| 954 | sage: p.is_maximization() # optional - Gurobi |
943 | 955 | False |
944 | 956 | """ |
945 | 957 | cdef int sense[1] |
… |
… |
|
960 | 972 | |
961 | 973 | EXAMPLE:: |
962 | 974 | |
963 | | sage: from sage.numerical.backends.generic_backend import get_solver # optional - GUROBI |
964 | | sage: p = get_solver(solver = "Gurobi") # optional - GUROBI |
965 | | sage: p.add_variable() # optional - GUROBI |
| 975 | sage: from sage.numerical.backends.generic_backend import get_solver # optional - Gurobi |
| 976 | sage: p = get_solver(solver = "Gurobi") # optional - Gurobi |
| 977 | sage: p.add_variable() # optional - Gurobi |
966 | 978 | 0 |
967 | | sage: p.col_bounds(0) # optional - GUROBI |
| 979 | sage: p.col_bounds(0) # optional - Gurobi |
968 | 980 | (0.0, None) |
969 | | sage: p.variable_upper_bound(0, 5) # optional - GUROBI |
970 | | sage: p.col_bounds(0) # optional - GUROBI |
| 981 | sage: p.variable_upper_bound(0, 5) # optional - Gurobi |
| 982 | sage: p.col_bounds(0) # optional - Gurobi |
971 | 983 | (0.0, 5.0) |
972 | 984 | """ |
973 | 985 | cdef double b[1] |
974 | 986 | |
975 | 987 | if not value is False: |
976 | 988 | check(self.env, GRBsetdblattrelement( |
977 | | self.model[0], "UB", |
978 | | index, |
| 989 | self.model[0], "UB", |
| 990 | index, |
979 | 991 | value if value is not None else GRB_INFINITY)) |
980 | 992 | |
981 | 993 | check(self.env,GRBupdatemodel(self.model[0])) |
… |
… |
|
998 | 1010 | |
999 | 1011 | EXAMPLE:: |
1000 | 1012 | |
1001 | | sage: from sage.numerical.backends.generic_backend import get_solver # optional - GUROBI |
1002 | | sage: p = get_solver(solver = "Gurobi") # optional - GUROBI |
1003 | | sage: p.add_variable() # optional - GUROBI |
| 1013 | sage: from sage.numerical.backends.generic_backend import get_solver # optional - Gurobi |
| 1014 | sage: p = get_solver(solver = "Gurobi") # optional - Gurobi |
| 1015 | sage: p.add_variable() # optional - Gurobi |
1004 | 1016 | 0 |
1005 | | sage: p.col_bounds(0) # optional - GUROBI |
| 1017 | sage: p.col_bounds(0) # optional - Gurobi |
1006 | 1018 | (0.0, None) |
1007 | | sage: p.variable_lower_bound(0, 5) # optional - GUROBI |
1008 | | sage: p.col_bounds(0) # optional - GUROBI |
| 1019 | sage: p.variable_lower_bound(0, 5) # optional - Gurobi |
| 1020 | sage: p.col_bounds(0) # optional - Gurobi |
1009 | 1021 | (5.0, None) |
1010 | 1022 | """ |
1011 | 1023 | cdef double b[1] |
1012 | 1024 | |
1013 | | |
| 1025 | |
1014 | 1026 | if not value is False: |
1015 | 1027 | check(self.env, GRBsetdblattrelement( |
1016 | | self.model[0], "LB", |
1017 | | index, |
| 1028 | self.model[0], "LB", |
| 1029 | index, |
1018 | 1030 | value if value is not None else -GRB_INFINITY)) |
1019 | 1031 | |
1020 | 1032 | check(self.env,GRBupdatemodel(self.model[0])) |
… |
… |
|
1029 | 1041 | Write the problem to a .lp file |
1030 | 1042 | |
1031 | 1043 | INPUT: |
1032 | | |
| 1044 | |
1033 | 1045 | - ``filename`` (string) |
1034 | 1046 | |
1035 | 1047 | EXAMPLE:: |
1036 | 1048 | |
1037 | | sage: from sage.numerical.backends.generic_backend import get_solver # optional - GUROBI |
1038 | | sage: p = get_solver(solver = "Gurobi") # optional - GUROBI |
1039 | | sage: p.add_variables(2) # optional - GUROBI |
| 1049 | sage: from sage.numerical.backends.generic_backend import get_solver # optional - Gurobi |
| 1050 | sage: p = get_solver(solver = "Gurobi") # optional - Gurobi |
| 1051 | sage: p.add_variables(2) # optional - Gurobi |
1040 | 1052 | 1 |
1041 | | sage: p.add_linear_constraint([[0, 1], [1, 2]], None, 3) # optional - GUROBI |
1042 | | sage: p.set_objective([2, 5]) # optional - GUROBI |
1043 | | sage: p.write_lp(SAGE_TMP+"/lp_problem.lp") # optional - GUROBI |
| 1053 | sage: p.add_linear_constraint([[0, 1], [1, 2]], None, 3) # optional - Gurobi |
| 1054 | sage: p.set_objective([2, 5]) # optional - Gurobi |
| 1055 | sage: p.write_lp(SAGE_TMP+"/lp_problem.lp") # optional - Gurobi |
1044 | 1056 | """ |
1045 | 1057 | check(self.env, GRBwrite(self.model[0], filename)) |
1046 | 1058 | |
… |
… |
|
1049 | 1061 | Write the problem to a .mps file |
1050 | 1062 | |
1051 | 1063 | INPUT: |
1052 | | |
| 1064 | |
1053 | 1065 | - ``filename`` (string) |
1054 | 1066 | |
1055 | 1067 | EXAMPLE:: |
1056 | 1068 | |
1057 | | sage: from sage.numerical.backends.generic_backend import get_solver # optional - GUROBI |
1058 | | sage: p = get_solver(solver = "Gurobi") # optional - GUROBI |
1059 | | sage: p.add_variables(2) # optional - GUROBI |
| 1069 | sage: from sage.numerical.backends.generic_backend import get_solver # optional - Gurobi |
| 1070 | sage: p = get_solver(solver = "Gurobi") # optional - Gurobi |
| 1071 | sage: p.add_variables(2) # optional - Gurobi |
1060 | 1072 | 1 |
1061 | | sage: p.add_linear_constraint([[0, 1], [1, 2]], None, 3) # optional - GUROBI |
1062 | | sage: p.set_objective([2, 5]) # optional - GUROBI |
1063 | | sage: p.write_lp(SAGE_TMP+"/lp_problem.lp") # optional - GUROBI |
| 1073 | sage: p.add_linear_constraint([[0, 1], [1, 2]], None, 3) # optional - Gurobi |
| 1074 | sage: p.set_objective([2, 5]) # optional - Gurobi |
| 1075 | sage: p.write_lp(SAGE_TMP+"/lp_problem.lp") # optional - Gurobi |
1064 | 1076 | """ |
1065 | 1077 | check(self.env, GRBwrite(self.model[0], filename)) |
1066 | 1078 | |
… |
… |
|
1068 | 1080 | """ |
1069 | 1081 | Destructor |
1070 | 1082 | """ |
1071 | | GRBfreemodel(self.model[0]) |
| 1083 | if self.model != NULL: |
| 1084 | GRBfreemodel(self.model[0]) |
1072 | 1085 | |
1073 | 1086 | |
1074 | 1087 | cdef dict errors = { |
diff --git a/sage/numerical/mip.pyx b/sage/numerical/mip.pyx
a
|
b
|
|
456 | 456 | others. |
457 | 457 | |
458 | 458 | ``lower_bound`` and ``upper_bound`` are numerical values. |
459 | | |
| 459 | |
460 | 460 | EXAMPLE: |
461 | 461 | |
462 | 462 | First, let us define a small LP:: |
… |
… |
|
526 | 526 | for i in indices: |
527 | 527 | lb, ub = b.row_bounds(i) |
528 | 528 | result.append((lb, b.row(i), ub)) |
529 | | |
| 529 | |
530 | 530 | return result |
531 | 531 | |
532 | 532 | # Weird Input |
… |
… |
|
576 | 576 | cdef GenericBackend b = self._backend |
577 | 577 | |
578 | 578 | # inv_variables associates a MIPVariable object to an id |
579 | | inv_variables = [0]*len(self._variables) |
| 579 | inv_variables = {} |
| 580 | |
580 | 581 | for (v, id) in self._variables.iteritems(): |
581 | 582 | inv_variables[id]=v |
582 | 583 | |
583 | | |
584 | 584 | # varid_name associates variables id to names |
585 | 585 | varid_name = {} |
586 | 586 | |
… |
… |
|
592 | 592 | |
593 | 593 | print ("Maximization:" if b.is_maximization() else "Minimization:") |
594 | 594 | print " ", |
595 | | |
| 595 | |
596 | 596 | first = True |
597 | 597 | for 0<= i< b.ncols(): |
598 | 598 | c = b.objective_coefficient(i) |
… |
… |
|
1493 | 1493 | |
1494 | 1494 | The command :: |
1495 | 1495 | |
1496 | | sage: p.solver_parameter("CPX_PARAM_TILIM", 60) # optional - CPLEX |
| 1496 | sage: p = MixedIntegerLinearProgram(solver = "CPLEX") # optional - CPLEX |
| 1497 | sage: p.solver_parameter("CPX_PARAM_TILIM", 60) # optional - CPLEX |
1497 | 1498 | |
1498 | 1499 | works as intended. |
1499 | 1500 | |