Skip to content

Commit

Permalink
value(d, x) for multivalued objective (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
iewaij authored and pkofod committed Aug 15, 2018
1 parent bf43441 commit e1d63de
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
12 changes: 11 additions & 1 deletion src/interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,19 @@ function jacobian(obj::AbstractObjective, x)
obj.DF
end

value(obj::NonDifferentiable{TF, TX}, x) where {TF<:AbstractArray, TX} = value(obj, copy(obj.F), x)
value(obj::OnceDifferentiable{TF, TDF, TX}, x) where {TF<:AbstractArray, TDF, TX} = value(obj, copy(obj.F), x)
function value(obj::AbstractObjective, F, x)
if x != obj.x_f
obj.f_calls .+= 1
return obj.f(F, x)
end
value(obj)
end

value!!(obj::NonDifferentiable{TF, TX}, x) where {TF<:AbstractArray, TX} = value!!(obj, obj.F, x)
value!!(obj::OnceDifferentiable{TF, TDF, TX}, x) where {TF<:AbstractArray, TDF, TX} = value!!(obj, obj.F, x)
function value!!(obj, F, x)
function value!!(obj::AbstractObjective, F, x)
obj.f(F, x)
copyto!(obj.x_f, x)
obj.f_calls .+= 1
Expand Down
17 changes: 9 additions & 8 deletions test/interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -223,27 +223,28 @@
# calculated by the value(obj, x) methods
@test value(nd) == value(od) == F_x_seed
@test value(nd, x_seed) == value(od, x_seed)
@test value(nd, x_alt) == value(od, x_alt)

# Test that the Jacobians match the intended values
@test jacobian(od) == J_x_seed

# Test that the call counters got incremented
@test nd.f_calls == od.f_calls == [1]
@test nd.f_calls == od.f_calls == [2]
@test od.df_calls == [1]

# Test that the call counters do not get incremented
# with single-"bang" methods...
value!(nd, x_seed)
value_jacobian!(od, x_seed)

@test nd.f_calls == od.f_calls == [1]
@test nd.f_calls == od.f_calls == [2]
@test od.df_calls == [1]

# ... and that they do with double-"bang" methods
value!!(nd, x_seed)
value_jacobian!!(od, x_seed)

@test nd.f_calls == od.f_calls == [2]
@test nd.f_calls == od.f_calls == [3]
@test od.df_calls == [2]

# Test that jacobian doesn't work for NonDifferentiable, but does otherwise
Expand All @@ -252,19 +253,19 @@

@test value(nd) == value(od) == F_x_seed
@test jacobian(od) == J_x_alt
@test nd.f_calls == od.f_calls == [2]
@test nd.f_calls == od.f_calls == [3]
@test od.df_calls == [3]

@test value(nd) == value(od) == F_x_seed
@test jacobian(od) == J_x_alt
@test nd.f_calls == od.f_calls == [2]
@test nd.f_calls == od.f_calls == [3]
@test od.df_calls == [3]

value!(nd, x_alt)
value!(od, x_alt)
@test value(nd) == value(od) == F_x_alt
@test jacobian(od) == J_x_alt
@test nd.f_calls == od.f_calls == [3]
@test nd.f_calls == od.f_calls == [4]
@test od.df_calls == [3]

@test_throws ErrorException value_jacobian!(nd, x_seed)
Expand All @@ -281,7 +282,7 @@
value_jacobian!(od, x_seed)
@test value(od) == F_x_seed
@test jacobian(od) == J_x_seed
@test od.f_calls == [4]
@test od.f_calls == [5]
@test od.df_calls == [4]

clear!(nd)
Expand Down Expand Up @@ -310,4 +311,4 @@
@test xxx3 == od.x_df

end
end
end

0 comments on commit e1d63de

Please sign in to comment.