-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathinterface.jl
62 lines (57 loc) · 1.45 KB
/
interface.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
function _unchecked_value!(obj, x)
obj.f_calls .+= 1
copy!(obj.last_x_f, x)
obj.f_x = obj.f(x)
end
function value(obj, x)
if x != obj.last_x_f
obj.f_calls += 1
return obj.f(x)
end
obj.f_x
end
function value!(obj, x)
if x != obj.last_x_f
_unchecked_value!(obj, x)
end
obj.f_x
end
function _unchecked_gradient!(obj, x)
obj.g_calls .+= 1
copy!(obj.last_x_g, x)
obj.g!(obj.g, x)
end
function gradient!(obj::AbstractObjective, x)
if x != obj.last_x_g
_unchecked_gradient!(obj, x)
end
end
function value_gradient!(obj::AbstractObjective, x)
if x != obj.last_x_f && x != obj.last_x_g
obj.f_calls .+= 1
obj.g_calls .+= 1
copy!(obj.last_x_f, x)
copy!(obj.last_x_g, x)
obj.f_x = obj.fg!(obj.g, x)
elseif x != obj.last_x_f
_unchecked_value!(obj, x)
elseif x != obj.last_x_g
_unchecked_gradient!(obj, x)
end
obj.f_x
end
function _unchecked_hessian!(obj::AbstractObjective, x)
obj.h_calls .+= 1
copy!(obj.last_x_h, x)
obj.h!(obj.H, x)
end
function hessian!(obj::AbstractObjective, x)
if x != obj.last_x_h
_unchecked_hessian!(obj, x)
end
end
# Getters are without ! and accept only an objective and index or just an objective
value(obj::AbstractObjective) = obj.f_x
gradient(obj::AbstractObjective) = obj.g
gradient(obj::AbstractObjective, i::Integer) = obj.g[i]
hessian(obj::AbstractObjective) = obj.H