def test_stays_at_low_point(self): gen = hill_climb.seek(0, 0.1, lambda x: math.fabs(x)) count = 0 for x, y in gen: self.assertEqual(x, 0) self.assertEqual(y, 0) self.assertEqual(count, 0)
def cosine_slope(): turtle.setworldcoordinates(-6.2, -12, 6.2, 12) f = lambda x: -x + 5 * math.cos(x) demo = Demo(f) demo.start(-6) demo.bag([x * 0.1 for x in range(-62, 62)]) gen = hill_climb.seek(-6, 0.1, f) for x, y in gen: demo.move(x, y, False)
def quadratic(): turtle.setworldcoordinates(-12, -2, 12, 102) f = lambda x: x**2 demo = Demo(f) demo.start(-10) demo.bag([x * 0.5 for x in range(-20, 21)]) gen = hill_climb.seek(-10, 0.5, f) for x, y in gen: demo.move(x, y, False)
def slanty_bag(): turtle.setworldcoordinates(-2.2, -2, 12.2, 22) demo = Demo(slanty_bag_curve) demo.bag([x * 0.5 for x in range(-1, 22)]) x = -0.5 step = 0.1 demo.start(x) gen = hill_climb.seek(x, step, slanty_bag_curve) for x, y in gen: demo.move(x, y, False)
def stuck(): turtle.setworldcoordinates(-12, -1, 12, 15) f = lambda x: math.fabs(x) demo = Demo(f) start = -10 step = 3 demo.start(start) demo.bag(range(-10, 11)) gen = hill_climb.seek(start, step, f) for x, y in gen: demo.move(x, y, False)
def test_goes_right_if_right_lower(self): gen = hill_climb.seek(0, 0.1, lambda x: -x) for x, y in gen: self.assertEqual(x, 0.1) break
def test_flatline_goes_right(self): gen = hill_climb.seek(0, 0.1, lambda x: 1) for x, y in gen: self.assertEqual(x, 0.1) break # will keep going right