Skip to content

ERROR: MethodError: no method matching view(::MTKParameters) while solving an optimization problem via MOI #3637

@sergio-dorado

Description

@sergio-dorado

I am trying to solve a constrained nonlinear optimization problem (optimal power flow) using ModelingToolkit.jl. For compatibility with the other part of the project, I need to implement the problem using MTK rather than Optimization.jl or JuMP.

While I am able to construct the optimization system, instantiate the particular problem I want to solve, and pass it to the MOI solver, I am getting an error concerning an internal view function:

ERROR: MethodError: no method matching view(::MTKParameters{Vector{Float64}, Tuple{}, Tuple{}, Tuple{}}, ::UnitRange{Int64})
The function `view` exists, but no method is defined for this combination of argument types.

An example is provided below:

using ModelingToolkit, Optimization, OptimizationMOI, Ipopt

# system matrix
Y = [1.9802-19.802im   -0.990099+9.90099im  -0.990099+9.90099im
 -0.990099+9.90099im     1.9802-19.802im   -0.990099+9.90099im
 -0.990099+9.90099im  -0.990099+9.90099im     1.9802-19.802im]
n = size(Y, 1)

@variables begin
    v[1:n], [bounds = (0.95, 1.05)]
    θ[1:n], [bounds = (-π, π)]
    p_g[1:n], [bounds = (0.0, 3.0)]
    q_g[1:n], [bounds = (-2.0, 2.0)]
end

@parameters begin
    p_d[1:n] = [0.0, 0.0, 0.8]
    q_d[1:n] = [0.0, 0.0, 0.6]
    c[1:n] = [1.0, 2.0, 0.0]
end

# constraints
cons = []

# power flow constraints
for i=1:n
    # active power balance
    push!(cons, p_g[i] - p_d[i] ~ sum(v[i]*v[j]*abs(Y[i, j]) * cos(angle(Y[i, j]) - θ[i] + θ[j]) for j=1:n))
    # reactive power balance
    push!(cons, q_g[i] - q_d[i] ~ -sum(v[i]*v[j]*abs(Y[i, j]) * sin(angle(Y[i, j]) - θ[i] + θ[j]) for j=1:n))
end

# slack bus and load bus constraints
push!(cons, θ[3] ~ 0.0);
push!(cons, p_g[3] ~ 0.0);
push!(cons, q_g[3] ~ 0.0);

# cost function
cost_fcn = sum(c[i] * p_g[i] for i=1:n)

@named sys = OptimizationSystem(cost_fcn, [v, θ, p_g, q_g], [p_d, q_d, c], constraints=cons)
sys = complete(sys)

x0 = [
    v => [1.0, 1.0, 1.0]
    θ => [0.1, 0.1, 0.1]
    p_g => [0.1, 0.1, 0.8]
    q_g => [0.1, 0.1, 0.6]
]

p = [
    p_d => [0.0, 0.0, 0.8]
    q_d => [0.0, 0.0, 0.6]
    c => [1.0, 2.0, 0.0]
]

prob = OptimizationProblem(sys, x0, p, grad=true, hess=true, cons_j=true, cons_h=true)

nlp_solver = OptimizationMOI.MOI.OptimizerWithAttributes(Ipopt.Optimizer,
    "print_level" => 0
)

sol = solve(prob, nlp_solver)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions