Python

Python

Python

 View Only

How to get the respective number of tokens

  • 1.  How to get the respective number of tokens

    Posted Thu February 16, 2023 11:14 PM
      |   view attached

    Hello All,

    I am creating python code for the Timed Event Graph as in the  screenshot.

    import numpy as np
    from itertools import combinations 
    
    m = [3,4,2,1]
    z = [2,1,2,0]
    
    p = [100,120,3,50]    
    c = [7,140,80,220]  
    
    n = len(m)
    S = np.arange(1,n+1)
    comb = combinations(S, 5)
    only_odd = [num for num in S if num % 2 == 1] 
    len_A = [num for num in only_odd if num !=1] 
    
    storage_j = []
    
    for x in len_A:
        comb = combinations(S,x)
        for j in list(comb): 
            storage_j.append(j)
    
    storage_i = []
    for A_j in storage_j:
        temp_i = []
        for j in A_j: 
            if j ==1:
                i = n-1 #minus 1 to adjust into the python index
            if j != 1:
                i = j-1-1 #minus 1 to adjust into the python index
            temp_i.append(i)
        storage_i.append(temp_i)

    For example: p_4 , c_1 , c_2

    Here is the respective values from the respective list of p and c.

    p_4 = 50
    c_1 = 7
    c_2 = 140

    Then, according to the rule, the combination for the token is z_4 , (m_1 - z_1) , z_2.

    Here is the calculation from the respective list of m and z.

    z_4               = 0 
    m_1 - z_1 = 3 - 2 = 1
    z_2               = 1

    The set of p and c as well as m and z are parameters.

    All of the possible combination is listed on the Table 1 under the Timed Event Graph.

    Then, I created code as follows.

    import numpy as np
    from itertools import combinations 
    
    m = [3,4,2,1]
    z = [2,1,2,0]
    
    p = [100,120,3,50]    
    c = [7,140,80,220]  
    
    n = len(m)
    S = np.arange(1,n+1)
    comb = combinations(S, 5)
    only_odd = [num for num in S if num % 2 == 1] 
    len_A = [num for num in only_odd if num !=1] 
    
    storage_j = []
    
    for x in len_A:
        comb = combinations(S,x)
        for j in list(comb): 
            storage_j.append(j)
    
    storage_i = []
    for A_j in storage_j:
        temp_i = []
        for j in A_j: 
            if j ==1:
                i = n-1 #minus 1 to adjust into the python index
            if j != 1:
                i = j-1-1 #minus 1 to adjust into the python index
            temp_i.append(i)
        storage_i.append(temp_i)

    The storage_i indicates the respective index for each combination in Table 1.

    storage_i = [[3, 0, 1], [3, 0, 2], [3, 1, 2], [0, 1, 2]]

    Then, I created the code for A6 in Table 1 as follows.

    final_A6 = []
        
    for ida in storage_i:
        
        temp = []
        
        for i in ida:    
            
            if i == (n-1):
                
                AC = ((i+1)%2)*c[i] + (1-((i+1)%2))*p[i] #np.remainder(i,2) #AC for R6
               
                
            if i != (n-1):
                
                if (len(temp)==0) or (len(temp)%2 ==0):
                    
                    AC = ((i+1)%2)*p[i] + (1-((i+1)%2))*c[i] #AC for R6
                    
                    
                if (len(temp)%2!=0) and (len(temp)!=0):
                
                    AC = ((i+1)%2)*c[i] + (1-((i+1)%2))*p[i] ##ACC for R6
            temp.append(AC)
        
        final_A6.append(temp)
    
    final_A6

    According to Table 1, the result of A6 is correct as follows.

    final_A6 =[[50, 7, 140], [50, 7, 3], [50, 120, 3], [100, 120, 3]]

    The possible combinations of tokens which I would like to look for are (python index):

    storage_token_A6 = [[z[3] , (m[0] - z[0]), z[1],

                                            [z[3] , (m[0] - z[0]), z[2],

                                            [z[3] , (m[1] - z[1]), z[2],

                                            [z[0] , (m[1] - z[1]), z[2]]

    or:

    expected result = [[0, 1, 1], [0, 1, 1], [0, 3, 1], [2, 3, 1]]

    I used the similar approach for creating the code for Token in Table 1 as follows:

    storage_token_A6 = []
    
    for ida in storage_i:
        
        temp_token_A6 = []
        
        for i in ida:    
            
            if i == (n-1):
                
                token_A6 = z[i]  
                
                
                
            if i != (n-1):
                
                if (len(temp_token_A6)==0) or (len(temp_token_A6)%2 ==0):
                    
                    token_A6 = z[i] 
                    
                    
                if (len(temp)%2!=0) and (len(temp)!=0):
                
                   token_A6 = m[i] - z[i]
                    
            temp_token_A6.append(token_A6)
        
        storage_token_A6.append(temp_token_A6)

    However the following result is incorrect.

    storage_token_A6 = [[0, 1, 3], [0, 1, 0], [0, 3, 0], [1, 3, 0]]

    The correct result of token according to the Table 1 is as follows.

    expected result = [[0, 1, 1], [0, 1, 1], [0, 3, 1], [2, 3, 1]]

    Can anyone let me know what's wrong with the code for Token? If you have other suggestion or questions, please let me know.

    Thank you in advance.



    ------------------------------
    Nicholas Nicholas
    ------------------------------