示例#1
0
def scheduler(world_object, country_name, num_output_schedules, depth_bound,
              frontier_max_size, transforms, transfers):
    frontier = DEPQ(maxlen=frontier_max_size)  # Successors
    schedules = DEPQ(maxlen=num_output_schedules)  # Output schedules
    initial_state = world_object
    current_path = initial_state.get_path()
    initial_state.reset_path()

    # Our queue is now keeps track of the first step and the final step (because we want to search for best
    # move several layers deep but only want the first move to be made)
    frontier.insert((initial_state, initial_state), 0)

    # While there are states to explore and we still want more schedules
    while (frontier.is_empty()
           is not True) and (schedules.size() < num_output_schedules):
        current_state, current_first_step = frontier.popfirst()[
            0]  # just the state not the tuple (state, util)

        # If we still want to explore further (if not add to list of finished schedules
        if current_state.get_depth() < depth_bound:
            successor_states = get_successors(current_state, country_name,
                                              transforms,
                                              transfers)  # Get successors

            # insert successors by their expected utility
            if len(successor_states) != 0 and successor_states[0].get_depth(
            ) == 1:
                for successor in successor_states:
                    frontier.insert(
                        (successor, successor),
                        successor.expected_utility(country_name,
                                                   initial_state))
            else:
                for successor in successor_states:
                    frontier.insert(
                        (successor, current_first_step),
                        successor.expected_utility(country_name,
                                                   initial_state))
        else:
            schedules.insert(
                (current_state, current_first_step),
                current_state.expected_utility(country_name, initial_state))
    # return schedules_to_string(schedules) this is what we used for our previous runs
    # There is a problem here where some countries seem to run out of schedules
    # I am also resetting the path at the start of this method so countries act as if this was their first turn
    final_state = initial_state
    if schedules.size() > 0:
        schedule_tuple = schedules.popfirst()[0]
        final_state = schedule_tuple[1]
    else:
        initial_state.country_passes(country_name)
        final_state = copy.deepcopy(initial_state)

    # This adds back the old path + the move that was just made
    new_path = final_state.get_path()
    current_path.append(new_path)
    final_state.set_path(current_path)
    return final_state
示例#2
0
class BoundedPriorityQueue:
    def __init__(self, N):
        self.pq = DEPQ(iterable=None, maxlen=N)
        self.N = N

    def enqueue(self, k, v, make_copy=False):
        if isinstance(k, np.ndarray):
            for pair in self.pq.data:
                if np.array_equal(k, pair[0]):
                    return
        elif k in [pair[0] for pair in self.pq]:
            return
        while v in [pair[1] for pair in self.pq]:
            v += 1e-4
        if make_copy:
            ck = copy.deepcopy(k)
            self.pq.insert(ck, v)
        else:
            self.pq.insert(k, v)

    def length(self):
        return self.pq.size()

    def empty(self):
        self.pq.clear()

    def isempty(self):
        return self.pq.is_empty()

    def haskey(self, k):
        return k in [pair[0] for pair in self.pq]

    def __iter__(self):
        return start(self)
示例#3
0
class BoundedPriorityQueue:
    """The bounded priority Queue.

    Parameters
    ----------
    N : int
        Size of the queue.
    """

    def __init__(self, N):
        self.pq = DEPQ(iterable=None, maxlen=N)
        self.N = N

    def enqueue(self, k, v, make_copy=False):
        """Storing k into the queue based on the priority value v.

        Parameters
        ----------
        k :
            The object to be stored.
        v : float
            The priority value.
        make_copy : bool, optional
            Whether to make a copy of the k.
        """
        if isinstance(k, np.ndarray):
            for pair in self.pq.data:
                if np.array_equal(k, pair[0]):
                    return
        elif k in [pair[0] for pair in self.pq]:
            return
        while v in [pair[1] for pair in self.pq]:
            v += 1e-4
        if make_copy:
            ck = copy.deepcopy(k)
            self.pq.insert(ck, v)
        else:
            self.pq.insert(k, v)

    def length(self):
        """Return the current size of the queue.

        Returns
        ----------
        length : int
            The current size of the queue.
        """
        return self.pq.size()

    def empty(self):
        """Clear the queue.
        """
        self.pq.clear()

    def isempty(self):
        """Check whether the queue is empty.

        Returns
        ----------
        is_empty : bool
            Whether the queue is empty.
        """
        return self.pq.is_empty()

    def haskey(self, k):
        """Check whether k in in the queue.

        Returns
        ----------
        has_key : bool
            Whether k in in the queue.
        """
        return k in [pair[0] for pair in self.pq]

    def __iter__(self):
        """The redefined iteration function.

        Returns
        ----------
        BPQ_Iterator : generator
            The BPQ iterator.
        """
        # return start(self)
        kvs = list(reversed(sorted(self.pq, key=lambda x: x[1])))
        return (kv for kv in kvs)
