Optimization-based PI controller tuning for a delay system

VPBML
Wed, 06 Aug 2025 13:24:56 +0000
Optimization (Mathematical)

Hi,
I am trying to find a good tuning for a PI controler on a first-order-plus-delay system using Optimization. A naive first try combining ControlSystems.jl and Optimization.jl did not work and gave some errors. I wonder if it is possible to twist this approach somehow or if there are better (and convenient) alternatives. Thank you.
Here is the MWE:

using ControlSystems
using Optimization, OptimizationOptimJL, ForwardDiff, Zygote

function loss_tf(x, p)
    Kp, Ti = x[1], x[2]
    C = pid(Kp, Ti)
    G = p
    L = feedback(C*G)
    t_sim = 0.0:0.1:50.0
    y, t = step(L, t_sim)
    ISE = (t[2] - t[1])*sum((1 .- y).^2)
    return ISE
end

x0 = [2.5, 5.0]
p = tf(1.0, [5.0, 1]) * delay(1.0)
f = OptimizationFunction(loss_tf, Optimization.AutoForwardDiff())
prob = OptimizationProblem(f, x0, p, lb = [-Inf, 0.1], ub = [Inf, Inf])
sol = solve(prob, BFGS())

And the error it gave:

ERROR: MethodError: no method matching Float64(::ForwardDiff.Dual{ForwardDiff.Tag{typeof(loss_tf), Float64}, Float64, 2})
The type `Float64` exists, but no method is defined for this combination of argument types when trying to construct it.

4 posts - 2 participants