Hi there!
I am quite new to Julia and JuMP/InfiniteOpt. I’m trying to solve a time optimal control problem. I have an issue when I incorporate an expression using the divide operator into the objective function. The following error comes up (full stack trace below) ERROR: MethodError: no method matching check_belongs_to_model(::NonlinearExpression, ::Model)
.
I have tested some nonlinear expressions which don’t include some division or a negative power e.g. (x[1] + x[2])^2
and I don’t encounter this problem. I think that the expression should be differentiable via automatic differentiation.
The following code should recreate the
using InfiniteOpt
using Ipopt
using Plots
#initial variable values
x0 = 5
y0 = 10
x_0 = [x0, y0]
vx0 = 2
vy0 = 2
v_0 = [vx0, vy0]
ux0 = 2
uy0 = 3
u_0 = [ux0, uy0]
N = 100
x_f = [1.0,1.0]
v_f = [1.0,1.0]
model = InfiniteModel(Ipopt.Optimizer)
@infinite_parameter(model, τ ∈ [0, 1], num_supports = N)
@variables(model, begin
# state variables
x[i=1:2], Infinite(τ), (start = x_0[i])
v[j=1:2], Infinite(τ), (start = v_0[j])
# control variables
-1 <= u[k=1:2] <= 1, Infinite(τ), (start = u_0[k])
end)
@variable(model, 5 ≤ tf ≤ 15, start = 12)
#initial conditions
@constraint(model, [i = [1, 2]], x[i](0) == x_0[i])
@constraint(model, [i = [1, 2]], v[i](0) == v_0[i])
@constraint(model, [i = [1, 2]], u[i](0) == 0)
#terminal conditions
@constraint(model, [i=[1,2]], x[i](1) == 0)
@constraint(model, [i = [1, 2]], v[i](1) == 0)
# ODEs
@constraint(model, [i = 1:2], ∂(x[i], τ) == tf * v[i])
@constraint(model, [i = 1:2], ∂(v[i], τ) == tf* u[i])
I = @expression(model, (x[1] + x[2])^(-1))
@objective(model, Min, tf * ∫(1 - I + u[1]^2 + u[2]^2, τ))
optimize!(model)
The full stack trace is:
ERROR: MethodError: no method matching check_belongs_to_model(::NonlinearExpression, ::Model)
Closest candidates are:
check_belongs_to_model(::GenericAffExpr, ::AbstractModel)
@ JuMP ~/.julia/packages/JuMP/as6Ji/src/aff_expr.jl:625
check_belongs_to_model(::AbstractVariableRef, ::AbstractModel)
@ JuMP ~/.julia/packages/JuMP/as6Ji/src/variables.jl:349
check_belongs_to_model(::VectorConstraint, ::Any)
@ JuMP ~/.julia/packages/JuMP/as6Ji/src/constraints.jl:674
...
Stacktrace:
[1] check_belongs_to_model(expr::NonlinearExpr, model::Model) (repeats 2 times)
@ JuMP ~/.julia/packages/JuMP/as6Ji/src/nlp_expr.jl:523
[2] set_objective_function
@ JuMP ~/.julia/packages/JuMP/as6Ji/src/objective.jl:279 [inlined]
[3] set_objective
@ JuMP ~/.julia/packages/JuMP/as6Ji/src/objective.jl:326 [inlined]
[4] _set_objective(trans_model::Model, sense::MathOptInterface.OptimizationSense, expr::NonlinearExpr)
@ InfiniteOpt.TranscriptionOpt ~/.julia/packages/InfiniteOpt/WWtLl/src/TranscriptionOpt/transcribe.jl:582
[5] transcribe_objective!(trans_model::Model, inf_model::InfiniteModel)
@ InfiniteOpt.TranscriptionOpt ~/.julia/packages/InfiniteOpt/WWtLl/src/TranscriptionOpt/transcribe.jl:605
[6] build_transcription_model!(trans_model::Model, inf_model::InfiniteModel; check_support_dims::Bool)
@ InfiniteOpt.TranscriptionOpt ~/.julia/packages/InfiniteOpt/WWtLl/src/TranscriptionOpt/transcribe.jl:965
[7] build_transcription_model!
@ ~/.julia/packages/InfiniteOpt/WWtLl/src/TranscriptionOpt/transcribe.jl:938 [inlined]
[8] #build_optimizer_model!#103
@ ~/.julia/packages/InfiniteOpt/WWtLl/src/TranscriptionOpt/optimize.jl:34 [inlined]
[9] build_optimizer_model!
@ ~/.julia/packages/InfiniteOpt/WWtLl/src/TranscriptionOpt/optimize.jl:26 [inlined]
[10] build_optimizer_model!(model::InfiniteModel; kwargs::@Kwargs{})
@ InfiniteOpt ~/.julia/packages/InfiniteOpt/WWtLl/src/optimize.jl:534
[11] build_optimizer_model!
@ InfiniteOpt ~/.julia/packages/InfiniteOpt/WWtLl/src/optimize.jl:528 [inlined]
[12] #optimize!#475
@ InfiniteOpt ~/.julia/packages/InfiniteOpt/WWtLl/src/optimize.jl:924 [inlined]
[13] optimize!(model::InfiniteModel)
@ InfiniteOpt ~/.julia/packages/InfiniteOpt/WWtLl/src/optimize.jl:922
[14] top-level scope
@ ~/Library/CloudStorage/GoogleDrive/information_optcontrol.jl:63
The versions I’m using are InfiniteOpt v0.5.8 and JuMP v1.21.1
Thanks in advance for any help!