4

次の種類の構造/エントリを持つ配列を使用しています(量子情報ゲームのマスタープロジェクト用); 1 列目のエントリ{0,1}、2 列目{0,1}、3 列目{0,2**(d-1)}、最後の列{0,d-1}。については次のとおりですd=3

G = 
[[0 0 0 0]
 [0 0 0 1]
 [0 0 0 2]
 [0 0 1 0]
 [0 0 1 1]
 [0 0 1 2]
 [0 0 2 0]
 [0 0 2 1]
 [0 0 2 2]
 [0 0 3 0]
 [0 0 3 1]
 [0 0 3 2]
 [0 1 0 0]
 [0 1 0 1]
 [0 1 0 2]
 [0 1 1 0]
 [0 1 1 1]
 [0 1 1 2]
 [0 1 2 0]
 [0 1 2 1]
 [0 1 2 2]
 [0 1 3 0]
 [0 1 3 1]
 [0 1 3 2]
 [1 0 0 0]
 [1 0 0 1]
 [1 0 0 2]
 [1 0 1 0]
 [1 0 1 1]
 [1 0 1 2]
 [1 0 2 0]
 [1 0 2 1]
 [1 0 2 2]
 [1 0 3 0]
 [1 0 3 1]
 [1 0 3 2]
 [1 1 0 0]
 [1 1 0 1]
 [1 1 0 2]
 [1 1 1 0]
 [1 1 1 1]
 [1 1 1 2]
 [1 1 2 0]
 [1 1 2 1]
 [1 1 2 2]
 [1 1 3 0]
 [1 1 3 1]
 [1 1 3 2]]

次の関数を使用して、この配列を構築しています。

def games(d = 3):
    res = np.empty(0).astype(int)
    for a in range(2):
        for b in range(2):
            for x in range(2**(d-1)):
                for y in range(d):
                    res = np.append(res,[a,b,x,y],axis=0)
    res = np.reshape(res,(-1,4))    
    return res

今私ができるようにしたいのは、列のエントリがカウントを開始する順序を簡単に選択することです。(上段右列から左列)

たとえば、1 列目からカウントを開始し、次に 3 列目、4 列目、最後に 2 列目とします。for-loops関数内を並べ替えることでこれを取得できます。

def games(d = 3):
    res = np.empty(0).astype(int)

    for b in range(2):
        for y in range(d):        
            for x in range(2**(d-1)):
                for a in range(2):
                    res = np.append(res,[a,b,x,y],axis=0)
    res = np.reshape(res,(-1,4))    
    return res

これにより、次のことが得られます。

G=
[[0 0 0 0]
 [1 0 0 0]
 [0 0 1 0]
 [1 0 1 0]
 [0 0 2 0]
 [1 0 2 0]
 [0 0 3 0]
 [1 0 3 0]
 [0 0 0 1]
 [1 0 0 1]
 [0 0 1 1]
 [1 0 1 1]
 [0 0 2 1]
 [1 0 2 1]
 [0 0 3 1]
 [1 0 3 1]
 [0 0 0 2]
 [1 0 0 2]
 [0 0 1 2]
 [1 0 1 2]
 [0 0 2 2]
 [1 0 2 2]
 [0 0 3 2]
 [1 0 3 2]
 [0 1 0 0]
 [1 1 0 0]
 [0 1 1 0]
 [1 1 1 0]
 [0 1 2 0]
 [1 1 2 0]
 [0 1 3 0]
 [1 1 3 0]
 [0 1 0 1]
 [1 1 0 1]
 [0 1 1 1]
 [1 1 1 1]
 [0 1 2 1]
 [1 1 2 1]
 [0 1 3 1]
 [1 1 3 1]
 [0 1 0 2]
 [1 1 0 2]
 [0 1 1 2]
 [1 1 1 2]
 [0 1 2 2]
 [1 1 2 2]
 [0 1 3 2]
 [1 1 3 2]]

関数内の for ループの順序を並べ替えることはできますが、すべての並べ替えをカバーするには 24 の異なるケースを作成する必要があります。一般的に、より良いものは何でしょうsolution/approachか?

4

3 に答える 3

0

の各列は、それぞれ、、および単位Gに拡張される 4 つの次元から作成されます。これらの次元は 24 の方法で並べ替えることができます。の各列を形成するために、これらの並べ替えられた次元のいずれかを=の方法で使用できます。22434G424permutations(24,4)10626

したがって、これをすべて正しく取得した場合、10626そのようなGバージョンが作成されます。したがって、メモリ効率を高めるには、ループを使用してこれらの方法を実行するのが理にかなってい10626ます。これがすべての話を実現するための実装です -

import itertools

# Create meshes with upto 2,2,4,3 numbers as is the case across G columns
D0,D1,D2,D3 = np.meshgrid(np.arange(2),np.arange(2),np.arange(4),np.arange(3))

# All possible dimension arrangements with 4 dimensions for each of D0,D1,D2,D3
dims = np.asarray(list(itertools.permutations(range(4))))

# All possible arrangements considering the dimension arrangements
dims_row_idx = np.asarray(list(itertools.combinations(range(dims.shape[0]),4)))

# Use dims_row_idx to select rows of dims and subsequently 
# permute dimensions of D0,D1,D2,D3 and stack them as columns
for d in dims_row_idx:
    c0 = D0.transpose(dims[d[0]]).ravel()
    c1 = D1.transpose(dims[d[1]]).ravel()
    c2 = D2.transpose(dims[d[2]]).ravel()
    c3 = D3.transpose(dims[d[3]]).ravel()
    out = np.column_stack((c0,c1,c2,c3))
    # print(out)
于 2015-09-19T08:28:06.470 に答える