Skip to content

Commit 5956c1a

Browse files
committed
DOC: comment function region_diff
1 parent 9c3c52f commit 5956c1a

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

polytope/polytope.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1988,6 +1988,11 @@ def region_diff(poly, reg, abs_tol=ABS_TOL, intersect_tol=ABS_TOL,
19881988
save=False):
19891989
"""Subtract a region from a polytope
19901990
1991+
For a discription of the algorithm, see algorithm 2 in:
1992+
Mato Baotić "Polytopic computations in constrained optimal control"
1993+
Automatika, 2009, Vol. 50, No. 3--4, pp. 119--134, 2009.
1994+
https://hrcak.srce.hr/46543
1995+
19911996
@param poly: polytope from which to subtract a region
19921997
@param reg: region which should be subtracted
19931998
@param abs_tol: absolute tolerance
@@ -2076,20 +2081,33 @@ def region_diff(poly, reg, abs_tol=ABS_TOL, intersect_tol=ABS_TOL,
20762081
ax.figure.savefig('./img/res' + str(res_count) + '.pdf')
20772082
res_count += 1
20782083
if counter[level] == 0:
2084+
# This is the first visit to this level
20792085
if save:
20802086
logger.debug('counter[level] is 0')
20812087

2088+
# Go through all remaining polytopes that we want to remove and
2089+
# check whether any of them removes anything from the current set
2090+
# of hyperplanes (constraints)
20822091
for j in xrange(level, N):
2092+
# Construct a set of constraints with the current hyperplanes
2093+
# and the hyperplanes of polytope j.
2094+
# This is the intersection of the current hyperplanes and
2095+
# polytope j
20832096
auxINDICES = np.hstack([
20842097
INDICES,
20852098
range(beg_mi[j], beg_mi[j] + mi[j])
20862099
])
20872100
Adummy = A[auxINDICES, :]
20882101
bdummy = B[auxINDICES]
2102+
# Will polytope j remove anything from the current set of
2103+
# hyperplanes?
20892104
R, _ = cheby_ball(Polytope(Adummy, bdummy))
20902105
if R > abs_tol:
2106+
# Yes, constrain the set of constraints with the negation
2107+
# of one of polytope j's hyperplanes
20912108
level = j
20922109
counter[level] = 1
2110+
# Offset M negates hyperplanes
20932111
INDICES = np.hstack([INDICES, beg_mi[level] + M])
20942112
break
20952113
if R < abs_tol:
@@ -2107,25 +2125,40 @@ def region_diff(poly, reg, abs_tol=ABS_TOL, intersect_tol=ABS_TOL,
21072125
range(beg_mi[level], beg_mi[level] + mi[level])
21082126
])
21092127
else:
2128+
# We have been at this level before
21102129
if save:
21112130
logger.debug('counter[level] > 0')
21122131
# counter(level) > 0
21132132
nzcount = np.nonzero(counter)[0]
2133+
# Start from the deepest level and pop out of it if we are done at
2134+
# that level
21142135
for jj in xrange(len(nzcount) - 1, -1, -1):
21152136
level = nzcount[jj]
21162137
counter[level] += 1
2138+
# Have we checked all hyperplanes?
21172139
if counter[level] <= mi[level]:
2140+
# No
2141+
# We have already handled the negative side of the last
2142+
# hyperplane, so negate it to get the
2143+
# positive side
21182144
INDICES[-1] -= M
2145+
# Add the next negative hyperplane of the polytope of the
2146+
# current level
21192147
INDICES = np.hstack([
21202148
INDICES,
21212149
beg_mi[level] + counter[level] + M - 1
21222150
])
2151+
# Stay at this level, since we haven't checked all
2152+
# hyperplanes yet
21232153
break
21242154
else:
2155+
# We have checked all hyperplanes at this level
2156+
# Pop out of this level by resetting our states
21252157
counter[level] = 0
21262158
INDICES = INDICES[0:m + np.sum(counter)]
21272159
level -= 1
21282160
if level == -1:
2161+
# We're done at all levels, return
21292162
if save:
21302163
if res:
21312164
ax = res.plot()
@@ -2137,10 +2170,17 @@ def region_diff(poly, reg, abs_tol=ABS_TOL, intersect_tol=ABS_TOL,
21372170
return res
21382171
test_poly = Polytope(A[INDICES, :], B[INDICES])
21392172
rc, _ = cheby_ball(test_poly)
2173+
# Do we have a non-empty diff at this level?
21402174
if rc > abs_tol:
21412175
if level == N - 1:
2176+
# The diff is non-empty, and we have no more polytopes to
2177+
# remove.
2178+
# Add the current set of constraints to the result.
21422179
res = union(res, reduce(test_poly), False)
21432180
else:
2181+
# The diff is non-empty, but we need to check whether the
2182+
# remaining polytopes remove anything.
2183+
# Go one level deeper to continue the search.
21442184
level += 1
21452185
logger.debug('returning res from end')
21462186
return res

0 commit comments

Comments
 (0)