如何在 Python 中跟踪对象 + 迭代对象并就地更新列表

How to keep track of objects in Python + iterating over objects and updating a list in place

提问人:Sabrina Hatch 提问时间:6/12/2023 更新时间:6/12/2023 访问量:36

问:

我正在尝试实现 Max-Min Fairness 算法,无论出于何种原因,当我遍历代码时,它都会清空不饱和流的数量。似乎当我尝试更新我的列表时,那就是事情出错的时候。我不确定我在这里做错了什么?有没有办法在迭代时跟踪 python 中的指针并查看对象的值?


# 


# A Python implementation of MmF via the water filling algorithm to demonstrate rate allocation solutions for congestion control and throughput efficiency
def maxMinFair():
  ###-------------------------------- CLASSES --------------------------------###

  # create class Flow and add attributes to each flow
  # source (which link it starts at)
  # dest (which link it ends at)
  # rate (current rate the flow is at)
  class Flow:

    def __init__(self, source, dest, rate):
      self.source = source
      self.dest = dest
      self.rate = rate

  # create class Link and add attributes to each link such as
  # fol (array of active flows on link)
  # cap (current available capacity on link)
  class Link:

    def __init__(self, fol, cap):
      self.fol = fol
      self.cap = cap

  ###-------------------------------- VARIABLES --------------------------------###

  ##hardcode infrastructure
  # links in network initialized with empty array for fol
  b1 = Link([], 1)
  b2 = Link([], 1)
  t1 = Link([], 1)
  t2 = Link([], 1)

  # flows in network
  f1 = Flow(b1, t1, 0)
  f2 = Flow(b1, t1, 0)
  f3 = Flow(b1, t2, 0)
  f4 = Flow(b2, t2, 0)

  # update values for fol now that they have been initialized
  b1 = Link([f1, f2, f3], 1)
  b2 = Link([f4], 1)
  t1 = Link([f1, f2], 1)
  t2 = Link([f3, f4], 1)

  ## initialize variables
  # set of unsaturated flows
  unsat_flows = [f1, f2, f3, f4]
  # set of saturated flows
  sat_flows = []
  # set of unsaturated links
  unsat_links = [b1, b2, t1, t2]
  # set of saturated links
  sat_links = []

  iter = 0
  # while loop to continue alg while there are unsat flows
  while (len(unsat_flows) != 0):
    iter += 1
    print(str(iter) + " this is the iteration")

    temp = []
    temp2 = []
    # break after 3rd iteration
    

    # need to remove the flows that have already been saturated 
# when considering the the calculation for  rate inc
    # for each unsat link, check each fol to see if it's saturated. If yes, update link's fol attribute by removing the sat link from the list of x.fol
    for i in unsat_links:
      for x in i.fol:
        if x in unsat_flows:
          temp2.append(x)
          #print(temp2)
      var = (i.cap / len(temp2))
      print(var)
      temp.append(var)
      
    min_inc = min(temp)
    # for x in unsat_links:
    #   var = (x.cap / len(temp2))
    #   temp.append(var)
    #   print(temp)
    # min_inc = min(temp)
    
    

    for x in unsat_flows:
      x.rate = x.rate + min_inc

    # calculates the capacity of the links and updates their attributes
    for x in unsat_links:
      x.cap = 1 - sum([i.rate for i in x.fol])

    # iterate over the list and if the cap is zero, append to the sat links list and delete from the unsat links list

    arraySize1 = len(unsat_links)
    i = 0
    while i < arraySize1:
      if unsat_links[i].cap == 0:
        sat_links.append(unsat_links[i])
        del unsat_links[i]
        
        arraySize1 -= 1
      else:
        i += 1

    # iterate over the list and if the flow is an attr of a sat link, append to the sat flows list and delete from the unsat flows list
    arraySize2 = len(unsat_flows)
    a = 0
    while a < arraySize2:
      if (unsat_flows[a].dest in sat_links) or (unsat_flows[a].source in sat_links):
        sat_flows.append(unsat_flows[a])
        del unsat_flows[a]                    
        arraySize2 -= 1
      else:
        a += 1

  if len(unsat_flows) == 0:
    print("Yay! We finished the water filling algorithm!")
    for x in sat_flows:
      print("This is flow: " + str(x))
      print("the rate of the flow is" + x.rate)
      print("the source link of the flow is" + x.source)
      print("the destination link of the flow is" + x.dest)
    for x in sat_links:
      print("This is link: " + str(x))
      print("the available capacity of the link is" + x.cap)

  else:
    print("error.")


if __name__ == '__main__':
  maxMinFair()
python list 对象 嵌套 for-loop

评论


答: 暂无答案