@@ -65,7 +65,7 @@ A mixed integer linear program can give you an answer:
65
65
The following example shows all these steps::
66
66
67
67
sage: p = MixedIntegerLinearProgram( maximization=False, solver='GLPK')
68
- sage: w = p. new_variable( integer=True, nonnegative=True)
68
+ sage: w = p. new_variable( integer=True, nonnegative=True, name='w' )
69
69
sage: p. add_constraint( w[0 ] + w[1 ] + w[2 ] - 14* w[3 ] == 0)
70
70
sage: p. add_constraint( w[1 ] + 2* w[2 ] - 8* w[3 ] == 0)
71
71
sage: p. add_constraint( 2* w[2 ] - 3* w[3 ] == 0)
@@ -74,18 +74,18 @@ The following example shows all these steps::
74
74
sage: p. set_objective( w[3 ])
75
75
sage: p. show( )
76
76
Minimization:
77
- x_3
77
+ w[ 3 ]
78
78
Constraints:
79
- 0. 0 <= x_0 + x_1 + x_2 - 14. 0 x_3 <= 0. 0
80
- 0. 0 <= x_1 + 2. 0 x_2 - 8. 0 x_3 <= 0. 0
81
- 0. 0 <= 2. 0 x_2 - 3. 0 x_3 <= 0. 0
82
- - x_0 + x_1 + x_2 <= 0. 0
83
- - x_3 <= -1. 0
79
+ 0. 0 <= w[ 0 ] + w[ 1 ] + w[ 2 ] - 14. 0 w[ 3 ] <= 0. 0
80
+ 0. 0 <= w[ 1 ] + 2. 0 w[ 2 ] - 8. 0 w[ 3 ] <= 0. 0
81
+ 0. 0 <= 2. 0 w[ 2 ] - 3. 0 w[ 3 ] <= 0. 0
82
+ - w[ 0 ] + w[ 1 ] + w[ 2 ] <= 0. 0
83
+ - w[ 3 ] <= -1. 0
84
84
Variables:
85
- x_0 is an integer variable ( min=0. 0, max=+ oo)
86
- x_1 is an integer variable ( min=0. 0, max=+ oo)
87
- x_2 is an integer variable ( min=0. 0, max=+ oo)
88
- x_3 is an integer variable ( min=0. 0, max=+ oo)
85
+ w[ 0 ] = x_0 is an integer variable ( min=0. 0, max=+ oo)
86
+ w[ 1 ] = x_1 is an integer variable ( min=0. 0, max=+ oo)
87
+ w[ 2 ] = x_2 is an integer variable ( min=0. 0, max=+ oo)
88
+ w[ 3 ] = x_3 is an integer variable ( min=0. 0, max=+ oo)
89
89
sage: print( 'Objective Value: {}'. format( p. solve( )))
90
90
Objective Value: 2. 0
91
91
sage: for i, v in sorted( p. get_values( w, convert=ZZ, tolerance=1e-3) . items( )) :
@@ -95,6 +95,34 @@ The following example shows all these steps::
95
95
w_2 = 3
96
96
w_3 = 2
97
97
98
+ If your problem is already in the standard form, for example::
99
+
100
+ sage: A = matrix( [[1, 2 ], [3, 5 ]])
101
+ sage: b = vector( [7, 11 ])
102
+ sage: c = vector( [5, 9 ])
103
+
104
+ You can add the constraint by treating the variable dictionary as a vector::
105
+
106
+ sage: p = MixedIntegerLinearProgram( maximization=True, solver='GLPK')
107
+ sage: w = p. new_variable( integer=True, name='w')
108
+ sage: p. add_constraint( A * w <= b)
109
+ sage: p. set_objective(( c. row( ) * w) [0 ])
110
+ sage: p. show( )
111
+ Maximization:
112
+ 5. 0 w[0 ] + 9. 0 w[1 ]
113
+ Constraints:
114
+ w[0 ] + 2. 0 w[1 ] <= 7. 0
115
+ 3. 0 w[0 ] + 5. 0 w[1 ] <= 11. 0
116
+ Variables:
117
+ w[0 ] = x_0 is an integer variable ( min=-oo, max=+ oo)
118
+ w[1 ] = x_1 is an integer variable ( min=-oo, max=+ oo)
119
+ sage: print( 'Objective Value: {}'. format( p. solve( )))
120
+ Objective Value: 25. 0
121
+ sage: for i, v in sorted( p. get_values( w, convert=ZZ, tolerance=1e-3) . items( )) :
122
+ .... : print( f'w_{i} = {v}')
123
+ w_0 = -13
124
+ w_1 = 10
125
+
98
126
Different backends compute with different base fields, for example::
99
127
100
128
sage: p = MixedIntegerLinearProgram( solver='GLPK')
@@ -144,8 +172,6 @@ also allowed::
144
172
sage: a[4, 'string', QQ ] - 7* b[2 ]
145
173
x_2 - 7* x_3
146
174
sage: mip. show( )
147
- Maximization:
148
- <BLANKLINE>
149
175
Constraints:
150
176
Variables:
151
177
a[1 ] = x_0 is a continuous variable ( min=-oo, max=+ oo)
@@ -778,8 +804,6 @@ cdef class MixedIntegerLinearProgram(SageObject):
778
804
sage: p. add_constraint( x[0 ] + x[3 ] <= 8)
779
805
sage: p. add_constraint( y[0 ] >= y[1 ])
780
806
sage: p. show( )
781
- Maximization:
782
- <BLANKLINE>
783
807
Constraints:
784
808
x_0 + x_1 <= 8. 0
785
809
- x_2 + x_3 <= 0. 0
@@ -795,8 +819,6 @@ cdef class MixedIntegerLinearProgram(SageObject):
795
819
sage: mip. <x, y, z> = MixedIntegerLinearProgram( solver='GLPK')
796
820
sage: mip. add_constraint( x[0 ] + y[1 ] + z[2 ] <= 10)
797
821
sage: mip. show( )
798
- Maximization:
799
- <BLANKLINE>
800
822
Constraints:
801
823
x[0 ] + y[1 ] + z[2 ] <= 10. 0
802
824
Variables:
@@ -860,8 +882,6 @@ cdef class MixedIntegerLinearProgram(SageObject):
860
882
sage: a[0] + b[2]
861
883
x_0 + x_1
862
884
sage: mip.show()
863
- Maximization:
864
- <BLANKLINE>
865
885
Constraints:
866
886
Variables:
867
887
a[0] = x_0 is a continuous variable (min=-oo, max=+oo)
@@ -1251,23 +1271,24 @@ cdef class MixedIntegerLinearProgram(SageObject):
1251
1271
varid_explainer[i] = varid_name[i] = default_name
1252
1272
1253
1273
# #### Sense and objective function
1254
- print (" Maximization:" if b.is_maximization() else " Minimization:" )
1255
- print (" " , end= " " )
1274
+ formula_parts = []
1256
1275
first = True
1257
1276
for 0 <= i< b.ncols():
1258
1277
c = b.objective_coefficient(i)
1259
1278
if c == 0 :
1260
1279
continue
1261
- print ( ((" + " if (not first and c> 0 ) else " " ) +
1262
- (" " if c == 1 else (" - " if c == - 1 else str (c)+ " " ))+ varid_name[i]
1263
- ), end = " " )
1280
+ formula_parts.append ((" + " if (not first and c> 0 ) else " " ) +
1281
+ (" " if c == 1 else (" - " if c == - 1 else str (c)+ " " ))+ varid_name[i]
1282
+ )
1264
1283
first = False
1265
1284
d = b.objective_constant_term()
1266
1285
if d > self ._backend.zero():
1267
- print (" + {} " .format(d))
1286
+ formula_parts.append (" + {}" .format(d))
1268
1287
elif d < self ._backend.zero():
1269
- print (" - {} " .format(- d))
1270
- print (" \n " )
1288
+ formula_parts.append(" - {}" .format(- d))
1289
+ if formula_parts:
1290
+ print (" Maximization:" if b.is_maximization() else " Minimization:" )
1291
+ print (" " + " " .join(formula_parts))
1271
1292
1272
1293
# #### Constraints
1273
1294
print (" Constraints:" )
@@ -2001,8 +2022,6 @@ cdef class MixedIntegerLinearProgram(SageObject):
2001
2022
sage: b = p. new_variable( nonnegative=True)
2002
2023
sage: p. add_constraint( b[8 ] - b[15 ] <= 3* b[8 ] + 9)
2003
2024
sage: p. show( )
2004
- Maximization:
2005
- <BLANKLINE>
2006
2025
Constraints:
2007
2026
-2. 0 x_0 - x_1 <= 9. 0
2008
2027
Variables:
@@ -2043,8 +2062,6 @@ cdef class MixedIntegerLinearProgram(SageObject):
2043
2062
sage: for each in range( 10) :
2044
2063
.... : lp. add_constraint( lp[0 ]-lp[1 ], min=1)
2045
2064
sage: lp. show( )
2046
- Maximization:
2047
- <BLANKLINE>
2048
2065
Constraints:
2049
2066
1. 0 <= x_0 - x_1
2050
2067
Variables:
@@ -2056,8 +2073,6 @@ cdef class MixedIntegerLinearProgram(SageObject):
2056
2073
sage: for each in range( 10) :
2057
2074
.... : lp. add_constraint( 2* lp[0 ] - 2* lp[1 ], min=2)
2058
2075
sage: lp. show( )
2059
- Maximization:
2060
- <BLANKLINE>
2061
2076
Constraints:
2062
2077
1. 0 <= x_0 - x_1
2063
2078
Variables:
@@ -2069,8 +2084,6 @@ cdef class MixedIntegerLinearProgram(SageObject):
2069
2084
sage: for each in range( 10) :
2070
2085
.... : lp. add_constraint( -2* lp[0 ] + 2* lp[1 ], min=-2)
2071
2086
sage: lp. show( )
2072
- Maximization:
2073
- <BLANKLINE>
2074
2087
Constraints:
2075
2088
1. 0 <= x_0 - x_1
2076
2089
-2. 0 <= -2. 0 x_0 + 2. 0 x_1
@@ -2268,17 +2281,13 @@ cdef class MixedIntegerLinearProgram(SageObject):
2268
2281
sage: p. add_constraint( x - y, max=0)
2269
2282
sage: p. add_constraint( x, max=4)
2270
2283
sage: p. show( )
2271
- Maximization:
2272
- <BLANKLINE>
2273
2284
Constraints:
2274
2285
x_0 + x_1 <= 10. 0
2275
2286
x_0 - x_1 <= 0. 0
2276
2287
x_0 <= 4. 0
2277
2288
...
2278
2289
sage: p. remove_constraint( 1)
2279
2290
sage: p. show( )
2280
- Maximization:
2281
- <BLANKLINE>
2282
2291
Constraints:
2283
2292
x_0 + x_1 <= 10. 0
2284
2293
x_0 <= 4. 0
@@ -2306,17 +2315,13 @@ cdef class MixedIntegerLinearProgram(SageObject):
2306
2315
sage: p. add_constraint( x - y, max=0)
2307
2316
sage: p. add_constraint( x, max=4)
2308
2317
sage: p. show( )
2309
- Maximization:
2310
- <BLANKLINE>
2311
2318
Constraints:
2312
2319
x_0 + x_1 <= 10. 0
2313
2320
x_0 - x_1 <= 0. 0
2314
2321
x_0 <= 4. 0
2315
2322
...
2316
2323
sage: p. remove_constraints( [0, 1 ])
2317
2324
sage: p. show( )
2318
- Maximization:
2319
- <BLANKLINE>
2320
2325
Constraints:
2321
2326
x_0 <= 4. 0
2322
2327
...
@@ -3627,8 +3632,19 @@ cdef class MIPVariable(FiniteFamily):
3627
3632
(1, 1/2)*x_0 + (2/3, 3/4)*x_1
3628
3633
sage: m * v
3629
3634
(1, 2/3)*x_0 + (1/2, 3/4)*x_1
3635
+
3636
+ sage: c = vector([1, 2])
3637
+ sage: v * c
3638
+ x_0 + 2*x_1
3639
+ sage: c * v
3640
+ x_0 + 2*x_1
3630
3641
"""
3642
+ from sage.structure.element import Vector
3643
+ if isinstance (left, Vector):
3644
+ left, right = right, left
3631
3645
if isinstance (left, MIPVariable):
3646
+ if isinstance (right, Vector):
3647
+ return (< MIPVariable> left)._matrix_rmul_impl(right.column())[0 ]
3632
3648
if not isinstance (right, Matrix):
3633
3649
return NotImplemented
3634
3650
return (< MIPVariable> left)._matrix_rmul_impl(right)
0 commit comments