2015-04-24 88 views
0

創建重疊的代理商使用下面的代碼,我得到的代理商是這樣的: Normal-view enter image description here在一條線上

to setup 
      ask breadth-patches [sprout-walls wall-agents[set color 2]] 
      ask length-patches [sprout-walls wall-agents[set color 2]] 
      ask gap-patches [sprout-walls wall-agents[set color 2]] 
      ask length-patches[align-inside-at-top] 
      ask breadth-patches [align-inside-at-right-left] 
      ask gap-patches[align-inside-at-top] 
     end 


to align-inside-at-top ;; patch procedure 
    let counter count walls-here ;; we will use this as a count-down, after using it in some calculations 
    if counter > 0     ;; could assume there are turtles, but we are not. 
    [ let gap1 1/counter   ;; size of turtles, gap between turtles 
     let half-gap gap1/2   ;; half-size of turtles 
     let ytop 0 
     if-else(pycor < 0)[set ytop pycor - .5 - half-gap] 
     [set ytop pycor + .5 - half-gap] 
     let xleft pxcor - .5 - half-gap 
     ask walls-here 
     [ set size gap1  
     set ycor ytop 
     set xcor xleft + gap1 * counter 
     set counter counter - 1 ;; so we're placing them from right to left 
            ; set ycor ycor + 0.125 
     ] 
    ] 
end 

to align-inside-at-right-left ;; patch procedure 
    let counter count turtles-here ;; we will use this as a count-down, after using it in some calculations 
    if counter > 0     ;; could assume there are turtles, but we are not. 
    [ let gap1 1/counter   ;; size of turtles, gap between turtles 
     let half-gap gap1/2   ;; half-size of turtles 
     let ytop pycor + .5 + half-gap 
     let xleft 0 
     if-else (pxcor < 0)[ 
     set xleft pxcor + .5 - half-gap] 
     [  set xleft pxcor - .5 + half-gap 
     ] 
     ask turtles-here 
     [ set size gap1 
     set ycor ytop - gap1 * counter 
     set xcor xleft ;+ gap * counter 
     set counter counter - 1 ;; so we're placing them from right to left 
     ] 
    ] 
end 

注:在矩形的差距是由於下面的代碼

ask patches with [pxcor > (gap * (-1)) and pxcor < gap and pycor =(breadthrec - 1)][ask walls-here[die]] 

這裏,gap = 1,即1個補丁的寬度。

所以輸入參數是wall-agents,它指定了沿長度和寬度補丁程序爲每個補丁創建的代理程序數量。 我希望更改爲創建重疊代理(如下圖所示)(對不起,該圖不太完美,但我希望它可以解釋它)。請幫助如何做到這一點。 enter image description here

回答

1

這是很多要求任何人爲你調試的代碼。

我會建議首先解決問題的一個簡單版本。你有代碼可以在一個補丁中創建wall-agents海龜,沿着一條線均勻間隔嗎?一旦你有了這樣的工作代碼,那麼你可以嘗試將它推廣到更復雜的問題。

如果您在編寫該簡單版本時遇到麻煩,您可以在這裏提出一個較小的問題,在這裏提出的Stack Overflow對於某人回答比您當前的非常大的問題要容易得多。

如果你能寫出更簡單的版本,不要扔掉它 - 保留它,所以你可以回到它,如果你需要。然後解決更大的問題。

您甚至可以將更簡單的版本放入過程中,然後從較大的解決方案中調用該過程。讓小程序起作用,然後從其他程序中調用小程序,通常是將問題分解成可管理部分的好方法。

+0

謝謝!這很有見地。我明白所說的是什麼,但我的確這麼做,因爲在這種情況下簡單地在一個補丁上解決它很簡單。由於間隙補丁和矩形尺寸限制,這個問題需要解決。 –

+0

好的,但是你的問題並不清楚。你能更具體地說明你有什麼困難嗎?你究竟在哪裏陷入困境?如果缺口部分很難,那麼可能會問一個關於缺口部分的問題。如果處理角落很困難,也許會問一個關於角落的問題。這是一堆複雜的代碼,我必須研究它30分鐘,以便全面瞭解它並真正理解它,以及如何改進它。幾乎沒有人有30分鐘的時間,所以如果你需要幫助,你肯定會找到更具體的問題。 –

+0

謝謝!你是對的,我應該調試得更多,更具體。我現在用很長很長的路解決了它。將嘗試修改該問題以尋找更好的解決方案。 –