-
Notifications
You must be signed in to change notification settings - Fork 32
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
Conversation
src/CartesianProduct.jl
Outdated
# create high-dimensional constraints list | ||
for X in clist_low | ||
for constr in X | ||
new_constr = LinearConstraint(sparsevec(prev_step : (dim(constr) + prev_step-1), constr.a), constr.b) |
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.
The indices are a constant vector for each X
, so I would pull it out of the inner loop:
if !isempty(X)
indices = prev_step : (dim(X[1]) + prev_step-1)
end
for constr in X
new_constr = LinearConstraint(sparsevec(indices, constr.a), constr.b)
...
src/CartesianProduct.jl
Outdated
sizehint!(clist, m) | ||
prev_step = 1 | ||
# create high-dimensional constraints list | ||
for X in clist_low |
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.
Maybe do not use X
; we usually use that for sets.
Since there is no difference between code base for CartesianProduct and CartesianProductArray I guess it makes sense to merge this code. However, I am not sure how bad this solution for performance (in other words how expensive is creating CartesianProductArray)
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.
Efficiency remark: Do not create a big array with all constraints that you only run through again in the end. Immediately create the new constraints.
src/CartesianProduct.jl
Outdated
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) |
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.
Use the indices
you defined above 😄
src/CartesianProduct.jl
Outdated
A list of constraints. | ||
|
||
""" | ||
function constraints_list(cpa::CartesianProductArray{N})::Vector{LinearConstraint{N}} where N<:Real |
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 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.
src/CartesianProduct.jl
Outdated
""" | ||
function constraints_list(cpa::CartesianProductArray{N})::Vector{LinearConstraint{N}} where N<:Real | ||
# collect low-dimensional constraints lists | ||
clist_low = [] |
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.
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.
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.
Now we need the docs and a unit test for each function.
The docs go to docs/src/lib/operations.md
and then in the respective section add
constraints_list(::CartesianProduct{N, <:AbstractPolytope}) where N<:Real
resp.
constraints_list(::CartesianProductArray{N, <:AbstractPolytope}) where N<:Real
.
The unit tests go to test/unit_CartesianProduct.jl
. A simple test with two Interval
s is sufficient. But please write them parametric in N
(the numeric type).
src/CartesianProduct.jl
Outdated
|
||
""" | ||
function constraints_list(cpa::CartesianProductArray{N, <:AbstractPolytope})::Vector{LinearConstraint{N}} where N<:Real | ||
# collect low-dimensional constraints lists |
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.
redundant now
src/CartesianProduct.jl
Outdated
### Output | ||
|
||
A list of constraints. | ||
|
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.
nit-picky: empty line
src/CartesianProduct.jl
Outdated
@@ -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 |
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.
update (copy the function header here (without function
))
src/CartesianProduct.jl
Outdated
@@ -353,6 +370,42 @@ function ∈(x::AbstractVector{N}, cpa::CartesianProductArray{N, <:LazySet{N}} | |||
return true | |||
end | |||
|
|||
""" | |||
constraints_list(cpa::CartesianProductArray{N})::Vector{LinearConstraint{N}} where N<:Real |
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.
update
src/CartesianProduct.jl
Outdated
function constraints_list(cpa::CartesianProductArray{N, <:AbstractPolytope})::Vector{LinearConstraint{N}} where N<:Real | ||
c_array = array(cpa) | ||
clist = Vector{LinearConstraint{N}}() | ||
sizehint!(clist, sum(dim(s_low) for s_low in c_array)) |
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.
note that dim(cpa)
exists too
src/CartesianProduct.jl
Outdated
@@ -391,10 +391,10 @@ function constraints_list(cpa::CartesianProductArray{N, <:LazySet{N}})::Vector{L | |||
for c_low in array(cpa) | |||
c_low_list = constraints_list(c_low) | |||
if !isempty(c_low_list) | |||
indices = prev_step : (dim(c_low_list[1]) + prev_step - 1) | |||
indices = collect(prev_step : (dim(c_low_list[1]) + prev_step - 1)) |
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.
What was bad about the colon notation? It was more efficient...
@kostakoida feel free to add your name in https://juliareach.github.io/LazySets.jl/latest/about.html#Credits-1 |
Closes #753.