Run multiple procedures at the same time in netlogo - performance

Hello i have the following code:
to go
move
play-papelvstije
play-papelvsrock
play-tijevsrock
play-tijevspapel
play-rockvspapel
play-rockvstije
end
to play-rockvspapel
ask turtles with [color = red]
[
let nearby other turtles in-radius 1
if any? nearby with [color = green]
[
set color green
]
]
end
to play-papelvstije
ask turtles with [color = green]
[
let nearby other turtles in-radius 1
if any? nearby with [color = blue]
[
set color blue
]
]
end
to play-tijevsrock
ask turtles with [color = blue]
[
let nearby other turtles in-radius 1
if any? nearby with [color = red]
[
set color red
]
]
end
to play-rockvstije
ask turtles with [color = red]
[
let nearby other turtles in-radius 1
if any? nearby with [color = blue]
[
set color red
]
]
end
to play-papelvsrock
ask turtles with [color = green]
[
let nearby other turtles in-radius 1
if any? nearby with [color = red]
[
set color green
]
]
end
to play-tijevspapel
ask turtles with [color = blue]
[
let nearby other turtles in-radius 1
if any? nearby with [color = green]
[
set color blue
]
]
end
So as you can see, i run this procedures play-papelvstije play-papelvsrock play-tijevsrock, play-tijevspapel, play-rockvspapel, play-rockvstije in this exactly order, so when run the simulation i get an slant in my results, because the first command to run is the one that have an increase population in the end, so what i want to do is finding a way to run this procedures but with one just command. I have tried with "foreach" and "map" commands, however i have not gotten results. Anny suggestions?

one solution can be to not directly ask blue turtles but for all turtles ... something like
to setup
clear-all
create-turtles 100 [
set color red
setxy random-pxcor random-pycor
]
ask n-of 10 turtles [
set color green
]
ask n-of 10 turtles with [color = red][
set color blue
]
reset-ticks
end
to go
move
changeColor
tick
end
to move
ask turtles [
rt random-float 90
lt random-float 90
fd 1
]
end
to changeColor
ask turtles [
let mycolor color
let nearby other turtles in-radius 1
if mycolor = blue [
if any? nearby with [color = green]
[
set color blue
]
if any? nearby with [color = red]
[
set color red
]
]
if mycolor = green [
if any? nearby with [color = red]
[
set color green
]
if any? nearby with [color = blue]
[
set color blue
]
]
if mycolor = red [
if any? nearby with [color = red][
if any? nearby with [color = blue]
[
set color red
]
if any? nearby with [color = green]
[
set color green
]
]
]
]
end

Related

How to make turtles / agents go faster or slower when being on a certain patch?

I have to make turtles go faster when being on a green patch, and when they are on a blue patch the speed should go down. I added part of the code that I tried, but it doesn't work. Could someone please help me?
Thanks in advance!!
turtles-own [speed]
to go
ask turtles [
left 60
right random 60
forward 1
if any? neighbors with [pcolor = green]
[
set speed speed + 1
]
if any? neighbors with [pcolor = blue]
[
set speed speed + 0.1
]
]
reset-ticks
I think what user2901351 was getting at was that you are using neighbors in your example code. If you look at the dictionary entry for neighbors, you'll see that it references the 8 patches around the current patch. If you instead want the turtle to check the patch it's currently on, you can use the patch-here primitive or, as a shortcut, just ask the turtle to check a patch-owned variable directly. Below is a toy example that shows an example of this at work- more details in the comments.
turtles-own [speed]
to setup
ca
crt 5 [ set speed 1 ]
ask n-of 20 patches [
ifelse random-float 1 < 0.5
[ set pcolor green ]
[ set pcolor blue ]
]
reset-ticks
end
to go
ask turtles [
; Since 'pcolor' is a patch variable, if the turtle queries pcolor
; it will check the patch's variable directly.
if pcolor = green [ set speed speed + 0.5 ]
if pcolor = blue [ set speed speed - 0.5 ]
; Make sure that the turtles are moving forward by their speed value,
; rather than the hard-coded value of "1" in the example code. Also,
; I've included a check here so the turtles don't either stop moving
; or start moving backwards.
if speed < 1 [ set speed 1 ]
rt random-float 60 - 30
fd speed
show speed
]
tick
end

How to create two different random color groups in NetLogo 6.1.1?

How to create two different random color groups in NetLogo 6.1.1?
I am trying to create two different groups from the group of 250 turtles. The starting situation is that all 250 turtles are gray and then they will turn to yellow and pink groups one by one.
This code of mine makes all turtles in the beginning to be gray and then they will all turn to pink. I do not want this but I want two randomly made groups where pink turtles is typically either larger or smaller group of turtles than the yellow group of turtles in the end of the code run.
I just started to code with NetLogo 6.1.1. Thank you for understanding and all help and have a nice day.
[
time
person
]
turtles-own [ x ]
to setup
clear-all
reset-ticks
set time 0
create-turtles 250
[
setxy random-xcor random-ycor
]
ask turtles
[
set shape "person"
set size 1
set color gray
]
end
to go
ask turtles
[
show random 2 = x
if x = 1 [set color yellow]
if x = 0 [set color pink]
]
end ```
I don't see any place where the values for the turtle variable x are set, so they will always have the default value of 0. In NetLogo = is used to check equality, not for assignment, so show random 2 = x is just going to print true or false depending on if random 2 is 0 or not (in case you thought that was assignment). You'd want something like this:
to go
ask turtles
[
set x random 2
if x = 1 [set color yellow]
if x = 0 [set color pink]
]
end
Or you could move the set x random 2 to the setup procedure if you just want to set the value once to use later in go.

Netlogo - find the closest agent based on Manhattan distance

I am modelling a big warehouse operations (see below pic).
I have implemented a vertex on each patch (green dots) and link them so that I could compute the shortest path (using dijkstra algorithm) from each vertex to all other vertices, stored in a table (dictionary) of each vertex. This process is done during setup stage and is quite time-consuming. Then the yellow inventory cells (rectangle) will issue the demand request for task fulfillment.
The forklift truck (some in the path) is assuming to stay put on one of the vertex (node) when it's not busy. When get the demand request, it will ask its current standing vertex (node) to get the starting_node and the end_node of the inventory cell it is going to. Then the forklift ask the starting_node to get the shortest path from its table to get the shortest path (combination of nodes) and drive to the end_node (the yellow cell).
Currently, the yellow cells just randomly pick free forklift truck in the warehouse. As a further development, I want to add the function that allows the yellow cell to identify the free truck that is closest to it based on the actual distance (not the euclidean distance built in Netlogo). As there are many trucks so it's not good to use "foreach" to loop through all available trucks and compute the actual distance using above method. You could use primitive "distance myself" to locate the truck quickly but that's not accurate. Is there a good way to identify the closest truck with a faster computational method?
I don't know if this would work with your current setup, but you may also find the nw extension helpful. For example, try this setup (I apologize for the length, just approximating your world):
extensions [ nw ]
breed [ nodes node ]
breed [ trucks truck ]
breed [ cells cell ]
to setup
ca
resize-world -29 29 -14 14
ask patches with [ ( pxcor mod 5 = 0 or pycor = 0 ) ] [
set pcolor grey
sprout-nodes 1 [
set size 0.5
set shape "circle"
set color green
]
]
ask nodes [
create-links-with turtles-on neighbors4 [
set color green
]
]
ask n-of 5 nodes [
hatch 1 [
set size 1
set color red
set breed trucks
set shape "car"
]
]
ask n-of 2 trucks [
set color blue
]
ask one-of nodes [
ask one-of neighbors with [ pcolor = black ] [
sprout-cells 1 [
set size 1.5
set shape "box"
set color yellow
]
]
]
reset-ticks
end
Gives a simple world like this:
You can use the nw extension to calculate the distances on the fly, rather than storing them in a per-cell table, and have the forklifts (trucks, here) follow the shortest path. More details in comments:
to summon-nearest-truck
; Treat blue trucks as 'empty' and ready to go to a cell
let ready-trucks trucks with [ color = blue ]
; Get the nodes associated with those ready trucks
let truck-proxies turtle-set map [ i -> [nodes-on patch-here] of i ] sort ready-trucks
; Randomly ask one of the cells to
ask one-of cells [
; Choose one of the neighboring road nodes as a proxy for distance calculation
let node-proxy one-of nodes-on neighbors4
; Get the node proxy to:
ask node-proxy [
; Find the nearest (by network distance) trucks, and select the node
; located on the same patch as that truck
let my-truck-proxy min-one-of truck-proxies [length ( nw:turtles-on-path-to myself) ]
; Get that truck-proxy node to generate the path to the original asking node
; and have the appropriate truck follow that path
ask my-truck-proxy [
; Generate the path to follow
let path-to-follow nw:turtles-on-path-to myself
; Highlight that path for visualization
ask turtle-set path-to-follow [
set color orange
]
; Get the truck ready to move
let ready-truck one-of trucks-here with [ color = blue ]
ask ready-truck[
set color yellow
]
; Move that truck along the path-to-follow
ask ready-truck [
foreach path-to-follow [
n ->
face n
move-to n
; For visualization only
wait 0.1
]
]
]
]
]
end
This has the yellow box summon the nearest truck based on the length of nw:turtles-on-path-to that is returned. That truck follows the path to the summoning node:

Use label instead of who number in NetLogo

We are using NetLogo for a simple infection. As a modell we use an imported gephi file. For our purpose we have to start the infection from the same turtle (meaning the one with the same specific label) for several times. In our code we have tried using the "who" number to make sure of that, but as soon as we setup, this number changes. So my question is:
Is there the possibility to instead of the who number use the turtle's label in the code?
So far we have been using this code
extensions [nw]
globals
[
num-informed
informed-size
]
turtles-own
[
informed?
]
to setup
clear-all
nw:load-graphml "jk.graphml"
ask turtles [ set size 1.5 ]
layout-radial turtles links turtle 61
ask turtles [set color red]
ask turtles [set shape "dot"]
ask links [set color grey + 1.5]
ask patches [set pcolor white]
ask turtles [set label-color black]
ask turtles [set informed? false]
ask turtle 72
[
set informed? true
set color green
]
set num-informed 1
set informed-size 2
reset-ticks
nw:save-graphml "jk1.graphml"
end
to spread
if (count turtles with [informed? = true] > .9 * count turtles) [stop]
ask turtles with [ informed? = true ]
[
ask link-neighbors with [not informed?]
[
if (random-float 1 <= 0.02)
[
set informed? true
show-turtle
set color green
]
]
]
set num-informed count turtles with [informed? = true]
tick
end
Thank you so much!
As King-Ink said in a comment, you can get a turtle with a particular label by doing one-of turtles with [ label = "some label" ]

NetLogo: patchy forest - keep total area and blob size stable and not overlap

I realize that my question is just slight modification of questions:
Adding patch clusters in a landscape
How to create cluster patches that do not overlap between them
and
To build patch clusters at large spatial scales
but I can't modify them to satisfied my needs.
I need to produce a patchy forest landscape. Each setup, the total area of "green" patches has to be same (20%, 10%... of total count patches) and the size of one blob should be same. Thus: blob_size = area / number_blobs
I suggest that
to create-forests
clear-all
ask n-of 1 patches [ set pcolor green ]
repeat 6 [
ask one-of patches with [pcolor = green ] [
ask one-of neighbors4 with [pcolor = black] [
set pcolor green ]
]
]
end
should be the answer, as by n-of 1 (number_blobs) patches I create number of blobs needed, and blob_size is constrained by repeat 6 (blob_size). However, in my simple example I have an error ASK expected input to be an agent or agentset but got NOBODY instead. apparently because one-of patches with [pcolor = green] has not black neighbors.
Please, how can I include the condition ask one-of patches with [pcolor = green ] and with min-one of neighbors4 with [pcolor = black] in my code?
Or what is the different way to do this? I need to keep my total area of green patches same, and patches size +- same too, the best would be if they will not overlap. Thank you a lot!
to create-forests
clear-all
ask n-of 1 patches [ set pcolor green ]
repeat 6 [
ask one-of patches with [pcolor = green and any? neighbors4 with [ pcolor = black ] ] [
ask one-of neighbors4 with [pcolor = black] [
set pcolor green ]
]
]
end
Modified to make-blob from: Creating a random shape (blob) of a given area in NetLogo
my blobs still can overlap but at least total "area" is the same for every run
to make-blob
let total_area 500 ; how patches I want to turn green
repeat 5 [ ; number of blobs I want to have
let blob-maker nobody
crt 1 [ set blob-maker self
setxy random-xcor random-ycor] ; set random position of "blob-makers"
repeat 10 [ ; size of one blob (number of patches of the same color close one to another)
ask blob-maker [
ask min-one-of patches with [ pcolor = black ] [ distance myself ] [ set pcolor green ]
rt random 360
fd 1
]
]
ask blob-maker [ die ]
]
end
resulting in

Resources