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

added constraints_list for CartesianProduct #761

Merged
merged 11 commits into from
Oct 11, 2018
64 changes: 64 additions & 0 deletions src/CartesianProduct.jl
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,23 @@ function ∈(x::AbstractVector{<:Real}, cp::CartesianProduct)::Bool
∈(view(x, n1+1:length(x)), cp.Y)
end

"""
constraints_list(cp::CartesianProduct{N})::Vector{LinearConstraint{N}} where N<:Real
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update (copy the function header here (without function))


Return the list of constraints of a (polytopic) Cartesian product.

### Input

- `cp` -- Cartesian product

### Output

A list of constraints.
"""
function constraints_list(cp::CartesianProduct{N})::Vector{LinearConstraint{N}} where N<:Real
return constraints_list(CartesianProductArray([cp.X, cp.Y]))
end

"""
vertices_list(cp::CartesianProduct{N})::Vector{Vector{N}} where N<:Real

Expand Down Expand Up @@ -353,6 +370,53 @@ function ∈(x::AbstractVector{N}, cpa::CartesianProductArray{N, <:LazySet{N}}
return true
end

"""
constraints_list(cpa::CartesianProductArray{N})::Vector{LinearConstraint{N}} where N<:Real
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update


Return the list of constraints of a (polytopic) Cartesian product.

### Input

- `cpa` -- Cartesian product

### Output

A list of constraints.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit-picky: empty line

"""
function constraints_list(cpa::CartesianProductArray{N})::Vector{LinearConstraint{N}} where N<:Real
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should better use the interface constraints_list(cpa::CartesianProductArray{N, <:AbstractPolytope} given the recent introduction of constraints_list list method for the whole interface.

There is a drawback in this approach (but i don't have a clear picture how to make this play nicely with interfaces), in that the interface restriction i propose will make constraints_list of, say, the lazy linear map of a polytope not work. But first we should add constraints_list(::LazySets.LinearMap{Float64,LazySets.HPolytope{Float64},Float64,Array{Float64,2}}) anyways.

# collect low-dimensional constraints lists
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

redundant now

clist_low = []
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clist_low = [] is an array of type Any. Passing the information known to the compiler will improve efficiency, basically a clist_low = Vector{LinearConstraint{N}}(). See type-stability performance notes.

n = 0
for c_low in array(cpa)
if c_low isa LinearConstraint
push!(clist_low, [c_low])
n += 1
else
constraints = constraints_list(c_low)
push!(clist_low, constraints)
n += length(constraints)
end
end

clist = Vector{LinearConstraint{N}}()
sizehint!(clist, n)
prev_step = 1
# create high-dimensional constraints list
for c_low in clist_low
if !isempty(c_low)
indices = prev_step : (dim(c_low[1]) + prev_step - 1)
end
for constr in c_low
new_constr = LinearConstraint(sparsevec(prev_step : (dim(constr) + prev_step-1), constr.a), constr.b)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the indices you defined above 😄

push!(clist, new_constr)
end
prev_step += dim(c_low[1])
end

return clist
end

"""
vertices_list(cpa::CartesianProductArray{N})::Vector{Vector{N}} where N<:Real

Expand Down