1 | ;; |
---|
2 | ;; Example from section 1.3 in the GLPK reference manual: |
---|
3 | ;; |
---|
4 | ;; maximize |
---|
5 | ;; z = (10 * x1) + (6 * x2) + (4 * x3) |
---|
6 | ;; |
---|
7 | ;; subject to |
---|
8 | ;; |
---|
9 | ;; x1 + x2 + x3 <= 100 |
---|
10 | ;; (10 * x1) + (4 * x2) + (5 * x3) <= 600 |
---|
11 | ;; ( 2 * x1) + (2 * x2) + (6 * x3) <= 300 |
---|
12 | ;; |
---|
13 | ;; where |
---|
14 | ;; |
---|
15 | ;; x1 >= 0, x2 >= 0, x3 >= 0 |
---|
16 | |
---|
17 | (require-extension srfi-1 srfi-4 glpk) |
---|
18 | (import srfi-1 srfi-4 glpk) |
---|
19 | |
---|
20 | |
---|
21 | ;; Auxiliary variables (rows) |
---|
22 | ;; |
---|
23 | ;; p = x1 + x2 + x3 |
---|
24 | ;; q = (10 * x1) + (4 * x2) + (5 * x3) |
---|
25 | ;; r = ( 2 * x1) + (2 * x2) + (6 * x3) |
---|
26 | ;; |
---|
27 | ;; -inf < p <= 100 |
---|
28 | ;; -inf < q <= 600 |
---|
29 | ;; -inf < r <= 300 |
---|
30 | |
---|
31 | (define pbounds `((upper-bound 100) (upper-bound 600) (upper-bound 300))) |
---|
32 | |
---|
33 | ;; Structural variables (columns) |
---|
34 | ;; |
---|
35 | ;; 0 <= x1 < +inf |
---|
36 | ;; 0 <= x2 < +inf |
---|
37 | ;; 0 <= x3 < +inf |
---|
38 | |
---|
39 | (define xbounds `((lower-bound 0) (lower-bound 0) (lower-bound 0))) |
---|
40 | |
---|
41 | ;; Objective coefficients: 10, 6, 4 |
---|
42 | |
---|
43 | (define objcoefs (list 10 6 4)) |
---|
44 | |
---|
45 | ;; Constraints matrix (in row-major order) |
---|
46 | ;; |
---|
47 | ;; 1 1 1 |
---|
48 | ;; 10 4 5 |
---|
49 | ;; 2 2 6 |
---|
50 | |
---|
51 | (define constraints (f64vector 1 1 1 10 4 5 2 2 6)) |
---|
52 | |
---|
53 | ;; Create the problem definition & run the solver |
---|
54 | (let ((lpp (lpx:make-problem 'maximize pbounds xbounds objcoefs constraints))) |
---|
55 | (lpx:scale-problem lpp) |
---|
56 | (lpx:use_presolver lpp #t) |
---|
57 | (let ((status (lpx:simplex lpp))) |
---|
58 | (assert (= status 200)) |
---|
59 | (assert (< (abs (- (lpx:get-objective-value lpp) 733.33333333333333)) 1e-15)) |
---|
60 | (assert (every (lambda (x y) (< (abs (- x y)) 1e-15)) |
---|
61 | (f64vector->list (lpx:get-column-primals lpp)) |
---|
62 | '(33.333333333333333 66.66666666666667 0.0))) |
---|
63 | )) |
---|