# HG changeset patch
# User Nathann Cohen <nathann.cohen@gmail.com>
# Date 1291057723 -3600
# Node ID 214981100765f40322a4c90db831d069bbec0689
# Parent 54d2524eb88c432db19ab7da9ef48c1ec80c2f1b
trac 10341 - some other modifications to the new LP interface, for CPLEX and Coin
diff -r 54d2524eb88c -r 214981100765 sage/numerical/backends/coin_backend.pyx
a
|
b
|
|
35 | 35 | else: |
36 | 36 | self.set_sense(-1) |
37 | 37 | |
38 | | cpdef int add_variable(self, lower_bound=0.0, upper_bound=None, binary=False, continuous=False, integer=False) except -1: |
| 38 | cpdef int add_variable(self, lower_bound=0.0, upper_bound=None, binary=False, continuous=False, integer=False, obj=0.0, name=None) except -1: |
39 | 39 | """ |
40 | 40 | Add a variable. |
41 | 41 | |
… |
… |
|
54 | 54 | |
55 | 55 | - ``integer`` - ``True`` if the variable is binary (default: ``False``). |
56 | 56 | |
| 57 | - ``obj`` - (optional) coefficient of this variable in the objective function (default: 0.0) |
| 58 | |
| 59 | - ``name`` - an optional name for the newly added variable (default: ``None``). |
| 60 | |
57 | 61 | OUTPUT: The index of the newly created variable |
58 | 62 | |
| 63 | .. NOTE:: |
| 64 | |
| 65 | The names are ignored by Coin at the moment !!! |
| 66 | |
59 | 67 | EXAMPLE:: |
60 | 68 | |
61 | 69 | sage: from sage.numerical.backends.generic_backend import get_solver |
… |
… |
|
74 | 82 | Traceback (most recent call last): |
75 | 83 | ... |
76 | 84 | ValueError: ... |
| 85 | sage: p.add_variable(name='x',obj=1.0) # optional - Coin |
| 86 | 3 |
| 87 | sage: p.col_name(3) # optional - Coin |
| 88 | '' |
| 89 | sage: p.objective_coefficient(3) # optional - Coin |
| 90 | 1.0 |
| 91 | |
77 | 92 | """ |
78 | 93 | |
79 | 94 | cdef int vtype = int(bool(binary)) + int(bool(continuous)) + int(bool(integer)) |
… |
… |
|
97 | 112 | elif integer: |
98 | 113 | self.set_variable_type(n,1) |
99 | 114 | |
| 115 | # THE NAMES ARE IGNORED BY COIN AT THE MOMENT |
| 116 | |
| 117 | if obj: |
| 118 | self.si.setObjCoeff(n, obj) |
| 119 | |
100 | 120 | return n |
101 | 121 | |
102 | | cpdef int add_variables(self, int number, lower_bound=0.0, upper_bound=None, binary=False, continuous=False, integer=False) except -1: |
| 122 | cpdef int add_variables(self, int number, lower_bound=0.0, upper_bound=None, binary=False, continuous=False, integer=False, obj=0.0, names=None) except -1: |
103 | 123 | """ |
104 | 124 | Add ``number`` new variables. |
105 | 125 | |
… |
… |
|
120 | 140 | |
121 | 141 | - ``integer`` - ``True`` if the variable is binary (default: ``False``). |
122 | 142 | |
| 143 | - ``obj`` - (optional) coefficient of all variables in the objective function (default: 0.0) |
| 144 | |
| 145 | - ``names`` - optional list of names (default: ``None``) |
| 146 | |
123 | 147 | OUTPUT: The index of the variable created last. |
124 | 148 | |
| 149 | .. NOTE:: |
| 150 | |
| 151 | The names are ignored by Coin at the moment !!! |
| 152 | |
125 | 153 | EXAMPLE:: |
126 | 154 | |
127 | 155 | sage: from sage.numerical.backends.generic_backend import get_solver |
… |
… |
|
132 | 160 | 4 |
133 | 161 | sage: p.ncols() # optional - Coin |
134 | 162 | 5 |
135 | | sage: p.add_variables(2, lower_bound=-2.0, integer=True) # optional - Coin |
| 163 | sage: p.add_variables(2, lower_bound=-2.0, integer=True, names=['a','b']) # optional - Coin |
136 | 164 | 6 |
137 | 165 | """ |
138 | 166 | cdef int vtype = int(bool(binary)) + int(bool(continuous)) + int(bool(integer)) |
… |
… |
|
160 | 188 | elif integer: |
161 | 189 | self.set_variable_type(n + i,1) |
162 | 190 | |
| 191 | if obj: |
| 192 | self.si.setObjCoeff(n + i, obj) |
| 193 | |
| 194 | # THE NAMES ARE IGNORED BY COIN AT THE MOMENT |
| 195 | |
163 | 196 | return n + number -1 |
164 | 197 | |
165 | 198 | cpdef set_variable_type(self, int variable, int vtype): |
… |
… |
|
221 | 254 | """ |
222 | 255 | self.si.setObjSense(-sense) |
223 | 256 | |
224 | | cpdef set_objective_coefficient(self, int variable, double coeff): |
225 | | r""" |
226 | | Sets the coefficient of a variable in the objective function |
| 257 | cpdef objective_coefficient(self, int variable, coeff=None): |
| 258 | """ |
| 259 | Set or get the coefficient of a variable in the objective function |
227 | 260 | |
228 | 261 | INPUT: |
229 | 262 | |
230 | 263 | - ``variable`` (integer) -- the variable's id |
231 | 264 | |
232 | | - ``coeff`` (double) -- its coefficient |
| 265 | - ``coeff`` (double) -- its coefficient or ``None`` for |
| 266 | reading (default: ``None``) |
233 | 267 | |
234 | 268 | EXAMPLE:: |
235 | 269 | |
236 | 270 | sage: from sage.numerical.backends.generic_backend import get_solver |
237 | | sage: p = get_solver(solver = "Coin") # optional - Coin |
238 | | sage: p.add_variable() # optional - Coin |
| 271 | sage: p = get_solver(solver = "Coin") # optional -- Coin |
| 272 | sage: p.add_variable() # optional -- Coin |
239 | 273 | 0 |
240 | | sage: p.get_objective_coefficient(0) # optional - Coin |
| 274 | sage: p.objective_coefficient(0) # optional -- Coin |
241 | 275 | 0.0 |
242 | | sage: p.set_objective_coefficient(0,2) # optional - Coin |
243 | | sage: p.get_objective_coefficient(0) # optional - Coin |
| 276 | sage: p.objective_coefficient(0,2) # optional -- Coin |
| 277 | sage: p.objective_coefficient(0) # optional -- Coin |
244 | 278 | 2.0 |
245 | 279 | """ |
246 | | |
247 | | self.si.setObjCoeff(variable, coeff) |
| 280 | if coeff is not None: |
| 281 | self.si.setObjCoeff(variable, coeff) |
| 282 | else: |
| 283 | return self.si.getObjCoefficients()[variable] |
248 | 284 | |
249 | 285 | cpdef set_objective(self, list coeff): |
250 | 286 | r""" |
… |
… |
|
262 | 298 | sage: p.add_variables(5) # optional - Coin |
263 | 299 | 4 |
264 | 300 | sage: p.set_objective([1, 1, 2, 1, 3]) # optional - Coin |
265 | | sage: map(lambda x :p.get_objective_coefficient(x), range(5)) # optional - Coin |
| 301 | sage: map(lambda x :p.objective_coefficient(x), range(5)) # optional - Coin |
266 | 302 | [1.0, 1.0, 2.0, 1.0, 3.0] |
267 | 303 | """ |
268 | 304 | |
… |
… |
|
292 | 328 | msg = model.messageHandler() |
293 | 329 | msg.setLogLevel(level) |
294 | 330 | |
295 | | cpdef add_linear_constraints(self, int number, lower_bound, upper_bound): |
| 331 | cpdef add_linear_constraints(self, int number, lower_bound, upper_bound, names = None): |
296 | 332 | """ |
297 | 333 | Add ``'number`` linear constraints. |
298 | 334 | |
… |
… |
|
304 | 340 | |
305 | 341 | - ``upper_bound`` - an upper bound, either a real value or ``None`` |
306 | 342 | |
| 343 | - ``names`` - an optional list of names (default: ``None``) |
| 344 | |
| 345 | .. NOTE:: |
| 346 | |
| 347 | The names are ignored by Coin at the moment !!! |
| 348 | |
307 | 349 | EXAMPLE:: |
308 | 350 | |
309 | 351 | sage: from sage.numerical.backends.generic_backend import get_solver |
… |
… |
|
315 | 357 | ([], []) |
316 | 358 | sage: p.row_bounds(4) # optional - Coin |
317 | 359 | (None, 2.0) |
| 360 | sage: p.add_linear_constraints(2, None, 2, names=['foo','bar']) # optional - Coin |
318 | 361 | """ |
319 | 362 | |
320 | 363 | cdef int i |
321 | 364 | for 0<= i<number: |
322 | 365 | self.add_linear_constraint([],lower_bound, upper_bound) |
323 | 366 | |
324 | | cpdef add_linear_constraint(self, coefficients, lower_bound, upper_bound): |
| 367 | cpdef add_linear_constraint(self, coefficients, lower_bound, upper_bound, name = None): |
325 | 368 | """ |
326 | 369 | Add a linear constraint. |
327 | 370 | |
… |
… |
|
334 | 377 | - ``lower_bound`` - a lower bound, either a real value or ``None`` |
335 | 378 | |
336 | 379 | - ``upper_bound`` - an upper bound, either a real value or ``None`` |
| 380 | |
| 381 | - ``name`` - an optional name for this row (default: ``None``) |
| 382 | |
| 383 | .. NOTE:: |
| 384 | |
| 385 | The names are ignored by Coin at the moment !!! |
337 | 386 | |
338 | 387 | EXAMPLE:: |
339 | 388 | |
… |
… |
|
346 | 395 | ([0, 1, 2, 3, 4], [0.0, 1.0, 2.0, 3.0, 4.0]) |
347 | 396 | sage: p.row_bounds(0) # optional - Coin |
348 | 397 | (2.0, 2.0) |
| 398 | sage: p.add_linear_constraint( zip(range(5), range(5)), 1.0, 1.0, name='foo') # optional - Coin |
| 399 | sage: p.row_name(1) # optional - Coin |
| 400 | '' |
| 401 | |
349 | 402 | """ |
350 | 403 | if lower_bound is None and upper_bound is None: |
351 | 404 | raise ValueError("At least one of 'upper_bound' or 'lower_bound' must be set.") |
… |
… |
|
481 | 534 | return (lb[i] if lb[i] != - self.si.getInfinity() else None, |
482 | 535 | ub[i] if ub[i] != + self.si.getInfinity() else None) |
483 | 536 | |
484 | | cpdef double get_objective_coefficient(self, int index): |
485 | | """ |
486 | | Returns the value of the objective function. |
487 | | |
488 | | .. NOTE:: |
489 | | |
490 | | Has no meaning unless ``solve`` has been called before. |
491 | | |
492 | | EXAMPLE:: |
493 | | |
494 | | sage: from sage.numerical.backends.generic_backend import get_solver |
495 | | sage: p = get_solver(solver = "Coin") # optional - Coin |
496 | | sage: p.add_variables(2) # optional - Coin |
497 | | 1 |
498 | | sage: p.add_linear_constraint([(0, 1), (1, 2)], None, 3) # optional - Coin |
499 | | sage: p.set_objective([2, 5]) # optional - Coin |
500 | | sage: p.solve() # optional - Coin |
501 | | 0 |
502 | | sage: p.get_objective_value() # optional - Coin |
503 | | 7.5 |
504 | | sage: p.get_variable_value(0) # optional - Coin |
505 | | 0.0 |
506 | | sage: p.get_variable_value(1) # optional - Coin |
507 | | 1.5 |
508 | | """ |
509 | | return self.si.getObjCoefficients()[index] |
510 | | |
511 | 537 | cpdef add_col(self, list indices, list coeffs): |
512 | 538 | r""" |
513 | 539 | Adds a column. |
… |
… |
|
582 | 608 | 0 |
583 | 609 | sage: p.add_linear_constraint([(0, 1)], None, 4) # optional - Coin |
584 | 610 | sage: p.add_linear_constraint([(0, 1)], 6, None) # optional - Coin |
585 | | sage: p.set_objective_coefficient(0,1) # optional - Coin |
| 611 | sage: p.objective_coefficient(0,1) # optional - Coin |
586 | 612 | sage: p.solve() # optional - Coin |
587 | 613 | Traceback (most recent call last): |
588 | 614 | ... |
… |
… |
|
897 | 923 | sage: print p.problem_name() # optional - Coin |
898 | 924 | <BLANKLINE> |
899 | 925 | """ |
900 | | |
901 | 926 | if name == NULL: |
902 | 927 | return "" |
903 | 928 | |
904 | 929 | |
905 | | cpdef row_name(self, int index, char * name = NULL): |
| 930 | cpdef row_name(self, int index): |
906 | 931 | r""" |
907 | | Returns or defines the ``index`` th row name |
| 932 | Returns the ``index`` th row name |
908 | 933 | |
909 | 934 | INPUT: |
910 | 935 | |
911 | 936 | - ``index`` (integer) -- the row's id |
912 | 937 | |
913 | | - ``name`` (``char *``) -- its name. When set to ``NULL`` |
914 | | (default), the method returns the current name. |
915 | | |
916 | 938 | EXAMPLE:: |
917 | 939 | |
918 | 940 | sage: from sage.numerical.backends.generic_backend import get_solver |
919 | | sage: p = get_solver(solver = "Coin") # optional - Coin |
920 | | sage: p.add_linear_constraints(1, 2, None) # optional - Coin |
921 | | sage: p.row_name(0, "Empty constraint 1") # optional - Coin |
922 | | sage: print p.row_name(0) # optional - Coin |
| 941 | sage: p = get_solver(solver = "Coin") # optional - Coin |
| 942 | sage: p.add_linear_constraints(1, 2, None, names=['Empty constraint 1']) # optional - Coin |
| 943 | sage: print p.row_name(0) # optional - Coin |
923 | 944 | <BLANKLINE> |
924 | 945 | """ |
925 | | if name == NULL: |
926 | | return "" |
| 946 | return "" |
927 | 947 | |
928 | | cpdef col_name(self, int index, char * name = NULL): |
| 948 | cpdef col_name(self, int index): |
929 | 949 | r""" |
930 | | Returns or defines the ``index`` th col name |
| 950 | Returns the ``index`` th col name |
931 | 951 | |
932 | 952 | INPUT: |
933 | 953 | |
934 | 954 | - ``index`` (integer) -- the col's id |
935 | 955 | |
936 | | - ``name`` (``char *``) -- its name. When set to ``NULL`` |
937 | | (default), the method returns the current name. |
938 | | |
939 | 956 | EXAMPLE:: |
940 | 957 | |
941 | 958 | sage: from sage.numerical.backends.generic_backend import get_solver |
942 | | sage: p = get_solver(solver = "Coin") # optional - Coin |
943 | | sage: p.add_variable() # optional - Coin |
| 959 | sage: p = get_solver(solver = "Coin") # optional - Coin |
| 960 | sage: p.add_variable(name='I am a variable') # optional - Coin |
944 | 961 | 0 |
945 | | sage: p.col_name(0, "I am a variable") # optional - Coin |
946 | | sage: print p.col_name(0) # optional - Coin |
| 962 | sage: print p.col_name(0) # optional - Coin |
947 | 963 | <BLANKLINE> |
948 | 964 | """ |
949 | | |
950 | | if name == NULL: |
951 | | return "" |
| 965 | return "" |
diff -r 54d2524eb88c -r 214981100765 sage/numerical/backends/cplex_backend.pyx
a
|
b
|
|
34 | 34 | else: |
35 | 35 | self.set_sense(-1) |
36 | 36 | |
37 | | cpdef int add_variable(self, lower_bound=0.0, upper_bound=None, binary=False, continuous=False, integer=False) except -1: |
| 37 | cpdef int add_variable(self, lower_bound=0.0, upper_bound=None, binary=False, continuous=False, integer=False, obj=0.0, name=None) except -1: |
38 | 38 | """ |
39 | 39 | Add a variable. |
40 | 40 | |
… |
… |
|
53 | 53 | |
54 | 54 | - ``integer`` - ``True`` if the variable is binary (default: ``False``). |
55 | 55 | |
| 56 | - ``obj`` - (optional) coefficient of this variable in the objective function (default: 0.0) |
| 57 | |
| 58 | - ``name`` - an optional name for the newly added variable (default: ``None``). |
| 59 | |
56 | 60 | OUTPUT: The index of the newly created variable |
57 | 61 | |
58 | 62 | EXAMPLE:: |
… |
… |
|
73 | 77 | Traceback (most recent call last): |
74 | 78 | ... |
75 | 79 | ValueError: ... |
| 80 | sage: p.add_variable(name='x',obj=1.0) # optional - CPLEX |
| 81 | 3 |
| 82 | sage: p.col_name(3) # optional - CPLEX |
| 83 | 'x' |
| 84 | sage: p.objective_coefficient(3) # optional - CPLEX |
| 85 | 1.0 |
| 86 | |
76 | 87 | """ |
77 | | |
| 88 | cdef char * c_name |
| 89 | cdef double c_coeff = obj |
78 | 90 | cdef int vtype = int(bool(binary)) + int(bool(continuous)) + int(bool(integer)) |
79 | 91 | if vtype == 0: |
80 | 92 | continuous = True |
… |
… |
|
98 | 110 | elif integer: |
99 | 111 | self.set_variable_type(n,1) |
100 | 112 | |
| 113 | if name is not None: |
| 114 | c_name = name |
| 115 | status = CPXchgcolname(self.env, self.lp, 1, &n, &c_name) |
| 116 | check(status) |
| 117 | |
| 118 | if c_coeff: |
| 119 | status = CPXchgobj(self.env, self.lp, 1, &n, &c_coeff) |
| 120 | check(status) |
| 121 | |
101 | 122 | return n |
102 | 123 | |
103 | | cpdef int add_variables(self, int number, lower_bound=0.0, upper_bound=None, binary=False, continuous=False, integer=False) except -1: |
| 124 | cpdef int add_variables(self, int number, lower_bound=0.0, upper_bound=None, binary=False, continuous=False, integer=False, obj=0.0, names=None) except -1: |
104 | 125 | """ |
105 | 126 | Add ``number`` new variables. |
106 | 127 | |
… |
… |
|
121 | 142 | |
122 | 143 | - ``integer`` - ``True`` if the variable is binary (default: ``False``). |
123 | 144 | |
| 145 | - ``obj`` - (optional) coefficient of all variables in the objective function (default: 0.0) |
| 146 | |
| 147 | - ``names`` - optional list of names (default: ``None``) |
| 148 | |
124 | 149 | OUTPUT: The index of the variable created last. |
125 | 150 | |
126 | 151 | EXAMPLE:: |
… |
… |
|
133 | 158 | 4 |
134 | 159 | sage: p.ncols() # optional - CPLEX |
135 | 160 | 5 |
136 | | sage: p.add_variables(2, lower_bound=-2.0, integer=True) # optional - CPLEX |
| 161 | sage: p.add_variables(2, lower_bound=-2.0, integer=True, names=['a','b']) # optional - CPLEX |
137 | 162 | 6 |
138 | 163 | """ |
| 164 | cdef char * c_name |
| 165 | cdef double c_coeff = obj |
139 | 166 | cdef int vtype = int(bool(binary)) + int(bool(continuous)) + int(bool(integer)) |
140 | 167 | if vtype == 0: |
141 | 168 | continuous = True |
… |
… |
|
149 | 176 | cdef int n |
150 | 177 | n = CPXgetnumcols(self.env, self.lp) - 1 |
151 | 178 | |
152 | | cdef int i |
| 179 | cdef int i, j |
153 | 180 | |
154 | 181 | for 0<= i < number: |
155 | 182 | if lower_bound != 0.0: |
… |
… |
|
162 | 189 | elif integer: |
163 | 190 | self.set_variable_type(n - i,1) |
164 | 191 | |
| 192 | if names: |
| 193 | j = n - i |
| 194 | c_name = names[i] |
| 195 | status = CPXchgcolname(self.env, self.lp, 1, &j, &c_name) |
| 196 | check(status) |
| 197 | |
| 198 | if c_coeff: |
| 199 | j = n - i |
| 200 | status = CPXchgobj(self.env, self.lp, 1, &j, &c_coeff) |
| 201 | check(status) |
| 202 | |
165 | 203 | return n |
166 | 204 | |
167 | 205 | cpdef set_variable_type(self, int variable, int vtype): |
… |
… |
|
228 | 266 | |
229 | 267 | CPXchgobjsen(self.env, self.lp, -sense) |
230 | 268 | |
231 | | cpdef set_objective_coefficient(self, int variable, double coeff): |
232 | | r""" |
233 | | Sets the coefficient of a variable in the objective function |
| 269 | cpdef objective_coefficient(self, int variable, coeff=None): |
| 270 | """ |
| 271 | Set or get the coefficient of a variable in the objective function |
234 | 272 | |
235 | 273 | INPUT: |
236 | 274 | |
237 | 275 | - ``variable`` (integer) -- the variable's id |
238 | 276 | |
239 | | - ``coeff`` (double) -- its coefficient |
| 277 | - ``coeff`` (double) -- its coefficient or ``None`` for |
| 278 | reading (default: ``None``) |
240 | 279 | |
241 | 280 | EXAMPLE:: |
242 | 281 | |
243 | 282 | sage: from sage.numerical.backends.generic_backend import get_solver |
244 | | sage: p = get_solver(solver = "CPLEX") # optional - CPLEX |
245 | | sage: p.add_variable() # optional - CPLEX |
| 283 | sage: p = get_solver(solver = "CPLEX") # optional -- CPLEX |
| 284 | sage: p.add_variable() # optional -- CPLEX |
246 | 285 | 0 |
247 | | sage: p.get_objective_coefficient(0) # optional - CPLEX |
| 286 | sage: p.objective_coefficient(0) # optional -- CPLEX |
248 | 287 | 0.0 |
249 | | sage: p.set_objective_coefficient(0,2) # optional - CPLEX |
250 | | sage: p.get_objective_coefficient(0) # optional - CPLEX |
| 288 | sage: p.objective_coefficient(0,2) # optional -- CPLEX |
| 289 | sage: p.objective_coefficient(0) # optional -- CPLEX |
251 | 290 | 2.0 |
252 | 291 | """ |
253 | 292 | |
254 | 293 | cdef int status |
255 | | status = CPXchgobj(self.env, self.lp, 1, &variable, &coeff) |
256 | | check(status) |
| 294 | cdef double value |
| 295 | |
| 296 | if coeff is None: |
| 297 | status = CPXgetobj(self.env, self.lp, &value, variable, variable) |
| 298 | check(status) |
| 299 | return value |
| 300 | |
| 301 | else: |
| 302 | value = coeff |
| 303 | status = CPXchgobj(self.env, self.lp, 1, &variable, &value) |
| 304 | check(status) |
257 | 305 | |
258 | 306 | cpdef problem_name(self, char * name = NULL): |
259 | 307 | r""" |
… |
… |
|
307 | 355 | sage: p.add_variables(5) # optional - CPLEX |
308 | 356 | 4 |
309 | 357 | sage: p.set_objective([1, 1, 2, 1, 3]) # optional - CPLEX |
310 | | sage: map(lambda x :p.get_objective_coefficient(x), range(5)) # optional - CPLEX |
| 358 | sage: map(lambda x :p.objective_coefficient(x), range(5)) # optional - CPLEX |
311 | 359 | [1.0, 1.0, 2.0, 1.0, 3.0] |
312 | 360 | """ |
313 | 361 | |
… |
… |
|
348 | 396 | status = CPXsetintparam (self.env, CPX_PARAM_SCRIND, CPX_ON) |
349 | 397 | check(status) |
350 | 398 | |
351 | | cpdef add_linear_constraints(self, int number, lower_bound, upper_bound): |
| 399 | cpdef add_linear_constraints(self, int number, lower_bound, upper_bound, names = None): |
352 | 400 | """ |
353 | 401 | Add ``'number`` linear constraints. |
354 | 402 | |
… |
… |
|
360 | 408 | |
361 | 409 | - ``upper_bound`` - an upper bound, either a real value or ``None`` |
362 | 410 | |
| 411 | - ``names`` - an optional list of names (default: ``None``) |
| 412 | |
363 | 413 | EXAMPLE:: |
364 | 414 | |
365 | 415 | sage: from sage.numerical.backends.generic_backend import get_solver |
… |
… |
|
371 | 421 | ([], []) |
372 | 422 | sage: p.row_bounds(4) # optional - CPLEX |
373 | 423 | (None, 2.0) |
| 424 | sage: p.add_linear_constraints(2, None, 2, names=['foo','bar']) # optional - Coin |
374 | 425 | """ |
375 | 426 | if lower_bound is None and upper_bound is None: |
376 | 427 | raise ValueError("At least one of 'upper_bound' or 'lower_bound' must be set.") |
… |
… |
|
380 | 431 | cdef double * bound = <double *> sage_malloc(number * sizeof(double)) |
381 | 432 | cdef double * rng = NULL |
382 | 433 | cdef int i |
| 434 | cdef char ** c_names = <char **> sage_malloc(number * sizeof(char *)) |
383 | 435 | |
384 | 436 | if upper_bound == lower_bound: |
385 | 437 | sense[0] = 'E' |
… |
… |
|
403 | 455 | elif lower_bound is not None: |
404 | 456 | sense[0] = 'G' |
405 | 457 | bound[0] = lower_bound |
| 458 | |
| 459 | if names: |
| 460 | c_names[0] = names[0] |
406 | 461 | |
407 | 462 | for 1<= i <number: |
408 | 463 | sense[i] = sense[0] |
409 | 464 | bound[i] = bound[0] |
410 | 465 | if rng != NULL: |
411 | 466 | rng[i] = rng[0] |
| 467 | if names: |
| 468 | c_names[i] = names[i] |
412 | 469 | |
413 | | status = CPXnewrows(self.env, self.lp, number, bound, sense, rng, NULL) |
| 470 | status = CPXnewrows(self.env, self.lp, number, bound, sense, rng, c_names if names else NULL) |
414 | 471 | check(status) |
415 | 472 | |
416 | | cpdef add_linear_constraint(self, coefficients, lower_bound, upper_bound): |
| 473 | cpdef add_linear_constraint(self, coefficients, lower_bound, upper_bound, name = None): |
417 | 474 | """ |
418 | 475 | Add a linear constraint. |
419 | 476 | |
… |
… |
|
426 | 483 | - ``lower_bound`` - a lower bound, either a real value or ``None`` |
427 | 484 | |
428 | 485 | - ``upper_bound`` - an upper bound, either a real value or ``None`` |
| 486 | |
| 487 | - ``name`` - an optional name for this row (default: ``None``) |
429 | 488 | |
430 | 489 | EXAMPLE:: |
431 | 490 | |
… |
… |
|
438 | 497 | ([1, 2, 3, 4], [1.0, 2.0, 3.0, 4.0]) |
439 | 498 | sage: p.row_bounds(0) # optional - CPLEX |
440 | 499 | (2.0, 2.0) |
| 500 | sage: p.add_linear_constraint( zip(range(5), range(5)), 1.0, 1.0, name='foo') # optional - CPLEX |
| 501 | sage: p.row_name(1) # optional - CPLEX |
| 502 | 'foo' |
| 503 | |
441 | 504 | """ |
442 | 505 | if lower_bound is None and upper_bound is None: |
443 | 506 | raise ValueError("At least one of 'upper_bound' or 'lower_bound' must be set.") |
… |
… |
|
448 | 511 | cdef int nrows = self.nrows() |
449 | 512 | cdef char sense |
450 | 513 | |
| 514 | cdef char * c_name |
| 515 | |
451 | 516 | cdef double * c_coeff |
452 | 517 | cdef int * c_indices |
453 | 518 | cdef int * c_row |
… |
… |
|
486 | 551 | sense = 'G' |
487 | 552 | bound = lower_bound |
488 | 553 | |
489 | | status = CPXnewrows(self.env, self.lp, 1, &bound, &sense, &rng, NULL) |
| 554 | if name: |
| 555 | c_name = name |
| 556 | |
| 557 | status = CPXnewrows(self.env, self.lp, 1, &bound, &sense, &rng, NULL if (name is None) else &c_name) |
| 558 | |
490 | 559 | check(status) |
491 | 560 | status = CPXchgcoeflist(self.env, self.lp, n, c_row, c_indices, c_coeff) |
492 | 561 | check(status) |
… |
… |
|
629 | 698 | return (lb if lb != -CPX_INFBOUND else None, |
630 | 699 | ub if ub != +CPX_INFBOUND else None) |
631 | 700 | |
632 | | cpdef double get_objective_coefficient(self, int index): |
633 | | r""" |
634 | | Sets the coefficient of a variable in the objective function |
635 | | |
636 | | INPUT: |
637 | | |
638 | | - ``variable`` (integer) -- the variable's id |
639 | | |
640 | | - ``coeff`` (double) -- its coefficient |
641 | | |
642 | | EXAMPLE:: |
643 | | |
644 | | sage: from sage.numerical.backends.generic_backend import get_solver |
645 | | sage: p = get_solver(solver = "CPLEX") # optional - CPLEX |
646 | | sage: p.add_variable() # optional - CPLEX |
647 | | 0 |
648 | | sage: p.get_objective_coefficient(0) # optional - CPLEX |
649 | | 0.0 |
650 | | sage: p.set_objective_coefficient(0,2) # optional - CPLEX |
651 | | sage: p.get_objective_coefficient(0) # optional - CPLEX |
652 | | 2.0 |
653 | | """ |
654 | | |
655 | | cdef int status |
656 | | cdef double value |
657 | | status = CPXgetobj(self.env, self.lp, &value, index, index) |
658 | | check(status) |
659 | | return value |
660 | | |
661 | | |
662 | 701 | cpdef add_col(self, list indices, list coeffs): |
663 | 702 | r""" |
664 | 703 | Adds a column. |
… |
… |
|
735 | 774 | sage: p.add_col(range(5), range(5)) # optional - CPLEX |
736 | 775 | sage: p.solve() # optional - CPLEX |
737 | 776 | 0 |
738 | | sage: p.set_objective_coefficient(0,1) # optional - CPLEX |
| 777 | sage: p.objective_coefficient(0,1) # optional - CPLEX |
739 | 778 | sage: p.solve() # optional - CPLEX |
740 | 779 | Traceback (most recent call last): |
741 | 780 | ... |
… |
… |
|
873 | 912 | |
874 | 913 | return CPXgetnumrows(self.env, self.lp) |
875 | 914 | |
876 | | cpdef row_name(self, int index, char * name = NULL): |
| 915 | cpdef row_name(self, int index): |
877 | 916 | r""" |
878 | | Returns or defines the ``index`` th row name |
| 917 | Return the ``index`` th row name |
879 | 918 | |
880 | 919 | INPUT: |
881 | 920 | |
882 | 921 | - ``index`` (integer) -- the row's id |
883 | 922 | |
884 | | - ``name`` (``char *``) -- its name. When set to ``NULL`` |
885 | | (default), the method returns the current name. |
886 | | |
887 | 923 | EXAMPLE:: |
888 | 924 | |
889 | 925 | sage: from sage.numerical.backends.generic_backend import get_solver |
890 | | sage: p = get_solver(solver = "CPLEX") # optional - CPLEX |
891 | | sage: p.add_linear_constraints(1, 2, None) # optional - CPLEX |
892 | | sage: p.row_name(0, "Empty constraint 1") # optional - CPLEX |
893 | | sage: p.row_name(0) # optional - CPLEX |
| 926 | sage: p = get_solver(solver = "CPLEX") # optional - CPLEX |
| 927 | sage: p.add_linear_constraints(1, 2, None, names=['Empty constraint 1']) # optional - CPLEX |
| 928 | sage: p.row_name(0) # optional - CPLEX |
894 | 929 | 'Empty constraint 1' |
895 | | |
896 | 930 | """ |
897 | 931 | |
898 | 932 | cdef int status |
899 | 933 | cdef int zero |
900 | 934 | cdef char * n |
901 | 935 | |
902 | | if name == NULL: |
903 | | n = <char *>sage_malloc(500*sizeof(char)) |
904 | | status = CPXgetrowname(self.env, self.lp, &n, n, 500, &zero, index, index) |
905 | | if status == 1219: |
906 | | sage_free(n) |
907 | | return "" |
908 | | check(status) |
| 936 | n = <char *>sage_malloc(500*sizeof(char)) |
| 937 | status = CPXgetrowname(self.env, self.lp, &n, n, 500, &zero, index, index) |
| 938 | if status == 1219: |
| 939 | sage_free(n) |
| 940 | return "" |
| 941 | check(status) |
909 | 942 | |
910 | | s = str(n) |
911 | | sage_free(n) |
| 943 | s = str(n) |
| 944 | sage_free(n) |
912 | 945 | |
913 | | return s |
| 946 | return s |
914 | 947 | |
915 | | pass |
916 | | else: |
917 | | status = CPXchgrowname(self.env, self.lp, 1, &index, &name) |
918 | | check(status) |
919 | | |
920 | | cpdef col_name(self, int index, char * name = NULL): |
| 948 | cpdef col_name(self, int index): |
921 | 949 | r""" |
922 | | Returns or defines the ``index`` th col name. |
| 950 | Returns the ``index`` th col name. |
923 | 951 | |
924 | 952 | INPUT: |
925 | 953 | |
926 | 954 | - ``index`` (integer) -- the col's id |
927 | 955 | |
928 | | - ``name`` (``char *``) -- its name. When set to ``NULL`` |
929 | | (default), the method returns the current name. |
930 | | |
931 | 956 | EXAMPLE:: |
932 | 957 | |
933 | 958 | sage: from sage.numerical.backends.generic_backend import get_solver |
934 | | sage: p = get_solver(solver = "CPLEX") # optional - CPLEX |
935 | | sage: p.add_variable() # optional - CPLEX |
| 959 | sage: p = get_solver(solver = "CPLEX") # optional - CPLEX |
| 960 | sage: p.add_variable(name='I am a variable') # optional - CPLEX |
936 | 961 | 0 |
937 | | sage: p.col_name(0, "I am a variable") # optional - CPLEX |
938 | | sage: p.col_name(0) # optional - CPLEX |
| 962 | sage: p.col_name(0) # optional - CPLEX |
939 | 963 | 'I am a variable' |
940 | 964 | """ |
941 | 965 | |
… |
… |
|
943 | 967 | cdef char * n |
944 | 968 | cdef int zero |
945 | 969 | |
946 | | if name == NULL: |
| 970 | n = <char *>sage_malloc(500*sizeof(char)) |
| 971 | status = CPXgetcolname(self.env, self.lp, &n, n, 500, &zero, index, index) |
| 972 | if status == 1219: |
| 973 | sage_free(n) |
| 974 | return "" |
| 975 | check(status) |
947 | 976 | |
948 | | n = <char *>sage_malloc(500*sizeof(char)) |
949 | | status = CPXgetcolname(self.env, self.lp, &n, n, 500, &zero, index, index) |
950 | | if status == 1219: |
951 | | sage_free(n) |
952 | | return "" |
953 | | check(status) |
954 | | |
955 | | s = str(n) |
956 | | sage_free(n) |
957 | | return s |
958 | | |
959 | | else: |
960 | | status = CPXchgcolname(self.env, self.lp, 1, &index, &name) |
961 | | check(status) |
962 | | |
963 | | |
| 977 | s = str(n) |
| 978 | sage_free(n) |
| 979 | return s |
964 | 980 | |
965 | 981 | cpdef bint is_variable_binary(self, int index): |
966 | 982 | r""" |
diff -r 54d2524eb88c -r 214981100765 sage/numerical/mip.pyx
a
|
b
|
|
367 | 367 | Displays the ``MixedIntegerLinearProgram`` in a human-readable |
368 | 368 | way. |
369 | 369 | |
370 | | EXAMPLES:: |
| 370 | EXAMPLES: |
| 371 | |
| 372 | When constraints have names :: |
371 | 373 | |
372 | 374 | sage: p = MixedIntegerLinearProgram() |
373 | 375 | sage: x = p.new_variable() |