def test_temperature_stable_point_when_cooled(self): start_x = 0 step = 0.1 f = lambda x: math.fabs(x) cool_temperature = -5.0 gen = sim_anneal.seek(start_x, step, f, cool_temperature) for x, y, temperature, jump in gen: self.assertEqual(temperature, cool_temperature)
def sa_demo(curr_x, step, f, temperature, x_points, min_x, max_x, *setup): turtle.setworldcoordinates(*setup) demo = Demo(f) demo.start(curr_x) demo.bag(x_points) gen = sim_anneal.seek(curr_x, step, f, temperature, min_x, max_x) for x, y, t, j in gen: demo.move(x, y, j) curr_x = x print(curr_x, f(curr_x))
def test_point_stays_within_bounds_when_bounded(self): max_x = 3 min_x = -3 start_x = 2 step = 1. start_temperature = 10.0 f = lambda x: math.fabs(x) gen = sim_anneal.seek(start_x, step, f, start_temperature, min_x, max_x) for x, y, temperature, jump in gen: self.assertLess(x, max_x) self.assertGreater(x, min_x)
def test_finds_close_to_lowest_point(self): prev_x = -1. prev_y = 0 step = 0.1 f = lambda x: math.fabs(x) temperature = 15.0 gen = sim_anneal.seek(prev_x, step, f, temperature) for x, y, temperature, jump in gen: prev_x = x prev_y = y self.assertLess(math.fabs(prev_x), 0.05) self.assertLess(math.fabs(prev_y), 0.05)
def sa_cosine_turtles(bounded): turtle.setworldcoordinates(-6.2, -12, 6.2, 12) curr_x = [-6.0, 0, +6.0] f = lambda x: 10 * math.cos(x) count = 3 demo = [Demo(f) for _ in range(count)] x_points = [x * 0.1 for x in range(-62, 62)] demo[0].bag(x_points) min_x, max_x = bounds(bounded, x_points) gens = [] temperature = 10.0 step = 0.2 for i, x in enumerate(curr_x): demo[i].start(curr_x[i]) gens.append(sim_anneal.seek(x, step, f, temperature, min_x, max_x)) for (x1, y1, t1, j1), (x2, y2, t2, j2), (x3, y3, t3, j3) in zip(*gens): demo[0].move(x1, y1, j1) demo[1].move(x2, y2, j2) demo[2].move(x3, y3, j3)