Skip to content

Commit da606cf

Browse files
committed
make environment lighter
1 parent 8fe7079 commit da606cf

File tree

4 files changed

+74
-77
lines changed

4 files changed

+74
-77
lines changed

Project.toml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,23 @@
11
name = "ControlSystemsMTK"
22
uuid = "687d7614-c7e5-45fc-bfc3-9ee385575c88"
33
authors = ["Fredrik Bagge Carlson"]
4-
version = "0.1.2"
4+
version = "0.1.3"
55

66
[deps]
7-
ControlSystemIdentification = "3abffc1c-5106-53b7-b354-a47bfc086282"
87
ControlSystemsBase = "aaaaaaaa-a6ca-5380-bf3e-84a91bcd477e"
98
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
109
ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78"
1110
ModelingToolkitStandardLibrary = "16a59e39-deab-5bd0-87e4-056b12336739"
12-
Optim = "429524aa-4258-5aef-a3af-852621145aeb"
1311
RobustAndOptimalControl = "21fd56a4-db03-40ee-82ee-a87907bee541"
14-
SymbolicControlSystems = "886cb795-8fd3-4b11-92f6-8071e46f71c5"
1512
Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7"
1613
UnPack = "3a884ed6-31ef-47d7-9d2a-63182c4928ed"
1714

1815
[compat]
19-
ControlSystemIdentification = "2.4.1"
2016
ControlSystemsBase = "1.0.1"
2117
ModelingToolkit = "8.26"
2218
ModelingToolkitStandardLibrary = "1.5"
23-
Optim = "1"
2419
OrdinaryDiffEq = "6"
2520
RobustAndOptimalControl = "0.4.14"
26-
SymbolicControlSystems = "0.1.3"
2721
Symbolics = "4.10"
2822
UnPack = "1"
2923
julia = "1.6"

src/ControlSystemsMTK.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,21 @@ A third idea: just use named systems with named indexing to obtain any system yo
1818
using LinearAlgebra
1919
using ModelingToolkit, ControlSystemsBase
2020
using ControlSystemsBase: ssdata, AbstractStateSpace, Continuous, nstates, noutputs, ninputs
21-
using ControlSystemIdentification
21+
# using ControlSystemIdentification
2222
using RobustAndOptimalControl
2323
import ModelingToolkit: ODESystem, FnType, Symbolics
2424
using ModelingToolkit: states, observed, isdifferential
2525
using ModelingToolkit.Symbolics
2626
using ModelingToolkit.Symbolics: jacobian, solve_for
2727
using UnPack
28-
using Optim, Optim.LineSearches
28+
# using Optim, Optim.LineSearches
2929

30-
using SymbolicControlSystems
30+
# using SymbolicControlSystems
3131

3232
export sconnect, feedback, ODESystem, states, observed, named_ss
3333
export build_quadratic_cost_matrix
3434

3535
include("ode_system.jl")
36-
include("symbolic_optimization.jl")
36+
# include("symbolic_optimization.jl")
3737

3838
end

src/symbolic_optimization.jl

