-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathMultivariateProblems.jl
80 lines (67 loc) · 2.44 KB
/
MultivariateProblems.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
module MultivariateProblems
import LinearAlgebra: dot
export UnconstrainedProblems
export OptimizationProblem, objective, gradient, objective_gradient, hessian
struct ConstraintData{F,J,H,Tx,Tc}
c!::F
jacobian!::J
h!::H
lx::Vector{Tx}
ux::Vector{Tx}
lc::Vector{Tc}
uc::Vector{Tc}
end
struct OptimizationProblem{P, Tfg, Tf <: Real, TS <: AbstractString,
CT <: Union{Nothing,ConstraintData}}
name::TS
f::Function
g!::Function
fg!::Tfg
h!::Function
constraintdata::CT
initial_x::Vector
solutions::Vector
minimum::Tf
isdifferentiable::Bool
istwicedifferentiable::Bool
parameters::P
end
OptimizationProblem(name::AbstractString,
f::Function,
g!::Function,
fg!::Tfg,
h!::Function,
constraints::Union{Nothing,ConstraintData},
initial_x::Vector,
solutions::Vector,
minimum::Tf,
isdifferentiable::Bool,
istwicedifferentiable::Bool) where Tf where Tfg =
OptimizationProblem(name, f, g!, fg!, h!, constraints,
initial_x, solutions, minimum,
isdifferentiable,
istwicedifferentiable,
nothing)
objective(p::OptimizationProblem{P}) where P<:Nothing = p.f
gradient(p::OptimizationProblem{P}) where P<:Nothing = p.g!
objective_gradient(p::OptimizationProblem{P}) where P<:Nothing = p.fg!
hessian(p::OptimizationProblem{P}) where P<:Nothing = p.h!
objective(p::OptimizationProblem{P}) where P = x-> p.f(x,p.parameters)
gradient(p::OptimizationProblem{P}) where P = (out,x)-> p.g!(out,x,p.parameters)
objective_gradient(p::OptimizationProblem{P}) where P = (out,x)-> p.fg!(out,x,p.parameters)
hessian(p::OptimizationProblem{P}) where P = (out,x)-> p.h!(out,x,p.parameters)
function objective_gradient(p::OptimizationProblem{P,Tfg}) where P where Tfg <: Nothing
(out,x) -> begin
gradient(p)(out,x)
return objective(p)(x)
end
end
function objective_gradient(p::OptimizationProblem{P,Tfg}) where P <: Nothing where Tfg <: Nothing
(out,x) -> begin
gradient(p)(out,x)
return objective(p)(x)
end
end
include("unconstrained.jl")
include("constrained.jl")
end