Skip to content
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

#520 - Accept lazyset for a constant input #526

Merged
merged 2 commits into from
Mar 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Utils/Utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,16 @@ export template_direction_symbols,
matrix_conversion,
matrix_conversion_lazy_explicit

# internal normalization
export normalize,
distribute_initial_set

# Extension of MathematicalSystems for use inside Reachability.jl
include("systems.jl")

# normalization
include("normalization.jl")

# abstract solution type
include("AbstractSolution.jl")

Expand Down
85 changes: 85 additions & 0 deletions src/Utils/normalization.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
"""
normalize(system)

Normalize a continuous system.

### Input

- `system` -- continuous system

### Output

Either the same system if it already conforms to our required structure, or a
new system otherwise.

### Algorithm

We apply the following normalization steps.

* [`normalize_inputs`](@ref)
"""
function normalize(system::InitialValueProblem{<:Union{AbstractContinuousSystem,
AbstractDiscreteSystem}})
return normalize_inputs(system)
end

"""
normalize_inputs(system)

Normalize the inputs of a continuous system.

### Input

- `system` -- continuous system

### Output

Either the same system if the inputs are of type `AbstractInput`, or a new
system that wraps the inputs in a `ConstantInput`.
"""
function normalize_inputs(system)
inputdim(system) == 0 && return system
U = inputset(system)
U isa AbstractInput && return system
if !(U isa LazySet)
throw(ArgumentError("inputs of type $(typeof(U)) are not supported"))
end
return IVP(wrap_inputs(system.s, U), system.x0)
end

function wrap_inputs(system::CLCCS, U::LazySet)
return CLCCS(system.A, system.B, system.X, ConstantInput(U))
end

function wrap_inputs(system::CLCDS, U::LazySet)
return CLCDS(system.A, system.B, system.X, ConstantInput(U))
end

function wrap_inputs(system::CACCS, U::LazySet)
schillic marked this conversation as resolved.
Show resolved Hide resolved
return CACCS(system.A, system.B, system.c, system.X, ConstantInput(U))
end

function wrap_inputs(system::CACDS, U::LazySet)
return CACDS(system.A, system.B, system.c, system.X, ConstantInput(U))
end

"""
distribute_initial_set(system::InitialValueProblem{<:HybridSystem, <:LazySet)

Distribute the set of initial states to each mode of a hybrid system.

### Input

- `system` -- an initial value problem wrapping a mathematical system (hybrid)
and a set of initial states

### Output

A new initial value problem with the same hybrid system but where the set of initial
states is the list of tuples `(state, X0)`, for each state in the hybrid system.
"""
function distribute_initial_set(system::InitialValueProblem{<:HybridSystem, <:LazySet})
HS, X0 = system.s, system.x0
initial_states = [(loc, X0) for loc in states(HS)]
return InitialValueProblem(HS, initial_states)
end
2 changes: 2 additions & 0 deletions src/Utils/systems.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const LCS = LinearContinuousSystem
const LDS = LinearDiscreteSystem
const CLCCS = ConstrainedLinearControlContinuousSystem
const CLCDS = ConstrainedLinearControlDiscreteSystem
const CACCS = ConstrainedAffineControlContinuousSystem
const CACDS = ConstrainedAffineControlDiscreteSystem

import Base: *

Expand Down
22 changes: 1 addition & 21 deletions src/solve.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,6 @@ function default_operator(system::InitialValueProblem)
return op
end

"""
distribute_initial_set(system::InitialValueProblem{<:HybridSystem, <:LazySet)

Distribute the set of initial states to each mode of a hybrid system.

### Input

- `system` -- an initial value problem wrapping a mathematical system (hybrid)
and a set of initial states

### Output

A new initial value problem with the same hybrid system but where the set of initial
states is the list of tuples `(state, X0)`, for each state in the hybrid system.
"""
function distribute_initial_set(system::InitialValueProblem{<:HybridSystem, <:LazySet})
HS, X0 = system.s, system.x0
initial_states = [(loc, X0) for loc in states(HS)]
return InitialValueProblem(HS, initial_states)
end

"""
solve(system, options) or solve(system, :key1 => val1, [...], keyK => valK)

Expand Down Expand Up @@ -83,6 +62,7 @@ function solve!(system::InitialValueProblem{<:Union{AbstractContinuousSystem,
op::ContinuousPost=default_operator(system),
invariant::Union{LazySet, Nothing}=nothing
)::AbstractSolution
system = normalize(system)
options = init(op, system, options_input)

# coordinate transformation
Expand Down
19 changes: 11 additions & 8 deletions test/Reachability/solve_continuous.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,6 @@ s = solve(IVP(LCS(sparse(A)), X0), Options(:T=>0.1),
op=BFFPSV18(:vars=>[1,3], :partition=>[1:2, 3:4, [5]], :block_options=>
[HPolygon, HPolygon, Interval]))

# ===============================
# System with an odd dimension
# ===============================
A = [1 2 1.; 0 0. 1; -2 1 4]
X0 = BallInf(ones(3), 0.1)
system = IVP(LCS(sparse(A)), X0)
Expand Down Expand Up @@ -142,9 +139,15 @@ X0 = BallInf(ones(4), 0.1)
# dense identity matrix
E = Matrix(1.0I, size(A))

# instantiate system
Δ = ConstrainedLinearControlContinuousSystem(A, E, nothing, ConstantInput(B*U))
𝒮 = InitialValueProblem(Δ, X0)
# inputs
U1 = ConstantInput(B*U)
U2 = B*U # use internal conversion

# default options (computes all variables)
s = solve(𝒮, :T=>0.1)
for inputs in [U1, U2]
# instantiate system
Δ = CLCCS(A, E, nothing, inputs)
𝒮 = InitialValueProblem(Δ, X0)

# default options (computes all variables)
s = solve(𝒮, :T=>0.1)
end