Skip to content

Commit

Permalink
Subsampled tests on hardcoded solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
mfalt committed Jan 17, 2018
1 parent cf88c3f commit caa5cb2
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 9 deletions.
8 changes: 4 additions & 4 deletions src/solve.jl
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ function pwq_dp_constrained{T}(ℓ::AbstractTransitionCost{T}, V_N::QuadraticPol

N = size(ℓ, 2)

@assert M-1 <= N "Cannot have more segments than N-1."
@assert M <= N-1 "Cannot have more segments than N-1."

Λ = Array{PiecewiseQuadratic{T}}(M, N)

Expand Down Expand Up @@ -570,9 +570,9 @@ i.e. so that `f[t[I]] .= Y`.
`lazy` = true, means that the internal transition costs `ℓ[i,j]` will be calculated when needed.
`tol` specifies the relative tolerance sent to `quadg` kused when calculating the integrals (continuous case).
"""
function fit_pwl_regularized(g::AbstractArray, ζ; lazy=true)
= lazy ? TransitionCostDiscrete{Float64}(g) :
compute_discrete_transition_costs(g)
function fit_pwl_regularized(g::AbstractArray, ζ; t=1:length(g), lazy=true)
= lazy ? TransitionCostDiscrete{Float64}(g, t) :
compute_discrete_transition_costs(g, t)
# Discrete case, so cost at endpoint is quadratic
cost_last = QuadraticPolynomial(1.0, -2*g[end], g[end]^2)
fit_pwl_regularized_internal(ℓ, cost_last, ζ)
Expand Down
17 changes: 17 additions & 0 deletions test/fit_pwl_constrained.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,21 @@ for problem_fcn in ["discontinuous1",
@test I_vec[m] == I_sols[m]
end
end

# Iterate only over problems where solution exists
@testset "Data set: $problem_fcn, subsampled, constrained fit with m=$m" for m in 1:length(f_sols)
# Generate max(length(g)/5, m) random gridpoints in range 1:length(g)
randI = shuffle(1:length(g))[1:max(floor(Int,length(g)/5), m+1)]
# Merge with correct solution, and [1,length(g)] if solution doesn't exist
t_grid = sort(union(randI, I_sols[m], [1, length(g)]))
# Solve on subsampled grid,
I_vec, _, f_vec = fit_pwl_constrained(g, m, t = t_grid)
# Should be same if we had a solution
if !isempty(I_sols[m])
@test f_vec[m] f_sols[m] atol=1e-10
@test t_grid[I_vec[m]] == I_sols[m]
else # Or higher cost if not
@test f_vec[m] >= f_sols[m] || isapprox(f_vec[m], f_sols[m], atol=1e-10)
end
end
end
22 changes: 22 additions & 0 deletions test/fit_pwl_regularized.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,26 @@ for problem_fcn in ["discontinuous1",


end

# Iterate only over problems where solution exists
@testset "Data set: $problem_fcn, subsampled, regularization with ζ=" for ζ in ζ_vec
# Use costs in the solution file to find out how many segments the solution should contain
m_expected = indmin([f_sols[m] + m*ζ for m=1:length(f_sols)])

# Generate max(length(g)/5, m) random gridpoints in range 1:length(g)
randI = shuffle(1:length(g))[1:max(floor(Int,length(g)/5), m_expected+1)]
# Merge with correct solution, and [1,length(g)] if solution doesn't exist
t_grid = sort(union(randI, I_sols[m_expected], [1, length(g)]))
# Solve on subsampled grid
I_reg, _, f_reg = fit_pwl_regularized(g, ζ; t = t_grid)

# Should be same if we had a solution
if !isempty(I_sols[m_expected])
@test m_expected == length(I_reg) - 1
@test f_reg f_sols[m_expected] atol=1e-10
@test t_grid[I_reg] == I_sols[m_expected]
else
@test f_reg >= f_sols[m_expected] || isapprox(f_reg, f_sols[m_expected], atol=1e-10)
end
end
end
2 changes: 1 addition & 1 deletion test/problems/discontinuous1.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ I_sols = [
[1, 10, 11, 15, 16, 20],
[1, 5, 10, 11, 15, 16, 20],
[1, 3, 8, 10, 11, 15, 16, 20],
[],
Int[],
[1, 2, 4, 7, 9, 10, 11, 15, 16, 20]]

f_sols = [
Expand Down
8 changes: 4 additions & 4 deletions test/problems/super_exponential5.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@ I_sols = [
[1, 3, 16, 18],
[1, 3, 15, 17, 18],
[1, 2, 4, 15, 17, 18],
[],
Int[],
[1, 2, 3, 5, 14, 16, 17, 18],
[1, 2, 3, 4, 6, 14, 16, 17, 18],
[1, 2, 3, 4, 6, 13, 15, 16, 17, 18],
[1, 2, 3, 4, 6, 12, 14, 15, 16, 17, 18],
[1, 2, 3, 4, 5, 7, 12, 14, 15, 16, 17, 18],
[],
Int[],
[1, 2, 3, 4, 5, 6, 7, 12, 13, 14, 15, 16, 17, 18],
[],
Int[],
[1, 2, 3, 4, 5, 6, 7, 8, 11, 12, 13, 14, 15, 16, 17, 18],
[],
Int[],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]]

f_sols = [
Expand Down

0 comments on commit caa5cb2

Please sign in to comment.