I am working on a project in netLogo in which i have a random network in which each and every link is assigned a bandwidth. The algorithm chooses a random Source and Destination by itself and after which it has to choose an optimal path between these two.
My question is how to choose the optimal path by exploring all the possible paths.
hereby attaching the sourcecode of my model:
breed[nodes node]
breed[ants ant ]
globals [nodename nodenumbersource nodenumberdestination relaynode dead-network num ]
nodes-own[visited ]
links-own[visit bandwidth]
ants-own
[
distance-gone
distance-to-go
target-node
current-node
]
to cr11
ask nodes with [label = "Source"]
[
set num count (link-neighbors)
]
create-ants num ;num-ants
[
let n one-of nodes with [label = "Source"]
setxy ([xcor] of n) ([ycor]of n)
set current-node n
set color white set size 0.95
set distance-gone 0
set distance-to-go 0
set target-node one-of nodes with [ label = "Relay Node" ] ;nobody
]
end
to face-targets
ask ants ;with [ target-node]; = node 4 ] ;nobody ]
[
let d 0
face (one-of nodes with [ label = "Relay Node" ]);target-node
ask current-node [
set d distance (one-of nodes with [ label = "Relay Node" ]);target-node)
]
set distance-to-go d
]
end
to move-forward
face-targets
ask ants [
while [ distance-gone < (distance-to-go )]
[
fd 1
set distance-gone (distance-gone + 1)
]
]
ask ants [
if distance-gone < distance-to-go
[
set current-node target-node
setxy ([xcor] of current-node) ([ycor] of current-node)
set distance-gone 0
set distance-to-go 0
]
]
end
;This is used to design the Network
to setup
setup1
setup-spatially-clustered-network
ask links [set color white
set visit false
set bandwidth (5 + random 10) ;min bw 5 max 15
]
end
to setup1
__clear-all-and-reset-ticks
set dead-network 0
create-nodes number-of-nodes
[
setxy (random-xcor * 0.95) (random-ycor * 0.95)
set shape "circle"
set color green
set visited false
set label who
]
end
;Links are created for the nodes
to setup-spatially-clustered-network
let num-links (6 * number-of-nodes) / 2
while [count links < num-links ]
[
ask one-of turtles
[
let choice (min-one-of (other turtles with [not link-neighbor? myself])
[distance myself])
if choice != nobody [ create-link-with choice
]
]
]
repeat 10
[
layout-spring turtles links 0.3 (world-width / (sqrt number-of-nodes)) 1
]
end
;This is to Generate Message for nodes
to test1
ask one-of nodes
[
set color red
set label "Source"
set nodenumbersource who
]
ask one-of nodes with [color = green]
[
set color red
set label "Destination"
set nodenumberdestination who
]
cr11
end
to test3
ask turtles with [label = "Source"]
[
set label "ants moving"
ask my-links
[
set color green
]
ask link-neighbors
[
set color blue
]
ask min-one-of turtles with [color = blue and my-links] [distance turtle nodenumberdestination ]
[
ask max-one-of my-links [bandwidth ]
[
set color red
]
set color white
set relaynode who
set label "Relay Node"
]
; face-targets
move-forward
end
to test4
ask turtle nodenumberdestination
[
while [color != white]
[
ask turtle relaynode
[
set label ""
ask my-links
[
set color green
]
ask link-neighbors
[
set color yellow
]
]
ask turtles with [color = yellow]
[
set color violet
]
ask turtles with [color = violet] with [visited = false]
[
set color magenta
]
ask min-one-of turtles with [color = magenta] [distance turtle nodenumberdestination]
[
set color white
set relaynode who
set label "Relay Node"
set visited true
]
move-forward
]
]
end
to test6
ask nodes ; turtles
[
set color green
set visited false
set label ""
]
ask links
[
set color white
set visit false
]
end
to test5
test1
test3
test4
end
You can use the NW-Extension for that! Just download it and stick it in NetLogo's extensions folder. Then, in your model, you can start using by adding
extensions [ nw ]
to the top of your code. Then you can use one of the weighted-path-to primitives to get the turtles or links along the shortest path between two nodes.
Note that the nw extension is under active development, though it is well tested. You also must be using the latest version of NetLogo.
If you want to implement it yourself, Dijkstra's algorithm is the classic solution if you have nonnegative weights in your graph.
Related
Suppose I have 2 breeds of agents, sharks (stars and dots) and fish. The stars (large sharks) can eat dots (smaller sharks) and fish that are in-radius 0.5 of a star. A star should eat what is closest to it, either a dot or fish. For simplicity purposes, agents cannot move, and whether or not a star can feed depends on its spatial proximity to dots and fish during setup.
But i'm struggling to get the code to work. Specifically,
I am encountering an "expected reporter" error message in my code and i cannot figure out how to solve this, although I don't know if fixing the code will achieve the aim. Any help is greatly appreciated.
Below is my code:
; Create the agents
breed [sharks shark]
breed [fishes fish]
sharks-own
[
energy
]
fishes-own
[
x0 ; Starting x-cor
y0 ; Starting y-cor
]
to setup
; Always start with this
clear-all
reset-ticks
ask patches [set pcolor gray]
create-sharks 666
[
set color blue
set energy 100
ifelse who >= 15
[
set shape "dot" set size 2.5
]
[
set shape "star" set size 3.5
]
setxy random-xcor random-ycor
]
create-fishes 300
[
setxy random-xcor random-ycor
set x0 xcor
set y0 ycor
set shape "fish"
set size 2.5
set color white
]
; this procedure gives an "expected reporter' error
if shape = "star"
[
ifelse min-one-of turtles with [shape = "dot"]
[
ifelse any? sharks with [shape = "dot"] in-radius 0.5 ; namely here
[
fd 0
]
[
set energy (energy + 100)
set color green
] ; End of 2nd ifelse
]
[
if min-one-of turtles with [shape = "fish"]
[
ifelse any? fishes with [shape = "fish"] in-radius 0.5
[
fd 0
]
[
set energy (energy + 1)
set color yellow
]
]
] ; End of 1st ifelse
] ; End of 1st if
end
You core issue is you are using ifelse min-one-of turtles with [shape = "dot"] as though min-one-of will give a true/false, but it will report a turtle. The error message NetLogo is giving is not great in this case. What I think you want to use any? in those cases (there are two of them that I see).
After those are resolved you have a context error, where you are checking if shape = "star" , but you aren't inside an ask [...] block where that check would have a turtle context to be valid. Maybe that's just a copy/paste issue in getting this question code ready, but I thought I'd note that, too.
Ok after many hours of blood, sweat and tears I finally arrived at the solution. In case the issue/objective is of interest to anyone else, the working code is as follows:
; Create the agents
breed [sharks shark]
breed [fishes fish]
sharks-own
[
energy
]
fishes-own
[
x0 ; Starting x-cor
y0 ; Starting y-cor
]
to setup
; Always start with this
clear-all
reset-ticks
ask patches [set pcolor gray]
create-sharks 666
[
set color blue
set energy 100
ifelse who >= 10
[
set shape "dot" set size 2.5
]
[
set shape "star" set size 3.5
]
setxy random-xcor random-ycor
]
create-fishes 300
[
setxy random-xcor random-ycor
set x0 xcor
set y0 ycor
set shape "fish"
set size 2.5
set color white
]
ask turtles [
if shape = "star"
[
let turtleID min-one-of other turtles [distance myself] ; Create a variable that identifies nearest turtle to apex
ifelse [shape] of turtleID = "dot" ; Evaluate whether the nearest turtle is a small meso
[
ifelse distance turtleID < 0.5 ; check if prey is still close enough
[
; do this if within radius 0.5
set color green
]
[
; do this if not within radius 0.5
set color yellow
]
]
[
if [shape] of turtleID = "fish" ; Evaluate whether the nearest turtle is a fish
[
ifelse distance turtleID < 0.5
[
; do this if within radius 0.5
set color red
]
[
; otehrwise do this if not within radius 0.5
set color orange
]
]
]
]
]
end
Im trying to setup a procedure to change the colour of the outer patches of my cell-like patches so that it can be easily identified which cell is which. So far I have the below code but im getting issues when trying to go-once the program. Code is below
globals [
radius
]
patches-own [
patch-state
CellID
]
to setup
clear-all
set radius 2
create-turtles #cells [
ifelse mode = "Restricted"
[ setxy random-float #units - random-float #units random-float #units - random-float #units ]
[ setxy random-xcor random-ycor ]
]
reset-ticks
end
to go
expand-cells
make-membrane
tick
end
to expand-cells
set radius radius + 1
ask turtles [ ask patches in-radius radius [
if pcolor = black or CellID = [who] of myself + 1 [
build-up-cells
]
]
]
ask patches with [ pcolor = black ] [
set patch-state "X"
]
end
to build-up-cells
set pcolor [ color ] of myself
set CellID [who] of myself + 1
end
to make-membrane
ask patches with [pcolor != black] [
ifelse not any? neighbors with [ CellID = [who] of myself + 1 ]
[ set patch-state "I" ]
[ set patch-state "M" ]
]
ask patches with [ patch-state = "M" ] [
set pcolor pcolor - 1
]
trim
end
to trim
end
Error im getting is this:
A patch can't access a turtle variable without specifying which turtle.
error while patch 40 6 running OF
called by procedure MAKE-MEMBRANE
called by procedure GO
called by Button 'go-once'
The error results from the wrong usage of who. If you take a look into the Netlogo dictionary, you will see that who is a turtle variable, but you run make-membrane on the patches.
I guess, what you want to do is check, if there are any neighbors, that don't belong to the same cells (i.e. don't have the same CellID), and if so, make that patch a membrane patch.
ifelse any? neighbors with [ CellID != [CellID] of myself]
[ set patch-state "M" ]
[ set patch-state "I" ]
I'm trying to iterate over agents to change them randomly.
Basically there ar "n" number of coins (n is defined by a slider that creates those n number of agents)
The function to change them that I'm trying is the following
to tossing
if any? coins with [color = white] [
ask coins [
let coin-toss random-float 1.0
if coin-toss > 0.5 [
print "hi"
;set shape "coin tails"
set color lime
set flip 1
]
if coin-toss < 0.5 [
set color pink
set flip 0
]
]
]
if not any? coins with [color = white] [
ask coins [
set color white
set flip 3
]
]
end
This doesn't even change the colors, and I'm sure it won't change each coin differently.
I've been searching how to approach this but can't find a thing.
Edited solution and adding previous procedure:
to go
ifelse (any? turtles with [color = red]) [
print "red"
restart
;ask coins [ tossing ]
]
[
ask one-of cryptographers [ update ]
wait 0.6
ask coins [ tossing ]
;recolor
]
tick
end
to tossing
if any? coins with [shape = "circle"] [
ask coins [
;let a random-float 1
ifelse (flip = 3) and (random-float 1 > 0.5) [
;print "hi"
set shape "coin tails"
set color lime
set flip 1
]
[
set shape "coin heads"
set flip 0
set color pink
]
wait 0.2
]
]
end
How to assign the shortest-distance to all agents
Current status: My codes only finds the shortest-path for 1 person but cannot when there are more than 1 person.
Question: How can I assign the shortest-path for each person?
Background
I manipulated the traffic grid model inside the NetLogo model library to create a shortest-path algorithm for pedestrians (somewhat close to A*). The interface below shows that an agent is standing on its origin which is coloured in pale yellow, and has the same colour as a destination. It also works fine for 3 people.
1 person
3 people
Setup
Here is a code chunck that I wrote as a setup. It doesn't seem to have any problem in this section.
globals
[
grid-x-inc ;; the amount of patches in between two roads in the x direction
grid-y-inc ;; the amount of patches in between two roads in the y direction
intersections ;; agentset containing the patches that are intersections
roads ;; agentset containing the patches that are roads
population
]
breed[people person]
patches-own
[
father
navigated?
active?
Heuristic
step
total-cost
is-intersection?
start
finish
]
people-own
[
origin
destination
h-distance ;; short for heuristic distance. Euclidean distance to goal
path
]
to setup
clear-all
setup-globals
setup-patches
setup-people
setup-destination
reset-ticks
end
to setup-globals
set grid-x-inc world-width / grid-size-x
set grid-y-inc world-height / grid-size-y
ask patches
[
set father nobody
set navigated? false
set active? false
set total-cost 0
]
end
to setup-patches
;; initialize the patch-owned variables and color the patches to a base-color
ask patches [ set pcolor brown + 3 ]
;; initialize the global variables that hold patch agentsets
set roads patches with
[(floor((pxcor + max-pxcor - floor(grid-x-inc - 1)) mod grid-x-inc) = 0) or
(floor((pycor + max-pycor) mod grid-y-inc) = 0)]
set intersections roads with
[(floor((pxcor + max-pxcor - floor(grid-x-inc - 1)) mod grid-x-inc) = 0) and
(floor((pycor + max-pycor) mod grid-y-inc) = 0)]
ask roads [ set pcolor white ]
ask intersections [ set is-intersection? true
ask neighbors [ set is-intersection? true ]]
end
to setup-people
create-people no-of-people
[
set shape "person"
set size 1
set color black
move-to one-of patches with [pcolor != brown + 3]
set origin patch-here
set destination one-of patches with [pcolor != brown + 3 and is-intersection? != true]
set h-distance distance destination
]
set population [] ;; create list of individuals
let sort-turtles sort people set population sort-turtles
end
to setup-destination
foreach population [ individual ->
ask individual [
let roads_ roads with [pcolor = white]
ask roads_ [
set navigated? false
set active? false
set Heuristic 0
set step 0
set total-cost 0
set start false
set finish false
]]
let current [origin] of individual
let target [destination] of individual
ask individual [
if target != nobody [
ask current [
set pcolor (10 + random 130)
set step 0
set start true
set finish false
set navigated? false
]
ask target [
set pcolor [pcolor] of current
set navigated? true
set start false
set finish true
]]
]]
end
Problem 1: Set all paths
set-path traces all the steps from the origin patch to everywhere inside the virtual world. The code will colour the road to yellow and add a label on the path. This procedure will repeat 50 times to find all the possible steps.
The problem occurs from this section where this code only works for one agent not for the entire agents.
to set-path
repeat 50 [
ask patches with [pcolor = white and pcolor != yellow] [
if any? neighbors4 with [ start = true ] or
any? neighbors4 with [ pcolor = yellow ]
[ set pcolor yellow
let laststep [step] of one-of neighbors4 with [pcolor = yellow or
start = true]
ifelse [plabel] of patches != nobody [
set step laststep + 1
set plabel step
set father step
set total-cost father + heuristic
set plabel-color red][set plabel ""]
]]]
let allroads [self] of patches with [pcolor != brown]
let final_location one-of patches with [finish = true]
foreach allroads [ road_patch ->
ask road_patch [set Heuristic distance final_location]
]
end
Problem 2: Set shortest path
This procedure finds the shortest path by finding the patches and adding them to a list named path. Because the previous procedure only worked for one agent this procedure will also assign the patches to one agent, which is not what I want.
to shortest-path
ask patches with [start = true] [
let maxsteps 0
ask patches with [navigated? = true]
[ let check-steps min-one-of neighbors4 with
[pcolor != brown + 3 and pcolor != white][step]
set maxsteps [step] of check-steps
]
let num (range maxsteps 0 -1)
let paths []
foreach num [ navigation ->
ask patches with [navigated? = true] [
let nav-patch min-one-of neighbors4 with
[step = navigation and pcolor != brown + 3][step]
if nav-patch != nobody [ask nav-patch [set navigated? true set paths fput self paths]]
]]
ask people [set path paths]
]
ask patches with [pcolor = yellow][set pcolor white set plabel ""]
end
Summary: What I want the model to look like
How can I assign the shortest-path for each person if there are more than 2 people inside NetLogo?
What I want the model to look like
File: https://drive.google.com/file/d/1gxhkcnlBniB5M4aSYmt65PE6LBekgVp6/view?usp=sharing
I want to select the network neighbours filtered by a link attribute rather than a turtle attribute. For example, this might be selecting only the closest friends based on a strength score on the friendship link. I could do this by having different link breeds for close friends, but this would require constantly changing breeds as the condition was or was not required.
Below is an example with a boolean condition.
links-own [flag?]
to testme
clear-all
ask patches [set pcolor white]
create-turtles 20
[ setxy 0.95 * random-xcor 0.95 * random-ycor
set color black
]
ask n-of 3 turtles [set color red]
repeat 40
[ ask one-of turtles
[ create-link-with one-of other turtles
[ set flag? random-float 1 < 0.4
set color ifelse-value flag? [red] [gray]
]
]
]
; colour-neighbours
colour-neighbours2
end
to colour-neighbours
ask turtles with [color = red ]
[ ask my-links with [flag?]
[ ask other-end [ set color blue ]
]
]
end
to colour-neighbours2
ask turtles with [color = red ]
[ ask turtle-set [ other-end ] of my-links with [flag?]
[ set color blue ]
]
end
I am currently doing the equivalent of colour-neighbours, but it involves stepping through several contexts. The colour-neighbours2 version is conceptually closer because it is referring directly to the network neighbours. However, because of the of, I get a list of neighbours that I then have to convert to an agentset.
This is for teaching and, while both work, they seem very convoluted when compared to the unconditional network neighbourhood with the link-neighbors primitive. That is, if I didn't care about the flag, I could simply say ask link-neighbors [ set color blue ].
Is there a more direct way to identify network neighbours conditional on a link attribute?
You already cover most possibilities. Another way to do it would be:
to colour-neighbours3
foreach [ other-end ] of my-links with [ flag? ] [ t ->
ask t [ set color blue ]
]
end
I would avoid colour-neighbours2 because, as you stated, it requires the conversion from a list to an agentset. Whether you should use colour-neighbours or colour-neighbours3 is, I think, a matter of personal preference.
Just to add a more-convoluted (worse?) option that uses an agentset:
to colour-neighbours4
ask turtles with [ color = red ] [
let flag-links my-links with [flag?]
ask link-neighbors with [ any? my-links with [ member? self flag-links ] ] [
set color blue
]
]
end