Пример #1
0
    def test_overwrites_queue_priority(self):
        q = RandomPriorityQueue(minimize)
        node = Node(size=0)
        node.bound = 0
        assert node.queue_priority is None
        assert q.put(node._data) == 0
        assert node.queue_priority is not None
        assert 0 <= node.queue_priority <= 1
        child = node.new_child()
        assert child.queue_priority is None
        assert q.put(child._data) == 1
        assert child.queue_priority is not None
        assert 0 <= child.queue_priority <= 1

        l1 = Node(size=0)
        l1.bound = 1
        l2 = l1.new_child()
        l3 = l2.new_child()
        q = RandomPriorityQueue(minimize)
        assert l2.queue_priority is None
        cnt, data = q.put_get(l2._data)
        assert data is l2._data
        assert l2.queue_priority is not None
        assert 0 <= l2.queue_priority <= 1
        assert cnt == 0
        cnt = q.put(l2._data)
        assert cnt == 1
        assert l3.queue_priority is None
        cnt, data_ = q.put_get(l3._data)
        assert cnt == 2
        assert l3.queue_priority is not None
        assert 0 <= l3.queue_priority <= 1
        assert data_ is max([l2, l3], key=lambda x_: x_.queue_priority)._data
Пример #2
0
    def test_overwrites_queue_priority(self):
        q = FIFOQueue(minimize)
        node = Node(size=0)
        node.bound = 0
        assert node.queue_priority is None
        assert q.put(node._data) == 0
        assert node.queue_priority == 0
        child = node.new_child()
        assert child.queue_priority is None
        assert q.put(child._data) == 1
        assert child.queue_priority == -1

        l1 = Node(size=0)
        l1.bound = 1
        l2 = l1.new_child()
        l3 = l2.new_child()
        q = FIFOQueue(minimize)
        cnt, data = q.put_get(l2._data)
        assert cnt == 0
        assert data is l2._data
        cnt = q.put(l2._data)
        assert cnt == 1
        cnt, data_ = q.put_get(l3._data)
        assert cnt == 2
        assert data_ is l2._data
        cnt, data_ = q.put_get(l1._data)
        assert cnt == 3
        assert data_ is l3._data
        assert q.bound() == 1
Пример #3
0
    def test_overwrites_queue_priority(self):
        q = DepthFirstPriorityQueue(minimize)
        node = Node(size=0)
        node.bound = 0
        assert node.queue_priority is None
        assert q.put(node._data) == 0
        assert node.tree_depth == 0
        assert node.queue_priority == 0
        child = node.new_child()
        assert child.tree_depth == 1
        assert child.queue_priority is None
        assert q.put(child._data) == 1
        assert child.queue_priority == child.tree_depth

        l1 = Node(size=0)
        l1.bound = 1
        l2 = l1.new_child()
        l3 = l2.new_child()
        q = DepthFirstPriorityQueue(minimize)
        q.put(l2._data)
        cnt, data_ = q.put_get(l3._data)
        assert cnt == 1
        assert data_ is l3._data
        cnt, data_ = q.put_get(l2._data)
        assert cnt == 2
        assert data_ is l2._data
        assert q.bound() == 1
Пример #4
0
    def test_overwrites_queue_priority(self):
        q = BestObjectiveFirstPriorityQueue(minimize)
        node = Node(size=0)
        node.bound = -1
        assert node.queue_priority is None
        node.objective = 1
        assert q.put(node._data) == 0
        assert node.objective == 1
        assert node.queue_priority == -1
        child = node.new_child()
        assert child.objective == 1
        child.objective = 0
        assert child.queue_priority is None
        cnt, data_ = q.put_get(child._data)
        assert child.queue_priority == 0
        assert cnt == 1
        assert data_ is child._data
        child.objective = 2
        cnt, data_ = q.put_get(child._data)
        assert child.queue_priority == -2
        assert cnt == 2
        assert data_ is node._data
        assert q.bound() == -1

        q = BestObjectiveFirstPriorityQueue(maximize)
        node = Node(size=0)
        node.bound = 3
        assert node.queue_priority is None
        node.objective = 1
        assert q.put(node._data) == 0
        assert node.objective == 1
        assert node.queue_priority == 1
        child = node.new_child()
        assert child.objective == 1
        child.objective = 2
        assert child.queue_priority is None
        cnt, data_ = q.put_get(child._data)
        assert child.queue_priority == 2
        assert cnt == 1
        assert data_ is child._data
        child.objective = 0
        cnt, data_ = q.put_get(child._data)
        assert child.queue_priority == 0
        assert cnt == 2
        assert data_ is node._data
        assert q.bound() == 3
Пример #5
0
 def test_new_child(self):
     node = Node()
     assert node.objective is None
     assert node.bound is None
     assert node.tree_depth is None
     assert node.queue_priority is None
     assert node.state is None
     node.tree_depth = 0
     node = node.new_child()
     assert node.objective is None
     assert node.bound is None
     assert node.tree_depth == 1
     assert node.queue_priority is None
     assert node.state is None
     node.objective = 1
     node.bound = -1
     node.queue_priority = 5
     node.state = "a"
     node = node.new_child()
     assert node.objective == 1
     assert node.bound == -1
     assert node.tree_depth == 2
     assert node.queue_priority is None
     assert node.state is None
