Skip to content

Commit 348e2a4

Browse files
committed
added tests for benchmarking and removed @parallel because it was messing things up
1 parent 39814c4 commit 348e2a4

File tree

5 files changed

+116
-16
lines changed

5 files changed

+116
-16
lines changed

REQUIRE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ KNITRO
88
Interpolations
99
MathProgBase
1010
CSV
11+
Images

src/Base.jl

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -173,17 +173,17 @@ function interpolateLagrange!(n; numPts::Int64=250, tfOptimal::Any=false)
173173
x_int, u_int = intervals(n, int, copy(n.r.X), n.r.U)
174174

175175
# sample polynomial in interval at n.r.t_polyPts
176-
@parallel for st in 1:n.numStates
176+
for st in 1:n.numStates
177177
n.r.X_polyPts[st][int] = interpolate_lagrange(n.r.t_polyPts[int], t_st_int[int], x_int[:,st])'
178178
end
179179

180-
@parallel for ctr in 1:n.numControls
180+
for ctr in 1:n.numControls
181181
n.r.U_polyPts[ctr][int] = interpolate_lagrange(n.r.t_polyPts[int], t_ctr_int[int], u_int[:,ctr])'
182182
end
183183

184184
# sample polynomial in interval at n.r.t_polyPts NOTE costate is missing the last point, that is the t_st_int[int][1:end-1]
185185
if n.s.evalCostates && n.s.evalConstraints
186-
@parallel for st in 1:n.numStates
186+
for st in 1:n.numStates
187187
n.r.CS_polyPts[st][int] = interpolate_lagrange(n.r.t_polyPts[int], t_st_int[int][1:end-1], n.r.CS[st][int])'
188188
end
189189
end
@@ -195,20 +195,20 @@ function interpolateLagrange!(n; numPts::Int64=250, tfOptimal::Any=false)
195195
totalPts = length(n.r.t_pts);
196196

197197
n.r.X_pts = Matrix{Float64}(totalPts, n.numStates)
198-
@parallel for st in 1:n.numStates # states
198+
for st in 1:n.numStates # states
199199
temp = [n.r.X_polyPts[st][int][1:end,:] for int in 1:n.Ni];
200200
n.r.X_pts[:,st] = [idx for tempM in temp for idx=tempM];
201201
end
202202

203203
n.r.U_pts = Matrix{Float64}(totalPts, n.numControls)
204-
@parallel for ctr in 1:n.numControls # controls
204+
for ctr in 1:n.numControls # controls
205205
temp = [n.r.U_polyPts[ctr][int][1:end,:] for int in 1:n.Ni];
206206
n.r.U_pts[:,ctr] = [idx for tempM in temp for idx=tempM];
207207
end
208208

209209
if n.s.evalCostates && n.s.evalConstraints
210210
n.r.CS_pts = Matrix{Float64}(totalPts, n.numStates)
211-
@parallel for st in 1:n.numStates # states
211+
for st in 1:n.numStates # states
212212
temp = [n.r.CS_polyPts[st][int][1:end,:] for int in 1:n.Ni];
213213
n.r.CS_pts[:,st] = [idx for tempM in temp for idx=tempM];
214214
end
@@ -241,12 +241,12 @@ function interpolateLinear!(n; numPts::Int64=250, tfOptimal::Any=false)
241241
n.r.U_pts = Matrix{Float64}(numPts, n.numControls)
242242

243243
knots = (n.r.t_st,)
244-
@parallel for st in 1:n.numStates
244+
for st in 1:n.numStates
245245
sp_st = interpolate(knots,n.r.X[:,st],Gridded(Linear()))
246246
n.r.X_pts[:,st] = sp_st[n.r.t_pts]
247247
end
248248

249-
@parallel for ctr in 1:n.numControls
249+
for ctr in 1:n.numControls
250250
sp_ctr = interpolate(knots,n.r.U[:,ctr],Gridded(Linear()))
251251
n.r.U_pts[:,ctr] = sp_ctr[n.r.t_pts]
252252
end
@@ -277,11 +277,11 @@ function plant2dfs!(n,sol)
277277
dfs_plant=DataFrame();
278278
dfs_plant[:t]=t_sample;
279279

