def test_seg_belt_shrink(self): sbelt = SegmentedBelt() sbelt.fix_ends(source=(10,10), sink=(15,15)) seg_nums = [] for l in SOL.shrinker_loop(sbelt.num_segs, init=10): seg_nums.append(l) self.assertEqual(seg_nums[-1], 2)
def test_seg_belt_contains(self): sb = SegmentedBelt() SOL.add(sb.contains(Point2D(0, 0))) SOL.add(sb.contains(Point2D(10, 10))) d = None for d in SOL.shrinker_loop(sb.num_segs): pass self.assertEqual(2, d)
def test_one_seg_belt(self): sbelt = SegmentedBelt() sbelt.fix_ends(source=(10, 10), sink=(15, 10)) SOL.add(sbelt.num_segs == 1) m = SOL.model() self.assertIsNotNone(m) s = sbelt.segment(0) self.assertEqual(s.p1.eval_as_tuple(), (10,10)) self.assertEqual(s.p2.eval_as_tuple(), (15,10))
def test_intersecting_segbelt_and_dseg(self): sb = SegmentedBelt() SOL.add(sb.corner(0) == Point2D(0, 0)) SOL.add(sb.corner(1) == Point2D(10, 0)) SOL.add(sb.sink() == Point2D(20, 0)) SOL.add(sb.num_segs >= 2) ds = Segment(Point2D(), Point2D(), is_diag=True) SOL.add(ds.p1 == Point2D(12, 0)) SOL.add(ds.p2 == Point2D(15, 3)) SOL.add(non_intersecting_seg_belt_diag_seg(sb, ds)) d = None for d in SOL.shrinker_loop(sb.num_segs): pass self.assertEqual(4, d)
def plot_segmented_belt(belt: P.SegmentedBelt, color='gray'): GAP = 0.2 corners = belt.eval_corners() for c1, c2 in zip(corners, corners[1:]): x1 = min(c1[0], c2[0]) x2 = max(c1[0], c2[0]) y1 = min(c1[1], c2[1]) y2 = max(c1[1], c2[1]) r = patches.Rectangle((x1 - 0.5 + GAP, y1 - 0.5 + GAP), x2 - x1 + 1 - 2 * GAP, y2 - y1 + 1 - 2 * GAP, color=color) plt.gca().add_patch(r)
def test_seg_contains(self): sb = SegmentedBelt() SOL.add(sb.num_segs >= 2) SOL.add(sb.segment(0).contains(Point2D(5, 2))) SOL.add(sb.segment(1).contains(Point2D(10, 5))) SOL.add(sb.segment(0).horizontal()) SOL.model() c1 = SOL.eval(sb.corner(1)) self.assertEqual(c1, Point2D(10,2))
def test_two_seg_belt(self): sbelt = SegmentedBelt() sbelt.fix_ends(source=(10, 10), sink=(15, 15)) SOL.add(sbelt.num_segs == 2) m = SOL.model() self.assertIsNotNone(m) s = sbelt.segment(0) p1 = s.p1 p2 = s.p2 self.assertEqual(p1.eval_as_tuple(), (10,10)) self.assertTrue(p2.eval_as_tuple() in [(15,10), (10,15)]) s = sbelt.segment(1) p2 = s.p2 self.assertEqual(p2.eval_as_tuple(), (15,15))
def test_seg_belt_enumerate(self): sb = SegmentedBelt() SOL.add(sb.source() == (0,0)) SOL.add(sb.sink() == (5,5)) SOL.add(sb.num_segs < 5) ln = sb.len(5) for d in SOL.shrinker_loop(ln): pass self.assertEqual(11, d) p0 = None for p in sb.enumerate_points(): if p0: dx = p.x - p0.x dy = p.y - p0.y self.assertTrue(dx == 0 and dy == 1 or dx == 1 and dy == 0) p0 = p
def test_three_seg_belts(self): """ There segmented belts crossing each other if drawn straight. Total length is minified by iterative tightening of constaint """ belt1 = SegmentedBelt() belt2 = SegmentedBelt() belt3 = SegmentedBelt() SOL.add(non_intersecting_seg_belts(belt1, belt2)) SOL.add(non_intersecting_seg_belts(belt1, belt3)) SOL.add(non_intersecting_seg_belts(belt2, belt3)) belt1.fix_ends((0,0), (20, 20)) belt2.fix_ends((0,10), (20, 10)) belt3.fix_ends((0,20), (20, 0)) sz = IntVal() SOL.add(sz.v == belt1.num_segs + belt2.num_segs + belt3.num_segs) t0 = time() dts = [] sizes = [] for cur_sz in SOL.shrinker_loop(sz, init=38, restore=False): t1 = time() dts.append(t1 - t0) t0 = time() sizes.append(cur_sz) self.assertGreater(len(sizes), 0) self.assertEqual(9, sizes[-1]) # number got from experiments with visualization