I’m trying to optimize a function that has an integral in the objective. While I can perform unconstrained optimization, I’m unsure how to use the Julia optimizer. I am trying to do this to add a constraint later, which itself is an integral.
using JuMP, Ipopt, Optim, ForwardDiff, QuadGK, NLsolve, Roots, LinearAlgebra, Base.Threads, Distributions
Set parameters
begin
r = 0.2
α = 0.85
s = 0.5
β = 0.9
A = 1.0
w = 10.0
γ = 0.0
ll = 0.0 # lower limit for optimization
ul = 1500.0 # upper limit for optimization
Dθ = Uniform(0.0, 5.0)
end
Define second period functions
begin
θ̃(b, α, w, A, r, s) = min(max(R̄(b,r)/f(b,α),0.0),5.0)
f(b,α) = b^α
R̄(b,r) = (1+r)*b
endY₂(b,θ,α) = θ*f(b,α)
end#C₂(R₂,b,θ,α,w) = Y₂(b,θ,α) - R₂ + w
end
Objective
begin
Objhᵦ(b, θ, α, w, A, r, s, β) = β * (A*log(w) - s*(R̄(b,r)-Y₂(b,θ,α)))
Objhᵪ(b, θ, α, w, A, r, s, β) = β * (A*log(Y₂(b,θ,α) - R̄(b,r) + w))
end
Integration over Dθ
begin
function FPopthᵦ(D, b, α, w, A, r, s, β)
θ̲ = minimum(D)
θ̄ = θ̃(b, α, w, A, r, s)
return quadgk(y -> Objhᵦ(b, y, α, w, A, r, s, β)*pdf(D, y), θ̲, θ̄)[1]
end
function FPopthᵪ(D, b, α, w, A, r, s, β)
θ̲ = θ̃(b, α, w, A, r, s)
θ̄ = maximum(D)
return quadgk(y -> Objhᵪ(b, y, α, w, A, r, s, β)*pdf(D, y), θ̲, θ̄)[1]
end
### objective function
FPθh(Dθ, b, α, w, A, r, s, β) = FPopthᵦ(Dθ, b, α, w, A, r, s, β) + FPopthᵪ(Dθ, b, α, w, A, r, s, β)
end
Outcome
optimize(b -> -FPθh(Dθ, b, α, w, A, r, s, β), ll, ul).minimizer
How to use Julia optimizer for the problem stated above?
function Opt()
model = Model(Ipopt.Optimizer)
@variable(model, 1 <= b <= 2, start = 1)
register(model, :FP, 7, FPθh; autodiff = true)
@NLobjective(model, Max, FPθ(Dθ, b, α, w, A, r, s, β))
optimize!(model)
return (b = value(b), value = objective_value(model))
end
This doesn’t work:
Opt()
Finally, how to add the constraint?
begin
function πbh(D, b, α, w, A, r, s)
θ̲ = minimum(D)
θ̄ = θ̃(b, α, w, A, r, s)
return quadgk(y -> Y₂(b,y,α)*pdf(D, y), θ̲, θ̄)[1]
end
function πch(D, b, α, w, A, r, s)
θ̲ = θ̃(b, α, w, A, r, s)
θ̄ = maximum(D)
return quadgk(y -> R̄(b,r)*pdf(D, y), θ̲, θ̄)[1]
end
πθh(Dθ, b, α, w, A, r, s, γ) = πbh(Dθ, b, α, w, A, r, s)+πch(Dθ, b, α, w, A, r, s) - (1+γ)*b
end