Ejemplo n.º 1
0
    def test_update_array_roulette_vacancy_picker_01(self):
        array = np.array([[0, 0, 0], [0, 1, 1], [1, 2, 2]])

        utility = get_utility_for_array(create_flat_utility(0.5), array, True)

        states_for_picked = [
            (0.1, np.array([[0, 0, 2], [0, 1, 1], [1, 0, 2]])),
            (0.2, np.array([[2, 0, 0], [0, 1, 1], [1, 0, 2]])),
            (1.2, np.array([[2, 0, 0], [0, 1, 1], [1, 0, 2]])),
            (2.3, np.array([[0, 2, 0], [0, 1, 1], [1, 0, 2]])),
            (3.4, np.array([[0, 0, 0], [2, 1, 1], [1, 0, 2]])),
            (3.5, np.array([[0, 0, 0], [2, 1, 1], [1, 0, 2]])),
        ]

        result = SimulationResult()

        for picked_value, expected_state in states_for_picked:
            array = np.array([[0, 0, 0], [0, 1, 1], [1, 2, 2]])
            vacancy_picker = _create_roulette_picker(0.1, utility, False,
                                                     lambda *a: picked_value)
            agent_picker = _create_roulette_picker(0.0, utility, True,
                                                   lambda *a: 0.0)

            update_array(array, utility, result, agent_picker, vacancy_picker,
                         True, ['entropy_average'], True)

            with self.subTest(v=picked_value, out=array, exp=expected_state):
                self.assertTrue(np.array_equal(array, expected_state))
Ejemplo n.º 2
0
    def test_roulette_picker_weight01(self):
        agent_indices = [
            (1, 2),
            (7, 8),
            (4, 5),
            (8, 9),
            (3, 4),
        ]

        agent_indices_sorted = [
            (8, 9),
            (7, 8),
            (4, 5),
            (1, 2),
            (3, 4),
        ]

        agent_utilities = [
            0.75,
            0.25,
            0.5,
            0.1,
            1.0,
        ]

        def utility(agent_index, agent_type=None):
            i = agent_indices.index(tuple(agent_index))
            return agent_utilities[i]

        agent_cum_weights = [1.0, 1.85, 2.45, 2.8, 2.9]

        picked_values = np.arange(0, 3.0, 0.1)

        for picked_value in picked_values:

            def uniform_dist(hibound):
                return picked_value

            roulette_picker = _create_roulette_picker(
                0.1, utility, for_agents=True, uniform_dist=uniform_dist)

            index = -1
            for i in range(len(agent_cum_weights)):
                if picked_value < agent_cum_weights[i] or math.isclose(
                        picked_value, agent_cum_weights[i]):
                    index = i
                    break

            expected_output = agent_indices_sorted[index]
            output = roulette_picker(np.array(agent_indices))
            with self.subTest(picked_value=picked_value):
                self.assertEqual(output, expected_output)