@@ -2,12 +2,12 @@ _not_row_one = "May not modify the header row of the Tableau"
22_not_col_one = " May not modify the z column of the Tableau"
33
44"""
5- pivot !(T::Tableau, i::Int, j::Int)
5+ old_pivot !(T::Tableau, i::Int, j::Int)
66
77Modify `T` by doing a pivot operation at contraint `i`
88and variable x_`j`.
99"""
10- function pivot !(T:: Tableau , i:: Int , j:: Int )
10+ function old_pivot !(T:: Tableau , i:: Int , j:: Int )
1111 M = T. M # for easier access
1212
1313 i += 1
@@ -29,108 +29,76 @@ function pivot!(T::Tableau, i::Int, j::Int)
2929end
3030
3131"""
32- pivot (T::Tableau, i:: Int, j::Int )
32+ set_basis! (T::Tableau, vars::Vector{ Int} )
3333
34- Non-modifying version of `pivot!`.
34+ Pivot `T` so that the variables specified in `vars`
35+ are the basic variables.
3536"""
36- function pivot(T:: Tableau , i:: Int , j:: Int )
37- TT = deepcopy(T)
38- return pivot!(TT, i, j)
39- end
37+ function set_basis!(T:: Tableau , vars:: Vector{Int} )
38+ n = T. n_vars
39+ m = T. n_cons
4040
41- """
42- scale!(T::Tableau, r::Int, s::_Exact)
41+ vars = sort(unique(vars))
4342
44- Multiply row `r` of `T` by `s` (which must be nonzero).
45- """
46- function scale!(T:: Tableau , r:: Int , s:: _Exact )
47- r += 1
48- if r== 1
49- error(_not_row_one)
50- end
51- if s == 0
52- error(" May not scale by 0" )
43+ if (length(vars) ≠ m) || ! (vars ⊆ collect(1 : n))
44+ error(" Invalid basis: $vars " )
5345 end
54- T. M[r, :] *= s
55- return T
56- end
5746
58- """
59- scale(T::Tableau, r::Int, s::_Exact)
47+ # bop up indices by 1 and make a list
48+ idx = [j+ 1 for j in vars]
49+ idx = vcat(1 , idx)
6050
61- Non-modifying version of
62- """
63- function scale(T:: Tableau , r:: Int , s:: _Exact )
64- TT = deepcopy(T)
65- scale!(TT, r, s)
66- return TT
51+ B = T. M[:, idx]
52+ BB = invx(B) # will fail if B is not invertible
53+ T. M = BB* T. M
54+ T. B = vars
55+ return T
6756end
6857
6958"""
70- swap! (T::Tableau, i::Int, j::Int )
59+ get_basis (T::Tableau)
7160
72- Swap constraints `i` and `j` of the Tableau .
61+ Return the current basis (indices of basic variables) .
7362"""
74- function swap!(T:: Tableau , i:: Int , j:: Int )
75- i += 1
76- j += 1
77- if i< 2 || j< 2
78- error(_not_row_one)
79- end
80-
81- if i== j
82- return T
83- end
84-
85- row_i = T. M[i, :]
86- row_j = T. M[j, :]
63+ get_basis(T:: Tableau ) = copy(T. B)
8764
88- T. M[j, :] = row_i
89- T. M[i, :] = row_j
90-
91- T
92- end
9365"""
94- swap (T::Tableau, i ::Int, j ::Int)
66+ pivot! (T::Tableau, leave ::Int, enter ::Int)
9567
96- Non-modifying version of `swap! `.
68+ Remove element `leave` from the basis and include element `enter `.
9769"""
98- function swap(T:: Tableau , i:: Int , j:: Int )
99- TT = deepcopy(T)
100- swap!(TT, i, j)
101- end
70+ function pivot!(T:: Tableau , leave:: Int , enter:: Int )
71+ B = Set(get_basis(T))
10272
103- """
104- basis_pivot!(T::Tableau, vars)
73+ # check for validity
74+ if 0 ∈ B
75+ error(" No basis has been established for this tableau" )
76+ end
10577
106- Pivot `T` so that the variables specified in `vars`
107- are the basic variables.
108- """
109- function basis_pivot!(T:: Tableau , vars)
110- # bop up indices by 1 and make a list
111- idx = [j+ 1 for j in vars]
112- idx = vcat(1 , idx)
78+ n = T. n_vars
79+ if ! (1 ≤ leave ≤ n)
80+ error(" Invalid variable index: $leave " )
81+ end
11382
114- B = T . M[:, idx]
115- BB = invx(B) # will fail if B is not invertible
116- new_M = BB * T . M
83+ if ! ( 1 ≤ enter ≤ n)
84+ error( " Invalid variable index: $enter " )
85+ end
11786
118- r, c = size(T. M)
119- for i in 1 : r
120- for j in 1 : c
121- T. M[i, j] = new_M[i, j]
122- end
87+ if leave == enter
88+ @warn " No pivot: enter = leave = $enter "
89+ return nothing
12390 end
124- return T
125- end
12691
127- """
128- basis_pivot(T::Tableau, vars)
92+ # form new basis
93+ B = Set(get_basis(T))
94+ if leave ∉ B
95+ error(" Element $leave is not in the basis; cannot remove it" )
96+ end
97+ if enter ∈ B
98+ error(" Element $enter is already in the basis; cannot add it" )
99+ end
129100
130- Non-modifying version of `basis_pivot!`.
131- """
132- function basis_pivot(T:: Tableau , vars)
133- TT = deepcopy(T)
134- basis_pivot!(TT, vars)
135- return TT
101+ delete!(B, leave)
102+ push!(B, enter)
103+ set_basis!(T, collect(B))
136104end
0 commit comments