We are generalising the following julia code
to two or more dimensional grids.
The following is the output (for each grid point):
"Number of Iterations…: 0
Number of objective function evaluations = 0
Number of objective gradient evaluations = 1
Number of equality constraint evaluations = 0
Number of inequality constraint evaluations = 1
Number of equality constraint Jacobian evaluations = 1
Number of inequality constraint Jacobian evaluations = 1
Number of Lagrangian Hessian evaluations = 0
Total CPU secs in IPOPT (w/o function evaluations) = 0.026
Total CPU secs in NLP function evaluations = 0.000
EXIT: Invalid number in NLP function or derivative detected."
The following is the full working example:
using MathOptInterface, LinearAlgebra, Statistics, Plots, QuantEcon, Interpolations, NLsolve, Optim, Random, IterTools, JuMP, Ipopt, MathOptInterface;
u(x) = sum(log(x[i]) for i in 1:numSectors)
w(x) = sum(5*log(x[i]) for i in 1:numSectors)
β = 0.96
α = 0.4
f(x) = x^α
numSectors = 2
numPoints1D = 2
grid = Vector{Vector{Float64}}(undef,(numPoints1D)^numSectors)
gridMax = 2
gridMin = 1
iter=1
for p in product(LinRange(gridMin,gridMax,numPoints1D),LinRange(gridMin,gridMax,numPoints1D))
grid[iter] = collect(p)
global iter += 1
end
wVal = w.(grid)
function T(wVal, grid, β, f ; compute_policy = false)
wVals = zeros(numPoints1D,numPoints1D);
for j in numPoints1D
for i in numPoints1D
wVals[i,j] = wVal[i+(i-1)*j]
end
end
function wFunc(x,y)
return interpolate((LinRange(gridMin,gridMax,numPoints1D),LinRange(gridMin,gridMax,numPoints1D)), wVals, Gridded(Linear()))(x,y)
end
Tw = zeros(length(grid))
σ = similar(grid)
for n in 1:length(grid)
y = grid[n]
modTrial = Model(Ipopt.Optimizer);
@variable(modTrial, c[1:numSectors])
@variable(modTrial, k[1:numSectors], start=1.5)
for i in 1:numSectors
@constraint(modTrial, 0.99*gridMin <= c[i] <= 0.999*y[i])
@constraint(modTrial, k[i] == y[i] - c[i])
end
register(modTrial, :wFunc, 2, wFunc; autodiff = true)
register(modTrial, :f, 1, f; autodiff = true)
@NLobjective(modTrial, Max, sum(log(c[i]) for i in 1:numSectors) + β*wFunc(f(k[1]),f(k[2])))
optimize!(modTrial)
Tw[n] = JuMP.objective_value(modTrial)
if compute_policy
σ[n] = value.(c)
return Tw, σ
end
end
return Tw
end
wVal = T(wVal, grid, β, f)
Would anyone be able to help with what the problem is here, and a way to possibly fix it?
Thanks