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
Merged

added constraints_list for CartesianProduct #761

merged 11 commits into from
Oct 11, 2018

Conversation

kpotomkin
Copy link
Collaborator

@kpotomkin kpotomkin commented Oct 10, 2018

Closes #753.

# 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)
Copy link
Member

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)
...

sizehint!(clist, m)
prev_step = 1
# create high-dimensional constraints list
for X in clist_low
Copy link
Member

@schillic schillic Oct 10, 2018

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)
@kpotomkin kpotomkin self-assigned this Oct 10, 2018
Copy link
Member

@schillic schillic left a 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.

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 😄

A list of constraints.

"""
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.

"""
function constraints_list(cpa::CartesianProductArray{N})::Vector{LinearConstraint{N}} where N<:Real
# collect low-dimensional constraints lists
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.

Copy link
Member

@schillic schillic left a 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 Intervals is sufficient. But please write them parametric in N (the numeric type).


"""
function constraints_list(cpa::CartesianProductArray{N, <:AbstractPolytope})::Vector{LinearConstraint{N}} where N<:Real
# 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

### 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

@@ -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))

@@ -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
Copy link
Member

Choose a reason for hiding this comment

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

update

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))
Copy link
Member

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

@@ -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))
Copy link
Member

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...

@schillic schillic merged commit 7ce4dc0 into master Oct 11, 2018
@schillic schillic deleted the kostemkin/753 branch October 11, 2018 19:05
@mforets
Copy link
Member

mforets commented Oct 11, 2018

@kostakoida feel free to add your name in https://juliareach.github.io/LazySets.jl/latest/about.html#Credits-1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants