-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/componentarray input output #21
base: main
Are you sure you want to change the base?
Changes from 14 commits
8a408b8
baaadd0
35ea84c
b9419d5
464b974
1ca19be
bbe3909
9c524f1
f81d1ac
39f5b06
7bd8601
449c1c2
7fb7b5f
f92b825
5b08675
9c294e4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,17 @@ | ||
# Implement the cospan-algebra of dynamical systems. | ||
module Optimizers | ||
|
||
export pullback_matrix, pushforward_matrix, Optimizer, OpenContinuousOpt, OpenDiscreteOpt, Euler, | ||
simulate | ||
export Optimizer, OpenContinuousOpt, OpenDiscreteOpt, Euler, | ||
simulate, pullback_function, pushforward_function, isapprox | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's bad practice to export functions from Base. We should just define the version of isapprox we need in the relevant test file (it's not critical to the function of this package). |
||
|
||
using ..FinSetAlgebras | ||
import ..FinSetAlgebras: hom_map, laxator | ||
using Catlab | ||
import Catlab: oapply, dom | ||
using SparseArrays | ||
using ComponentArrays | ||
import Base.isapprox | ||
|
||
""" pullback_matrix(f::FinFunction) | ||
|
||
The pullback of f : n → m is the linear map f^* : Rᵐ → Rⁿ defined by | ||
f^*(y)[i] = y[f(i)]. | ||
""" | ||
function pullback_matrix(f::FinFunction) | ||
n = length(dom(f)) | ||
sparse(1:n, f.(dom(f)), ones(Int,n), dom(f).n, codom(f).n) | ||
end | ||
|
||
""" pushforward_matrix(f::FinFunction) | ||
|
||
The pushforward is the dual of the pullback. | ||
""" | ||
pushforward_matrix(f::FinFunction) = pullback_matrix(f)' | ||
|
||
""" Optimizer | ||
|
||
|
@@ -46,17 +33,18 @@ struct DiscreteOpt <: FinSetAlgebra{Optimizer} end | |
|
||
The hom map is defined as ϕ ↦ (s ↦ ϕ_*∘s∘ϕ^*). | ||
""" | ||
hom_map(::ContinuousOpt, ϕ::FinFunction, s::Optimizer) = | ||
Optimizer(codom(ϕ), x->pushforward_matrix(ϕ)*s(pullback_matrix(ϕ)*x)) | ||
function hom_map(::ContinuousOpt, ϕ::FinFunction, s::Optimizer) | ||
Optimizer(codom(ϕ), x -> pushforward_function(ϕ, s(pullback_function(ϕ, x)))) | ||
end | ||
|
||
""" hom_map(::DiscreteOpt, ϕ::FinFunction, s::Optimizer) | ||
|
||
The hom map is defined as ϕ ↦ (s ↦ id + ϕ_*∘(s - id)∘ϕ^*). | ||
""" | ||
hom_map(::DiscreteOpt, ϕ::FinFunction, s::Optimizer) = | ||
Optimizer(codom(ϕ), x-> begin | ||
y = pullback_matrix(ϕ)*x | ||
return x + pushforward_matrix(ϕ)*(s(y) - y) | ||
Optimizer(codom(ϕ), x -> begin | ||
y = pullback_function(ϕ, x) | ||
return x + pushforward_function(ϕ, (s(y) - y)) | ||
end) | ||
|
||
""" laxator(::ContinuousOpt, Xs::Vector{Optimizer}) | ||
|
@@ -65,10 +53,10 @@ Takes the "disjoint union" of a collection of optimizers. | |
""" | ||
function laxator(::ContinuousOpt, Xs::Vector{Optimizer}) | ||
c = coproduct([dom(X) for X in Xs]) | ||
subsystems = [x -> X(pullback_matrix(l)*x) for (X,l) in zip(Xs, legs(c))] | ||
subsystems = [x -> X(pullback_function(l, x)) for (X, l) in zip(Xs, legs(c))] | ||
function parallel_dynamics(x) | ||
res = Vector{Vector}(undef, length(Xs)) # Initialize storage for results | ||
#=Threads.@threads=# for i = 1:length(Xs) | ||
for i = 1:length(Xs) #=Threads.@threads=# | ||
res[i] = subsystems[i](x) | ||
end | ||
return vcat(res...) | ||
|
@@ -78,7 +66,14 @@ end | |
# Same as continuous opt | ||
laxator(::DiscreteOpt, Xs::Vector{Optimizer}) = laxator(ContinuousOpt(), Xs) | ||
|
||
|
||
Open{Optimizer}(S::FinSet, v::Function, m::FinFunction) = Open{Optimizer}(S, Optimizer(S, v), m) | ||
Open{Optimizer}(s::Int, v::Function, m::FinFunction) = Open{Optimizer}(FinSet(s), v, m) | ||
|
||
# Special cases: m is an identity | ||
Open{Optimizer}(S::FinSet, v::Function) = Open{Optimizer}(S, Optimizer(S, v), id(S)) | ||
Open{Optimizer}(s::Int, v::Function) = Open{Optimizer}(FinSet(s), v) | ||
|
||
|
||
# Turn into cospan-algebras. | ||
struct OpenContinuousOpt <: CospanAlgebra{Open{Optimizer}} end | ||
|
@@ -94,16 +89,102 @@ end | |
|
||
# Euler's method is a natural transformation from continous optimizers to discrete optimizers. | ||
function Euler(f::Open{Optimizer}, γ::Float64) | ||
return Open{Optimizer}(f.S, Optimizer(f.S, x->x+γ*f.o(x)), f.m) | ||
return Open{Optimizer}(f.S, | ||
Optimizer(f.S, x -> x .+ γ .* f.o(x)), f.m) | ||
end | ||
|
||
# Run a discrete optimizer the designated number of time-steps. | ||
function simulate(f::Open{Optimizer}, x0::Vector{Float64}, tsteps::Int) | ||
function simulate(f::Open{Optimizer}, x0::Vector, tsteps::Int) | ||
res = x0 | ||
for i in 1:tsteps | ||
res = f.o(res) | ||
end | ||
return res | ||
end | ||
|
||
end | ||
# Run a discrete optimizer the designated number of time-steps. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can't fight the feeling that there is a simpler/cleaner way to do this. I'm not sure what it is, but I will think about it. |
||
function simulate(f::Open{Optimizer}, d::AbstractUWD, x0::ComponentArray, tsteps::Int) | ||
# Format initial conditions | ||
initial_cond_vec = Vector{Any}(undef, length(d[:variable])) | ||
var_to_index = Dict() | ||
curr_index = 1 | ||
for junction in d[:junction] | ||
if !haskey(var_to_index, d[:variable][junction]) | ||
var_to_index[d[:variable][junction]] = curr_index | ||
curr_index += 1 | ||
end | ||
end | ||
|
||
for (var, index) in var_to_index | ||
initial_cond_vec[index] = x0[var] | ||
end | ||
res = initial_cond_vec | ||
# Simulate | ||
for i in 1:tsteps | ||
res = f.o(res) | ||
end | ||
|
||
res_formatted = copy(x0) | ||
|
||
# Rebuild component array | ||
for (var, index) in var_to_index | ||
res_formatted[var] = res[index] | ||
end | ||
return res_formatted | ||
end | ||
|
||
function (f::Open{Optimizer})(x0::Vector) | ||
return f.o(x0) | ||
end | ||
|
||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we want pullback and pushforward functions to return the functions that do the pushforward and pullback when applied to a vector (similar to the matrix versions). This is because usually we will be calling pushforward and pullback with the same finfunction but different vectors over and over. So it would be nice to generate the function for a single finfunction once and then be able to call that multiple times on different inputs. This also maintains consistency with the matrix version. |
||
""" pullback_function(f::FinFunction, v::Vector) | ||
|
||
The pullback of f : n → m is the linear map f^* : Rᵐ → Rⁿ defined by | ||
f^*(y)[i] = y[f(i)]. | ||
""" | ||
function pullback_function(f::FinFunction, v::Vector)::Vector | ||
return [v[f(i)] for i in 1:length(dom(f))] | ||
end | ||
|
||
|
||
""" pushforward_function(f::FinFunction, v::Vector{Vector{Float64}}) | ||
|
||
The pushforward of f : n → m is the linear map f_* : Rⁿ → Rᵐ defined by | ||
f_*(y)[j] = ∑ y[i] for i ∈ f⁻¹(j). | ||
""" | ||
function pushforward_function(f::FinFunction, v::Vector{Vector{Float64}})::Vector | ||
output = [[] for _ in 1:length(codom(f))] | ||
for i in 1:length(dom(f)) | ||
if isempty(output[f(i)]) | ||
output[f(i)] = v[i] | ||
else | ||
output[f(i)] += v[i] | ||
end | ||
end | ||
return output | ||
end | ||
|
||
|
||
""" pushforward_function(f::FinFunction, v::Vector{Float64}) | ||
|
||
The pushforward of f : n → m is the linear map f_* : Rⁿ → Rᵐ defined by | ||
f_*(y)[j] = ∑ y[i] for i ∈ f⁻¹(j). | ||
""" | ||
function pushforward_function(f::FinFunction, v::Vector{Float64})::Vector | ||
output = [0.0 for _ in 1:length(codom(f))] | ||
|
||
for i in 1:length(dom(f)) | ||
output[f(i)] += v[i] | ||
end | ||
|
||
return output | ||
end | ||
|
||
|
||
|
||
end # module | ||
|
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should still support the matrix versions of these functions.