This commit is contained in:
Casper 2018-10-11 17:10:31 +02:00
parent c6bfb7d30e
commit c9f4182529
No known key found for this signature in database
GPG Key ID: B1156723DB3BDDA8
2 changed files with 10 additions and 12 deletions

View File

@ -1,11 +1,7 @@
import random
import statistics import statistics
from math import inf from math import inf
from typing import Set, List, Tuple from typing import Set, List, Tuple
from scipy.optimize import linprog
import util
from util import Side, Point, gen_point, display from util import Side, Point, gen_point, display
@ -46,10 +42,12 @@ def solve_2dlp(c: Tuple[float, float], constraints: List[Tuple[Tuple[float, floa
:return: x1, x2 :return: x1, x2
""" """
c1, c2 = c c1, c2 = c
x1, x2 = (-inf, -inf) if c1 > 0 else (inf, inf) x1 = -inf if c1 > 0 else inf
x2 = -inf if c2 > 0 else inf
#random.shuffle(constraints) #random.shuffle(constraints)
for i, ((a1, a2), b) in enumerate(constraints[1:], start=0): for i, ((a1, a2), b) in enumerate(constraints[1:]):
if not a1*x1 + a2*x2 <= b: if not a1*x1 + a2*x2 <= b:
x1 = solve_1dlp(c1 - c2*a1/a2, x1 = solve_1dlp(c1 - c2*a1/a2,
[(ai1 - ai2*a1 / a2, bi - ai2*b / a2) for (ai1, ai2), bi in constraints[:i]]) [(ai1 - ai2*a1 / a2, bi - ai2*b / a2) for (ai1, ai2), bi in constraints[:i]])
@ -70,8 +68,8 @@ def mbc_ch(points: Set[Point], flipper: callable) -> Set[Point]:
pr = {p for p in points if p.x >= med_x} pr = {p for p in points if p.x >= med_x}
# Find the bridge over the vertical line in pm # Find the bridge over the vertical line in pm
slope, intercept = solve_2dlp((flipper(med_x), flipper(1)), slope, intercept = solve_2dlp((flipper(med_x), flipper(1)), # confirmed correct
[((flipper(-p.x), flipper(-1)), flipper(-p.y)) for p in points]) [((flipper(-p.x), flipper(-1)), flipper(-p.y)) for p in points]) # confirmed correct
# Find the two points which are on the line, should work # Find the two points which are on the line, should work
on = {p for p in points if sidedness(slope, intercept, p, flipper) == Side.ON} on = {p for p in points if sidedness(slope, intercept, p, flipper) == Side.ON}
@ -79,8 +77,8 @@ def mbc_ch(points: Set[Point], flipper: callable) -> Set[Point]:
right_point = max(on) right_point = max(on)
# Prune the points between the two line points # Prune the points between the two line points
pl = {p for p in points if p.x <= left_point.x} pl = {p for p in pl if p.x <= left_point.x}
pr = {p for p in points if p.x >= right_point.x} pr = {p for p in pr if p.x >= right_point.x}
return set.union(mbc_ch(pl, flipper), {left_point, right_point}, mbc_ch(pr, flipper)) return set.union(mbc_ch(pl, flipper), {left_point, right_point}, mbc_ch(pr, flipper))

View File

@ -7,11 +7,11 @@ from graham import graham_scan
from mbc import mbc from mbc import mbc
from quick_hull import quick_hull from quick_hull import quick_hull
#random.seed(1337_420) random.seed(1337_420)
while True: while True:
print("="*100) print("="*100)
points = {util.gen_point(-100, 100) for i in range(100)} points = {util.gen_point(-100, 100) for i in range(5)}
print("Points:", sorted(points)) print("Points:", sorted(points))
# Sanity check # Sanity check