Netlogo: agentset of patches to single agent - netlogo

I have a list of turtles (carriers) that i'm trying to narrow down in my go method.
Here is my program:
globals [
metal-sheets
cut-sheets
standard-skeleton
finished-standard-skeleton
prestige-skeleton
finished-prestige-skeleton
]
breed[carriers carrier]
turtles-own [
on-job?
]
patches-own [
processing-time
machine-type ;;cutter, standard-welder, prestige-welder, riveter
status ;;import, export, pending
]
to setup
clear-all
set-default-shape carriers "circle"
create-carriers number-of-carriers ;;number-of-carriers is a slider
[set color grey
set on-job? false
setxy random-xcor random-ycor]
setup-patches
reset-ticks
end
to setup-patches
ask patches [
if pxcor = 1 and pycor = 1 [set machine-type "cutter"]
if pxcor = 1 and pycor = 5 [set machine-type "standard-welder"]
if pxcor = 5 and pycor = 1 [set machine-type "prestige-welder"]
if pxcor = 5 and pycor = 5 [set machine-type "riveter"]
if machine-type = "cutter"
[set pcolor red
set status "import"]
if machine-type = "standard-welder"
[set pcolor green
set status "import"]
if machine-type = "prestige-welder"
[set pcolor blue
set status "import"]
if machine-type = "riveter"
[set pcolor yellow
set status "import"]
]
end
to Go
let cutter patches with [machine-type = "cutter"]
let standard-welder patches with [machine-type = "standard-welder"]
let prestige-welder patches with [machine-type = "prestige-welder"]
let riveter patches with [machine-type = "riveter"]
let free-carriers carriers with [on-job? = false]
let closest-carrier min-one-of free-carriers [distance cutter] ;;Distance expects agent, got agentset
ask closest-carrier [
set color green
]
end
I'm getting an error on the line let closest-carrier min-one-of free-carriers [distance cutter] saying that distance expected a agent but got a agentset although there only is one agent in the set.
The full error message is:
DISTANCE expected input to be an agent but got the agentset (agentset, 1 patch) instead.
How do narrow down the patches to one patch?

It doesn't matter if there happens to be only one agent in the agentset, it is still an agentset. Put a one-of in front, which selects one agent from the agentset and therefore changes the way the code interprets the code

Related

NetLogo Trouble detecting turtles in radius

I'm trying to have a car visit 50 different cities in the shortest route possible, and every time he visits a place he changes the colour of the city form blue to yellow and moves on, trouble is I'm finding errors trying to find a way to implement this. I'm getting many errors in the last line of code, where it says CAR expected 1 input, a number. Any help would be appreciated.
breed [cities city]
breed [cars car]
cars-own [history travelled-distance]
to setup
clear-all
reset-ticks
setup-patches
setup-turtles
end
to setup-turtles
ask n-of 50 patches with [pcolor = 55 and not any? other turtles-here][sprout-cities 1 [set color blue set size 2 set shape "square"]]
create-cars 1[
setxy -90 -90
set color red
set size 5
]
end
to setup-patches
ask patches [set pcolor green]
ask n-of 100 patches [set pcolor brown ask neighbors [set pcolor brown]]
end
to go
ask cars [
pendown
if history <= 50
[ ;set heading towards city in-radius 10]]
move-to min-one-of cities in-radius 360 [distance myself]
]
if car in-radius 5 = true [set color = yellow]]
end
set heading towards one-of cities in-radius 10

Spatial Autocorrelation in NetLogo

