-
Notifications
You must be signed in to change notification settings - Fork 38
/
linprog.jl
91 lines (73 loc) · 2.55 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
using Base.Test
using MathProgBase
function linprogtest(solver; objtol = 1e-7, primaltol = 1e-6)
println("Testing linprog and subfunctions with solver ", string(typeof(solver)))
# min -x
# s.t. 2x + y <= 1.5
# x,y >= 0
# solution is (0.75,0) with objval -0.75
# test buildlp and solvelp
m = buildlp([-1,0],[2 1],'<',1.5,solver)
sol = solvelp(m)
@test sol.status == :Optimal
@test isapprox(sol.objval, -0.75, atol=objtol)
@test isapprox(norm(sol.sol - [0.75,0.0]), 0.0, atol=primaltol)
# test linprog
sol = linprog([-1,0],[2 1],'<',1.5,solver)
@test sol.status == :Optimal
@test isapprox(sol.objval, -0.75, atol=objtol)
@test isapprox(norm(sol.sol - [0.75,0.0]), 0.0, atol=primaltol)
# test buildlp and solvelp
m = buildlp([-1,0],[2 1],'<',1.5,solver)
sol = solvelp(m)
@test sol.status == :Optimal
@test isapprox(sol.objval, -0.75, atol=objtol)
@test isapprox(norm(sol.sol - [0.75,0.0]), 0.0, atol=primaltol)
# test linprog
sol = linprog([-1,0],sparse([2 1]),'<',1.5,solver)
@test sol.status == :Optimal
@test isapprox(sol.objval, -0.75, atol=objtol)
@test isapprox(norm(sol.sol - [0.75,0.0]), 0.0, atol=primaltol)
# test infeasible problem:
# min x
# s.t. 2x+y <= -1
# x,y >= 0
# test buildlp and solvelp
m = buildlp([1,0],[2 1],'<',-1,solver)
sol = solvelp(m)
@test sol.status == :Infeasible
r = sol.attrs[:infeasibilityray][1]
@test isapprox(r/abs(r), -1.0)
# test linprog
sol = linprog([1,0],[2 1],'<',-1,solver)
@test sol.status == :Infeasible
r = sol.attrs[:infeasibilityray][1]
@test isapprox(r/abs(r), -1.0)
# test unbounded problem:
# min -x-y
# s.t. -x+2y <= 0
# x,y >= 0
# test buildlp and solvelp
m = buildlp([-1,-1],[-1 2],'<',[0],solver)
sol = solvelp(m)
@test sol.status == :Unbounded
# test linprog
sol = linprog([-1,-1],[-1 2],'<',[0],solver)
@test sol.status == :Unbounded
# unbounded problem with unique ray:
# min -x-y
# s.t. x-y == 0
# x,y >= 0
# test buildlp and solvelp
m = buildlp([-1,-1],[1 -1],'=',0,solver)
sol = solvelp(m)
@test sol.status == :Unbounded
@test sol.attrs[:unboundedray][1] > 1e-7
@test isapprox(sol.attrs[:unboundedray][1], sol.attrs[:unboundedray][2])
# test linprog
sol = linprog([-1,-1],[1 -1],'=',0,solver)
@test sol.status == :Unbounded
@test sol.attrs[:unboundedray][1] > 1e-7
@test isapprox(sol.attrs[:unboundedray][1], sol.attrs[:unboundedray][2])
println("Passed")
end