Python 如何避免优先级队列中的饥饿
我试图创建一个队列,以避免队列中的元素太长时间。我使用的是链表。我想要实现它的方式是,如果添加了更高的优先级,则被向后推的优先级将增加0.4。我还需要为链表实现排序,但这已经有了一些意义。我真的不明白我应该如何将这0.4添加到已被取代的优先级中Python 如何避免优先级队列中的饥饿,python,python-3.x,linked-list,priority-queue,Python,Python 3.x,Linked List,Priority Queue,我试图创建一个队列,以避免队列中的元素太长时间。我使用的是链表。我想要实现它的方式是,如果添加了更高的优先级,则被向后推的优先级将增加0.4。我还需要为链表实现排序,但这已经有了一些意义。我真的不明白我应该如何将这0.4添加到已被取代的优先级中 class Node: def __init__(self, data=None): self.data = data self.next = None class LinkedList: def __
class Node:
def __init__(self, data=None):
self.data = data
self.next = None
class LinkedList:
def __init__(self,):
self.head = Node()
def append(self, data):
new_node = Node(data)
current = self.head
while current.next is not None:
current = current.next
current.next = new_node
def __str__(self):
data = []
current = self.head
while current is not None:
data.append(current.data)
current = current.next
return "[%s]" %(', '.join(str(i) for i in data))
def __repr__(self):
return self.__str__()
def length(self):
current = self.head
total = 0
while current.next is not None:
total += 1
current = current.next
return total
def display(self):
elements = []
current_node = self.head
while current_node.next is not None:
current_node = current_node.next
elements.append(current_node.data)
print("Elements ", elements)
def get(self, index):
if index >= self.length():
print("Error: Index is out of range!")
return None
current_index = 0
current_node = self.head
while True:
current_node = current_node.next
if current_index == index:
return current_node.data
current_index += 1
def remove(self):
current = self.head
if current is not None:
self.head = current.next
else:
print("Queue is empty!")
def main():
queue = LinkedList()
queue.append(5)
queue.append(2)
queue.display()
main()
我建议使用python而不是链表。因为您正在进行自定义比较,所以可能需要实现中描述的内容 除了优先级之外,添加一个字段,该字段包含将项添加到堆中的时间。然后,您的实际优先级是指定优先级和项目在堆中的时间长度的函数。例如,优先级可能类似于:
elapsed_time = current_time - added_time;
real_priority = assigned_priority * elapsed_time;
// assume items a and b are being compared
if (a.dequeue_time < current_time) {
if (b.dequeue_time < a.dequeue_time) {
// b sorts before a
}
else if (b.dequeue_time > a.dequeue_time) {
// return a sorts before b
}
else { // dequeue times are equal
return result of comparing a.priority to b.priority
}
}
else {
// here, return result of comparing a.priority and b.priority
}
您可能希望将乘法器设置为所用时间的一小部分
另一种可能性是,如果您希望确保队列中的某些内容不会超过某个设定的时间。例如,如果您想确保事情不会停留超过5分钟,可以添加dequeue\u time
字段。然后,您的比较会变成:
elapsed_time = current_time - added_time;
real_priority = assigned_priority * elapsed_time;
// assume items a and b are being compared
if (a.dequeue_time < current_time) {
if (b.dequeue_time < a.dequeue_time) {
// b sorts before a
}
else if (b.dequeue_time > a.dequeue_time) {
// return a sorts before b
}
else { // dequeue times are equal
return result of comparing a.priority to b.priority
}
}
else {
// here, return result of comparing a.priority and b.priority
}
//假设正在比较项目a和b
如果(a.出列时间<当前时间){
if(b.出列时间a.出列时间){
//在b之前返回a
}
否则{//出列时间相等
返回比较a.priority和b.priority的结果
}
}
否则{
//这里,返回比较a.priority和b.priority的结果
}
heapq将比链表更有效,尤其是当队列中的项目数增加时。另外,它已经被写出来了,并且已经开始工作了。您所要做的就是提供比较函数。编写一个函数,调用它
get\u true\u priority(base\u priority,age)
,这样它就可以产生有用的输出,从而“避免饥饿”(可能还有一些其他规则应该遵守,比如单调工作?)。这个函数可以绘制在一个图上,我可能会先绘制我“期望”它如何工作的图,然后再返回到代码。你不需要链表,Pythonlist
就可以了。类中需要两种结构:一种是按当前优先级对项目进行排序或重分类,另一种是按剩余时间“直到添加另一个0.4”。更新优先级得到更新的项目的列表位置。