-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsolver.go
493 lines (402 loc) · 9.36 KB
/
solver.go
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
package casso
import (
"errors"
"math"
)
type Tag struct {
priority Priority
marker Symbol
other Symbol
}
type Edit struct {
tag Tag
val float64
}
type Solver struct {
tabs map[Symbol]Constraint // symbol id -> constraint
edits map[Symbol]Edit // variable id -> value
tags map[Symbol]Tag // marker id -> tag
infeasible []Symbol
objective Expr
artificial Expr
}
func NewSolver() *Solver {
return &Solver{
tabs: make(map[Symbol]Constraint),
edits: make(map[Symbol]Edit),
tags: make(map[Symbol]Tag),
}
}
func (s *Solver) Val(id Symbol) float64 {
row, ok := s.tabs[id]
if !ok {
return 0
}
return row.expr.constant
}
func (s *Solver) AddConstraint(cell Constraint) (Symbol, error) {
return s.AddConstraintWithPriority(Required, cell)
}
func (s *Solver) AddConstraintWithPriority(priority Priority, cell Constraint) (Symbol, error) {
tag := Tag{priority: priority}
c := cell
c.expr.terms = make([]Term, 0, len(c.expr.terms))
// 1. filter away terms with coefficients that are zero
// 2. check that all variables in the constraint are registered
// 3. replace variables with their values if they have values assigned to them
for _, term := range cell.expr.terms {
if eqz(term.coeff) {
continue
}
if term.id.Zero() {
return zero, ErrBadTermInConstraint
}
resolved, exists := s.tabs[term.id]
if !exists {
c.expr.addSymbol(term.coeff, term.id)
continue
}
c.expr.addExpr(term.coeff, resolved.expr)
}
// convert constraint to augmented simplex form
switch c.op {
case LTE, GTE:
coeff := 1.0
if c.op == GTE {
coeff = -1.0
}
tag.marker = next(Slack)
c.expr.addSymbol(coeff, tag.marker)
if priority < Required {
tag.other = next(Error)
c.expr.addSymbol(-coeff, tag.other)
s.objective.addSymbol(float64(priority), tag.other)
}
case EQ:
if priority < Required {
tag.marker = next(Error)
tag.other = next(Error)
c.expr.addSymbol(-1.0, tag.marker)
c.expr.addSymbol(1.0, tag.other)
s.objective.addSymbol(float64(priority), tag.marker)
s.objective.addSymbol(float64(priority), tag.other)
} else {
tag.marker = next(Dummy)
c.expr.addSymbol(1.0, tag.marker)
}
}
if c.expr.constant < 0.0 {
c.expr.negate()
}
// find a subject variable to pivot on
subject, err := s.findSubject(c, tag)
if err != nil {
return zero, err
}
if subject.Zero() {
err := s.augmentArtificialVariable(c)
if err != nil {
return tag.marker, err
}
} else {
// 1. solve for the subject variable
// 2. substitute the solution into our tableau
c.expr.solveFor(subject)
s.substitute(subject, c.expr)
s.tabs[subject] = c
}
s.tags[tag.marker] = tag
return tag.marker, s.optimizeAgainst(&s.objective)
}
func (s *Solver) RemoveConstraint(marker Symbol) error {
tag, exists := s.tags[marker]
if !exists {
return ErrBadConstraintMarker
}
delete(s.tags, tag.marker)
if tag.marker.Error() {
row, exists := s.tabs[tag.marker]
if exists {
s.objective.addExpr(float64(-tag.priority), row.expr)
} else {
s.objective.addSymbol(float64(-tag.priority), tag.marker)
}
}
if tag.other.Error() {
row, exists := s.tabs[tag.other]
if exists {
s.objective.addExpr(float64(-tag.priority), row.expr)
} else {
s.objective.addSymbol(float64(-tag.priority), tag.other)
}
}
row, exists := s.tabs[tag.marker]
if !exists {
r1 := math.MaxFloat64
r2 := math.MaxFloat64
exit := zero
first := zero
second := zero
third := zero
for symbol, row := range s.tabs {
idx := row.expr.find(tag.marker)
if idx == -1 {
continue
}
coeff := row.expr.terms[idx].coeff
if eqz(coeff) {
continue
}
if symbol.External() {
third = symbol
} else {
r := -row.expr.constant / coeff
switch {
case coeff < 0 && r < r1:
r1, first = r, symbol
case coeff >= 0 && r < r2:
r2, second = r, symbol
}
}
}
switch {
case !first.Zero():
exit = first
case !second.Zero():
exit = second
default:
exit = third
}
row = s.tabs[exit]
delete(s.tabs, exit)
row.expr.solveForSymbols(exit, tag.marker)
s.substitute(tag.marker, row.expr)
return s.optimizeAgainst(&s.objective)
}
delete(s.tabs, tag.marker)
return s.optimizeAgainst(&s.objective)
}
func (s *Solver) Edit(id Symbol, priority Priority) error {
if priority < 0 || priority >= Required {
return ErrBadPriority
}
if _, exists := s.edits[id]; exists {
return nil
}
constraint := Constraint{op: EQ, expr: NewExpr(0.0, id.T(1.0))}
marker, err := s.AddConstraintWithPriority(priority, constraint)
if err != nil {
return err
}
s.edits[id] = Edit{tag: s.tags[marker], val: 0.0}
return nil
}
func (s *Solver) Suggest(id Symbol, val float64) error {
edit, ok := s.edits[id]
if !ok {
return ErrBadEditVariable
}
defer s.optimizeDualObjective()
delta := val - edit.val
edit.val = val
s.edits[id] = edit
row, exists := s.tabs[edit.tag.marker]
if exists {
row.expr.constant -= delta
if row.expr.constant < 0.0 {
s.infeasible = append(s.infeasible, edit.tag.marker)
}
s.tabs[edit.tag.marker] = row
return nil
}
row, exists = s.tabs[edit.tag.other]
if exists {
row.expr.constant -= delta
if row.expr.constant < 0.0 {
s.infeasible = append(s.infeasible, edit.tag.other)
}
s.tabs[edit.tag.other] = row
return nil
}
for symbol := range s.tabs {
row := s.tabs[symbol]
idx := row.expr.find(edit.tag.marker)
if idx == -1 {
continue
}
coeff := row.expr.terms[idx].coeff
if eqz(coeff) {
continue
}
row.expr.constant += coeff * delta
s.tabs[symbol] = row
if row.expr.constant >= 0.0 {
continue
}
if symbol.External() {
continue
}
s.infeasible = append(s.infeasible, symbol)
}
return nil
}
// findSubject finds a subject variable to pivot on. It must either:
// 1. be an external variable,
// 2. be a negative slack/error variable, or
// 3. be a dummy variable that has previously been cancelled out
func (s *Solver) findSubject(cell Constraint, tag Tag) (Symbol, error) {
for _, term := range cell.expr.terms {
if term.id.External() {
return term.id, nil
}
}
if tag.marker.Restricted() {
idx := cell.expr.find(tag.marker)
if idx != -1 && cell.expr.terms[idx].coeff < 0.0 {
return tag.marker, nil
}
}
if tag.other.Restricted() {
idx := cell.expr.find(tag.other)
if idx != -1 && cell.expr.terms[idx].coeff < 0.0 {
return tag.other, nil
}
}
for _, term := range cell.expr.terms {
if !term.id.Dummy() {
return zero, nil
}
}
if !eqz(cell.expr.constant) {
return zero, ErrBadDummyVariable
}
return tag.marker, nil
}
func (s *Solver) substitute(id Symbol, expr Expr) {
for symbol := range s.tabs {
row := s.tabs[symbol]
row.expr.substitute(id, expr)
s.tabs[symbol] = row
if symbol.External() || row.expr.constant >= 0.0 {
continue
}
s.infeasible = append(s.infeasible, symbol)
}
s.objective.substitute(id, expr)
s.artificial.substitute(id, expr)
}
func (s *Solver) optimizeAgainst(objective *Expr) error {
for {
entry := zero
exit := zero
for _, term := range objective.terms {
if !term.id.Dummy() && term.coeff < 0.0 {
entry = term.id
break
}
}
if entry.Zero() {
return nil
}
ratio := math.MaxFloat64
for symbol := range s.tabs {
if symbol.External() {
continue
}
idx := s.tabs[symbol].expr.find(entry)
if idx == -1 {
continue
}
coeff := s.tabs[symbol].expr.terms[idx].coeff
if coeff >= 0.0 {
continue
}
r := -s.tabs[symbol].expr.constant / coeff
if r < ratio {
ratio, exit = r, symbol
}
}
row := s.tabs[exit]
delete(s.tabs, exit)
row.expr.solveForSymbols(exit, entry)
s.substitute(entry, row.expr)
s.tabs[entry] = row
}
}
func (s *Solver) augmentArtificialVariable(row Constraint) error {
art := next(Slack)
s.tabs[art] = row.clone()
s.artificial = row.expr.clone()
err := s.optimizeAgainst(&s.artificial)
if err != nil {
return err
}
success := eqz(s.artificial.constant)
s.artificial = NewExpr(0.0)
artificial, ok := s.tabs[art]
if ok {
delete(s.tabs, art)
if len(artificial.expr.terms) == 0 {
return nil
}
entry := zero
for _, term := range artificial.expr.terms {
if term.id.Restricted() {
entry = term.id
break
}
}
if entry.Zero() {
return errors.New("unsatisfiable")
}
artificial.expr.solveForSymbols(art, entry)
s.substitute(entry, artificial.expr)
s.tabs[entry] = artificial
}
for symbol, row := range s.tabs {
idx := row.expr.find(art)
if idx == -1 {
continue
}
row.expr.delete(idx)
s.tabs[symbol] = row
}
idx := s.objective.find(art)
if idx != -1 {
s.objective.delete(idx)
}
if !success {
return errors.New("unsatisfiable")
}
return nil
}
// optimizeDualObjective optimizes away infeasible constraints.
func (s *Solver) optimizeDualObjective() {
for len(s.infeasible) > 0 {
exit := s.infeasible[len(s.infeasible)-1]
s.infeasible = s.infeasible[:len(s.infeasible)-1]
row, exists := s.tabs[exit]
if !exists || row.expr.constant >= 0.0 {
continue
}
delete(s.tabs, exit)
entry := zero
ratio := math.MaxFloat64
for _, term := range row.expr.terms {
if term.coeff <= 0.0 || term.id.Dummy() {
continue
}
idx := s.objective.find(term.id)
if idx == -1 {
continue
}
r := s.objective.terms[idx].coeff / term.coeff
if r < ratio {
entry, ratio = term.id, r
}
}
row.expr.solveForSymbols(exit, entry)
s.substitute(entry, row.expr)
s.tabs[entry] = row
}
}