Lines changed: 65 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,68 @@
1-
using Base.Threads: @threads, nthreads, threadid
2-
3-
function get_funs(Ps, vars)
4-
@info "Converting to Num"
5-
nums = Num.(Ps)
6-
numsv = reduce(vcat, nums)
7-
@info "Extracting variables"
8-
found_vars = Symbolics.get_variables.(numsv)
9-
found_vars = unique(reduce(vcat, found_vars))
10-
si = findfirst(v -> string(v) == "s", found_vars)
11-
si === nothing && error("Didn't find s symbol")
12-
s = found_vars[si]
13-
svars = Set([vars; s])
14-
all(v svars for v in found_vars) || error(
15-
"Found some variables that were not listed in vars. Found vars = $found_vars, expected vars: $svars",
16-
)
17-
# all(any(isequal(v, v2) for v2 in [vars; s]) for v in found_vars) || error("Found some variables that were not listed in vars. Found vars = $found_vars")
18-
@info "Building functions"
19-
funs = [Symbolics.build_function(n, s, vars...; expression = Val(false)) for n in nums] # the first element in the tuple is out-of-place. If n is scalar, only a single function is returned.
20-
end
21-
22-
23-
"""
24-
cost, funs = coeff_cost_freq(Pd::Vector{StateSpace}, Ps, freqs, vars, dist = abs2)
25-
cost, funs = coeff_cost_freq(Pd::Vector{FRD}, Ps, vars, dist = abs2)
26-
27-
Return a cost function that if optimized fits the symbolic parameters in `Ps` to the numerical models in `Pd`.
28-
29-
# Arguments:
30-
- `Pd`: Either a vector of statespace objects with numerical coefficients, or a vector with frequency-domain models `::FRD`.
31-
- `Ps`: A vector of symbolic statespace objects
32-
- `freqs`: the frequency grid to eval the cost on. If `Pd` is a set of `FRD` objects, this argument is omitted.
33-
- `vars`: the symbolic variables to optimize
34-
- `dist`: The distance measure.
35-
"""
36-
function coeff_cost_freq(
37-
Pd::AbstractVector{<:AbstractStateSpace},
38-
Ps,
39-
freqs,
40-
vars,
41-
dist::F = abs2,
42-
) where {F}
43-
funs = get_funs(Ps, vars)
44-
local cost
45-
logabs(x) = @fastmath log(abs(x))
46-
let Pd = Pd, funs = funs, freqs = freqs # closure bug trick
47-
function cost(x::AbstractArray{T}, _ = nothing)::T where {T}
48-
any(<=(0), x) && (return T(Inf))
49-
c = [zero(T) for _ = 1:nthreads()]
50-
for (Pd, symfun) in zip(Pd, funs)
51-
for f in freqs # @threads
52-
freq = complex(0, f)
53-
data_val = evalfr(Pd, freq)
54-
model_val = symfun(freq, x...)
55-
c[threadid()] += sum(dist.(logabs.(data_val) .- logabs.(model_val)))
56-
end
57-
end
58-
sum(c) / (length(freqs) * length(Pd))
59-
end
60-
end
61-
cost, funs
62-
end
1+
# using Base.Threads: @threads, nthreads, threadid
2+
3+
# function get_funs(Ps, vars)
4+
# @info "Converting to Num"
5+
# nums = Num.(Ps)
6+
# numsv = reduce(vcat, nums)
7+
# @info "Extracting variables"
8+
# found_vars = Symbolics.get_variables.(numsv)
9+
# found_vars = unique(reduce(vcat, found_vars))
10+
# si = findfirst(v -> string(v) == "s", found_vars)
11+
# si === nothing && error("Didn't find s symbol")
12+
# s = found_vars[si]
13+
# svars = Set([vars; s])
14+
# all(v ∈ svars for v in found_vars) || error(
15+
# "Found some variables that were not listed in vars. Found vars = $found_vars, expected vars: $svars",
16+
# )
17+
# # all(any(isequal(v, v2) for v2 in [vars; s]) for v in found_vars) || error("Found some variables that were not listed in vars. Found vars = $found_vars")
18+
# @info "Building functions"
19+
# funs = [Symbolics.build_function(n, s, vars...; expression = Val(false)) for n in nums] # the first element in the tuple is out-of-place. If n is scalar, only a single function is returned.
20+
# end
21+
22+
23+
# """
24+
# cost, funs = coeff_cost_freq(Pd::Vector{StateSpace}, Ps, freqs, vars, dist = abs2)
25+
# cost, funs = coeff_cost_freq(Pd::Vector{FRD}, Ps, vars, dist = abs2)
26+
27+
# Return a cost function that if optimized fits the symbolic parameters in `Ps` to the numerical models in `Pd`.
28+
29+
# # Arguments:
30+
# - `Pd`: Either a vector of statespace objects with numerical coefficients, or a vector with frequency-domain models `::FRD`.
31+
# - `Ps`: A vector of symbolic statespace objects
32+
# - `freqs`: the frequency grid to eval the cost on. If `Pd` is a set of `FRD` objects, this argument is omitted.
33+
# - `vars`: the symbolic variables to optimize
34+
# - `dist`: The distance measure.
35+
# """
36+
# function coeff_cost_freq(
37+
# Pd::AbstractVector{<:AbstractStateSpace},
38+
# Ps,
39+
# freqs,
40+
# vars,
41+
# dist::F = abs2,
42+
# ) where {F}
43+
# funs = get_funs(Ps, vars)
44+
# local cost
45+
# logabs(x) = @fastmath log(abs(x))
46+
# let Pd = Pd, funs = funs, freqs = freqs # closure bug trick
47+
# function cost(x::AbstractArray{T}, _ = nothing)::T where {T}
48+
# any(<=(0), x) && (return T(Inf))
49+
# c = [zero(T) for _ = 1:nthreads()]
50+
# for (Pd, symfun) in zip(Pd, funs)
51+
# for f in freqs # @threads
52+
# freq = complex(0, f)
53+
# data_val = evalfr(Pd, freq)
54+
# model_val = symfun(freq, x...)
55+
# c[threadid()] += sum(dist.(logabs.(data_val) .- logabs.(model_val)))
56+
# end
57+
# end
58+
# sum(c) / (length(freqs) * length(Pd))
59+
# end
60+
# end
61+
# cost, funs
62+
# end
63+
64+
65+
6366

6467
# function coeff_cost_freq(Pd::AbstractVector{<:FRD}, Ps, vars, dist::F = abs2) where {F}
6568
# funs = get_funs(Ps, vars)

test/runtests.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ using Test
77
include("test_ODESystem.jl")
88
end
99

10-
@testset "symbolic_opt" begin
11-
@info "Testing symbolic_opt"
12-
include("test_symbolic_opt.jl")
13-
end
10+
# @testset "symbolic_opt" begin
11+
# @info "Testing symbolic_opt"
12+
# include("test_symbolic_opt.jl")
13+
# end
1414

1515
end

0 commit comments

Comments
 (0)