我创建了一个小的无向网络,其中一些节点作为源,一些是目标。然后我创建了放置在源节点上的步行器。 现在,我想使用这个网络实现一个非常简单的本地路由算法。 在这里,我的算法步骤;
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
答案 0 :(得分:2)
我的回答是对我对your other question的回答稍作修改。我不确定check is-loop
究竟是什么意思,所以在我的解决方案中,如果他们没有可以移动的相邻节点(因为他们已经访问过该节点),我只会让步行者死亡。此外,我建议采用略微不同的方法来实现与您概述的算法相同的想法。这里的步骤更像是:
locations-list
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
答案 1 :(得分:0)
我的问题/算法说明: