/
vecrep.jl
283 lines (244 loc) · 10 KB
/
vecrep.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# collect(::Vector) does a copy.
# lazy_collect avoid this copy in case `v` is a Vector
lazy_collect(v::Vector) = v
lazy_collect(v) = collect(v)
# H-representation
"""
hrep(hyperplanes::HyperPlaneIt, halfspaces::HalfSpaceIt; d::FullDim)
Creates an H-representation for the polyhedron of full dimension `d` equal to
the intersection of the hyperplanes `hyperplanes` and halfspaces `halfspaces`.
### Examples
For instance, the simplex
```math
\\begin{align*}
x_1 + x_2 &= 1 \\\\
x_1 &\\geq 0 \\\\
x_2 &\\geq 0
\\end{align*}
```
can be created as follows:
```jldoctest
julia> hrep([HyperPlane([1, 1], 1)], [HalfSpace([0, -1], 0), HalfSpace([-1, 0], 0)])
H-representation Polyhedra.Intersection{Int64,Array{Int64,1},Int64}:
1-element iterator of HyperPlane{Int64,Array{Int64,1}}:
HyperPlane([1, 1], 1),
2-element iterator of HalfSpace{Int64,Array{Int64,1}}:
HalfSpace([0, -1], 0)
HalfSpace([-1, 0], 0)
```
"""
function hrep(hyperplanes::HyperPlaneIt, halfspaces::HalfSpaceIt;
d::FullDim=FullDim_rec(hyperplanes, halfspaces))
return Intersection(d, hyperplanes, halfspaces)
end
"""
hrep(halfspaces::HalfSpaceIt; d::FullDim)
Creates an H-representation for the polyhedron of full dimension `d` equal to
the intersection of the halfspaces `halfspaces`.
### Examples
For instance, the polytope
```math
\\begin{align*}
x_1 + x_2 &\\leq 1 \\\\
x_1 - x_2 &\\leq 0 \\\\
x_1 & \\geq 0.
\\end{align*}
```
can be created as follows:
```julia
hrep([HalfSpace([1, 1], 1), HalfSpace([1, -1], 0), HalfSpace([-1, 0], 0)])
```
"""
function hrep(halfspaces::ElemIt{HalfSpace{T, AT}}; kws...) where {T, AT}
return hrep(HyperPlane{T, AT}[], halfspaces; kws...)
end
mutable struct Intersection{T, AT, D<:FullDim} <: HRepresentation{T}
hyperplanes::HyperPlanesIntersection{T, AT, D}
halfspaces::Vector{HalfSpace{T, AT}}
function Intersection{T, AT, D}(d::FullDim, hyperplanes::HyperPlaneIt{T},
halfspaces::HalfSpaceIt{T}) where {T, AT, D}
new{T, AT, D}(HyperPlanesIntersection{T, AT, D}(d, hyperplanes),
lazy_collect(halfspaces))
end
end
function Intersection(d::FullDim, hyperplanes::ElemIt{HyperPlane{T, AT}},
halfspaces::ElemIt{HalfSpace{T, AT}}) where {T, AT}
return Intersection{T, AT, typeof(d)}(d, hyperplanes, halfspaces)
end
FullDim(h::Intersection) = FullDim(h.hyperplanes)
hvectortype(::Type{<:Intersection{T, AT}}) where {T, AT} = AT
similar_type(PT::Type{<:Intersection}, d::FullDim, ::Type{T}) where {T} = Intersection{T, similar_type(hvectortype(PT), d, T), typeof(d)}
Intersection(h::HRepresentation{T}) where {T} = Intersection{T}(h)
Intersection{T}(h::HRepresentation) where {T} = convert(Intersection{T, similar_type(hvectortype(typeof(h)), T), typeof(FullDim(h))}, h)
function Base.intersect!(h::Intersection, hp::HyperPlane)
intersect!(h.hyperplanes, hp)
return h
end
function Base.intersect!(h::Intersection, hs::HalfSpace)
push!(h.halfspaces, hs)
return h
end
@subrepelem Intersection HyperPlane hyperplanes
@vecrepelem Intersection HalfSpace halfspaces
fulltype(::Type{<:Union{Intersection{T, AT, D}, HyperPlanesIntersection{T, AT, D}}}) where {T, AT, D} = Intersection{T, AT, D}
# V-representation
#"""
# vrep(sympoints::SymPointIt)
#
#Creates a V-representation for the symmetric polytope equal to the convex hull of the symmetric points `sympoints`.
#
#### Examples
#The following creates a square
#```julia
#vrep([SymPoint([1, 1])], [SymPoint([1, -1])])
#```
#"""
#vrep(sympoints::SymPointIt) = SymPointsHull(sympoints)
#
#mutable struct SymPointsHull{T, AT} <: VSymPolytope{T}
# sympoints::Vector{SymPoint{T, AT}}
# function SymPointsHull{T, AT}(sympoints::SymPointIt{T}) where {T, AT}
# new{T, AT}(lazy_collect(sympoints))
# end
#end
#SymPointsHull(ps::ElemIt{SymPoint{T, AT}}) where {T, AT<:AbstractVector{T}} = SymPointsHull{T, AT}(collect(ps))
#vectortype(::Union{SymPointsHull{T, AT}, Type{SymPointsHull{T, AT}}}) where {T, AT} = AT
#similar_type(PT::Type{<:SymPointsHull}, d::FullDim, ::Type{T}) where {T} = SymPointsHull{T, similar_type(vectortype(PT), d, T)}
#
#SymPointsHull{T, AT}(sympoints::SymPointIt, points::PointIt, lines::LineIt, rays::RayIt) where {T, AT} = Hull{T, AT}(sympoints, points, lines, rays)
#SymPointsHull{T, AT}(sympoints::SymPointIt, lines::LineIt, rays::RayIt) where {T, AT} = Hull{T, AT}(sympoints, AT[], lines, rays)
#
#@vecrepelem SymPointsHull SymPoint sympoints
#
## SymPoint's can be split
#removevredundancy(vrep::SymPointsHull, hrep::HRep; kws...) = removevredundancy(PointsHull(sympoints(vrep), points(vrep)), hrep; kws...)
#"""
# vrep(sympoints::SymPointIt, points::PointIt)
#
#Creates a V-representation for the polytope equal to the convex hull of the symmetric points `sympoints` and points `points`.
#
#### Examples
#The convex hull of ``(0, -1)``, ``(0, 1)`` and ``(1/2, 1/2)`` can be created as follows:
#```julia
#vrep([SymPoint([0, 1])], [[1/2, 1/2]])
#```
#"""
#vrep(sympoints::SymPointIt, points::PointIt) = PointsHull(sympoints, points)
"""
vrep(points::PointIt; d::FullDim)
Creates a V-representation for the polytope of full dimension `d` equal to the
convex hull of the points `points`.
### Examples
The convex hull of ``(0, 0)``, ``(0, 1)`` and ``(1/2, 1/2)`` can be created as follows using exact arithmetic
```julia
vrep([[0, 0], [0, 1], [1//2, 1//2]])
```
or as follows using floating point arithmetic
```julia
vrep([[0, 0], [0, 1], [1/2, 1/2]])
```
"""
vrep(points::PointIt; d::FullDim = FullDim_rec(points)) = PointsHull(d, points)
mutable struct PointsHull{T, AT, D<:FullDim} <: VPolytope{T}
d::D
points::Vector{AT}
function PointsHull{T, AT, D}(d::FullDim, points::PointIt) where {T, AT, D}
new{T, AT, D}(FullDim_convert(D, d), lazy_collect(points))
end
end
function PointsHull(d::FullDim, points::PointIt)
return PointsHull{coefficient_type(eltype(points)), eltype(points),
typeof(d)}(d, points)
end
FullDim(v::PointsHull) = v.d
vvectortype(::Type{<:PointsHull{T, AT}}) where {T, AT} = AT
similar_type(PT::Type{<:PointsHull}, d::FullDim, ::Type{T}) where {T} = PointsHull{T, similar_type(vvectortype(PT), d, T), typeof(d)}
vreptype(::Type{PointsHull{T, AT, D}}) where {T, AT, D} = Hull{T, AT, D}
@vecrepelem PointsHull Point points
"""
vrep(lines::LineIt, rays::RayIt; d::FullDim)
Creates a V-representation for the polyhedral cone of full dimension `d` equal
to the conic hull of the lines `lines` and rays `rays`.
### Examples
```julia
vrep([Line([0, 1])], [Ray([1, 0])])
```
creates a V-representation for the halfspace ``x_1 \\ge 0``.
"""
function vrep(lines::LineIt, rays::RayIt; d = FullDim_rec(lines, rays))
return RaysHull(d, lines, rays)
end
"""
vrep(rays::RayIt)
Creates a V-representation for the polyhedral cone of full dimension `d` equal
to the conic hull of the rays `rays`.
### Examples
```julia
vrep([Ray([1, 0]), Ray([0, 1])])
```
creates a V-representation for positive orthant.
"""
function vrep(rays::ElemIt{Ray{T, AT}}; kws...) where {T, AT}
return vrep(Line{T, AT}[], rays; kws...)
end
mutable struct RaysHull{T, AT, D<:FullDim} <: VCone{T}
lines::LinesHull{T, AT, D}
rays::Vector{Ray{T, AT}}
function RaysHull{T, AT, D}(d::FullDim, ls::LineIt{T},
rs::RayIt{T}) where {T, AT, D}
new{T, AT, D}(LinesHull{T, AT, D}(d, ls), lazy_collect(rs))
end
end
function RaysHull(d::FullDim, ls::ElemIt{Line{T, AT}}, rs::ElemIt{Ray{T, AT}}) where {T, AT}
RaysHull{T, AT, typeof(d)}(d, ls, rs)
end
FullDim(v::RaysHull) = FullDim(v.lines)
vvectortype(::Type{<:RaysHull{T, AT}}) where {T, AT} = AT
similar_type(PT::Type{<:RaysHull}, d::FullDim, ::Type{T}) where {T} = RaysHull{T, similar_type(vvectortype(PT), d, T), typeof(d)}
@vecrepelem RaysHull Ray rays
@subrepelem RaysHull Line lines
vreptype(::Type{RaysHull{T, AT, D}}) where {T, AT, D} = Hull{T, AT, D}
"""
vrep(points::PointIt, lines::LineIt, rays::RayIt)
Creates a V-representation for the polyhedron of full dimension `d` equal to the
minkowski sum of the convex hull of `points` with the conic hull of `lines` and
`rays`.
"""
function vrep(points::PointIt, lines::LineIt, rays::RayIt;
d::FullDim = FullDim_rec(points, lines, rays))
return Hull(d, points, lines, rays)
end
function vrep(points::ElemIt{AT}, lines::ElemIt{Line{T, AT}};
kws...) where {T, AT}
return vrep(points, lines, Ray{T, AT}[]; kws...)
end
mutable struct Hull{T, AT, D<:FullDim} <: VRepresentation{T}
points::PointsHull{T, AT, D}
rays::RaysHull{T, AT, D}
function Hull{T, AT, D}(d::FullDim, vits::VIt{T}...) where {T, AT, D}
points, lines, rays = fillvits(d, vits...)
new{T, AT, D}(PointsHull(d, points), RaysHull(d, lines, rays))
end
end
function Hull(d::FullDim, points::ElemIt{AT}, lines::ElemIt{Line{T, AT}},
rays::ElemIt{Ray{T, AT}}) where {T, AT}
Hull{T, AT, typeof(d)}(d, points, lines, rays)
end
FullDim(v::Hull) = FullDim(v.points)
vvectortype(::Type{<:Hull{T, AT}}) where {T, AT} = AT
similar_type(PT::Type{<:Hull}, d::FullDim, ::Type{T}) where {T} = Hull{T, similar_type(vvectortype(PT), d, T), typeof(d)}
Hull(v::VRepresentation{T}) where {T} = Hull{T}(v)
Hull{T}(v::VRepresentation) where {T} = convert(Hull{T, similar_type(vvectortype(typeof(v)), T), typeof(FullDim(v))}, v)
@subrepelem Hull Point points
@subrepelem Hull Line rays
@subrepelem Hull Ray rays
fulltype(::Type{<:Union{Hull{T, AT, D}, PointsHull{T, AT, D}, LinesHull{T, AT, D}, RaysHull{T, AT, D}}}) where {T, AT, D} = Hull{T, AT, D}
dualtype(::Type{<:Intersection{T, BT, D}}, ::Type{AT}) where {T, AT, BT, D} = Hull{T, AT, D}
dualtype(::Type{<:Hull{T, BT, D}}, ::Type{AT}) where {T, AT, BT, D} = Intersection{T, AT, D}
const AnyIntersection{T, AT, D} = Union{Intersection{T, AT, D}, HyperPlanesIntersection{T, AT, D}}
function dualfullspace(h::Union{AnyIntersection, Type{<:AnyIntersection}},
d::FullDim, ::Type{T}, ::Type{AT}) where {T, AT}
Hull{T, AT, typeof(d)}(d, [origin(AT, fulldim(d))],
Line{T, AT}.(basis.(AT, Ref(d), 1:fulldim(d))),
Ray{T, AT}[])
end