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
33 changes: 33 additions & 0 deletions src/CartesianProduct.jl
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,39 @@ 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
# collect low-dimensional constraints lists
clist_low = (constraints_list(cp.X), constraints_list(cp.Y))

clist = Vector{LinearConstraint{N}}()
m = length(clist_low[1]) + length(clist_low[2])
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.

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

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

return clist
end

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

Expand Down