问题
I created a small undirected network, where some nodes are as sources and some are targets. then I created walkers placed on source nodes. Now, I want to implement a very simple local routing algorithm using this network. Here, my algo steps;
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
Question: I'm new to Netlog, and don't know how to implement this algorithm. Here is my code.
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
回答1:
My answer here is a slight modification to my answer to your other question. I'm not sure exactly what you mean by check is-loop
, so in my solution I just have the walkers die if they have no neighboring nodes to which they can move (as they have already visited that node). Additionally, I'm suggesting a slightly different approach to accomplish the same idea as the algorithm you outline. The steps here are more like:
- Walker selects one of the neighbors to which it has not already moved
- If no such neighbor exists (as it has already visited all neighbors of its current location) the walker dies.
- If a neighbor that has not been visited does exist, the walker will move to that neighbor, and add its new location to its variable
locations-list
- If the new location is a target:
- Target node is marked as visited
- Target node changes its color
- The walker dies
- If no walkers remain at the end of the
go
procedure, a new walker is spawned on the source node. - If all targets are visited, the model stops
Obviously if there is a linear path with multiple target nodes on it, the walkers will die each time they come to the first target and so will never visit those nodes that are farther along- but this is just an example. Remove the die
chunk or modify other things to play around.
Like I said, this is only a very small modification to the answer linked above, but I copy the whole code below for easy access.
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
回答2:
My Question/ algorithm Description:
- Walker Start from Source node
- At each node, each walker chooses the next node of its neighbor's
- If all the neighboring nodes have not been visited, then the next neighbor is chosen among the nodes that have not been visited
- If all the neighboring nodes have been visited previously, then the next node is chosen uniformly among all the neighbors. The walker is forced to return to a previously visited node
- If a cycle is detected, that is, the walker has to die
- If all targets are visited, the model stops
来源:https://stackoverflow.com/questions/45559595/local-routing-on-complex-network-using-netlogo