280-
@parallel for st in 1:n.numStates
280+
for st in 1:n.numStates
281281
dfs_plant[n.state.name[st]]=[sol(t)[st] for t in t_sample];
282282
end
283283

284-
@parallel for ctr in 1:n.numControls
284+
for ctr in 1:n.numControls
285285
dfs_plant[n.control.name[ctr]]= n.r.U[ctr];
286286
end
287287

@@ -305,10 +305,10 @@ Date Create: 2/10/2017, Last Modified: 11/10/2017 \n
305305
function dvs2dfs(n)
306306
dfs=DataFrame()
307307
dfs[:t]=n.r.t_st + n.mpc.t0;
308-
@parallel for st in 1:n.numStates
308+
for st in 1:n.numStates
309309
dfs[n.state.name[st]]=n.r.X[:,st];
310310
end
311-
@parallel for ctr in 1:n.numControls
311+
for ctr in 1:n.numControls
312312
if n.s.integrationMethod==:tm
313313
dfs[n.control.name[ctr]]=n.r.U[:,ctr];
314314
else
@@ -318,12 +318,12 @@ function dvs2dfs(n)
318318

319319
if n.s.evalCostates && n.s.integrationMethod == :ps && n.s.evalConstraints
320320
CS_vector = Matrix{Float64}(n.numStatePoints, n.numStates)
321-
@parallel for st in 1:n.numStates # states
321+
for st in 1:n.numStates # states
322322
temp = [n.r.CS[st][int][1:end,:] for int in 1:n.Ni];
323323
CS_vector[1:end-1,st] = [idx for tempM in temp for idx=tempM];
324324
CS_vector[end,st] = NaN;
325325
end
326-
@parallel for st in 1:n.numStates # states
326+
for st in 1:n.numStates # states
327327
dfs[Symbol(n.state.name[st],:_cs)]=CS_vector[:,st];
328328
end
329329
end

src/NLOptControl.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ using FastGaussQuadrature
1010
using DataFrames
1111
using Ranges
1212
using CSV
13+
using Images
1314

1415
include("Base.jl")
1516
using .Base

