I am new to Julia and transferred from Matlab. Now I am using JuMP to write an optimization problem with a quadratic cost function like:
and my code for optimization problem is:
function get_cost_func(sys, R, M)
obj_matrix = zeros(sys.nx+sys.nu,sys.nx)
for k in 1:sys.tSLS
cost_matrix=
[sqrt(sys.Q)*R[:,:,k]; sqrt(sys.R)*M[:,:,k]]
obj_matrix = [obj_matrix cost_matrix]
end
obj = vec(obj_matrix)'*vec(obj_matrix)
return obj
end
function state_fbk_sls(sys)
model = Model(Mosek.Optimizer)
@variable(model, R[1:sys.nx,1:sys.nx,1:sys.tSLS])
@variable(model, M[1:sys.nu,1:sys.nx,1:sys.tSLS])
@constraint(model, R[:,:,1].==[1 0;0 1])
for t in 1:sys.tSLS-1
@constraint(model, R[:,:,t+1] .== sys.A*R[:,:,t]+sys.B*M[:,:,t])
end
@objective(model, Min, get_cost_func(sys, R, M))
optimize!(model)
@show value(R)
@show value(M)
end
But I have the error message like:
ERROR: MathOptInterface.AddConstraintNotAllowed{MathOptInterface.VectorAffineFunction{Float64}, MathOptInterface.RotatedSecondOrderCone}: Adding `MathOptInterface.VectorAffineFunction{Float64}`-in-`MathOptInterface.RotatedSecondOrderCone` constraints cannot be performed. You may want to use a `CachingOptimizer` in `AUTOMATIC` mode or you may need to call `reset_optimizer` before doing this operation if the `CachingOptimizer` is in `MANUAL` mode. Stacktrace: [1] correct_throw_add_constraint_error_fallback(model::MosekTools.Optimizer, func::MathOptInterface.VectorAffineFunction{Float64}, set::MathOptInterface.RotatedSecondOrderCone; error_if_supported::MathOptInterface.AddConstraintNotAllowed{MathOptInterface.VectorAffineFunction{Float64}, MathOptInterface.RotatedSecondOrderCone}) @ MathOptInterface C:\Users\Steve Zhang\.julia\packages\MathOptInterface\Ohzb2\src\constraints.jl:209 [2] correct_throw_add_constraint_error_fallback(model::MosekTools.Optimizer, func::MathOptInterface.VectorAffineFunction{Float64}, set::MathOptInterface.RotatedSecondOrderCone) @ MathOptInterface C:\Users\Steve Zhang\.julia\packages\MathOptInterface\Ohzb2\src\constraints.jl:202 [3] throw_add_constraint_error_fallback(model::MosekTools.Optimizer, func::MathOptInterface.VectorAffineFunction{Float64}, set::MathOptInterface.RotatedSecondOrderCone; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}) @ MathOptInterface C:\Users\Steve Zhang\.julia\packages\MathOptInterface\Ohzb2\src\constraints.jl:173 [4] throw_add_constraint_error_fallback(model::MosekTools.Optimizer, func::MathOptInterface.VectorAffineFunction{Float64}, set::MathOptInterface.RotatedSecondOrderCone) @ MathOptInterface C:\Users\Steve Zhang\.julia\packages\MathOptInterface\Ohzb2\src\constraints.jl:167 [5] add_constraint(model::MosekTools.Optimizer, func::MathOptInterface.VectorAffineFunction{Float64}, set::MathOptInterface.RotatedSecondOrderCone) @ MathOptInterface C:\Users\Steve Zhang\.julia\packages\MathOptInterface\Ohzb2\src\constraints.jl:148 [6] add_constraint(b::MathOptInterface.Bridges.LazyBridgeOptimizer{MosekTools.Optimizer}, f::MathOptInterface.VectorAffineFunction{Float64}, s::MathOptInterface.RotatedSecondOrderCone) @ MathOptInterface.Bridges C:\Users\Steve Zhang\.julia\packages\MathOptInterface\Ohzb2\src\Bridges\bridge_optimizer.jl:1666 [7] bridge_constraint(#unused#::Type{MathOptInterface.Bridges.Constraint.QuadtoSOCBridge{Float64}}, model::MathOptInterface.Bridges.LazyBridgeOptimizer{MosekTools.Optimizer}, func::MathOptInterface.ScalarQuadraticFunction{Float64}, set::MathOptInterface.LessThan{Float64}) @ MathOptInterface.Bridges.Constraint C:\Users\Steve Zhang\.julia\packages\MathOptInterface\Ohzb2\src\Bridges\Constraint\bridges\quad_to_soc.jl:120 [8] add_bridged_constraint(b::MathOptInterface.Bridges.LazyBridgeOptimizer{MosekTools.Optimizer}, BridgeType::Type, f::MathOptInterface.ScalarQuadraticFunction{Float64}, s::MathOptInterface.LessThan{Float64}) @ MathOptInterface.Bridges C:\Users\Steve Zhang\.julia\packages\MathOptInterface\Ohzb2\src\Bridges\bridge_optimizer.jl:1589 [9] add_constraint(b::MathOptInterface.Bridges.LazyBridgeOptimizer{MosekTools.Optimizer}, f::MathOptInterface.ScalarQuadraticFunction{Float64}, s::MathOptInterface.LessThan{Float64}) @ MathOptInterface.Bridges C:\Users\Steve Zhang\.julia\packages\MathOptInterface\Ohzb2\src\Bridges\bridge_optimizer.jl:1664 [10] #normalize_and_add_constraint#110 @ C:\Users\Steve Zhang\.julia\packages\MathOptInterface\Ohzb2\src\Utilities\constraints.jl:25 [inlined] [11] normalize_and_add_constraint @ C:\Users\Steve Zhang\.julia\packages\MathOptInterface\Ohzb2\src\Utilities\constraints.jl:19 [inlined] [12] bridge_objective(#unused#::Type{MathOptInterface.Bridges.Objective.SlackBridge{Float64, MathOptInterface.ScalarQuadraticFunction{Float64}, MathOptInterface.ScalarQuadraticFunction{Float64}}}, model::MathOptInterface.Bridges.LazyBridgeOptimizer{MosekTools.Optimizer}, func::MathOptInterface.ScalarQuadraticFunction{Float64}) @ MathOptInterface.Bridges.Objective C:\Users\Steve Zhang\.julia\packages\MathOptInterface\Ohzb2\src\Bridges\Objective\bridges\slack.jl:71 [13] _bridge_objective(b::MathOptInterface.Bridges.LazyBridgeOptimizer{MosekTools.Optimizer}, BridgeType::Type, func::MathOptInterface.ScalarQuadraticFunction{Float64}) @ MathOptInterface.Bridges C:\Users\Steve Zhang\.julia\packages\MathOptInterface\Ohzb2\src\Bridges\bridge_optimizer.jl:1027 [14] set(b::MathOptInterface.Bridges.LazyBridgeOptimizer{MosekTools.Optimizer}, attr::MathOptInterface.ObjectiveFunction{MathOptInterface.ScalarQuadraticFunction{Float64}}, func::MathOptInterface.ScalarQuadraticFunction{Float64}) @ MathOptInterface.Bridges C:\Users\Steve Zhang\.julia\packages\MathOptInterface\Ohzb2\src\Bridges\bridge_optimizer.jl:1065 [15] _pass_attribute(dest::MathOptInterface.Bridges.LazyBridgeOptimizer{MosekTools.Optimizer}, src::MathOptInterface.Utilities.UniversalFallback{MathOptInterface.Utilities.Model{Float64}}, index_map::MathOptInterface.Utilities.IndexMap, attr::MathOptInterface.ObjectiveFunction{MathOptInterface.ScalarQuadraticFunction{Float64}}) @ MathOptInterface.Utilities C:\Users\Steve Zhang\.julia\packages\MathOptInterface\Ohzb2\src\Utilities\copy.jl:45 [16] pass_attributes(dest::MathOptInterface.Bridges.LazyBridgeOptimizer{MosekTools.Optimizer}, src::MathOptInterface.Utilities.UniversalFallback{MathOptInterface.Utilities.Model{Float64}}, index_map::MathOptInterface.Utilities.IndexMap) @ MathOptInterface.Utilities C:\Users\Steve Zhang\.julia\packages\MathOptInterface\Ohzb2\src\Utilities\copy.jl:32 [17] default_copy_to(dest::MathOptInterface.Bridges.LazyBridgeOptimizer{MosekTools.Optimizer}, src::MathOptInterface.Utilities.UniversalFallback{MathOptInterface.Utilities.Model{Float64}}) @ MathOptInterface.Utilities C:\Users\Steve Zhang\.julia\packages\MathOptInterface\Ohzb2\src\Utilities\copy.jl:496 [18] copy_to @ C:\Users\Steve Zhang\.julia\packages\MathOptInterface\Ohzb2\src\Bridges\bridge_optimizer.jl:451 [inlined] [19] optimize! @ C:\Users\Steve Zhang\.julia\packages\MathOptInterface\Ohzb2\src\MathOptInterface.jl:86 [inlined] [20] optimize!(m::MathOptInterface.Utilities.CachingOptimizer{MathOptInterface.Bridges.LazyBridgeOptimizer{MosekTools.Optimizer}, MathOptInterface.Utilities.UniversalFallback{MathOptInterface.Utilities.Model{Float64}}}) @ MathOptInterface.Utilities C:\Users\Steve Zhang\.julia\packages\MathOptInterface\Ohzb2\src\Utilities\cachingoptimizer.jl:316 [21] optimize!(model::Model; ignore_optimize_hook::Bool, _differentiation_backend::MathOptInterface.Nonlinear.SparseReverseMode, kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}) @ JuMP C:\Users\Steve Zhang\.julia\packages\JuMP\gVq7V\src\optimizer_interface.jl:185 [22] optimize! @ C:\Users\Steve Zhang\.julia\packages\JuMP\gVq7V\src\optimizer_interface.jl:155 [inlined] [23] state_fbk_sls(sys::sysParam) @ Main c:\Users\Steve Zhang\.julia\example\cartpole_sfbsls.jl:70 [24] top-level scope @ c:\Users\Steve Zhang\.julia\example\cartpole_sfbsls.jl:76
Is it because the way I implement the cost function is wrong? What is the correct way to implement the quadratic cost fucntion like this? Thanks for the help!