Pythonは銀行家アルゴリズムを実現する


銀行家のアルゴリズムはもう紹介する必要はありません.直接コードをつけます.
import copy

#     
def bank_init():
    Process = [0]


#       
def bank_safe(Available,Need,Allocation,Pn):
    #  Work       Avaliable
    Work = copy.deepcopy(Available)
    #  Finish         0 (False)
    Finish = [0]*Pn
    #      
    safeSequence = []
    #   5 
    count = 0
    while count#                      ,   Finish[i]=False
        #  Needi <= Work    Pi,             
        i = 0  #     
        while i < Pn:
            isSafe = True  #                 
            #      finish  0(False)
            if not Finish[i]:
                #   Need[i] Work     
                for ch in Work.keys():
                    #         
                    if Need[i][ch] > Work[ch]:
                        isSafe = False
                # for      isSafe               
                if isSafe:  # isSafe True          
                    safeSequence.append(i+1)  #       
                    Finish[i] = True  #      finish   True
                    for ch2 in Work.keys():
                        # Work = Work+Allocation[i]
                        Work[ch2] = Work[ch2] + Allocation[i][ch2]
            i += 1  #       ,         
        count += 1 #        
    #             ,       Pn     ,      
    if len(safeSequence) ==Pn:
        print("        ,        :",safeSequence)
        return True
    #       False
    print("         ,      ")
    return False


#          
#         request  
def bank_request(processId,requestDict):
    print("          Allocate:", Allocation)
    #   :                    
    for ch in requestDict.keys():
        #                  
        if requestDict[ch] > Need[processId-1][ch]:
            print("          ,       !")
            return False #    
    #   :              
    for ch2 in requestDict.keys():
        #        
        if requestDict[ch2] > Available[ch2]:
            print("        ,       !")
            return False #    

     #    :    
    for ch3 in requestDict.keys():
        #Available[j] = Available[j] - Requesti[j]
        Available[ch3] = Available[ch3] - requestDict[ch3]
    #Allocation[i,j] = Allocation[i,j] + Requesti[j]
    allo = Allocation[processId-1]
    for ch4 in allo.keys():
        allo[ch4] = allo[ch4] +requestDict[ch4]
    #Need[i, j] = Need[i, j] - Requesti[j]
    need = Need[processId - 1]
    for ch5 in need.keys():
        need[ch5] = need[ch5] - requestDict[ch5]


    #   :           ,       ,    ,           
    if  bank_safe(Available,Need,Allocation,Pn):
        print("         Allocate:", Allocation)
    else:
        #          
        for ch3 in requestDict.keys():
            # Available[j] = Available[j] + Requesti[j]
            Available[ch3] = Available[ch3] + requestDict[ch3]
        # Allocation[i,j] = Allocation[i,j] - Requesti[j]
        allo = Allocation[processId - 1]
        for ch4 in allo.keys():
            allo[ch4] = allo[ch4] - requestDict[ch4]
        # Need[i, j] = Need[i, j] + Requesti[j]
        need = Need[processId - 1]
        for ch5 in need.keys():
            need[ch5] = need[ch5] + requestDict[ch5]

    return Allocation

#  Need  
def bank_need(Allocation,Max):
    Need = []
    cn = 0
    for allo in Allocation:
        tmp = copy.deepcopy(allo)  #               need
        for ch in allo.keys():
            tmp[ch] = Max[cn][ch] - allo[ch]
        Need.append(tmp)
        cn += 1
    return Need

#  Available  
def bank_Available(Available,Allocation):
    for allo in Allocation:
        for ch in allo.keys():
            Available[ch] =Available[ch] -  allo[ch]
    return Available


if __name__ == '__main__':
    print("---------            ---------")
    print("---------          ,      ---------' ")

    #       Pn,          
    Pn = int(input("           n:"))
    #          Available
    Available = eval(input("           ,     dict, '{'A':10, 'B':10, 'C':10}' :"))
    #                   、       、         
    print("---------      Max   ---------' ")
    Max = []
    for i in range(1,Pn+1):
        print("    ",i,"             ,     dict, '{'A':1, 'B':2, 'C':3}' ")
        max = eval(input(" :"))
        Max.append(max)
    print("---------      Allocation   ---------' ")
    Allocation = []
    for i in range(1,Pn+1):
        print("    ",i,"               ,     dict, '{'A':1, 'B':2, 'C':3}' ")
        allo = eval(input(" :"))
        Allocation.append(allo)

    #Pn = 5
    #Available = {'A':17, 'B':5, 'C':20}
    # Max = [{'A':5, 'B':5, 'C':9},
    #               {'A':5, 'B':3, 'C':6},
    #               {'A':4, 'B':0, 'C':11},
    #               {'A':4, 'B':2, 'C':5},
    #              {'A':4, 'B':2, 'C':4}]
    # Allocation = [{'A':2, 'B':1, 'C':2},
    #                     {'A':4, 'B':0, 'C':2},
    #                    {'A':4, 'B':0, 'C':5},
    #                    {'A':2, 'B':0, 'C':4},
    #                    {'A':3, 'B':1, 'C':4}]
    print("---------          Need  ---------' ")
    Need =bank_need(Allocation, Max)#          need  
    print("---------          Available  ---------' ")
    bank_Available(Available,Allocation)#          Available  

    while True:
        print("---------           1---------")
        print("---------              2---------")
        print("---------             3---------")
        print("---------       quit---------")
        x = input('          x:')#    
        if x == '1':
            print("---------            ---------")
            processId = int(input("           n:"))
            requestDict = eval(input("         ,     '{'A':10, 'B':10, 'C':10}' :"))
            bank_request(processId, requestDict) #      
            print("---------           ---------")
        elif x == '2':
            print("---------            ---------")
            bank_safe(Available, Need, Allocation, Pn)
            print("---------           ---------")
        elif x == '3':
            print("---------          ---------")
            print(Allocation)
        elif x=='quit':
            print('      ')
            break