2012-02-20 5 views
0

私は奇妙な結果が出ているので、これは再投稿です。私は隣接セルに基づいて土地利用コードを変更するセルオートマトンコードを変更するセルのシミュレーションループを実行しようとしています。セルIDキー=土地利用コード値を作成するテキストファイルをインポートします。私はまた、各セルの隣接する隣人とテキストファイルをインポートします。私がコードを初めて実行したとき、7509個のセルは隣接する近隣の土地利用に基づいて土地利用を変更しました。私は辞書のテキストファイルを読み上げてコメントをつけてもう一度実行して、約5,000個のセルを変更しました。もう一度やり直してください。私がやりたいことは、ループ全体を0.0001だけ変更するまでループ内で実行し、その後はループを中断させることです。セル数が変更されるまでループしています。

私はwhileループを試しましたが、私が探している結果が得られません。最初の実行後、カウントは7509で正しいです。その後、カウントは28,476回繰り返します。なぜ私はカウントがゼロに戻るべきなのか分からない。誰かが私が間違っていることを教えてもらえますか?コードは次のとおりです。

import sys, string, csv 

#Creating a dictionary of FID: LU_Codes from external txt file 
text_file = open("H:\SWAT\NC\FID_Whole_Copy.txt", "rb") 
#Lines = text_file.readlines() 
FID_GC_dict = dict() 
reader = csv.reader(text_file, delimiter='\t') 
for line in reader: 
    FID_GC_dict[line[0]] = int(line[1]) 
text_file.close() 

#Importing neighbor list file for each FID value 
Neighbors_file = open("H:\SWAT\NC\Pro_NL_Copy.txt","rb") 
Entries = Neighbors_file.readlines() 
Neighbors_file.close() 

Neighbors_List = map(string.split, Entries) 

#print Neighbors_List 

#creates a list of the current FID 
FID = [x[0] for x in Neighbors_List] 

gridList = [] 
for nlist in Neighbors_List: 
    row = [] 
    for item in nlist: 
     row.append(FID_GC_dict[item]) 
    gridList.append(row) 
#print gridList 

#Calculate when to end of one sweep 
tot_cells = len(FID) 
end_sim = tot_cells 
p = 0.0001 
#Performs cellular automata rules on land use grid codes 
while (end_sim > tot_cells*p): 
    i = iter(FID) 
    count = 0 
    for glist in gridList: 
     Cur_FID = i.next() 
     Cur_GC = glist[0] 
     glist.sort() 
     lr_Value = glist[-1] 
     if lr_Value < 6: 
      tie_LR = glist.count(lr_Value) 
      if tie_LR >= 4 and lr_Value > Cur_GC: 
       FID_GC_dict[Cur_FID] = lr_Value 
       #print "The updated gridcode for FID ", Cur_FID, "is ", FID_GC_dict[Cur_FID] 
       count += 1 
    end_sim = count 
    print end_sim 

ありがとうございました....もう一度! :(

答えて

1

をシミュレーションが停止するように、私は、コードを固定変化した細胞の数が全細胞の0.0001未満である。 whileループを間違った場所に置いています。もし誰かが興味があれば、土地利用セルオートマトンの改訂コードがあります。

import sys, string, csv 

#Creating a dictionary of FID: LU_Codes from external txt file 
text_file = open("H:\SWAT\NC\FID_Whole_Copy.txt", "rb") 
#Lines = text_file.readlines() 
FID_GC_dict = dict() 
reader = csv.reader(text_file, delimiter='\t') 
for line in reader: 
    FID_GC_dict[line[0]] = int(line[1]) 
text_file.close() 

#Importing neighbor list file for each FID value 
Neighbors_file = open("H:\SWAT\NC\Pro_NL_Copy.txt","rb") 
Entries = Neighbors_file.readlines() 
Neighbors_file.close() 
Neighbors_List = map(string.split, Entries) 
#print Neighbors_List 

#creates a list of the current FID 
FID = [x[0] for x in Neighbors_List] 
#print FID 

#Calculate when to end the simulations (neglible change in land use) 
tot_cells = len(FID) 
end_sim = tot_cells 
p = 0.0001 

#Performs cellular automata rules on land use grid codes 
while (end_sim > tot_cells*p): 
    gridList = [] 
    for nlist in Neighbors_List: 
     row = [] 
     for item in nlist: 
      row.append(FID_GC_dict[item]) 
     gridList.append(row) 
    #print gridList 

    i = iter(FID) 
    count = 0 
    for glist in gridList: 
     Cur_FID = i.next() 
     Cur_GC = glist[0] 
     glist.sort() 
     lr_Value = glist[-1] 
     if lr_Value < 6: 
      tie_LR = glist.count(lr_Value) 
      if tie_LR >= 4 and lr_Value > Cur_GC: 
       FID_GC_dict[Cur_FID] = lr_Value 
       print "The updated gridcode for FID ", Cur_FID, "is ", FID_GC_dict[Cur_FID] 
       count += 1 
    end_sim = count    
    print count 
0

私はあなたがそれはただの推測だ採掘が、通常のセルオートマトンはフェーズが終了するまで更新された値を無視して、全体の位相を更新することで動作します。

ときにプログラミングしているセルオートマトンの種類を知りません単純なセルオートマトンの予期しない結果が出たのは、バックアップグリッドにフェーズを適用するのを忘れていたからですが、私が作業していたグリッドに直接適用していました。グリッドをgrid1grid2と呼び、

のようにしますあなたはほとんどまだ現在のフェーズを終えた前に更新する必要があり、セルの隣人が変更されますので、直接GRID1の値を変更
init grid1 with data 
while number of generations < total generations needed 
    calculate grid2 as the next generation of grid1 
    grid1 = grid2 (you replace the real grid with the buffer) 

は異なる結果につながる。..

+0

ありがとうございます。それは理にかなっている。私は自分のコードでその作業をしようとします。私は、forループの後に辞書のコピーを作成し、それを何らかの形で最初の辞書に関連付けると思います。あなたの助けをもう一度ありがとう。 – Linda

関連する問題