diff --git a/h2/MBC_carlsen.py b/h2/MBC_carlsen.py index ca8304b..beb7e55 100644 --- a/h2/MBC_carlsen.py +++ b/h2/MBC_carlsen.py @@ -1,10 +1,10 @@ - import matplotlib.pyplot as plt import collections from enum import Enum, auto from random import randint -Point = collections.namedtuple("Point", "x y") +Point = collections.namedtuple("Point", ("x", "y")) + class Side(Enum): ON = auto() @@ -14,13 +14,14 @@ class Side(Enum): def sidedness(slope, intersection, p3, eps=0.0000001): print(slope * p3[0] + intersection ) - """ finds where a point is in regards to a line """ + # finds where a point is in regards to a line if p3[1] - eps <= slope * p3[0] + intersection <= p3[1] + eps or p3[0] - eps <= (p3[1] - intersection)/slope <= p3[0] + eps: return Side.ON elif p3[1] > slope * p3[0] + intersection: return Side.ABOVE return Side.BELOW + def diplay_prune_points(points, p1, p2): xs = [p[0] for p in points] ys = [p[1] for p in points] @@ -90,7 +91,6 @@ def solve1D(points, xm, iteration_num): return v - def findBridge(points, xm): if xm > 0: v = (-float('Inf'), -float('Inf')) @@ -190,5 +190,3 @@ res_ys = [p[1] for p in result] plt.plot(res_xs, res_ys) plt.plot(xs, ys, 'ro') plt.show() - - diff --git a/h2/howto b/h2/howto new file mode 100644 index 0000000..03a94a3 --- /dev/null +++ b/h2/howto @@ -0,0 +1,56 @@ +---- MbC --- +MbC(P: l, r): (finds UH of P from x-coordinates l to r) + Find median x-coordinate x_m + Partition points on x_m into Pl and Pr + Find bridge uv over x_m + Remove points below bridge + Recurse MbC(Pl: l, u.x) and MbC(Pr: v.x, r) + +Find bridge is finding the line (y=ax+b) such that: + 1) passes above all the points + 2) has the lowest intersection point with the median line + + + +---- 1D ---- +Unknown x1 +Minimize c1*x1 +such that +a1*x1 \leq b1 +a2*x1 \leq b2 +... +an*x1 \leq bn + +for each of the constraints: + ai*x1 \leq bi + => + if ai > 0: x1 <= bi/ai + if ai < 0: x1 >= bi/ai + if ai = 0: + if bi < 0: impossible + if bi \geq 0: always satisfied + +this means that each constraint half of the real line +therefore run for-loop on bi/ai and check the ifs + + +when we have feasible region/interval: + if c1 > 0: choose minimum of interval + if c1 < 0: choose maximum of interval + if c1 = 0: degenerate + + + +---- 2D ---- +a and b are unknowns +minimize ax_m + b +such that y_i \leq ax_i + b, for every input point (x_i, y_i) +where x_m is given (median) + +Algorithm: + Initialise v + Randomly shuffle constraints + for i=1 to n: + if constraint i does not violate v, do nothing + else solve 1d LP on boundary of constraint i +