-
Notifications
You must be signed in to change notification settings - Fork 0
/
interface.jl
140 lines (93 loc) · 2.6 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using ForwardDiff, LinearAlgebra
J(r, ϕ, θ) =
[[sin(θ) * cos(ϕ), r * cos(θ) * cos(ϕ), -r * sin(θ) * sin(ϕ)],
[sin(θ) * sin(ϕ), r * cos(θ) * sin(ϕ), r * sin(θ) * cos(ϕ)],
[cos(θ), -r * sin(θ), 0]]
struct RestrainedProblem
χ::Float64
h::Vector{Float64}
mₚ::Vector{Float64}
S::Function
∂S::Function
∂²S::Function
obj::Function
∇obj::Function
∇²obj::Function
objOnBall::Function
∇objOnBall::Function
∇²objOnBall::Function
end
function RestrainedProblem(χ, h, mₚ, S; kwags...)
if !haskey(kwags, :jac)
jac(x) = ForwardDiff.gradient(S, x)
else
jac = kwags[:jac]
end
if !haskey(kwags, :hes)
hes(x) = ForwardDiff.hessian(S, x)
else
hes = kwags[:hes]
end
transformToEuklidean(ϕ, θ, r, h) = h + [r * sin(θ) * cos(ϕ), r * sin(θ) * sin(ϕ), r * cos(θ)]
obj(u) = S(u) - u ⋅ mₚ
∇obj(u) = jac(u) - mₚ
∇²obj = hes
objᵩ(x) = obj(transformToEuklidean(x..., χ, h))
∇objᵩ(x) = ForwardDiff.gradient(objᵩ, x)
∇²objᵩ(x) = ForwardDiff.hessian(objᵩ, x)
RestrainedProblem(χ, h, mₚ, S, jac, hes, obj, ∇obj, ∇²obj, objᵩ, ∇objᵩ, ∇²objᵩ)
end
struct UnrestrainedProblem
χ::Float64
h::Vector{Float64}
mₚ::Vector{Float64}
U::Function
∂U::Function
∂²U::Function
diffPart::Function
∇diffPart::Function
obj::Function
∇obj::Function
∇²obj::Function
end
function UnrestrainedProblem(χ, h, mₚ, U; kwags...)
if :jac ∉ keys(kwags)
jac(x) = ForwardDiff.gradient(U, x)
else
jac = kwags[:jac]
end
if :hes ∉ keys(kwags)
hes(x) = ForwardDiff.hessian(U, x)
else
hes = kwags[:hes]
end
diffPart(m) = U(m) - h ⋅ m
∇diffPart(m) = jac(m) - h
obj(m) = U(m) - h ⋅ m + χ*norm(m - mₚ)
∇obj(m) = jac(m) - h + χ/norm(m - mₚ) * (m - mₚ)
∇²obj(m) = hes(m) + χ * (1/norm(m - mₚ) * I - 1/norm(m - mₚ)^3 * (m - mₚ) * (m - mₚ)')
UnrestrainedProblem(χ, h, mₚ, U, jac, hes,diffPart,∇diffPart,obj,∇obj,∇²obj)
end
mutable struct Interface{T}
prob::T
x0::Vector{Float64}
xk::Vector{Float64}
err::Float64
end
function Interface(prob::RestrainedProblem, x0)
xk = x0
err = Inf64
Interface(prob, x0, xk, err)
end
function Interface(prob::UnrestrainedProblem,x0)
xk = x0
err = Inf64
Interface(prob, x0, xk, err)
end
struct Solution
prob
xk::Vector{Float64}
err::Float64
convergent::Bool
iter :: Int64
end