Skip to content

Commit 86a4cf1

Browse files
blegatodow
andauthored
Split linear program and quad programs (#246)
* Split linear program and quad programs * Update linear_program.jl --------- Co-authored-by: Oscar Dowson <[email protected]>
1 parent edb31fe commit 86a4cf1

File tree

2 files changed

+250
-217
lines changed

2 files changed

+250
-217
lines changed

test/linear_program.jl

Lines changed: 250 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,250 @@
1+
# Copyright (c) 2020: Akshay Sharma and contributors
2+
#
3+
# Use of this source code is governed by an MIT-style license that can be found
4+
# in the LICENSE.md file or at https://opensource.org/licenses/MIT.
5+
6+
module TestLinearProgram
7+
8+
using Test
9+
import DiffOpt
10+
import HiGHS
11+
import Ipopt
12+
import MathOptInterface as MOI
13+
import SCS
14+
15+
const ATOL = 2e-4
16+
const RTOL = 2e-4
17+
18+
function runtests()
19+
for name in names(@__MODULE__; all = true)
20+
if startswith("$name", "test_")
21+
@testset "$(name)" begin
22+
getfield(@__MODULE__, name)()
23+
end
24+
end
25+
end
26+
return
27+
end
28+
29+
include(joinpath(@__DIR__, "utils.jl"))
30+
31+
function test_differentiating_LP_checking_gradients_for_non_active_contraints()
32+
# Issue #40 from Gurobi.jl
33+
# min x
34+
# s.t. x >= 0
35+
# x >= 3
36+
nz = 1
37+
qp_test_with_solutions(
38+
HiGHS.Optimizer;
39+
q = ones(nz),
40+
G = -ones(2, nz),
41+
h = [0.0, -3.0],
42+
dzb = ones(nz),
43+
dqf = ones(nz),
44+
# Expected solutions
45+
dGb = [0.0, 3.0],
46+
dhb = [0.0, -1.0],
47+
)
48+
return
49+
end
50+
51+
function test_differentiating_a_simple_LP_with_GreaterThan_constraint()
52+
# this is canonically same as above test
53+
# min x
54+
# s.t. x >= 3
55+
nz = 1
56+
qp_test_with_solutions(
57+
Ipopt.Optimizer;
58+
q = ones(nz),
59+
G = -ones(1, nz),
60+
h = [-3.0],
61+
dzb = ones(nz),
62+
dqf = ones(nz),
63+
# Expected solutions
64+
dGb = [3.0],
65+
dhb = [-1.0],
66+
)
67+
return
68+
end
69+
70+
function test_differentiating_lp()
71+
# refered from - https://en.wikipedia.org/wiki/Simplex_algorithm#Example
72+
# max 2x + 3y + 4z
73+
# s.t. 3x+2y+z <= 10
74+
# 2x+5y+3z <= 15
75+
# x,y,z >= 0
76+
nz = 3
77+
qp_test_with_solutions(
78+
SCS.Optimizer;
79+
q = [-2.0, -3.0, -4.0],
80+
G = [
81+
3.0 2.0 1.0
82+
2.0 5.0 3.0
83+
-1.0 0.0 0.0
84+
0.0 -1.0 0.0
85+
0.0 0.0 -1.0
86+
],
87+
h = [10.0, 15.0, 0.0, 0.0, 0.0],
88+
dzb = ones(nz),
89+
dqf = ones(nz),
90+
# Expected solutions
91+
dqb = zeros(nz),
92+
dGb = [
93+
0.0 0.0 0.0
94+
0.0 0.0 -5/3
95+
0.0 0.0 5/3
96+
0.0 0.0 -10/3
97+
0.0 0.0 0.0
98+
],
99+
dhb = [0.0, 1 / 3, -1 / 3, 2 / 3, 0.0],
100+
)
101+
return
102+
end
103+
104+
function test_differentiating_LP_with_variable_bounds()
105+
# max 2x + 3y + 4z
106+
# s.t. 3x+2y+z <= 10
107+
# 2x+5y+3z <= 15
108+
# x ≤ 3
109+
# 0 ≤ y ≤ 2
110+
# z ≥ 2
111+
# x,y,z >= 0
112+
# variant of previous test with same solution
113+
nz = 3
114+
qp_test_with_solutions(
115+
HiGHS.Optimizer;
116+
q = [-2.0, -3.0, -4.0],
117+
G = [
118+
3.0 2.0 1.0
119+
2.0 5.0 3.0
120+
-1.0 0.0 0.0
121+
0.0 -1.0 0.0
122+
0.0 0.0 -1.0
123+
1.0 0.0 0.0
124+
0.0 1.0 0.0
125+
0.0 0.0 1.0
126+
],
127+
h = [10.0, 15.0, 0.0, 0.0, 0.0, 3.0, 2.0, 6.0],
128+
dzb = ones(nz),
129+
dqf = ones(nz),
130+
# Expected solutions
131+
dqb = zeros(nz),
132+
dGb = [
133+
0.0 0.0 0.0
134+
0.0 0.0 -5/3
135+
0.0 0.0 5/3
136+
0.0 0.0 -10/3
137+
0.0 0.0 0.0
138+
0.0 0.0 0.0
139+
0.0 0.0 0.0
140+
0.0 0.0 0.0
141+
],
142+
dhb = [0.0, 1 / 3, -1 / 3, 2 / 3, 0.0, 0.0, 0.0, 0.0],
143+
)
144+
return
145+
end
146+
147+
function test_differentiating_LP_with_variable_bounds_2()
148+
nz = 3
149+
qp_test_with_solutions(
150+
HiGHS.Optimizer;
151+
q = [-2.0, -3.0, -4.0],
152+
G = [
153+
3.0 2.0 1.0
154+
2.0 5.0 3.0
155+
0.0 -1.0 0.0
156+
0.0 0.0 -1.0
157+
],
158+
h = [10.0, 15.0, 0.0, 0.0],
159+
fix_indices = [1],
160+
fix_values = [0.0],
161+
dzb = ones(nz),
162+
dqf = ones(nz),
163+
# Expected solutions
164+
dqb = zeros(nz),
165+
dGb = [
166+
0.0 0.0 0.0
167+
0.0 0.0 -5/3
168+
0.0 0.0 -10/3
169+
0.0 0.0 0.0
170+
],
171+
dhb = [0.0, 1 / 3, 2 / 3, 0.0],
172+
dAb = [0.0 0.0 -5 / 3],
173+
dbb = [1 / 3],
174+
)
175+
return
176+
end
177+
178+
"""
179+
max 2x + 3y + 4z
180+
s.t. 3x+2y+z <= 10
181+
2x+5y+3z <= 15
182+
x ≤ 3
183+
0 ≤ y ≤ 2
184+
6 ≥ z ≥ -1
185+
x, y, z >= 0
186+
variant of previous test with same solution
187+
"""
188+
function test_differentiating_lp_with_saf_with_le_ge_constraints()
189+
nz = 3
190+
qp_test_with_solutions(
191+
HiGHS.Optimizer;
192+
q = [-2.0, -3.0, -4.0],
193+
G = [
194+
3.0 2.0 1.0
195+
2.0 5.0 3.0
196+
-1.0 0.0 0.0
197+
0.0 -1.0 0.0
198+
0.0 0.0 -1.0
199+
],
200+
h = [10.0, 15.0, 0.0, 0.0, 0.0], #5
201+
ub_indices = [1, 2, 3],
202+
ub_values = [3.0, 2.0, 6.0],
203+
lb_indices = [1],
204+
lb_values = [-1.0],
205+
dzb = ones(nz),
206+
dqb = zeros(nz),
207+
dGb = [
208+
0.0 0.0 0.0
209+
0.0 0.0 -5/3
210+
0.0 0.0 5/3
211+
0.0 0.0 -10/3
212+
0.0 0.0 0.0
213+
0.0 0.0 0.0
214+
0.0 0.0 0.0
215+
0.0 0.0 0.0
216+
0.0 0.0 0.0
217+
],
218+
dhb = [0.0, 1 / 3, -1 / 3, 2 / 3, 0.0, 0.0, 0.0, 0.0, 0.0],
219+
)
220+
return
221+
end
222+
223+
function test_differentiating_lp_with_nonactive_constraints()
224+
# Issue #40 from Gurobi.jl
225+
# min x
226+
# s.t. x >= 0
227+
# x >= 3
228+
qp_test_with_solutions(
229+
HiGHS.Optimizer;
230+
q = [1.0],
231+
G = -ones(2, 1),
232+
h = [0.0, -3.0],
233+
dzb = -ones(1),
234+
dhf = [0.0, 1.0],
235+
# Expected solutions
236+
z = [3.0],
237+
λ = [0.0, 1.0],
238+
dzf = -ones(1),
239+
dλb = zeros(2),
240+
dhb = [0.0, 1.0],
241+
∇zb = zeros(1),
242+
∇λb = [0.0, -1.0],
243+
dλf = zeros(2),
244+
)
245+
return
246+
end
247+
248+
end # module
249+
250+
TestLinearProgram.runtests()

0 commit comments

Comments
 (0)