-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.lisp
332 lines (310 loc) · 11.7 KB
/
update.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
(in-package :rationalsimplex)
;;;;; Simplex object and basis updating functions
;;;;; at the end of a dual simplex iteration
;;;;;
;;;; Determines non-basic primal value flag for exiting variable
(defun basis-exiting-variable-flag (in-phase1 exit-val exit-rcost exit-col)
(if in-phase1
(cond ((and (not (column-has-l exit-col))
(not (column-has-u exit-col)))
(error "exiting variable is free in phase 2"))
((and (= -1 exit-val)
(not (column-has-l exit-col)))
'nonbasic-lower-bound)
((and (= 0 exit-val)
(column-has-l exit-col)
(<= 0 exit-rcost))
'nonbasic-lower-bound)
((and (= 0 exit-val)
(column-has-u exit-col)
(>= 0 exit-rcost))
'nonbasic-upper-bound)
((and (= 1 exit-val)
(not (column-has-u exit-col)))
'nonbasic-upper-bound)
(t
(error "bad exiting variable or reduced cost value in phase 1")))
(cond ((and (column-has-l exit-col)
(= (column-l exit-col) exit-val)
(<= 0 exit-rcost))
'nonbasic-lower-bound)
((and (column-has-u exit-col)
(= (column-u exit-col) exit-val)
(>= 0 exit-rcost))
'nonbasic-upper-bound)
((and (not (column-has-l exit-col))
(not (column-has-u exit-col)))
(error "exiting variable is free in phase 2"))
(t
(error "bad exiting variable value or reduced cost in phase 2")))))
;;;; Updates dual-steepest-edge weights
(defun simplex-basis-update-dse (sd)
(declare (optimize (speed 1) (safety 0) (debug 0)))
(let* ((b (simplex-basis sd))
(dse-tr (simplex-dse-ftran sd))
(alphaq (tran-hsv (simplex-ftran sd)))
(exit-row (simplex-pivot-row-index sd))
(alpha-index (hsv-find exit-row alphaq))
(alphaqr (aref (hsv-vis alphaq) alpha-index))
(m (length (basis-header b)))
(tau (tran-hsv dse-tr))
(flags (simplex-dse-update-flags sd)))
;; reset flags
(bit-xor flags flags t)
;; build new dse-coef and new dse-vis
(let* ((alphaq-n (numerator (hsv-coef alphaq)))
(alphaq-d (denominator (hsv-coef alphaq)))
(tau-n (numerator (hsv-coef tau)))
(tau-d (denominator (hsv-coef tau)))
(dse-n (numerator (basis-dse-coef b)))
(dse-d (denominator (basis-dse-coef b)))
(a (* tau-d dse-n))
(d (* alphaqr alphaqr))
(c (* alphaq-n alphaq-n))
(f (aref (basis-dse-weight-vis b) exit-row))
(g (* 2 alphaqr tau-n dse-d))
(h (gcd d f))
(a.h (* a h))
(j (gcd a.h g))
(c.j (* c j))
(l (* alphaq-d alphaq-d a f))
(gcd-c.j-l (gcd c.j l))
(c/ (/ c.j gcd-c.j-l))
(a/ (/ a.h j))
(g/ (/ g j))
(d/ (/ d h))
(f/ (/ f h)))
(declare (integer alphaq-n alphaq-d tau-n tau-d dse-n dse-d))
(declare (integer c/ a/ g/ d/ f/))
;; update coefficient
(setf (basis-dse-coef b) (/ gcd-c.j-l (* dse-d d c tau-d)))
;; update dse weights for nonzero elements in alphaq vector
(dotimes (k (hsv-length alphaq))
(let ((i (aref (hsv-is alphaq) k))
(alphaqi (aref (hsv-vis alphaq) k)))
(unless (zerop alphaqi)
(setf (sbit flags i) 1)
(setf (aref (basis-dse-weight-vis b) i)
(if (= i exit-row)
(/ l gcd-c.j-l)
(let ((taui-ref (hsv-find i tau)))
(declare (fixnum taui-ref))
(if (= -1 taui-ref)
(* c/ a/ (+ (* d/ (aref (basis-dse-weight-vis b) i))
(* f/ alphaqi alphaqi)))
(* c/ (- (* a/ (+ (* d/ (aref (basis-dse-weight-vis b) i))
(* f/ alphaqi alphaqi)))
(* g/ alphaqi (aref (hsv-vis tau) taui-ref)))))))))))
;; update all other dse weights
(dotimes (i m)
(when (zerop (sbit flags i))
(setf (aref (basis-dse-weight-vis b) i)
(* c/ a/ d/ (aref (basis-dse-weight-vis b) i))))))
;; normalize signs and values
(let ((coef-signum (signum (basis-dse-coef b)))
(common-factor (aref (basis-dse-weight-vis b) 0)))
(declare (integer common-factor))
(dotimes (i m)
(when (= 1 (setf common-factor
(gcd common-factor (aref (basis-dse-weight-vis b) i))))
(return)))
(when (> 0 coef-signum)
(setf common-factor (- common-factor)))
(unless (= 1 common-factor)
(setf (basis-dse-coef b) (* (basis-dse-coef b) common-factor))
(dotimes (i m)
(let ((r (/ (aref (basis-dse-weight-vis b) i) common-factor)))
(declare (integer r))
(setf (aref (basis-dse-weight-vis b) i) r)))))))
;;;; Updates primal values, primal infeasabilities, and dual-steepest-edge weights
(defun simplex-basis-update-primal (sd)
(declare (optimize (speed 1) (safety 0) (debug 0)))
(let* ((b (simplex-basis sd))
(alphaq (tran-hsv (simplex-ftran sd)))
(exit-row (simplex-pivot-row-index sd))
(primal-values (basis-primal-values b))
(lpcols (lp-columns (simplex-lp sd)))
(alpha-index (hsv-find exit-row alphaq)))
(declare (fixnum alpha-index))
(assert (/= -1 alpha-index))
(assert (/= 0 (aref (hsv-vis alphaq) alpha-index)))
;; compute primal step
(setf (simplex-primal-step sd)
(/ (simplex-delta sd)
(* (aref (hsv-vis alphaq) alpha-index)
(hsv-coef alphaq))))
;; update values
(dotimes (k (hsv-length alphaq))
(let ((i (aref (hsv-is alphaq) k)))
;; update basic primal values
(decf (aref primal-values i)
(* (simplex-primal-step sd)
(hsv-coef alphaq)
(aref (hsv-vis alphaq) k)))
;; update primal infeasabilities
(basis-update-primal-infeasability
(basis-primal-infeas b)
i
(aref primal-values i)
(adjvector-column-ref lpcols (aref (basis-header b) i))
(basis-in-phase1 b))))))
;;;; Temporarily uses pivot row arrays to compute weighted column sum
(defun simplex-compute-flip-ftran (sd)
(let* ((lp (simplex-lp sd))
(inphase1 (basis-in-phase1 (simplex-basis sd)))
(n-cols (simplex-n-flips sd))
(last-heap-index (- n-cols 1))
(heap (simplex-add-heap sd))
(rhs-is (simplex-pivot-row-col-refs sd))
(rhs-values (simplex-pivot-row-values sd))
(rhs-length -1)
(flags (basis-column-flags (simplex-basis sd)))
(prev-row-ref -1))
(labels ((get-row-ref (k)
(let* ((counter (aref (simplex-add-counters sd) k))
(col-ref (aref (simplex-flip-col-refs sd) k))
(col-hsv (column-hsv (adjvector-column-ref (lp-columns lp) col-ref))))
(if (= counter (hsv-length col-hsv))
most-positive-fixnum
(aref (hsv-is col-hsv) counter))))
(sift-down (root end)
(loop
(let ((child (+ 1 (* 2 root))))
(when (> child end)
(return))
(when (and (< child end)
(> (get-row-ref (aref heap child))
(get-row-ref (aref heap (+ child 1)))))
(incf child))
(when (<= (get-row-ref (aref heap root))
(get-row-ref (aref heap child)))
(return))
(rotatef (aref heap root) (aref heap child))
(setf root child)))))
;; initialization
(assert (not (zerop n-cols)))
(reset-hsv (simplex-hsv sd))
(dotimes (k n-cols)
(let* ((col-ref (aref (simplex-flip-col-refs sd) k))
(col (adjvector-column-ref (lp-columns lp) col-ref))
(diff (if (eq (aref flags col-ref) 'nonbasic-lower-bound)
(column-u-minus-l inphase1 col)
(column-l-minus-u inphase1 col))))
(setf (aref (simplex-add-counters sd) k) 0
(aref (simplex-flip-col-coefs sd) k) (* diff (hsv-coef (column-hsv col)))
(aref (simplex-add-heap sd) k) k)))
;; heapify
(loop for heapify-start from (floor (- last-heap-index 1) 2) downto 0
do (sift-down heapify-start last-heap-index))
;; build weighted column sum
(loop
;; pop element with smallest row index off heap
(let* ((smallest-k (aref heap 0))
(row-ref (get-row-ref smallest-k)))
(cond ((= row-ref most-positive-fixnum)
(incf rhs-length)
(return))
((> prev-row-ref row-ref)
(error "bad row index order"))
((= prev-row-ref row-ref))
(t
(incf rhs-length)
(setf prev-row-ref row-ref
(aref rhs-is rhs-length) row-ref
(aref rhs-values rhs-length) 0)))
;; update rhs value
(incf (aref rhs-values rhs-length)
(* (aref (simplex-flip-col-coefs sd) smallest-k)
(aref (hsv-vis (column-hsv (adjvector-column-ref
(lp-columns lp)
(aref (simplex-flip-col-refs sd) smallest-k))))
(aref (simplex-add-counters sd) smallest-k))))
;; update counter and heap
(incf (aref (simplex-add-counters sd) smallest-k))
(sift-down 0 last-heap-index)))
;; normalize rhs
(let ((cdenom (common-denominator rhs-values rhs-length)))
(setf (hsv-coef (simplex-hsv sd)) (/ 1 cdenom))
(dotimes (k rhs-length)
(assert (integerp (* cdenom (aref rhs-values k))))
(hsv-add (adjvector-fixnum-ref (lp-active-row-inds lp) (aref rhs-is k))
(* cdenom (aref rhs-values k)) (simplex-hsv sd)))
(hsv-sort-indices-increasing (simplex-hsv sd))
(hsv-normalize (simplex-hsv sd))
(check-flip-rhs sd))
;; perform ftran
(ftran (simplex-flip-ftran sd) (simplex-hsv sd))
(check-ftran (simplex-basis sd) lp (simplex-flip-ftran sd) (simplex-hsv sd)))))
;;;; Ties it all together.
;;;; Updates reduced costs, computes primal step, updates basis,
;;;; as well as the entering and exiting variable primal values.
;;;; Performs bound flips, updates objective, primal values and infeasabilites.
(defun simplex-basis-update (sd)
(declare (optimize (speed 1) (safety 0) (debug 0)))
(let* ((b (simplex-basis sd))
(header (basis-header b))
(lp (simplex-lp sd))
(q (simplex-pivot-col-ref sd))
(exit-row (simplex-pivot-row-index sd))
(exit-col-ref (aref header exit-row))
(primal-values (basis-primal-values b))
(primal-infeas (basis-primal-infeas b))
(flags (basis-column-flags b))
(rcosts (basis-reduced-costs b)))
;; update reduced costs
(setf (aref rcosts exit-col-ref) (- (simplex-dual-step sd)))
(unless (zerop (simplex-dual-step sd))
(dotimes (k (simplex-pivot-row-length sd))
(let* ((j (aref (simplex-pivot-row-col-refs sd) k))
(flag (aref flags j)))
(when (or (eq flag 'nonbasic-lower-bound)
(eq flag 'nonbasic-upper-bound))
(decf (aref rcosts j)
(* (simplex-dual-step sd)
(aref (simplex-pivot-row-values sd) k)))))))
;; in case of bound flips
(unless (zerop (simplex-n-flips sd))
(simplex-compute-flip-ftran sd)
;; update objective, primal basic values and primal infeasabilities
(let ((delta-x (tran-hsv (simplex-flip-ftran sd))))
(dotimes (k (hsv-length delta-x))
(let* ((i (aref (hsv-is delta-x) k))
(xk (* (hsv-coef delta-x) (aref (hsv-vis delta-x) k)))
(col (adjvector-column-ref (lp-columns lp) (aref (basis-header b) i))))
(decf (aref primal-values i) xk)
(decf (basis-obj-value b) (* xk (column-c col)))
(basis-update-primal-infeasability primal-infeas i
(aref primal-values i) col
(basis-in-phase1 b)))))
;; update primal nonbasic values
(dotimes (k (simplex-n-flips sd))
(let ((j (aref (simplex-flip-col-refs sd) k)))
(setf (aref flags j)
(if (eq (aref flags j) 'nonbasic-lower-bound)
'nonbasic-upper-bound
'nonbasic-lower-bound)))))
;; compute primal step, update primal basic values, update dse weights
(simplex-basis-update-primal sd)
;; update exiting variable
(setf (aref flags exit-col-ref)
(basis-exiting-variable-flag (basis-in-phase1 b)
(aref primal-values exit-row)
(aref rcosts exit-col-ref)
(adjvector-column-ref (lp-columns lp) exit-col-ref)))
;; update entering variable
(setf (aref header exit-row) q)
(setf (aref primal-values exit-row)
(+ (simplex-primal-step sd)
(nonbasic-value (basis-in-phase1 b)
(adjvector-column-ref (lp-columns lp) q)
(aref flags q))))
(basis-update-primal-infeasability primal-infeas
exit-row
(aref primal-values exit-row)
(adjvector-column-ref (lp-columns lp) q)
(basis-in-phase1 b))
(setf (aref flags q) 'basic)
;; update objective
(incf (basis-obj-value b)
(* (simplex-dual-step sd)
(simplex-delta sd)))))