示例#4
0
文件: test_depq.py 项目: ofek/depq
class DEPQTest(unittest.TestCase):
    def setUp(self):
        self.depq = DEPQ()
        self.random = SystemRandom()

    def tearDown(self):
        self.depq.clear()

    def test_init_default(self):
        self.assertEqual(len(self.depq.data), 0)
        self.assertEqual(self.depq._maxlen, None)

    def test_init_set_iterable(self):
        depq_non_default = DEPQ([['last', 1], ['first', 3]])
        self.assertEqual(len(depq_non_default), 2)
        self.assertEqual(is_ordered(depq_non_default), True)

    def test_maxlen(self):
        self.assertEqual(self.depq.maxlen, None)

    def test_set_maxlen(self):
        self.depq.extend((None, i) for i in range(7))
        self.depq.set_maxlen(5)
        self.assertEqual(self.depq.low(), 2)

    def test_count_unset_with_hashable(self):
        self.assertEqual(self.depq.count('test'), 0)

    def test_count_unset_with_unhashable(self):
        self.assertEqual(self.depq.count(['test']), 0)

    def test_in_operator_unset_hashable(self):
        self.assertEqual('test' in self.depq, False)

    def test_in_operator_unset_unhashable(self):
        self.assertEqual(['test'] in self.depq, False)

    def test_insert_initial_membership_new_hashable_with_in_operator(self):
        self.depq.insert('test', 7)
        self.assertEqual('test' in self.depq, True)

    def test_insert_initial_membership_new_unhashable_with_in_operator(self):
        self.depq.insert(['test'], 7)
        self.assertEqual(['test'] in self.depq, True)

    def test_insert_mass_membership_new_hashable_with_in_operator(self):
        self.depq.insert('test1', 7)
        self.depq.insert('test2', 5)
        self.assertEqual('test2' in self.depq, True)

    def test_insert_mass_membership_new_unhashable_with_in_operator(self):
        self.depq.insert('test1', 7)
        self.depq.insert(['test2'], 5)
        self.assertEqual(['test2'] in self.depq, True)

    def test_insert_mass_membership_add_hashable_with_count(self):
        self.depq.insert('test', 7)
        self.depq.insert('test', 5)
        self.assertEqual(self.depq.count('test'), 2)

    def test_insert_mass_membership_add_unhashable_with_count(self):
        self.depq.insert(['test'], 7)
        self.depq.insert(['test'], 5)
        self.assertEqual(self.depq.count(['test']), 2)

    def test_insert_initial_populate_first(self):
        self.depq.insert(None, 4)
        self.depq.insert(None, 6)
        self.assertEqual(is_ordered(self.depq), True)
        self.depq.insert(None, 5)
        self.assertEqual(is_ordered(self.depq), True)

    def test_insert_initial_populate_last(self):
        self.depq.insert(None, 6)
        self.depq.insert(None, 4)
        self.assertEqual(is_ordered(self.depq), True)
        self.depq.insert(None, 5)
        self.assertEqual(is_ordered(self.depq), True)

    def test_insert_mass_populate_order(self):
        for i in range(self.random.randrange(20, 100)):
            self.depq.insert(None, self.random.randrange(-1000, 1000))
        self.assertEqual(is_ordered(self.depq), True)

    def test_insert_exceed_maxlen(self):
        depq_non_default = DEPQ(((None, i) for i in range(5)), 4)
        self.assertEqual(depq_non_default.low(), 1)

    def test__repr__empty(self):
        self.assertEqual(repr(self.depq), "DEPQ([])")

    def test__repr__one_item(self):
        self.depq.insert(None, 5)
        self.assertEqual(repr(self.depq), "DEPQ([(None, 5)])")

    def test__repr__multiple_items(self):
        self.depq.insert(None, 5)
        self.depq.insert('test', 3)
        self.assertEqual(repr(self.depq), "DEPQ([(None, 5), ('test', 3)])")

    def test__str__and__unicode__empty(self):
        self.assertEqual(str(self.depq), "DEPQ([])")
        self.assertEqual(str(self.depq), self.depq.__unicode__())

    def test__str__and__unicode__one_item(self):
        self.depq.insert(None, 5)
        self.assertEqual(str(self.depq), "DEPQ([(None, 5)])")
        self.assertEqual(str(self.depq), self.depq.__unicode__())

    def test__str__and__unicode__multiple_items(self):
        self.depq.insert(None, 5)
        self.depq.insert('test', 3)
        self.assertEqual(str(self.depq), "DEPQ([(None, 5), ('test', 3)])")
        self.assertEqual(str(self.depq), self.depq.__unicode__())

    def test__setitem__calls_insert(self):
        self.depq[None] = 5
        self.assertEqual(None in self.depq, True)

    def test__delitem__raise_error(self):
        self.depq.insert(None, 5)
        with self.assertRaises(NotImplementedError):
            del self.depq[0]

    def test__getitem__empty_raise_error(self):
        with self.assertRaises(IndexError):
            self.depq[0] += 1

    def test__getitem__with_items(self):
        self.depq.insert('last', 1)
        self.depq.insert('first', 7)
        self.depq.insert('middle', 5)
        self.assertEqual(self.depq[0], ('first', 7))
        self.assertEqual(self.depq[1], ('middle', 5))
        self.assertEqual(self.depq[2], ('last', 1))
        self.assertEqual(self.depq[-1], ('last', 1))

    def test_first_and_last_empty_raise_error(self):
        with self.assertRaises(IndexError):
            self.depq.first()
        with self.assertRaises(IndexError):
            self.depq.last()

    def test_first_and_last_one_item(self):
        self.depq.insert(None, 1)
        self.assertEqual(self.depq.first(), None)
        self.assertEqual(self.depq.first(), self.depq.last())

    def test_first_and_last_multiple_items(self):
        self.depq.insert('last', 1)
        self.depq.insert('first', 5)
        self.assertEqual(self.depq.first(), 'first')
        self.assertEqual(self.depq.last(), 'last')

    def test_high_and_low_empty_raise_error(self):
        with self.assertRaises(IndexError):
            self.depq.high()
        with self.assertRaises(IndexError):
            self.depq.low()

    def test_high_and_low_one_item(self):
        self.depq.insert(None, 1)
        self.assertEqual(self.depq.high(), 1)
        self.assertEqual(self.depq.high(), self.depq.low())

    def test_high_and_low_multiple_items(self):
        self.depq.insert(None, 1)
        self.depq.insert(None, 5)
        self.assertEqual(self.depq.high(), 5)
        self.assertEqual(self.depq.low(), 1)

    def test_size_and_len(self):
        self.assertEqual(len(self.depq), 0)
        self.assertEqual(len(self.depq), self.depq.size())
        self.depq.insert(None, 5)
        self.assertEqual(len(self.depq), 1)
        self.assertEqual(len(self.depq), self.depq.size())

    def test_is_empty(self):
        self.assertEqual(self.depq.is_empty(), True)
        self.depq.insert(None, 5)
        self.assertEqual(self.depq.is_empty(), False)

    def test_clear(self):
        self.depq.insert('last', 1)
        self.assertEqual(self.depq.size(), 1)
        self.assertEqual(len(self.depq.items), 1)
        self.depq.clear()
        self.assertEqual(self.depq.size(), 0)
        self.assertEqual(len(self.depq.items), 0)

    def test_addfirst_populate_default(self):
        for i in range(self.random.randrange(20, 100)):
            self.depq.addfirst(None)
        self.assertEqual(is_ordered(self.depq), True)
        self.assertEqual(self.depq.high(), self.depq.low())

    def test_addfirst_populate_with_arg(self):
        for i in range(self.random.randrange(20, 100)):
            self.depq.addfirst(None, i)
        self.assertEqual(is_ordered(self.depq), True)
        self.assertGreater(self.depq.high(), self.depq.low())

    def test_addfirst_initial_priority(self):
        self.depq.addfirst(None)
        self.assertEqual(self.depq.high(), 0)

    def test_addfirst_initial_priority_with_arg(self):
        self.depq.addfirst(None, 10)
        self.assertEqual(self.depq.high(), 10)

    def test_addfirst_smaller_priority_raise_error(self):
        self.depq.addfirst(None, 7)
        with self.assertRaises(ValueError):
            self.depq.addfirst(None, 6)

    def test_addfirst_membership_new_hashable(self):
        self.depq.addfirst('test')
        self.assertEqual(self.depq.count('test'), 1)

    def test_addfirst_membership_new_unhashable(self):
        self.depq.addfirst(['test'])
        self.assertEqual(self.depq.count(['test']), 1)

    def test_addfirst_membership_add_hashable(self):
        self.depq.addfirst('test')
        self.depq.addfirst('test')
        self.assertEqual(self.depq.count('test'), 2)

    def test_addfirst_membership_add_unhashable(self):
        self.depq.addfirst(['test'])
        self.depq.addfirst(['test'])
        self.assertEqual(self.depq.count(['test']), 2)

    def test_addfirst_exceed_maxlen(self):
        depq_non_default = DEPQ(((None, i) for i in range(5)), 5)
        depq_non_default.addfirst(None, 10)
        self.assertEqual(depq_non_default.low(), 1)

    def test_addlast_populate_default(self):
        for i in range(self.random.randrange(20, 100)):
            self.depq.addlast(None)
        self.assertEqual(is_ordered(self.depq), True)
        self.assertEqual(self.depq.high(), self.depq.low())

    def test_addlast_populate_with_arg(self):
        for i in reversed(range(self.random.randrange(20, 100))):
            self.depq.addlast(None, i)
        self.assertEqual(is_ordered(self.depq), True)
        self.assertGreater(self.depq.high(), self.depq.low())

    def test_addlast_initial_priority(self):
        self.depq.addlast(None)
        self.assertEqual(self.depq.high(), 0)

    def test_addlast_initial_priority_with_arg(self):
        self.depq.addlast(None, 10)
        self.assertEqual(self.depq.high(), 10)

    def test_addlast_larger_priority_raise_error(self):
        self.depq.addlast(None, 7)
        with self.assertRaises(ValueError):
            self.depq.addlast(None, 8)

    def test_addlast_membership_new_hashable(self):
        self.depq.addlast('test')
        self.assertEqual(self.depq.count('test'), 1)

    def test_addlast_membership_new_unhashable(self):
        self.depq.addlast(['test'])
        self.assertEqual(self.depq.count(['test']), 1)

    def test_addlast_membership_add_hashable(self):
        self.depq.addlast('test')
        self.depq.addfirst('test')
        self.assertEqual(self.depq.count('test'), 2)

    def test_addlast_membership_add_unhashable(self):
        self.depq.addlast(['test'])
        self.depq.addfirst(['test'])
        self.assertEqual(self.depq.count(['test']), 2)

    def test_addlast_exceed_maxlen(self):
        depq_non_default = DEPQ(((None, i) for i in range(5)), 5)
        depq_non_default.addlast(None, -1)
        self.assertEqual(depq_non_default.low(), 0)

    def test_popfirst_empty_raise_error(self):
        with self.assertRaises(IndexError):
            self.depq.popfirst()

    def test_popfirst_membership_remove_hashable(self):
        self.depq.insert('test', 5)
        self.depq.popfirst()
        self.assertEqual(self.depq.count('test'), 0)

    def test_popfirst_membership_remove_unhashable(self):
        self.depq.insert(['test'], 5)
        self.depq.popfirst()
        self.assertEqual(self.depq.count(['test']), 0)

    def test_popfirst_membership_decrement_hashable(self):
        self.depq.insert('test', 5)
        self.depq.insert('test', 7)
        self.depq.popfirst()
        self.assertEqual(self.depq.count('test'), 1)

    def test_popfirst_membership_decrement_unhashable(self):
        self.depq.insert(['test'], 5)
        self.depq.insert(['test'], 7)
        self.depq.popfirst()
        self.assertEqual(self.depq.count(['test']), 1)

    def test_popfirst_order(self):
        for i in range(5):
            self.depq.insert('test', i)
        self.depq.popfirst()
        self.assertEqual(self.depq.high(), 3)

    def test_poplast_empty_raise_error(self):
        with self.assertRaises(IndexError):
            self.depq.poplast()

    def test_poplast_membership_remove_hashable(self):
        self.depq.insert('test', 5)
        self.depq.poplast()
        self.assertEqual(self.depq.count('test'), 0)

    def test_poplast_membership_remove_unhashable(self):
        self.depq.insert(['test'], 5)
        self.depq.poplast()
        self.assertEqual(self.depq.count(['test']), 0)

    def test_poplast_membership_decrement_hashable(self):
        self.depq.insert('test', 5)
        self.depq.insert('test', 7)
        self.depq.poplast()
        self.assertEqual(self.depq.count('test'), 1)

    def test_poplast_membership_decrement_unhashable(self):
        self.depq.insert(['test'], 5)
        self.depq.insert(['test'], 7)
        self.depq.poplast()
        self.assertEqual(self.depq.count(['test']), 1)

    def test_poplast_order(self):
        for i in range(5):
            self.depq.insert('test', i)
        self.depq.poplast()
        self.assertEqual(self.depq.low(), 1)

    def test_remove_invalid_count_raise_error(self):
        with self.assertRaises(ValueError):
            self.depq.remove('test', 'test')
        with self.assertRaises(TypeError):
            self.depq.remove('test', [])

    def test_remove_unset_hashable(self):
        self.assertEqual(self.depq.remove('test'), [])

    def test_remove_unset_unhashable(self):
        self.assertEqual(self.depq.remove(['test']), [])

    def test_remove_zero_does_nothing(self):
        self.depq.insert('test', 5)
        self.assertEqual(self.depq.remove('test', 0), [])
        self.assertEqual(self.depq.count('test'), 1)

    def test_remove_default_membership_remove_hashable(self):
        self.depq.insert('test', 5)
        self.depq.remove('test')
        self.assertEqual(self.depq.count('test'), 0)

    def test_remove_default_membership_remove_unhashable(self):
        self.depq.insert(['test'], 5)
        self.depq.remove(['test'])
        self.assertEqual(self.depq.count(['test']), 0)

    def test_remove_default_membership_decrement_hashable(self):
        self.depq.insert('test', 5)
        self.depq.insert('test', 7)
        self.depq.remove('test')
        self.assertEqual(self.depq.count('test'), 1)

    def test_remove_default_membership_decrement_unhashable(self):
        self.depq.insert(['test'], 5)
        self.depq.insert(['test'], 7)
        self.depq.remove(['test'])
        self.assertEqual(self.depq.count(['test']), 1)

    def test_remove_inbound_arg_membership_remove_hashable(self):
        self.depq.insert('test', 5)
        self.depq.insert('test', 7)
        self.depq.remove('test', 2)
        self.assertEqual(self.depq.count('test'), 0)

    def test_remove_inbound_arg_membership_remove_unhashable(self):
        self.depq.insert(['test'], 5)
        self.depq.insert(['test'], 7)
        self.depq.remove(['test'], 2)
        self.assertEqual(self.depq.count(['test']), 0)

    def test_remove_outbound_arg_membership_remove_hashable(self):
        self.depq.insert('test', 5)
        self.depq.insert('test', 7)
        self.depq.remove('test', 100)
        self.assertEqual(self.depq.count('test'), 0)

    def test_remove_outbound_arg_membership_remove_unhashable(self):
        self.depq.insert(['test'], 5)
        self.depq.insert(['test'], 7)
        self.depq.remove(['test'], 100)
        self.assertEqual(self.depq.count(['test']), 0)

    def test_remove_inbound_arg_membership_decrement_hashable(self):
        self.depq.insert('test', 5)
        self.depq.insert('test', 7)
        self.depq.insert('test', 3)
        self.depq.remove('test', 2)
        self.assertEqual(self.depq.count('test'), 1)

    def test_remove_inbound_arg_membership_decrement_unhashable(self):
        self.depq.insert(['test'], 5)
        self.depq.insert(['test'], 7)
        self.depq.insert(['test'], 3)
        self.depq.remove(['test'], 2)
        self.assertEqual(self.depq.count(['test']), 1)

    def test_remove_membership_with_elim(self):
        self.depq.insert('test', 5)
        self.depq.insert('test', 7)
        self.depq.elim('test')
        self.assertEqual(self.depq.count('test'), 0)

    def test_remove_order(self):
        self.depq.insert('test', 5)
        self.depq.insert('test', 7)
        self.depq.insert('test', 3)
        self.depq.insert('test', 1)
        self.depq.remove('test', 2)
        self.assertEqual(self.depq.low(), 5)
        self.assertEqual(is_ordered(self.depq), True)

    def test_pickle(self):
        for i in range(5):
            self.depq.insert([i], i)
        binary_depq = pickle.dumps(self.depq)
        depq_from_pickle = pickle.loads(binary_depq)
        self.assertEqual(self.depq.data, depq_from_pickle.data)
        self.assertEqual(self.depq.items, depq_from_pickle.items)
        self.assertEqual(type(depq_from_pickle.lock).__name__, 'lock')

    def test_json(self):
        for i in range(5):
            self.depq.insert([i], i)
        json_depq = json.dumps(self.depq.to_json())
        depq_from_json = DEPQ.from_json(json_depq)
        self.assertEqual(self.depq.data, depq_from_json.data)
        self.assertEqual(self.depq.items, depq_from_json.items)
        self.assertEqual(type(depq_from_json.lock).__name__, 'lock')