# HG changeset patch
# User Martin Albrecht <martinralbrecht@googlemail.com>
# Date 1290951182 0
# Node ID cf07c22147a765fdb31736896575452e92719168
# Parent 941d427801567a358c491463387f86495369a323
#10341 more API changes
* drop setting a variable name by col_name()
* drop setting a constraint name by row_name()
* add an optional parameter name to add_variable and add_linear_constraint
* add an optional parameter obj to add_variable
* unify get/set_objective_coefficient
diff -r 941d42780156 -r cf07c22147a7 sage/numerical/backends/generic_backend.pxd
a
|
b
|
|
6 | 6 | ############################################################################## |
7 | 7 | |
8 | 8 | cdef class GenericBackend: |
9 | | cpdef int add_variable(self, lower_bound=*, upper_bound=*, binary=*, continuous=*, integer=*) except -1 |
10 | | cpdef int add_variables(self, int, lower_bound=*, upper_bound=*, binary=*, continuous=*, integer=*) except -1 |
| 9 | cpdef int add_variable(self, lower_bound=*, upper_bound=*, binary=*, continuous=*, integer=*, obj=*, name=*) except -1 |
| 10 | cpdef int add_variables(self, int, lower_bound=*, upper_bound=*, binary=*, continuous=*, integer=*, obj=*, names=*) except -1 |
11 | 11 | cpdef set_variable_type(self, int variable, int vtype) |
12 | 12 | cpdef set_sense(self, int sense) |
13 | | cpdef set_objective_coefficient(self, int variable, double coeff) |
| 13 | cpdef objective_coefficient(self, int variable, coeff=*) |
14 | 14 | cpdef set_objective(self, list coeff) |
15 | 15 | cpdef set_verbosity(self, int level) |
16 | | cpdef add_linear_constraint(self, constraints, lower_bound, upper_bound) |
| 16 | cpdef add_linear_constraint(self, constraints, lower_bound, upper_bound, name=*) |
17 | 17 | cpdef add_col(self, list indices, list coeffs) |
18 | | cpdef add_linear_constraints(self, int number, lower_bound, upper_bound) |
| 18 | cpdef add_linear_constraints(self, int number, lower_bound, upper_bound, names=*) |
19 | 19 | cpdef int solve(self) except -1 |
20 | 20 | cpdef double get_objective_value(self) |
21 | 21 | cpdef double get_variable_value(self, int variable) |
… |
… |
|
23 | 23 | cpdef write_lp(self, char * name) |
24 | 24 | cpdef write_mps(self, char * name, int modern) |
25 | 25 | cpdef row(self, int i) |
26 | | cpdef double get_objective_coefficient(self, int i) |
27 | 26 | cpdef int ncols(self) |
28 | 27 | cpdef int nrows(self) |
29 | 28 | cpdef bint is_variable_binary(self, int) |
… |
… |
|
33 | 32 | cpdef problem_name(self, char * name = *) |
34 | 33 | cpdef row_bounds(self, int index) |
35 | 34 | cpdef col_bounds(self, int index) |
36 | | cpdef row_name(self, int index, char * name = *) |
37 | | cpdef col_name(self, int index, char * name = *) |
| 35 | cpdef row_name(self, int index) |
| 36 | cpdef col_name(self, int index) |
38 | 37 | cpdef variable_upper_bound(self, int index, value = *) |
39 | 38 | cpdef variable_lower_bound(self, int index, value = *) |
40 | 39 | |
diff -r 941d42780156 -r cf07c22147a7 sage/numerical/backends/generic_backend.pyx
a
|
b
|
|
26 | 26 | |
27 | 27 | |
28 | 28 | cdef class GenericBackend: |
29 | | cpdef int add_variable(self, lower_bound=0.0, upper_bound=None, binary=False, continuous=True, integer=False) except -1: |
| 29 | cpdef int add_variable(self, lower_bound=0.0, upper_bound=None, binary=False, continuous=True, integer=False, obj=0.0, name=None) except -1: |
30 | 30 | """ |
31 | 31 | Add a variable. |
32 | 32 | |
… |
… |
|
45 | 45 | |
46 | 46 | - ``integer`` - ``True`` if the variable is binary (default: ``False``). |
47 | 47 | |
| 48 | - ``obj`` - (optional) coefficient of this variable in the objective function (default: 0.0) |
| 49 | |
| 50 | - ``name`` - an optional name for the newly added variable (default: ``None``). |
| 51 | |
48 | 52 | OUTPUT: The index of the newly created variable |
49 | 53 | |
50 | 54 | EXAMPLE:: |
… |
… |
|
65 | 69 | Traceback (most recent call last): |
66 | 70 | ... |
67 | 71 | ValueError: ... |
| 72 | sage: p.add_variable(name='x',obj=1.0) # optional - Nonexistent_LP_solver |
| 73 | 3 |
| 74 | sage: p.col_name(3) # optional - Nonexistent_LP_solver |
| 75 | 'x' |
| 76 | sage: p.objective_coefficient(3) # optional - Nonexistent_LP_solver |
| 77 | 1.0 |
68 | 78 | """ |
69 | 79 | raise NotImplementedError() |
70 | 80 | |
71 | | cpdef int add_variables(self, int n, lower_bound=0.0, upper_bound=None, binary=False, continuous=True, integer=False) except -1: |
| 81 | cpdef int add_variables(self, int n, lower_bound=0.0, upper_bound=None, binary=False, continuous=True, integer=False, obj=0.0, names=None) except -1: |
72 | 82 | """ |
73 | 83 | Add ``n`` variables. |
74 | 84 | |
… |
… |
|
89 | 99 | |
90 | 100 | - ``integer`` - ``True`` if the variable is binary (default: ``False``). |
91 | 101 | |
| 102 | - ``obj`` - (optional) coefficient of all variables in the objective function (default: 0.0) |
| 103 | |
| 104 | - ``names`` - optional list of names (default: ``None``) |
| 105 | |
92 | 106 | OUTPUT: The index of the variable created last. |
93 | 107 | |
94 | 108 | EXAMPLE:: |
… |
… |
|
101 | 115 | 4 |
102 | 116 | sage: p.ncols() # optional - Nonexistent_LP_solver |
103 | 117 | 5 |
104 | | sage: p.add_variables(2, lower_bound=-2.0, integer=True) # optional - Nonexistent_LP_solver |
| 118 | sage: p.add_variables(2, lower_bound=-2.0, integer=True, names=['a','b']) # optional - Nonexistent_LP_solver |
105 | 119 | 6 |
106 | 120 | """ |
107 | 121 | raise NotImplementedError() |
… |
… |
|
157 | 171 | """ |
158 | 172 | raise NotImplementedError() |
159 | 173 | |
160 | | cpdef set_objective_coefficient(self, int variable, double coeff): |
| 174 | cpdef objective_coefficient(self, int variable, coeff=None): |
161 | 175 | """ |
162 | | Set the coefficient of a variable in the objective function |
| 176 | Set or get the coefficient of a variable in the objective |
| 177 | function |
163 | 178 | |
164 | 179 | INPUT: |
165 | 180 | |
… |
… |
|
173 | 188 | sage: p = get_solver(solver = "Nonexistent_LP_solver") # optional - Nonexistent_LP_solver |
174 | 189 | sage: p.add_variable() # optional - Nonexistent_LP_solver |
175 | 190 | 1 |
176 | | sage: p.get_objective_coefficient(0) # optional - Nonexistent_LP_solver |
| 191 | sage: p.objective_coefficient(0) # optional - Nonexistent_LP_solver |
177 | 192 | 0.0 |
178 | | sage: p.set_objective_coefficient(0,2) # optional - Nonexistent_LP_solver |
179 | | sage: p.get_objective_coefficient(0) # optional - Nonexistent_LP_solver |
| 193 | sage: p.objective_coefficient(0,2) # optional - Nonexistent_LP_solver |
| 194 | sage: p.objective_coefficient(0) # optional - Nonexistent_LP_solver |
180 | 195 | 2.0 |
181 | 196 | """ |
182 | 197 | raise NotImplementedError() |
… |
… |
|
197 | 212 | sage: p.add_variables(5) # optional - Nonexistent_LP_solver |
198 | 213 | 5 |
199 | 214 | sage: p.set_objective([1, 1, 2, 1, 3]) # optional - Nonexistent_LP_solver |
200 | | sage: map(lambda x :p.get_objective_coeffient(x), range(5)) # optional - Nonexistent_LP_solver |
| 215 | sage: map(lambda x :p.objective_coefficient(x), range(5)) # optional - Nonexistent_LP_solver |
201 | 216 | [1.0, 1.0, 2.0, 1.0, 3.0] |
202 | 217 | """ |
203 | 218 | raise NotImplementedError() |
… |
… |
|
218 | 233 | """ |
219 | 234 | raise NotImplementedError() |
220 | 235 | |
221 | | cpdef add_linear_constraint(self, coefficients, lower_bound, upper_bound): |
| 236 | cpdef add_linear_constraint(self, coefficients, lower_bound, upper_bound, name=None): |
222 | 237 | """ |
223 | 238 | Add a linear constraint. |
224 | 239 | |
… |
… |
|
232 | 247 | |
233 | 248 | - ``upper_bound`` - an upper bound, either a real value or ``None`` |
234 | 249 | |
| 250 | - ``name`` - an optional name for this row (default: ``None``) |
| 251 | |
235 | 252 | EXAMPLE:: |
236 | 253 | |
237 | 254 | sage: from sage.numerical.backends.generic_backend import get_solver |
… |
… |
|
243 | 260 | ([4, 3, 2, 1], [4.0, 3.0, 2.0, 1.0]) # optional - Nonexistent_LP_solver |
244 | 261 | sage: p.row_bounds(0) # optional - Nonexistent_LP_solver |
245 | 262 | (2.0, 2.0) |
| 263 | sage: p.add_linear_constraint( zip(range(5), range(5)), 1.0, 1.0, name='foo') # optional - Nonexistent_LP_solver |
| 264 | sage: p.row_name(-1) # optional - Nonexistent_LP_solver |
| 265 | "foo" |
246 | 266 | """ |
247 | 267 | raise NotImplementedError() |
248 | 268 | |
… |
… |
|
282 | 302 | """ |
283 | 303 | raise NotImplementedError() |
284 | 304 | |
285 | | cpdef add_linear_constraints(self, int number, lower_bound, upper_bound): |
| 305 | cpdef add_linear_constraints(self, int number, lower_bound, upper_bound, names=None): |
286 | 306 | """ |
287 | 307 | Add constraints. |
288 | 308 | |
… |
… |
|
294 | 314 | |
295 | 315 | - ``upper_bound`` - an upper bound, either a real value or ``None`` |
296 | 316 | |
| 317 | - ``names`` - an optional list of names (default: ``None``) |
| 318 | |
297 | 319 | EXAMPLE:: |
298 | 320 | |
299 | 321 | sage: from sage.numerical.backends.generic_backend import get_solver |
… |
… |
|
326 | 348 | sage: p.add_col(range(5), range(5)) # optional - Nonexistent_LP_solver |
327 | 349 | sage: p.solve() # optional - Nonexistent_LP_solver |
328 | 350 | 0 |
329 | | sage: p.set_objective_coefficient(0,1) # optional - Nonexistent_LP_solver |
| 351 | sage: p.objective_coefficient(0,1) # optional - Nonexistent_LP_solver |
330 | 352 | sage: p.solve() # optional - Nonexistent_LP_solver |
331 | 353 | Traceback (most recent call last): |
332 | 354 | ... |
… |
… |
|
530 | 552 | """ |
531 | 553 | raise NotImplementedError() |
532 | 554 | |
533 | | cpdef double get_objective_coefficient(self, int i): |
534 | | """ |
535 | | Set the coefficient of a variable in the objective function |
536 | | |
537 | | INPUT: |
538 | | |
539 | | - ``variable`` (integer) -- the variable's id |
540 | | |
541 | | - ``coeff`` (double) -- its coefficient |
542 | | |
543 | | EXAMPLE:: |
544 | | |
545 | | sage: from sage.numerical.backends.generic_backend import get_solver |
546 | | sage: p = get_solver(solver = "Nonexistent_LP_solver") # optional - Nonexistent_LP_solver |
547 | | sage: p.add_variable() # optional - Nonexistent_LP_solver |
548 | | 1 |
549 | | sage: p.get_objective_coeffient(0) # optional - Nonexistent_LP_solver |
550 | | 0.0 |
551 | | sage: p.set_objective_coefficient(0,2) # optional - Nonexistent_LP_solver |
552 | | sage: p.get_objective_coeffient(0) # optional - Nonexistent_LP_solver |
553 | | 2.0 |
554 | | """ |
555 | | raise NotImplementedError() |
556 | | |
557 | 555 | cpdef row_bounds(self, int index): |
558 | 556 | """ |
559 | 557 | Return the bounds of a specific constraint. |
… |
… |
|
680 | 678 | """ |
681 | 679 | raise NotImplementedError() |
682 | 680 | |
683 | | cpdef row_name(self, int index, char * name = NULL): |
| 681 | cpdef row_name(self, int index): |
684 | 682 | """ |
685 | | Return or define the ``index`` th row name |
| 683 | Return the ``index`` th row name |
686 | 684 | |
687 | 685 | INPUT: |
688 | 686 | |
689 | 687 | - ``index`` (integer) -- the row's id |
690 | 688 | |
691 | | - ``name`` (``char *``) -- its name. When set to ``NULL`` |
692 | | (default), the method returns the current name. |
693 | | |
694 | 689 | EXAMPLE:: |
695 | 690 | |
696 | 691 | sage: from sage.numerical.backends.generic_backend import get_solver |
697 | 692 | sage: p = get_solver(solver = "Nonexistent_LP_solver") # optional - Nonexistent_LP_solver |
698 | | sage: p.add_linear_constraints(1, 2, None) # optional - Nonexistent_LP_solver |
699 | | sage: p.row_name(0, "Empty constraint 1") # optional - Nonexistent_LP_solver |
700 | | sage: p.row_name(0) # optional - Nonexistent_LP_solver |
| 693 | sage: p.add_linear_constraints(1, 2, None, name="Empty constraint 1") # optional - Nonexistent_LP_solver |
| 694 | sage: p.row_name(0) # optional - Nonexistent_LP_solver |
701 | 695 | 'Empty constraint 1' |
702 | 696 | |
703 | 697 | """ |
704 | 698 | raise NotImplementedError() |
705 | 699 | |
706 | | cpdef col_name(self, int index, char * name = NULL): |
| 700 | cpdef col_name(self, int index): |
707 | 701 | """ |
708 | | Return or define the ``index`` th col name |
| 702 | Return the ``index`` th col name |
709 | 703 | |
710 | 704 | INPUT: |
711 | 705 | |
… |
… |
|
718 | 712 | |
719 | 713 | sage: from sage.numerical.backends.generic_backend import get_solver |
720 | 714 | sage: p = get_solver(solver = "Nonexistent_LP_solver") # optional - Nonexistent_LP_solver |
721 | | sage: p.add_variable() # optional - Nonexistent_LP_solver |
| 715 | sage: p.add_variable(name="I am a variable") # optional - Nonexistent_LP_solver |
722 | 716 | 1 |
723 | | sage: p.col_name(0, "I am a variable") # optional - Nonexistent_LP_solver |
724 | | sage: p.col_name(0) # optional - Nonexistent_LP_solver |
| 717 | sage: p.col_name(0) # optional - Nonexistent_LP_solver |
725 | 718 | 'I am a variable' |
726 | 719 | """ |
727 | 720 | raise NotImplementedError() |
diff -r 941d42780156 -r cf07c22147a7 sage/numerical/backends/glpk_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 | |
42 | 42 | This amounts to adding a new column to the matrix. By default, |
43 | | the variable is both positive and real. |
| 43 | the variable is both positive, real and the coefficient in the |
| 44 | objective function is 0.0. |
44 | 45 | |
45 | 46 | INPUT: |
46 | 47 | |
… |
… |
|
54 | 55 | |
55 | 56 | - ``integer`` - ``True`` if the variable is binary (default: ``False``). |
56 | 57 | |
| 58 | - ``obj`` - (optional) coefficient of this variable in the objective function (default: 0.0) |
| 59 | |
| 60 | - ``name`` - an optional name for the newly added variable (default: ``None``). |
| 61 | |
57 | 62 | OUTPUT: The index of the newly created variable |
58 | 63 | |
59 | 64 | EXAMPLE:: |
… |
… |
|
74 | 79 | Traceback (most recent call last): |
75 | 80 | ... |
76 | 81 | ValueError: ... |
| 82 | sage: p.add_variable(name='x',obj=1.0) |
| 83 | 3 |
| 84 | sage: p.col_name(3) |
| 85 | 'x' |
| 86 | sage: p.objective_coefficient(3) |
| 87 | 1.0 |
77 | 88 | """ |
78 | 89 | cdef int vtype = int(bool(binary)) + int(bool(continuous)) + int(bool(integer)) |
79 | 90 | if vtype == 0: |
… |
… |
|
93 | 104 | glp_set_col_kind(self.lp, n_var, GLP_BV) |
94 | 105 | elif integer: |
95 | 106 | glp_set_col_kind(self.lp, n_var, GLP_IV) |
| 107 | |
| 108 | if name is not None: |
| 109 | glp_set_col_name(self.lp, n_var, name) |
96 | 110 | |
| 111 | if obj: |
| 112 | self.objective_coefficient(n_var - 1, obj) |
| 113 | |
97 | 114 | return n_var - 1 |
98 | 115 | |
99 | | cpdef int add_variables(self, int number, lower_bound=0.0, upper_bound=None, binary=False, continuous=False, integer=False) except -1: |
| 116 | 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: |
100 | 117 | """ |
101 | 118 | Add ``number`` new variables. |
102 | 119 | |
103 | 120 | This amounts to adding new columns to the matrix. By default, |
104 | | the variables are both positive and real. |
| 121 | the variables are both positive, real and theor coefficient in |
| 122 | the objective function is 0.0. |
105 | 123 | |
106 | 124 | INPUT: |
107 | 125 | |
… |
… |
|
117 | 135 | |
118 | 136 | - ``integer`` - ``True`` if the variable is binary (default: ``False``). |
119 | 137 | |
| 138 | - ``obj`` - (optional) coefficient of all variables in the objective function (default: 0.0) |
| 139 | |
| 140 | - ``names`` - optional list of names (default: ``None``) |
| 141 | |
120 | 142 | OUTPUT: The index of the variable created last. |
121 | 143 | |
122 | 144 | EXAMPLE:: |
… |
… |
|
129 | 151 | 4 |
130 | 152 | sage: p.ncols() |
131 | 153 | 5 |
132 | | sage: p.add_variables(2, lower_bound=-2.0, integer=True) |
| 154 | sage: p.add_variables(2, lower_bound=-2.0, integer=True, names=['a','b']) |
133 | 155 | 6 |
134 | 156 | """ |
135 | 157 | cdef int vtype = int(bool(binary)) + int(bool(continuous)) + int(bool(integer)) |
… |
… |
|
154 | 176 | glp_set_col_kind(self.lp, n_var - i, GLP_BV) |
155 | 177 | elif integer: |
156 | 178 | glp_set_col_kind(self.lp, n_var - i, GLP_IV) |
| 179 | |
| 180 | if obj: |
| 181 | self.bjective_coefficient(n_var - i - 1, obj) |
| 182 | |
| 183 | if names is not None: |
| 184 | glp_set_col_name(self.lp, n_var, names[number - i - 1]) |
| 185 | |
157 | 186 | return n_var - 1 |
158 | 187 | |
159 | 188 | cpdef set_variable_type(self, int variable, int vtype): |
… |
… |
|
218 | 247 | else: |
219 | 248 | glp_set_obj_dir(self.lp, GLP_MIN) |
220 | 249 | |
221 | | cpdef set_objective_coefficient(self, int variable, double coeff): |
| 250 | cpdef objective_coefficient(self, int variable, coeff=None): |
222 | 251 | """ |
223 | | Set the coefficient of a variable in the objective function |
| 252 | Set or get the coefficient of a variable in the objective function |
224 | 253 | |
225 | 254 | INPUT: |
226 | 255 | |
227 | 256 | - ``variable`` (integer) -- the variable's id |
228 | 257 | |
229 | | - ``coeff`` (double) -- its coefficient |
| 258 | - ``coeff`` (double) -- its coefficient or ``None`` for |
| 259 | reading (default: ``None``) |
230 | 260 | |
231 | 261 | EXAMPLE:: |
232 | 262 | |
… |
… |
|
234 | 264 | sage: p = get_solver(solver = "GLPK") |
235 | 265 | sage: p.add_variable() |
236 | 266 | 0 |
237 | | sage: p.get_objective_coefficient(0) |
| 267 | sage: p.objective_coefficient(0) |
238 | 268 | 0.0 |
239 | | sage: p.set_objective_coefficient(0,2) |
240 | | sage: p.get_objective_coefficient(0) |
| 269 | sage: p.objective_coefficient(0,2) |
| 270 | sage: p.objective_coefficient(0) |
241 | 271 | 2.0 |
242 | 272 | """ |
243 | | glp_set_obj_coef(self.lp, variable + 1, coeff) |
| 273 | if coeff is None: |
| 274 | return glp_get_obj_coef(self.lp, variable + 1) |
| 275 | else: |
| 276 | glp_set_obj_coef(self.lp, variable + 1, coeff) |
244 | 277 | |
245 | 278 | cpdef problem_name(self, char * name = NULL): |
246 | 279 | """ |
… |
… |
|
287 | 320 | sage: p.add_variables(5) |
288 | 321 | 4 |
289 | 322 | sage: p.set_objective([1, 1, 2, 1, 3]) |
290 | | sage: map(lambda x :p.get_objective_coefficient(x), range(5)) |
| 323 | sage: map(lambda x :p.objective_coefficient(x), range(5)) |
291 | 324 | [1.0, 1.0, 2.0, 1.0, 3.0] |
292 | 325 | """ |
293 | 326 | cdef int i |
… |
… |
|
319 | 352 | else: |
320 | 353 | self.iocp.msg_lev = GLP_MSG_ALL |
321 | 354 | |
322 | | cpdef add_linear_constraint(self, coefficients, lower_bound, upper_bound): |
| 355 | cpdef add_linear_constraint(self, coefficients, lower_bound, upper_bound, name=None): |
323 | 356 | """ |
324 | 357 | Add a linear constraint. |
325 | 358 | |
… |
… |
|
332 | 365 | - ``lower_bound`` - a lower bound, either a real value or ``None`` |
333 | 366 | |
334 | 367 | - ``upper_bound`` - an upper bound, either a real value or ``None`` |
| 368 | |
| 369 | - ``name`` - an optional name for this row (default: ``None``) |
335 | 370 | |
336 | 371 | EXAMPLE:: |
337 | 372 | |
… |
… |
|
344 | 379 | ([4, 3, 2, 1], [4.0, 3.0, 2.0, 1.0]) |
345 | 380 | sage: p.row_bounds(0) |
346 | 381 | (2.0, 2.0) |
| 382 | sage: p.add_linear_constraint( zip(range(5), range(5)), 1.0, 1.0, name='foo') |
| 383 | sage: p.row_name(1) |
| 384 | 'foo' |
347 | 385 | """ |
348 | 386 | if lower_bound is None and upper_bound is None: |
349 | 387 | raise ValueError("At least one of 'upper_bound' or 'lower_bound' must be set.") |
… |
… |
|
373 | 411 | else: |
374 | 412 | glp_set_row_bnds(self.lp, n, GLP_DB, lower_bound, upper_bound) |
375 | 413 | |
| 414 | if name is not None: |
| 415 | glp_set_row_name(self.lp, n, name) |
| 416 | |
376 | 417 | sage_free(row_i) |
377 | 418 | sage_free(row_values) |
378 | 419 | |
379 | | cpdef add_linear_constraints(self, int number, lower_bound, upper_bound): |
| 420 | cpdef add_linear_constraints(self, int number, lower_bound, upper_bound, names=None): |
380 | 421 | """ |
381 | 422 | Add ``'number`` linear constraints. |
382 | 423 | |
… |
… |
|
388 | 429 | |
389 | 430 | - ``upper_bound`` - an upper bound, either a real value or ``None`` |
390 | 431 | |
| 432 | - ``names`` - an optional list of names (default: ``None``) |
| 433 | |
391 | 434 | EXAMPLE:: |
392 | 435 | |
393 | 436 | sage: from sage.numerical.backends.generic_backend import get_solver |
… |
… |
|
399 | 442 | ([], []) |
400 | 443 | sage: p.row_bounds(4) |
401 | 444 | (None, 2.0) |
| 445 | sage: p.add_linear_constraints(2, None, 2, names=['foo','bar']) |
402 | 446 | """ |
403 | 447 | if lower_bound is None and upper_bound is None: |
404 | 448 | raise ValueError("At least one of 'upper_bound' or 'lower_bound' must be set.") |
… |
… |
|
417 | 461 | glp_set_row_bnds(self.lp, n-i, GLP_FX, lower_bound, upper_bound) |
418 | 462 | else: |
419 | 463 | glp_set_row_bnds(self.lp, n-i, GLP_DB, lower_bound, upper_bound) |
| 464 | if names is not None: |
| 465 | glp_set_row_name(self.lp, n-i, names[number-i-1]) |
420 | 466 | |
421 | 467 | cpdef row(self, int index): |
422 | 468 | r""" |
… |
… |
|
537 | 583 | (ub if ub != +DBL_MAX else None) |
538 | 584 | ) |
539 | 585 | |
540 | | cpdef double get_objective_coefficient(self, int index): |
541 | | """ |
542 | | Return the coefficient of a variable in the objective |
543 | | function. |
544 | | |
545 | | INPUT: |
546 | | |
547 | | - ``index`` (integer) -- the variable's id. |
548 | | |
549 | | EXAMPLE:: |
550 | | |
551 | | sage: from sage.numerical.backends.generic_backend import get_solver |
552 | | sage: p = get_solver(solver = "GLPK") |
553 | | sage: p.add_variable() |
554 | | 0 |
555 | | sage: p.get_objective_coefficient(0) |
556 | | 0.0 |
557 | | sage: p.set_objective_coefficient(0,2) |
558 | | sage: p.get_objective_coefficient(0) |
559 | | 2.0 |
560 | | """ |
561 | | return glp_get_obj_coef(self.lp, index + 1) |
562 | | |
563 | | |
564 | 586 | cpdef add_col(self, list indices, list coeffs): |
565 | 587 | """ |
566 | 588 | Add a column. |
… |
… |
|
632 | 654 | sage: p.add_col(range(5), range(5)) |
633 | 655 | sage: p.solve() |
634 | 656 | 0 |
635 | | sage: p.set_objective_coefficient(0,1) |
| 657 | sage: p.objective_coefficient(0,1) |
636 | 658 | sage: p.solve() |
637 | 659 | Traceback (most recent call last): |
638 | 660 | ... |
… |
… |
|
741 | 763 | |
742 | 764 | return glp_get_num_rows(self.lp) |
743 | 765 | |
744 | | cpdef col_name(self, int index, char * name = NULL): |
| 766 | cpdef col_name(self, int index): |
745 | 767 | """ |
746 | | Return or define the ``index`` th col name |
| 768 | Return the ``index`` th col name |
747 | 769 | |
748 | 770 | INPUT: |
749 | 771 | |
750 | 772 | - ``index`` (integer) -- the col's id |
751 | 773 | |
752 | | - ``name`` (``char *``) -- its name. When set to ``NULL`` |
753 | | (default), the method returns the current name. |
| 774 | EXAMPLE:: |
| 775 | |
| 776 | sage: from sage.numerical.backends.generic_backend import get_solver |
| 777 | sage: p = get_solver(solver = "GLPK") |
| 778 | sage: p.add_variable(name='I am a variable') |
| 779 | 0 |
| 780 | sage: p.col_name(0) |
| 781 | 'I am a variable' |
| 782 | """ |
| 783 | cdef char * s |
| 784 | |
| 785 | glp_create_index(self.lp) |
| 786 | s = <char*> glp_get_col_name(self.lp, index + 1) |
| 787 | |
| 788 | if s != NULL: |
| 789 | return s |
| 790 | else: |
| 791 | return "" |
| 792 | |
| 793 | cpdef row_name(self, int index): |
| 794 | """ |
| 795 | Return the ``index`` th row name |
| 796 | |
| 797 | INPUT: |
| 798 | |
| 799 | - ``index`` (integer) -- the row's id |
754 | 800 | |
755 | 801 | EXAMPLE:: |
756 | 802 | |
757 | 803 | sage: from sage.numerical.backends.generic_backend import get_solver |
758 | 804 | sage: p = get_solver(solver = "GLPK") |
759 | | sage: p.add_variable() |
760 | | 0 |
761 | | sage: p.col_name(0, "I am a variable") |
762 | | sage: p.col_name(0) |
763 | | 'I am a variable' |
764 | | """ |
765 | | cdef char * s |
766 | | |
767 | | if name == NULL: |
768 | | glp_create_index(self.lp) |
769 | | s = <char*> glp_get_col_name(self.lp, index + 1) |
770 | | |
771 | | if s != NULL: |
772 | | return s |
773 | | else: |
774 | | return "" |
775 | | else: |
776 | | glp_set_col_name(self.lp, index + 1, name) |
777 | | |
778 | | cpdef row_name(self, int index, char * name = NULL): |
779 | | """ |
780 | | Return or define the ``index`` th row name |
781 | | |
782 | | INPUT: |
783 | | |
784 | | - ``index`` (integer) -- the row's id |
785 | | |
786 | | - ``name`` (``char *``) -- its name. When set to ``NULL`` |
787 | | (default), the method returns the current name. |
788 | | |
789 | | EXAMPLE:: |
790 | | |
791 | | sage: from sage.numerical.backends.generic_backend import get_solver |
792 | | sage: p = get_solver(solver = "GLPK") |
793 | | sage: p.add_linear_constraints(1, 2, None) |
794 | | sage: p.row_name(0, "Empty constraint 1") |
| 805 | sage: p.add_linear_constraints(1, 2, None, names=['Empty constraint 1']) |
795 | 806 | sage: p.row_name(0) |
796 | 807 | 'Empty constraint 1' |
797 | 808 | """ |
798 | 809 | cdef char * s |
799 | 810 | |
800 | | if name == NULL: |
801 | | glp_create_index(self.lp) |
802 | | s = <char*> glp_get_row_name(self.lp, index + 1) |
| 811 | glp_create_index(self.lp) |
| 812 | s = <char*> glp_get_row_name(self.lp, index + 1) |
803 | 813 | |
804 | | if s != NULL: |
805 | | return s |
806 | | else: |
807 | | return "" |
| 814 | if s != NULL: |
| 815 | return s |
808 | 816 | else: |
809 | | glp_set_row_name(self.lp, index + 1, name) |
| 817 | return "" |
810 | 818 | |
811 | 819 | cpdef bint is_variable_binary(self, int index): |
812 | 820 | """ |
diff -r 941d42780156 -r cf07c22147a7 sage/numerical/mip.pyx
a
|
b
|
|
274 | 274 | |
275 | 275 | self._backend.problem_name(name) |
276 | 276 | |
277 | | def _update_variables_name(self): |
278 | | r""" |
279 | | Updates the names of the variables. |
| 277 | # def _update_variables_name(self): |
| 278 | # r""" |
| 279 | # Updates the names of the variables. |
280 | 280 | |
281 | | Only called before writing the Problem to a MPS or LP file. |
| 281 | # Only called before writing the Problem to a MPS or LP file. |
282 | 282 | |
283 | | EXAMPLE:: |
| 283 | # EXAMPLE:: |
284 | 284 | |
285 | | sage: p=MixedIntegerLinearProgram() |
286 | | sage: v=p.new_variable(name="Test") |
287 | | sage: v[5]+v[99] |
288 | | x_0 +x_1 |
289 | | sage: p._update_variables_name() |
290 | | """ |
| 285 | # sage: p=MixedIntegerLinearProgram() |
| 286 | # sage: v=p.new_variable(name="Test") |
| 287 | # sage: v[5]+v[99] |
| 288 | # x_0 +x_1 |
| 289 | # sage: p._update_variables_name() |
| 290 | # """ |
291 | 291 | |
292 | | for v in self._mipvariables: |
293 | | v._update_variables_name() |
| 292 | # for v in self._mipvariables: |
| 293 | # v._update_variables_name() |
294 | 294 | |
295 | 295 | |
296 | 296 | def new_variable(self, real=False, binary=False, integer=False, dim=1,name=None): |
… |
… |
|
386 | 386 | cdef int i, j |
387 | 387 | cdef double c |
388 | 388 | cdef GenericBackend b = self._backend |
389 | | self._update_variables_name() |
| 389 | #self._update_variables_name() |
390 | 390 | |
391 | 391 | inv_variables = [0]*len(self._variables) |
392 | 392 | for (v,id) in self._variables.iteritems(): |
… |
… |
|
399 | 399 | |
400 | 400 | first = True |
401 | 401 | for 0<= i< b.ncols(): |
402 | | c = b.get_objective_coefficient(i) |
| 402 | c = b.objective_coefficient(i) |
403 | 403 | if c != 0: |
404 | 404 | |
405 | 405 | value+= ((" +" if (not first and c>0) else " ") + |
406 | | str(inv_variables[i]*b.get_objective_coefficient(i)) |
| 406 | str(inv_variables[i]*b.objective_coefficient(i)) |
407 | 407 | ) |
408 | 408 | first = False |
409 | 409 | |
… |
… |
|
729 | 729 | sage: b = p.new_variable() |
730 | 730 | sage: p.add_constraint( b[8] - b[15] <= 3*b[8] + 9) |
731 | 731 | sage: p.show() |
732 | | |
| 732 | Maximization: |
| 733 | <BLANKLINE> |
| 734 | Constraints: |
| 735 | -2.0 x_0 -1.0 x_1 <= 9.0 |
| 736 | Variables: |
| 737 | x_0 is a real variable (min=0.0, max=+oo) |
| 738 | x_1 is a real variable (min=0.0, max=+oo) |
| 739 | |
733 | 740 | Empty constraint:: |
734 | 741 | |
735 | 742 | sage: p=MixedIntegerLinearProgram() |
… |
… |
|
776 | 783 | if min == None and max == None: |
777 | 784 | raise ValueError("Both max and min are set to None ? Weird!") |
778 | 785 | |
779 | | self._backend.add_linear_constraint(C, min, max) |
780 | | |
781 | | if name != None: |
782 | | self._backend.row_name(self._backend.nrows()-1,name) |
| 786 | self._backend.add_linear_constraint(C, min, max, name) |
783 | 787 | |
784 | 788 | elif isinstance(linear_function,LinearConstraint): |
785 | 789 | functions = linear_function.constraints |
… |
… |
|
1335 | 1339 | self._dict[i] = MIPVariable(self._p, self._vtype, dim=self._dim-1) |
1336 | 1340 | return self._dict[i] |
1337 | 1341 | |
1338 | | def _update_variables_name(self, prefix=None): |
1339 | | r""" |
1340 | | Updates the names of the variables in the parent instant of ``MixedIntegerLinearProgram``. |
| 1342 | # def _update_variables_name(self, prefix=None): |
| 1343 | # r""" |
| 1344 | # Updates the names of the variables in the parent instant of ``MixedIntegerLinearProgram``. |
1341 | 1345 | |
1342 | | Only called before writing the Problem to a MPS or LP file. |
| 1346 | # Only called before writing the Problem to a MPS or LP file. |
1343 | 1347 | |
1344 | | EXAMPLE:: |
| 1348 | # EXAMPLE:: |
1345 | 1349 | |
1346 | | sage: p=MixedIntegerLinearProgram() |
1347 | | sage: v=p.new_variable(name="Test") |
1348 | | sage: v[5]+v[99] |
1349 | | x_0 +x_1 |
1350 | | sage: v._update_variables_name() |
1351 | | """ |
| 1350 | # sage: p=MixedIntegerLinearProgram() |
| 1351 | # sage: v=p.new_variable(name="Test") |
| 1352 | # sage: v[5]+v[99] |
| 1353 | # x_0 +x_1 |
| 1354 | # sage: v._update_variables_name() |
| 1355 | # """ |
1352 | 1356 | |
1353 | | if prefix == None: |
1354 | | prefix = self._name |
| 1357 | # if prefix == None: |
| 1358 | # prefix = self._name |
1355 | 1359 | |
1356 | | if self._dim == 1: |
1357 | | for (k,v) in self._dict.iteritems(): |
1358 | | name = prefix + "[" + str(k) + "]" |
1359 | | self._p._backend.col_name(self._p._variables[v], name) |
1360 | | #self._p._variables_name[self._p._variables[v]]=prefix + "[" + str(k) + "]" |
1361 | | else: |
1362 | | for v in self._dict.itervalues(): |
1363 | | v._update_variables_name(prefix=prefix + "(" + str(k) + ")") |
1364 | | |
1365 | | |
| 1360 | # if self._dim == 1: |
| 1361 | # for (k,v) in self._dict.iteritems(): |
| 1362 | # name = prefix + "[" + str(k) + "]" |
| 1363 | # self._p._backend.col_name(self._p._variables[v], name) |
| 1364 | # #self._p._variables_name[self._p._variables[v]]=prefix + "[" + str(k) + "]" |
| 1365 | # else: |
| 1366 | # for v in self._dict.itervalues(): |
| 1367 | # v._update_variables_name(prefix=prefix + "(" + str(k) + ")") |
| 1368 | |
1366 | 1369 | |
1367 | 1370 | def __repr__(self): |
1368 | 1371 | r""" |