2018-09-24 13:27:08 +00:00
|
|
|
import statistics
|
2018-10-08 16:31:00 +00:00
|
|
|
from math import inf
|
|
|
|
from typing import Set, List, Tuple
|
2018-10-08 13:17:02 +00:00
|
|
|
|
2018-10-09 17:26:55 +00:00
|
|
|
from util import Side, Point, gen_point, display
|
2018-09-24 13:27:08 +00:00
|
|
|
|
|
|
|
|
2018-10-11 13:54:52 +00:00
|
|
|
def sidedness(slope: float, intersection: float, p3: Point, flipper: callable, eps=0.0000001) -> Side:
|
2018-10-09 17:26:55 +00:00
|
|
|
# finds where a point is in regards to a line
|
2018-10-11 13:54:52 +00:00
|
|
|
if flipper(p3.y) - eps <= flipper(slope * p3.x + intersection) <= flipper(p3.y) + eps:
|
2018-10-08 13:17:02 +00:00
|
|
|
return Side.ON
|
|
|
|
elif p3.y > slope * p3.x + intersection:
|
|
|
|
return Side.ABOVE
|
|
|
|
return Side.BELOW
|
|
|
|
|
|
|
|
|
2018-10-08 16:31:00 +00:00
|
|
|
def solve_1dlp(c: float, constraints: List[Tuple[float, float]]):
|
|
|
|
"""
|
|
|
|
:param c: c1
|
|
|
|
:param constraints: [(ai, bi), ...]
|
|
|
|
:return: x1
|
|
|
|
"""
|
2018-10-09 17:26:55 +00:00
|
|
|
try:
|
|
|
|
if c > 0:
|
|
|
|
return max(b/a for a, b in constraints if a < 0)
|
|
|
|
return min(b/a for a, b in constraints if a > 0)
|
|
|
|
except ValueError: # unbounded
|
|
|
|
return -inf if c > 0 else inf
|
2018-10-09 15:52:53 +00:00
|
|
|
|
|
|
|
|
2018-10-09 17:26:55 +00:00
|
|
|
assert solve_1dlp(1, [(-1, -2)]) == 2
|
|
|
|
assert solve_1dlp(1, [(-1, -2), (-1, -3)]) == 3
|
|
|
|
assert solve_1dlp(1, [(-1, -3), (-1, -2)]) == 3
|
|
|
|
assert solve_1dlp(-1, [(1, 3), (1, 2)]) == 2
|
|
|
|
assert solve_1dlp(1, [(-1, 3), (-1, 2)]) == -2
|
2018-10-08 16:31:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
def solve_2dlp(c: Tuple[float, float], constraints: List[Tuple[Tuple[float, float], float]]):
|
|
|
|
"""
|
|
|
|
:param c: (c1, c2)
|
|
|
|
:param constraints: [(ai1, ai2, bi), ...]
|
|
|
|
:return: x1, x2
|
|
|
|
"""
|
2018-10-09 17:26:55 +00:00
|
|
|
c1, c2 = c
|
2018-10-11 15:10:31 +00:00
|
|
|
x1 = -inf if c1 > 0 else inf
|
|
|
|
x2 = -inf if c2 > 0 else inf
|
|
|
|
|
2018-10-09 17:26:55 +00:00
|
|
|
#random.shuffle(constraints)
|
2018-10-09 15:52:53 +00:00
|
|
|
|
2018-10-11 15:10:31 +00:00
|
|
|
for i, ((a1, a2), b) in enumerate(constraints[1:]):
|
2018-10-11 13:54:52 +00:00
|
|
|
if not a1*x1 + a2*x2 <= b:
|
|
|
|
x1 = solve_1dlp(c1 - c2*a1/a2,
|
|
|
|
[(ai1 - ai2*a1 / a2, bi - ai2*b / a2) for (ai1, ai2), bi in constraints[:i]])
|
|
|
|
x2 = (b - a1*x1) / a2
|
2018-10-08 18:02:33 +00:00
|
|
|
|
2018-10-08 16:31:00 +00:00
|
|
|
return x1, x2
|
|
|
|
|
|
|
|
|
2018-10-11 13:54:52 +00:00
|
|
|
def mbc_ch(points: Set[Point], flipper: callable) -> Set[Point]:
|
2018-10-08 13:17:02 +00:00
|
|
|
if len(points) <= 2:
|
|
|
|
return points
|
|
|
|
|
|
|
|
# Find the point with median x-coordinate, and partition the points on this point
|
|
|
|
med_x = statistics.median(p.x for p in points)
|
|
|
|
|
|
|
|
# Find left and right points in regards to median
|
|
|
|
pl = {p for p in points if p.x < med_x}
|
|
|
|
pr = {p for p in points if p.x >= med_x}
|
2018-09-24 13:27:08 +00:00
|
|
|
|
|
|
|
# Find the bridge over the vertical line in pm
|
2018-10-11 15:10:31 +00:00
|
|
|
slope, intercept = solve_2dlp((flipper(med_x), flipper(1)), # confirmed correct
|
|
|
|
[((flipper(-p.x), flipper(-1)), flipper(-p.y)) for p in points]) # confirmed correct
|
2018-10-08 13:17:02 +00:00
|
|
|
|
|
|
|
# Find the two points which are on the line, should work
|
2018-10-11 13:54:52 +00:00
|
|
|
on = {p for p in points if sidedness(slope, intercept, p, flipper) == Side.ON}
|
|
|
|
left_point = min(on)
|
|
|
|
right_point = max(on)
|
2018-10-08 13:17:02 +00:00
|
|
|
|
|
|
|
# Prune the points between the two line points
|
2018-10-11 15:10:31 +00:00
|
|
|
pl = {p for p in pl if p.x <= left_point.x}
|
|
|
|
pr = {p for p in pr if p.x >= right_point.x}
|
2018-10-08 13:17:02 +00:00
|
|
|
|
2018-10-11 13:54:52 +00:00
|
|
|
return set.union(mbc_ch(pl, flipper), {left_point, right_point}, mbc_ch(pr, flipper))
|
2018-10-08 13:17:02 +00:00
|
|
|
|
|
|
|
|
2018-10-11 12:38:59 +00:00
|
|
|
def mbc(points: Set[Point]) -> Set[Point]:
|
|
|
|
return set.union(mbc_ch(points, lambda x: x), mbc_ch(points, lambda x: -x))
|
2018-10-08 13:17:02 +00:00
|
|
|
|
|
|
|
|
2018-10-11 12:38:59 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
points = {gen_point(1, 10) for _ in range(20)}
|
|
|
|
|
|
|
|
upper_hull_points = mbc_ch(points, lambda x: x)
|
|
|
|
lower_hull_points = mbc_ch(points, lambda x: -x)
|
|
|
|
|
|
|
|
display(points, upper_hull_points.union(lower_hull_points))
|