2017-08-08 81 views
1

我創建了一個小型無向網絡,其中一些節點作爲源,一些作爲目標。然後我創建了放在源節點上的步行者。 現在,我想要使用這個網絡實現一個非常簡單的本地路由算法。 在這裏,我的算法步驟;使用Netlogo在複雜網絡上進行本地路由

1 go 
2 get-list-of-neighbors 
3 select one-of from list of neighbors 
    check is-visited: 
    if yes: [remove from the list 
      check is-loop 
      if yes: Die 
      else go to setp 3] 

    4 else Move-to selected node 
    5 check is-target? 
    if yes: die 
    else add to list-of-visited and Go 

問題: 我是新使用Netlog,不知道如何實現這個算法。 這是我的代碼。

to go 
ask walkers[ 
set list-of-neighbors (list [link-neighbors] of location) 
let selected-node one-of list-of-neighbors 
if (visited?=true)[ set list-of-neighbors remove-duplicate list-of-neighbors 
chek if loop? exist 
    if yes: 
    if no: 
if(visited?=false)[ move-to selected-node] 
set location selected-node 
ask location[ ifelse target=true[die][set list-of-visited lput location 
go ] 
end 

回答

2

我的答案是對your other question的回答稍作修改。我不確定check is-loop究竟是什麼意思,所以在我的解決方案中,如果他們沒有可移動的鄰居節點(因爲他們已經訪問了該節點),我就會讓步行者死亡。另外,我建議採用一種稍微不同的方法來完成與您所概述的算法相同的想法。這裏的步驟更像是:

  • 沃克選擇將其尚未移動的鄰居之一
  • 如果沒有這樣的鄰居存在(因爲它已經訪問了當前位置的所有鄰居)的步行者模具
  • 如果尚未訪問過鄰居不存在,遊走將移動到該鄰居,並添加了新的位置,它的可變locations-list
  • 如果新位置是一個目標:
    1. 目標節點被標記爲訪問
    2. 目標節點改變其顏色
    3. 沃克去世
  • 如果在步驟go結束時沒有步行者,則在源節點上產生新的步行者。
  • 如果被訪問的所有目標,該模型將停止

顯然,如果有一個與它多目標節點的線性路徑,步行者將在每次他們來的第一個目標時死去,所以永遠不會訪問這些更遠的節點 - 但這只是一個例子。刪除die塊或修改其他東西玩耍。

就像我說的,這只是上面鏈接的答案的一個非常小的修改,但我複製下面的整個代碼以方便訪問。

breed [nodes node] 
breed [walkers walker] 

walkers-own [location locations-list] 
nodes-own [ source? target? visited? ] 

to setup 
    clear-all 
    set-default-shape nodes "circle" 
    create-nodes 30 [ 
    set color blue 
    set target? false 
    set source? false 
    set visited? false 
    ] 
    ask nodes [ create-link-with one-of other nodes ] 
    repeat 500 [ layout ] 
    ask nodes [ 
    setxy 0.95 * xcor 0.95 * ycor 
    ] 
    ask n-of 3 nodes [ 
    set target? true 
    set color white 
    ] 

    ask n-of 1 nodes with [ target? = false ] [ 
    set source? true 
    set color green 
    ] 

    spawn-walkers 

    reset-ticks 
end 

to layout 
    layout-spring nodes links 0.5 2 1 
end 

to spawn-walkers 

    create-walkers 1 [ 
    set color red 
    set location one-of nodes with [ source? ] 
    move-to location 
    set locations-list (list location) 
    ] 

end 

to go 
    ask links [ set thickness 0 ] 
    ask walkers [ 
    let new-location one-of ([link-neighbors] of location) with [ not member? self [locations-list] of myself ] 
    ifelse new-location = nobody [ 
     print "I'm stuck!" 
     die 
    ] 
    [ 
     move-to new-location 
     set location new-location 
     set locations-list lput location locations-list 
     ask location [ 
     set visited? true 
     if target? = true [ 
      set color color + 1 
      ask myself [ 
      die 
      ] 
     ] 
     ] 
    ] 
    ] 

    if not any? nodes with [ target? = true and visited? = false ] [ 
    print ("All target nodes have been visited.") 
    stop 
    ] 

    if count walkers < 1 [ 
    spawn-walkers 
    ] 

    tick 
end 
+0

太感謝你了,先生,我得到了我的答案。通過檢查Is循環?我的意思是檢查是否有任何圈子?通過移動到選定的節點。所以行人應該死亡。 –

0

我的問題/算法描述:

  1. 沃克開始從源節點
  2. 在每個節點,每個沃克選擇的下一個節點及其鄰居的
  3. 如果所有的相鄰節點都沒有被訪問,則在未被訪問的節點中選擇下一個鄰居
  4. 如果之前已經訪問了所有相鄰節點,則選擇下一個節點unifo在所有的鄰居之間。步行者被迫返回到以前訪問過的節點
  5. 如果檢測到循環,也就是說,沃克如果被訪問的所有目標死
  6. ,該模型將停止
+0

尊敬的先生們,請您根據以上描述更改代碼。 @Luke C –

+0

您應該研究代碼並嘗試弄清楚 - 大多數新的需求都需要我的答案在下面做一些改動。如果您遇到問題,請發佈一個顯示您嘗試過的代碼的新問題。你的「如果一個週期被發現」是值得自己的問題。本網站旨在解決特定的編碼問題,或者指引您朝着正確的方向而不是提供完整的解決方案。有關更多詳細信息,請參見[詢問幫助](https://stackoverflow.com/help/asking)。此外,如果我的答案在下面解決了原始問題,您應該接受它(帶有複選標記)來解決問題。 –