test/ocp.jl

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,100 @@ dx=Array{Expr}(2);dx[1]=:(x[j]);dx[2]=:(u[j]-1.625);
3636
@show n.r.dfs_opt[1][:t_solve][1]
3737
@test isapprox(8.9253,n.r.obj_val[1],atol=tol)
3838
end
39+
40+
############################
41+
# Benchmarking Test
42+
v0 = -2
43+
h0 = 10
44+
pts = 10000
45+
tf_star = 2/3*v0 +4/3*(1/2*v0^2+3/2*h0)^0.5
46+
ts = tf_star/2+v0/3
47+
48+
function optimal_solution(t)
49+
if t < ts
50+
h = -3/4*t^2 + v0*t + h0
51+
v = -3/2*t + v0
52+
u = 0
53+
else
54+
h = 3/4*t^2 + (-3*ts + v0)*t + 3/2*ts^2 + h0
55+
v = 3/2*t + (-3*ts + v0)
56+
u = 3
57+
end
58+
return h, v, u
59+
end
60+
61+
t_opt = linspace(0,tf_star,pts)
62+
h_opt = zeros(pts); v_opt = zeros(pts); u_opt = zeros(pts);
63+
for i in 1:pts
64+
h, v, u = optimal_solution(t_opt[i])
65+
h_opt[i] = h; v_opt[i] = v; u_opt[i] = u;
66+
end
67+
68+
opt_runs = 2
69+
col_pts = 10:2:12
70+
Nck_vec = [[col_pts[i]] for i in 1:length(col_pts)]
71+
opt_num = length(Nck_vec)
72+
73+
@testset "MoonLander test to make sure that a benchmark test will be able to be executed with (:integrationScheme=>$(integrationConfig)) " for integrationConfig in integrationConfigs
74+
75+
# final average results
76+
t_opt_ave = NaN*zeros(opt_num)
77+
h_error_ave = NaN*zeros(opt_num)
78+
v_error_ave = NaN*zeros(opt_num)
79+
u_error_ave = NaN*zeros(opt_num)
80+
max_error_ave = NaN*zeros(opt_num)
81+
82+
for num in 1:length(col_pts)
83+
n=define(numStates=2,numControls=1,X0=[h0,v0],XF=[0.,0.],XL=[-20,-20],XU=[20,20],CL=[0.],CU=[3.]);
84+
n.s.tf_max = 1000.0
85+
n.s.numInterpPts = pts # must be the same as above to calculate error
86+
n.s.tfOptimal = tf_star # must be the same as above to calculate error
87+
states!(n,[:h,:v];descriptions=["h(t)","v(t)"]);
88+
controls!(n,[:T];descriptions=["T(t)"]);
89+
dx=[:(v[j]),:(T[j]-1.5)]
90+
dynamics!(n,dx)
91+
92+
if (integrationConfig == :lgrImplicit || integrationConfig == :lgrExplicit)
93+
Nck = Nck_vec[num]
94+
configure!(n;(:integrationScheme=>integrationConfig),(:Nck=>Nck),(:finalTimeDV=>true));
95+
else
96+
configure!(n;(:integrationScheme=>integrationConfig),(:N=>col_pts[num]),(:finalTimeDV=>true));
97+
end
98+
x1=n.r.x[:,1];x2=n.r.x[:,2];
99+
obj=integrate!(n,:(T[j]));
100+
@NLobjective(n.mdl, Min, obj);
101+
setvalue(n.tf, 1.5)
102+
for i in 1:length(x1); setvalue(x1[i], 0.0); setvalue(x2[i], 0.0); end
103+
# cache functions; inital optimization
104+
optimize!(n);
105+
106+
# RESET temp variables for averaging results
107+
t_solve = NaN*zeros(opt_runs)
108+
h_error = NaN*zeros(opt_runs)
109+
v_error = NaN*zeros(opt_runs)
110+
u_error = NaN*zeros(opt_runs)
111+
max_error = NaN*zeros(opt_runs)
112+
113+
for j in 1:opt_runs
114+
optimize!(n);
115+
if n.r.status == :Optimal
116+
t_solve[j] = n.r.t_solve
117+
h_error[j] = maximum(abs.(n.r.X_pts[:,1] - h_opt))
118+
v_error[j] = maximum(abs.(n.r.X_pts[:,2] - v_opt))
119+
u_error[j] = maximum(abs.(n.r.U_pts[:,1] - u_opt))
120+
max_error[j] = maximum([h_error[j], v_error[j]])
121+
end
122+
end
123+
124+
t_opt_ave[num] = Images.meanfinite(t_solve,1)[1]
125+
h_error_ave[num] = Images.meanfinite(h_error,1)[1]
126+
v_error_ave[num] = Images.meanfinite(v_error,1)[1]
127+
u_error_ave[num] = Images.meanfinite(u_error,1)[1]
128+
max_error_ave[num] = Images.meanfinite(max_error,1)[1]
129+
end
130+
@show maximum(max_error_ave)
131+
@test isapprox(0, maximum(max_error_ave),atol=big_tol)
132+
end
133+
134+
# Benchmarking Test
135+
############################

test/runtests.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ using NLOptControl
22
using Base.Test
33

44
# constants
5-
const tol=5e-2;
6-
const integrationConfigs=[:lgrImplicit,:lgrExplicit,:trapezoidal,:bkwEuler]
5+
const tol = 5e-2
6+
const big_tol = 0.5 # can reduce if the number of points are increased
7+
const integrationConfigs = [:lgrExplicit,:lgrImplicit,:trapezoidal,:bkwEuler]
78

89
include("ocp.jl")
910

0 commit comments

Comments
 (0)