Skip to content

Commit

Permalink
Updated tests, one test failing.
Browse files Browse the repository at this point in the history
  • Loading branch information
olof3 committed Sep 20, 2017
1 parent 3b85cf3 commit a495127
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 11 deletions.
25 changes: 25 additions & 0 deletions test/problems/square_wave_problem.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
function square_wave_problem()

g = kron([1, -1, 1, -1, 1], ones(5))

M_bf = 5

ζvec = [100, 30, 10, 3, 1, 0.3, 0.1, 0.03, 0.01]

# For each m there are many non-unique solution
I_sols =
[Vector{Int}() for m=1:24]

f_sols =
[[24.0,
18.10194908759502,
15.757575757575758,
6.602807597027249,
4.84848484848485,
4.285714285714288,
2.4242424242424248,
2.1356521739130434];
zeros(Float64, 16)]

return g, M_bf, ζvec, I_sols, f_sols
end
17 changes: 17 additions & 0 deletions test/problems/straight_line_problem.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function straight_line_problem()

g = 0:0.02:1

M_bf = 2

ζvec = [100, 30, 10, 3, 1, 0.3, 0.1, 0.03, 0.01]

# For each m there are many non-unique solution
I_sols =
[[[1, length(g)]];
[Vector{Int}() for m=1:9]]

f_sols = zeros(Float64, 10)

return g, M_bf, ζvec, I_sols, f_sols
end
27 changes: 16 additions & 11 deletions test/test_discrete_fit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@ include(joinpath(Pkg.dir("DynamicApproximations"),"src","dev.jl"))
include(joinpath(Pkg.dir("DynamicApproximations"),"test","problems", "snp500_problem.jl"))
include(joinpath(Pkg.dir("DynamicApproximations"),"test","problems", "exp_problem.jl"))
include(joinpath(Pkg.dir("DynamicApproximations"),"test","problems", "square_wave_problem.jl"))
include(joinpath(Pkg.dir("DynamicApproximations"),"test","problems", "straight_line_problem.jl"))

##

@testset "Data set: $problem_fcn" for problem_fcn in [exp_problem, snp500_problem]
@testset "Data set: $problem_fcn" for problem_fcn in [straight_line_problem, square_wave_problem, exp_problem, snp500_problem]

##
#problem_fcn = snp500_problem
problem_fcn = straight_line_problem
g, M_bf, ζvec, I_sols, f_sols = problem_fcn()

g = 0:0.1:1

M = length(I_sols)

#g = exp.(linspace(0,5,11))
Expand All @@ -24,24 +27,26 @@ cost_last = dev.QuadraticPolynomial(1.0, -2*g[end], g[end]^2)
Λ = dev.find_optimal_fit(ℓ, cost_last, M, Inf);

@testset "Data set: $problem_fcn, constrained with m=$m" for m in 1:M
I2, y2, f2 = dev.recover_optimal_index_set(Λ[m, 1])
Y2, f2_2 = dev.find_optimal_y_values(ℓ, cost_last, I2)
I, Y, f = dev.recover_solution(Λ[m, 1], ℓ, cost_last)

@test I2 == I_sols[m]
@test Y2[1] y2 rtol=1e-10
@test f2 f_sols[m] rtol=1e-10
@test isempty(I_sols[m]) || I == I_sols[m]
@test f f_sols[m] rtol=1e-10 atol=1e-10
end


##

@testset "Data set: $problem_fcn, bruteforce with m=$m" for m in 1:M_bf
Y_recovered, f_recovered = dev.find_optimal_y_values(ℓ, cost_last, I_sols[m])

I_bf, Y_bf, f_bf = dev.brute_force_optimization(ℓ, cost_last, m)

@test I_bf == I_sols[m]
@test Y_bf Y_recovered rtol=1e-10
@test isempty(I_sols[m]) || I_bf == I_sols[m]
@test f_bf f_sols[m] rtol=1e-10 atol=1e-10

if !isempty(I_sols[m])
Y_recovered, f_recovered = dev.find_optimal_y_values(ℓ, cost_last, I_sols[m])
@test Y_bf Y_recovered rtol=1e-10
end
end

##
Expand All @@ -59,7 +64,7 @@ end
m_expected = indmin([f_sols[m] + m*ζ for m=1:length(f_sols)])

@test m_expected == length(I) - 1
@test I == I_sols[m_expected]
@test isempty(I_sols[m]) || I == I_sols[m_expected]
@test f_reg f_sols[m_expected] + ζ*m_expected

#println(problem_fcn, " " , length(I))
Expand Down

0 comments on commit a495127

Please sign in to comment.