forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinprog.jl
95 lines (68 loc) · 2.01 KB
/
linprog.jl
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
### Linear programming
cd("../extras") do
require("linprog.jl")
## Simplex method
# Set options (disable all output
# excpept for errors, turn on presolver)
lps_opts = GLPSimplexParam()
lps_opts["msg_lev"] = GLP_MSG_ERR
lps_opts["presolve"] = GLP_ON
lps_opts["it_lim"] = 1000
# A small dense optimization problem
f = [ 3.; 2. ];
A = [ 2. 1. ;
1. 1. ];
b = [ 100.; 80 ];
lb = [ 0.; 0.];
(z, x, flag) = linprog_simplex(-f, A, b, [], [], lb, [], lps_opts);
@assert flag == 0
@assert z == -180.0
@assert isequal(x, [20.; 60.])
# A constraint satisfaction (matching) problem
# passing a sparse representation
# to linprog_simplex
f = [ 3. 2. 2. ;
1. 0. 1. ;
3. 3. 5. ];
f = reshape(f, (9,));
Aeq = [ 1. 1. 1. 0. 0. 0. 0. 0. 0. ;
0. 0. 0. 1. 1. 1. 0. 0. 0. ;
0. 0. 0. 0. 0. 0. 1. 1. 1. ;
1. 0. 0. 1. 0. 0. 1. 0. 0. ;
0. 1. 0. 0. 1. 0. 0. 1. 0. ;
0. 0. 1. 0. 0. 1. 0. 0. 1. ];
Aeq = sparse(Aeq);
beq = ones(Float64, 6);
lb = zeros(Float64, 9);
ub = ones(Float64, 9);
(z, x, flag) = linprog_simplex(f, [], [], Aeq, beq, lb, ub, lps_opts);
@assert flag == 0
@assert z == 5.
@assert isequal(x, [ 0.; 0.; 1. ;
0.; 1.; 0. ;
1.; 0.; 0. ])
## Interior point method
# Same problem and options as above
lpi_opts = GLPInteriorParam()
lpi_opts["msg_lev"] = GLP_MSG_ERR
(z, x, ret) = linprog(f, [], [], Aeq, beq, lb, ub, lpi_opts);
tol = 1e-4
@assert flag == 0
@assert abs(z - 5.) < tol
@assert max(abs(x - [ 0.; 0.; 1. ;
0.; 1.; 0. ;
1.; 0.; 0. ])) < tol
### Mixed interger progamming
# Same problem and options as above
mip_opts = GLPIntoptParam()
mip_opts["msg_lev"] = GLP_MSG_ERR
mip_opts["presolve"] = GLP_ON
# Use binary variables
colkind = int32([ GLP_BV for i = 1 : 9 ])
(z, x, ret, ret_ps) = mixintprog(f, [], [], Aeq, beq, [], [], colkind, mip_opts);
@assert flag == 0
@assert z == 5.
@assert isequal(x, [ 0.; 0.; 1. ;
0.; 1.; 0. ;
1.; 0.; 0. ])
end # cd