Is there any straight-forward way to adjust spatial autocorrelation for three different patch colors? I am trying to control both the number of red patches and how spatially autocorrelated (how close same colored patches are to each other). I can control the proportion of red patches, but don't know how to setup the autocorrelation.
Here is my code so far:
to setup-patches
resize-world 0 15 0 15
set-patch-size 30
ask patches [
set pcolor one-of [ green brown ]
]
ask patches [
let close-patches patches with [pcolor != red]
ask n-of ((proportion-red-plants * count patches) - count patches with [pcolor = red]) close-patches
[set pcolor red]
]
end
proportion-red-plants is a slider in the interface
If you know that AC of 0 means pick a patch that has no red neighbours, and that AC of 1 means pick a neighbour of any red patch, then all that is required is to choose AC=1 method with the given probability and the AC=0 method otherwise. This is what I meant by a design issue, you need to work out the steps required before trying to code those steps.
Here is an almost solution. I haven't bothered to do things like make sure the patches being turned red aren't already red so the counts will be incorrect.
to setup
clear-all
let prop-red 0.1
let AC 0
ask one-of patches [set pcolor red]
ask n-of (prop-red * count patches) patches
[ ifelse random-float 1 < AC
[ ask one-of patches with [ pcolor = red ]
[ ask one-of neighbors [ set pcolor red ]
]
]
[ let candidates patches with [not any? neighbors with [pcolor = red] ]
if any? candidates
[ ask one-of candidates [ set pcolor red ]
]
]
]
end
Thank you JenB again for helping. This is the code I ended up using which proved to get the job done (while keeping counts of red patches correct)
to setup-patches
resize-world 0 15 0 15
set-patch-size 30
ask patches [set pcolor one-of [green brown]]
let first-patch one-of patches
ask first-patch [set pcolor red]
repeat (proportion-red-plants * count patches - 1) [ask one-of patches [assign]]
end
to assign
ifelse random-float 1 < AC
[let candds patches with [any? (neighbors with [pcolor = red])]
ask one-of candds [set pcolor red]]
[ask one-of patches [set pcolor red]]
end

Nested if queries in patch context NetLogo

I want to change color of patches to green under these conditions:
-IF there are any two blue patches on same column and their distance is smaller than 25 AND
-IF there are any yellow patches on same column and between these selected blue patches
-Then change color of all patches satisfying these conditions to green.
I am struggling to make it in NetLogo, tried using nested loop but couldn't find a way. Thank you for any help. And I have added a sample image which I want to achieve and marked example blue patches.
As addition, to show what I want to do in code (sorry about code):
if any? patches with [pcolor = blue and
(if any? patches with [pcolor = blue and pycor = ?(selected_first_blue's_pycor)
if any? patches with [pcolor = yellow and pycor < ??(selected_first_blue's_pycor) and pycor > ?(selected_second_blue's_pycor)))
[ask patches [set pcolor green]]
The way you are approaching it, you need fairly convoluted statements like:
let upper-blues patches with [color = blue and
any? other patches with [color = blue and pxcor = [pxcor] of myself
and pycor < [pycor] of myself and pycor > [pycor] of myself - 25]
I believe this would be much easier to take the perspective of the patch you want to potentially turn green. If I have interpreted your conditions correctly, that patch needs to work out the closest yellow patch above/below/at and then check if there are two blue patches bracketing both the yellow and itself with the blue patches sufficiently close to each other. I assume you have wrapping turned off.
Here is a complete model that puts in a red turtle instead of turning the patch green so you can see whether it is identifying the correct patches.
to setup
clear-all
ask patches [set pcolor white]
ask n-of 100 patches [set pcolor blue]
ask n-of 100 patches [set pcolor yellow]
end
to convert-to-green
let turn-green nobody
ask patches
[ let my-column patches with [pxcor = [pxcor] of myself]
let above-yellow min-one-of my-column with [pcolor = yellow and pycor >= [pycor] of myself][pycor]
let above-blue ifelse-value (above-yellow != nobody) [min-one-of my-column with [pcolor = blue and pycor > [pycor] of above-yellow][pycor]][nobody]
let below-yellow max-one-of my-column with [pcolor = yellow and pycor <= [pycor] of myself][pycor]
let below-blue ifelse-value (below-yellow != nobody) [max-one-of my-column with [pcolor = blue and pycor < [pycor] of below-yellow][pycor]][nobody]
if above-blue != nobody and below-blue != nobody and ([pycor] of above-blue - [pycor] of below-blue < 25)
[ set turn-green (patch-set self turn-green)
]
]
ask turn-green [sprout 1 [set color red]]
end
Once you are satisfied it is working correctly, change ask turn-green [sprout 1 [set color red]] to ask turn-green [set pcolor green].
This code checks each patch in random order and adds it to the set of patches (called turn-green) if the conditions are satisfied. Once all patches have been tested, the set of selected patches then change their colour. This avoids issues of yellow or blue patches turning green and not being available for later patches to check against.

Netlogo: Ask specific turtle to do something

I'm trying to simulate a car factory using robots called carriers. In my go method i'm trying to ask one carriers who is not on a job to find a cutter and go to it.
How do I ask the specific carrier to do something?
This is what i've done so far:
metal-sheets
cut-sheets
standard-skeleton
finished-standard-skeleton
prestige-skeleton
finished-prestige-skeleton
]
breed[carriers carrier]
turtles-own [
on-job?
]
patches-own [
processing-time
machine-type ;;cutter, standard-welder, prestige-welder, riveter
status ;;import, export, pending
]
to setup
set-default-shape carriers "circle"
create-carriers number-of-carriers
[set color grey
set on-job? false]
setup-patches
reset-ticks
end
to setup-patches
ask patches [
if pxcor = 1 and pycor = 1 [set machine-type "cutter"]
if pxcor = 1 and pycor = 5 [set machine-type "standard-welder"]
if pxcor = 5 and pycor = 1 [set machine-type "prestige-welder"]
if pxcor = 5 and pycor = 5 [set machine-type "riveter"]
if machine-type = "cutter"
[set pcolor red
set status "import"]
if machine-type = "standard-welder"
[set pcolor green
set status "import"]
if machine-type = "prestige-welder"
[set pcolor blue
set status "import"]
if machine-type = "riveter"
[set pcolor yellow
set status "import"]
]
end
to Go
let cutter patches with [machine-type = "cutter"]
let standard-welder patches with [machine-type = "standard-welder"]
let prestige-welder patches with [machine-type = "prestige-welder"]
let riveter patches with [machine-type = "riveter"]
let free-carriers carriers with [in-job? false]
ask free-carriers [
;;on-job is a carrier-owned variable
ask cutter [
if status = "import" [
set status "pending"
face cutter ;; I want the carrier to face the cutter and move towards it
fd 1
]
]
]
end```
I'm assuming that you want free-carriers to find a cutter patch with status "import", have that cutter change its status, and then have the carrier move toward it.
ask free-carriers [
;;on-job is a carrier-owned variable
let my-cutter one-of cutter with [status = "import"]
if my-cutter != nobody [
ask my-cutter [ set status "pending" ]
face cutter ;; I want the carrier to face the cutter and move towards it
fd 1
]
]
Since each free-carrier does this in (random) turn, no two free-carriers should go to the same cutter. This will choose an eligible cutter randomly. You could have free-carriers go to the nearest one if you wished.

Only observer can ask all turtles - NetLogo error

I am new user for Netlogo,
Although when I checked the code, there are no issues. However, when i run the program a while, it throws error message
Only the observer can ASK the set of all turtles. Error while turtle 0 running ASK. called by procedure EXIT called by procedure Customer, GO and by button 'go'
to go
customer
end
to setup-turtles
create-turtles 1
ask turtles
[
set shape "person"
set size 3
set heading -90
fd 10
setxy 15 -15
set color red
]
end
to customer
ask turtles
[
set products ( products )
rt (random 360)
fd 1
if patch-here = one-of patches with
[
pcolor = green
]
[
set pcolor orange
set products (products + 1)
]
if patch-here = one-of patches with
[
pcolor = gray
]
[exit]
show count patches with
[pcolor = green ]
move-to one-of patches with
[
pcolor = black
]
]
end
to exit
ask turtles
[
ifelse patch-here = one-of patches with
[
pcolor = gray
]
[ifelse count products >= 2
[
die
]
[move-to one-of patches with
[
pcolor = green or pcolor = black
]
]
]
[
die
]
move-to one-of patches with
[pcolor = green
]
move-to one-of patches with
[pcolor = black
]
]
end
In your customer procedure, you have
ask turtles
[ ...
if patch-here = one-of patches with [pcolor = gray]
[ exit ]
...
]
So the exit procedure is being called by any turtle that is on a gray patch. Each turtle that meets that condition enters the exit procedure. As soon as it enters that procedure, the first command (in the exit procedure) is to ask turtles. So a turtle is asking all turtles to do something.
This is explicitly banned by the NetLogo language partly because it is a common source of beginner errors and is generally both unnecessary and inefficient. You have already selected the turtle to exit, what is it that this particular turtle needs to do to actually exit. It is very unlikely that they need to identify all the turtles on gray patches.