works ish

This commit is contained in:
Alexander Munch-Hansen 2018-10-09 17:52:53 +02:00
parent 20c484809a
commit 8a507c08fa

View File

@ -75,16 +75,38 @@ def solve_1dlp(c: float, constraints: List[Tuple[float, float]]):
:param constraints: [(ai, bi), ...]
: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:
return max(b/a for a, b in constraints if a < 0)
return min(b/a for a, b in constraints if a > 0)
return min_
else:
return max_
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
# 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)
#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]]):
@ -93,44 +115,51 @@ def solve_2dlp(c: Tuple[float, float], constraints: List[Tuple[Tuple[float, floa
:param constraints: [(ai1, ai2, bi), ...]
: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 = []
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 = []
# a_prime = a1 - (a2*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
new_obj = c[0] - ((c[1]*a1)/a2)
for constraint in our_constraints:
(a_i1, a_i2), b_i = constraint
print("lel", a_i1, a_i2, b_i)
a_prime = a_i1 - ((a_i2*a1)/a2)
b_prime = b_i - ((a_i2*b)/a2)
constraint_for_1d.append((a_prime, b_prime))
print("lal", a1, a2, b)
print(new_obj)
print(constraint_for_1d)
print("obj", new_obj)
print("const", 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)
x2 = ((b/a2) - (a1/a2))*x1
# x1 = res.x[0]
x2 = ((b/a2) - (a1/a2)*x1)
our_constraints.append(((a1, a2), b))
return x1, x2
@ -146,7 +175,7 @@ constraints = [
result = solve_2dlp(c, constraints)
print(result)
exit()
#exit()
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]
b = [linprog_flipper(-p.y) for p in points]
result = linprog(c, A, b, bounds=[(None, None), (None, None)], options={"tol": 0.00001})
slope, intercept = result.x[0], result.x[1]
#result = linprog(c, A, b, bounds=[(None, None), (None, None)], options={"tol": 0.00001})
#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
left_point = next(p for p in pl if sidedness(slope, intercept, p, linprog_flipper) == Side.ON)