Esempio n. 1
0
 def __init__(self, *args, **kwargs):
     unittest.TestCase.__init__(self, *args, **kwargs)
     angles = np.linspace(0, 2 * np.pi, 8)[:-1]
     self.vertices = [[np.cos(3 * angle), np.sin(3 * angle)] for angle in angles]
     self.vertices = np.array(self.vertices)
     self.params = {'n_steps' : 3, 'vertices' : self.vertices, 'temperature' : 0.001,
                    'temp_cool' : 0.99, 'rand_state' : fake_random.State([])}
Esempio n. 2
0
    def test_run_proposal_trial(self):
        uniform_results = np.linspace(0, 1.0, 10)
        uniform_stack = list(np.flip(uniform_results))
        params = self.params.copy()
        params['rand_state'] = fake_random.State(uniform_stack)
        annealer = tsp_draw.base.Annealer(**params)

        energy_diff = 0.5 * annealer.temperature 
        critical_val = np.exp(-energy_diff / annealer.temperature)
        test_trials = [annealer._run_proposal_trial(energy_diff) for _ in uniform_results] 
        true_trials = [prob < critical_val for prob in uniform_results] 
        self.assertEqual(test_trials, true_trials)
    def test_make_random_pair(self):
        vertices = np.array([[0,0], [1,0], [3,0], [4,0], [5,0], [5,3], [4,3],
                             [3,3], [2,3], [1,3], [0,3]])
        params = self.params.copy()
        params['vertices'] = vertices
        params['size_scale'] = 1.0
        int_stack = [0, 0, 1, 1, 5, 3][::-1]
        params['rand_state'] = fake_random.State(int_stack = int_stack)
        annealer = tsp_draw.size_scale.Annealer(**params)
        true_pool_v = np.array([0, 1, 2, 4, 5, 10])

        true_pair = (4, 10)
        annealer._find_scale_pool()
        pair = annealer._make_random_pair()
        self.assertEqual(true_pair, pair)
    def test_next(self):
        '''
        Vertices are:
        [0, 1]           [3, 1]  [4, 1]
        [0, 0]           [3, 0]  [4, 0]
        Start off with criss-cross diagonals on large spanse with size scale large enough to
        include large horizontals. Then correctly switch diagonals. Try to incorrectly switch
        diagonals and fail. Then incorrectly switch diagonals back.
        '''
        vertices = np.array([[0, 0], [3, 1], [4, 1], [4, 0], [3, 0], [0, 1]])
        true_vertices = vertices.copy()
        int_stack = [2, 1, 1, 2, 2, 1][::-1]
        crit_prob = np.exp(6 - 2 * np.sqrt(10))
        uniform_stack = [np.sqrt(crit_prob), 0.9 * crit_prob][::-1] 
        params = self.params.copy()
        params['vertices'] = vertices
        params['temperature'] = 1.0
        params['size_scale'] = 2.5 
        params['rand_state'] = fake_random.State(int_stack = int_stack, uniform_stack = uniform_stack)
        annealer = tsp_draw.size_scale.Annealer(**params)

        # First test that the size scale pool is right.
        np.testing.assert_equal(annealer.pool_v, np.array([0, 1, 4, 5]))

        # The large diagonals are un-criss crossed.
        next(annealer)
        true_vertices = true_vertices[[0, 4, 3, 2, 1, 5], :]
        np.testing.assert_equal(annealer.vertices, true_vertices)

        # Propose to criss-cross the large diagonals again (this results in larger energy).
        # The proposal should fail.
        next(annealer)
        np.testing.assert_equal(annealer.vertices, true_vertices)

        # Again propose to criss-cross the large diagonals. This time the proposal should
        # be accepted.
        next(annealer)
        true_vertices = true_vertices[[0, 4, 3, 2, 1, 5], :]
        np.testing.assert_equal(annealer.vertices, true_vertices)