works ish
This commit is contained in:
parent
20c484809a
commit
8a507c08fa
89
h2/mbc.py
89
h2/mbc.py
|
@ -75,16 +75,38 @@ def solve_1dlp(c: float, constraints: List[Tuple[float, float]]):
|
||||||
:param constraints: [(ai, bi), ...]
|
:param constraints: [(ai, bi), ...]
|
||||||
:return: x1
|
:return: x1
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
min_ = -10000
|
||||||
|
max_ = 10000
|
||||||
|
|
||||||
|
for constraint in constraints:
|
||||||
|
(a, b) = constraint
|
||||||
|
|
||||||
|
if a == 0:
|
||||||
|
assert(b >= 0)
|
||||||
|
|
||||||
|
if a > 0:
|
||||||
|
max_ = min(b/a, max_)
|
||||||
|
else:
|
||||||
|
min_ = max(b/a, min_)
|
||||||
|
|
||||||
|
|
||||||
if c > 0:
|
if c > 0:
|
||||||
return max(b/a for a, b in constraints if a < 0)
|
return min_
|
||||||
return min(b/a for a, b in constraints if a > 0)
|
else:
|
||||||
|
return max_
|
||||||
|
|
||||||
|
|
||||||
assert solve_1dlp(1, [(-1, -2)]) == 2
|
# if c > 0:
|
||||||
assert solve_1dlp(1, [(-1, -2), (-1, -3)]) == 3
|
# return max(b/a for a, b in constraints if a < 0)
|
||||||
assert solve_1dlp(1, [(-1, -3), (-1, -2)]) == 3
|
# return min(b/a for a, b in constraints if a > 0)
|
||||||
assert solve_1dlp(-1, [(1, 3), (1, 2)]) == 2
|
|
||||||
assert solve_1dlp(1, [(-1, 3), (-1, 2)]) == -2
|
|
||||||
|
#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
|
||||||
|
|
||||||
|
|
||||||
def solve_2dlp(c: Tuple[float, float], constraints: List[Tuple[Tuple[float, float], float]]):
|
def solve_2dlp(c: Tuple[float, float], constraints: List[Tuple[Tuple[float, float], float]]):
|
||||||
|
@ -93,44 +115,51 @@ def solve_2dlp(c: Tuple[float, float], constraints: List[Tuple[Tuple[float, floa
|
||||||
:param constraints: [(ai1, ai2, bi), ...]
|
:param constraints: [(ai1, ai2, bi), ...]
|
||||||
:return: x1, x2
|
:return: x1, x2
|
||||||
"""
|
"""
|
||||||
x1, x2 = -inf, -inf # TODO: something other than -inf (?)
|
|
||||||
|
if c[0] > 0:
|
||||||
|
x1 = -10000
|
||||||
|
else:
|
||||||
|
x1 = 10000
|
||||||
|
|
||||||
|
if c[1] > 0:
|
||||||
|
x2 = -10000
|
||||||
|
else:
|
||||||
|
x2 = 10000
|
||||||
|
|
||||||
our_constraints = []
|
our_constraints = []
|
||||||
for (a1, a2), b in constraints: # TODO: random.shuffle()
|
for (a1, a2), b in constraints: # TODO: random.shuffle()
|
||||||
|
|
||||||
|
print("x1 and x2", x1, x2)
|
||||||
|
|
||||||
|
if len(our_constraints) == 0:
|
||||||
|
our_constraints.append(((a1, a2), b))
|
||||||
|
continue
|
||||||
|
|
||||||
|
print("{} + {} <= {}".format(a1*x1, a2*x2, b))
|
||||||
|
if not (a1*x1 + a2*x2 <= b):
|
||||||
|
|
||||||
if not a1*x1 + a2*x2 <= b:
|
|
||||||
print("rrgeerg"*30)
|
|
||||||
constraint_for_1d = []
|
constraint_for_1d = []
|
||||||
|
|
||||||
# a_prime = a1 - (a2*a1)/a2
|
new_obj = c[0] - ((c[1]*a1)/a2)
|
||||||
# b_prime = b - (a2*b)/a2
|
|
||||||
|
|
||||||
# print("a,b prime", a_prime, b_prime)
|
|
||||||
# constraint_for_1d.append((a_prime, b_prime))
|
|
||||||
|
|
||||||
# Fix this
|
|
||||||
new_obj = c[0] - (c[1]*a1)/a2
|
|
||||||
|
|
||||||
for constraint in our_constraints:
|
for constraint in our_constraints:
|
||||||
(a_i1, a_i2), b_i = constraint
|
(a_i1, a_i2), b_i = constraint
|
||||||
print("lel", a_i1, a_i2, b_i)
|
|
||||||
|
|
||||||
a_prime = a_i1 - ((a_i2*a1)/a2)
|
a_prime = a_i1 - ((a_i2*a1)/a2)
|
||||||
b_prime = b_i - ((a_i2*b)/a2)
|
b_prime = b_i - ((a_i2*b)/a2)
|
||||||
constraint_for_1d.append((a_prime, b_prime))
|
constraint_for_1d.append((a_prime, b_prime))
|
||||||
|
|
||||||
print("lal", a1, a2, b)
|
print("obj", new_obj)
|
||||||
print(new_obj)
|
print("const", constraint_for_1d)
|
||||||
print(constraint_for_1d)
|
|
||||||
|
|
||||||
|
print("lol",[cons[0] for cons in constraint_for_1d])
|
||||||
|
# res = linprog([new_obj], [[cons[0]] for cons in constraint_for_1d], [[cons[1]] for cons in constraint_for_1d], bounds=[(None, None)])
|
||||||
x1 = solve_1dlp(new_obj, constraint_for_1d)
|
x1 = solve_1dlp(new_obj, constraint_for_1d)
|
||||||
x2 = ((b/a2) - (a1/a2))*x1
|
# x1 = res.x[0]
|
||||||
|
x2 = ((b/a2) - (a1/a2)*x1)
|
||||||
|
|
||||||
our_constraints.append(((a1, a2), b))
|
our_constraints.append(((a1, a2), b))
|
||||||
|
|
||||||
|
|
||||||
return x1, x2
|
return x1, x2
|
||||||
|
|
||||||
|
|
||||||
|
@ -146,7 +175,7 @@ constraints = [
|
||||||
|
|
||||||
result = solve_2dlp(c, constraints)
|
result = solve_2dlp(c, constraints)
|
||||||
print(result)
|
print(result)
|
||||||
exit()
|
#exit()
|
||||||
|
|
||||||
|
|
||||||
def mbc_ch(points: Set[Point], linprog_flipper: callable) -> Set[Point]:
|
def mbc_ch(points: Set[Point], linprog_flipper: callable) -> Set[Point]:
|
||||||
|
@ -165,8 +194,12 @@ def mbc_ch(points: Set[Point], linprog_flipper: callable) -> Set[Point]:
|
||||||
A = [[linprog_flipper(-p.x), linprog_flipper(-1)] for p in points]
|
A = [[linprog_flipper(-p.x), linprog_flipper(-1)] for p in points]
|
||||||
b = [linprog_flipper(-p.y) for p in points]
|
b = [linprog_flipper(-p.y) for p in points]
|
||||||
|
|
||||||
result = linprog(c, A, b, bounds=[(None, None), (None, None)], options={"tol": 0.00001})
|
#result = linprog(c, A, b, bounds=[(None, None), (None, None)], options={"tol": 0.00001})
|
||||||
slope, intercept = result.x[0], result.x[1]
|
#slope, intercept = result.x[0], result.x[1]
|
||||||
|
|
||||||
|
slope, intercept = solve_2dlp(c, list(zip(A, b)))
|
||||||
|
|
||||||
|
#display_line_only(points, slope, intercept, [])
|
||||||
|
|
||||||
# Find the two points which are on the line, should work
|
# Find the two points which are on the line, should work
|
||||||
left_point = next(p for p in pl if sidedness(slope, intercept, p, linprog_flipper) == Side.ON)
|
left_point = next(p for p in pl if sidedness(slope, intercept, p, linprog_flipper) == Side.ON)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user