Пример #6
0
 def test_missing_queue_priority(self):
     q = CustomPriorityQueue(minimize)
     node = Node(size=0)
     node.bound = 0
     assert node.queue_priority is None
     with pytest.raises(ValueError):
         q.put(node._data)
     with pytest.raises(ValueError):
         q.put_get(node._data)
     node.queue_priority = 1
     q.put(node._data)
     child = node.new_child()
     assert child.queue_priority is None
     with pytest.raises(ValueError):
         q.put(child._data)
     with pytest.raises(ValueError):
         q.put_get(child._data)
Пример #7
0
 def test_children(self):
     parent = Node()
     assert parent.queue_priority is None
     assert parent.tree_id is None
     assert parent.parent_tree_id is None
     assert parent.tree_depth == 0
     assert len(parent.state) == 0
     parent.queue_priority = 10
     assert parent.queue_priority == 10
     assert parent.tree_id is None
     assert parent.parent_tree_id is None
     assert parent.tree_depth == 0
     assert len(parent.state) == 0
     parent.bound = -1
     assert parent.queue_priority == 10
     assert parent.tree_id is None
     assert parent.parent_tree_id is None
     assert parent.tree_depth == 0
     assert parent.bound == -1
     parent.objective = -2
     assert parent.queue_priority == 10
     assert parent.tree_id is None
     assert parent.parent_tree_id is None
     assert parent.tree_depth == 0
     assert parent.bound == -1
     assert parent.objective == -2
     assert len(parent.state) == 0
     parent.resize(5)
     assert parent.queue_priority == 10
     assert parent.tree_id is None
     assert parent.parent_tree_id is None
     assert parent.tree_depth == 0
     assert parent.bound == -1
     assert parent.objective == -2
     assert len(parent.state) == 5
     children = [parent.new_child() for i in range(3)]
     assert len(children) == 3
     for child in children:
         assert child.queue_priority is None
         assert child.tree_id is None
         assert child.parent_tree_id is None
         assert child.tree_depth == 1
         assert child.bound == -1
         assert child.objective == -2
         assert len(child.state) == 5
     Node._insert_tree_id(parent._data, 0)
     assert parent.tree_id == 0
     children = [parent.new_child() for i in range(3)]
     assert len(children) == 3
     for child in children:
         assert child.queue_priority is None
         assert child.tree_id is None
         assert child.parent_tree_id == 0
         assert child.tree_depth == 1
         assert child.bound == -1
         assert child.objective == -2
         assert len(child.state) == 5
     children = [parent.new_child(size=10) for i in range(4)]
     assert len(children) == 4
     for child in children:
         assert child.queue_priority is None
         assert child.tree_id is None
         assert child.parent_tree_id == 0
         assert child.tree_depth == 1
         assert child.bound == -1
         assert child.objective == -2
         assert len(child.state) == 10
Пример #8
0
    def test_overwrites_queue_priority(self):
        q = LocalGapPriorityQueue(minimize)
        node = Node(size=0)
        node.bound = -inf
        node.objective = inf
        assert node.queue_priority is None
        assert q.put(node._data) == 0
        assert node.queue_priority is not None
        assert node.queue_priority == inf
        child = node.new_child()
        assert child.bound == -inf
        assert child.objective == inf
        child.bound = 0
        assert child.queue_priority is None
        assert q.put(child._data) == 1
        assert child.queue_priority is not None
        assert child.queue_priority == inf
        child = child.new_child()
        assert child.bound == 0
        assert child.objective == inf
        child.objective = 1
        assert child.queue_priority is None
        assert q.put(child._data) == 2
        assert child.queue_priority is not None
        assert child.queue_priority == 1

        l1 = Node(size=0)
        l1.bound = 1
        l1.objective = 5
        l2 = l1.new_child()
        l3 = l2.new_child()
        q = LocalGapPriorityQueue(minimize)
        assert l2.queue_priority is None
        cnt, data = q.put_get(l2._data)
        assert data is l2._data
        assert l2.queue_priority is not None
        assert l2.queue_priority == 4
        assert cnt == 0
        cnt = q.put(l2._data)
        assert cnt == 1
        assert l3.queue_priority is None
        l3.objective = 6
        cnt, data_ = q.put_get(l3._data)
        assert cnt == 2
        assert l3.queue_priority is not None
        assert l3.queue_priority == 5
        assert data_ is l3._data

        q = LocalGapPriorityQueue(maximize)
        node = Node(size=0)
        node.bound = inf
        node.objective = -inf
        assert node.queue_priority is None
        assert q.put(node._data) == 0
        assert node.queue_priority is not None
        assert node.queue_priority == inf
        child = node.new_child()
        assert child.bound == inf
        assert child.objective == -inf
        child.bound = 0
        assert child.queue_priority is None
        assert q.put(child._data) == 1
        assert child.queue_priority is not None
        assert child.queue_priority == inf
        child = child.new_child()
        assert child.bound == 0
        assert child.objective == -inf
        child.objective = -1
        assert child.queue_priority is None
        assert q.put(child._data) == 2
        assert child.queue_priority is not None
        assert child.queue_priority == 1

        l1 = Node(size=0)
        l1.bound = -1
        l1.objective = -5
        l2 = l1.new_child()
        l3 = l2.new_child()
        q = LocalGapPriorityQueue(maximize)
        assert l2.queue_priority is None
        cnt, data = q.put_get(l2._data)
        assert data is l2._data
        assert l2.queue_priority is not None
        assert l2.queue_priority == 4
        assert cnt == 0
        cnt = q.put(l2._data)
        assert cnt == 1
        assert l3.queue_priority is None
        l3.objective = -6
        cnt, data_ = q.put_get(l3._data)
        assert cnt == 2
        assert l3.queue_priority is not None
        assert l3.queue_priority == 5
        assert data_ is l3._data