Make progress on MbC; 1DLP working with unittests. Still working on the real problem: 2DLP.

This commit is contained in:
Casper 2018-10-08 18:31:00 +02:00
parent bb2797a8c2
commit 09e59b2028
No known key found for this signature in database
GPG Key ID: B1156723DB3BDDA8

View File

@ -2,7 +2,8 @@ import random
import statistics
from collections import namedtuple
from enum import Enum, auto
from typing import Set
from math import inf
from typing import Set, List, Tuple
import matplotlib.pyplot as plt
import numpy as np
@ -68,6 +69,65 @@ def sidedness(slope: int, intersection: int, p3: Point, linprog_flipper: callabl
return Side.BELOW
def solve_1dlp(c: float, constraints: List[Tuple[float, float]]):
"""
:param c: c1
:param constraints: [(ai, bi), ...]
:return: x1
"""
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]]):
"""
:param c: (c1, c2)
:param constraints: [(ai1, ai2, bi), ...]
:return: x1, x2
"""
x1, x2 = -inf, -inf # TODO: something other than -inf (?)
our_constraints = []
for (a1, a2), b in constraints: # TODO: random.shuffle()
if not a1*x1 + a2*x2 <= b:
our_constraints.append(((a1, a2), b))
new_obj = c[0] + (b/a2 - a1/a2)
constraint_for_1d = []
for constraint in our_constraints:
(old_a1, old_a2), old_b = constraint
new_a1 = old_a1 + ((old_b/old_a2) - (old_a1/old_a2))
constraint_for_1d.append((new_a1, old_b))
x1 = solve_1dlp(new_obj, constraint_for_1d)
x2 = ((b/a2) - (a1/a2))*x1
return x1, x2
# TODO: assert solve_2dlp((-3, -2), [((-1, 3), 12), ((1, 1), 8), ((2, -1), 10)]) == (6, 2)
c = (-3, -2)
constraints = [
((-1, 3), 12),
((1, 1), 8),
((2, -1), 10),
]
# = (6.0, 2.0)
result = solve_2dlp(c, constraints)
print(result)
exit()
def mbc_ch(points: Set[Point], linprog_flipper: callable) -> Set[Point]:
if len(points) <= 2:
return points