Netlogo - Create only one turtle per patch - netlogo

I'm trying to create turtles in an office space area where designated grey patches represent their cubicles. However, when the turtles are created some overlap on top of each other within a cubicle.
How can I ensure only one turtle is created per cubicle? (find the code under ***)
This is the code I have:
breed [programmers programmer]
globals []
patches-own [
a-space
b-space
c-space
d-space
a-cubicle
b-cubicle
c-cubicle
d-cubicle
cubicle?]
programmers-own [
speed
team-name
motivation
myCubicle
teammates
projects
counter ]
to setup-environment
ask patches with [ pycor mod 2 = 0 and pxcor = -16] [ set pcolor grey ]
ask patches with [ pycor mod 2 = 0 and pxcor = 16] [ set pcolor grey ]
ask patches with [ pxcor mod 2 = 0 and pycor = -16] [ set pcolor grey ]
ask patches with [ pxcor mod 2 = 0 and pycor = 16] [ set pcolor grey ]
ask patches with [ pycor = 0] [ set pcolor red ]
ask patches with [ pxcor = 0] [ set pcolor red ]
ask patches [
set a-space patches with [(pxcor > -14) and (pxcor < 0) and (pycor > 0) and (pycor < 14)]
set b-space patches with [(pxcor < 14) and (pxcor > 0) and (pycor > 0) and (pycor < 14)]
set c-space patches with [(pxcor < 14) and (pxcor > 0) and (pycor < 0) and (pycor > -14)]
set d-space patches with [(pxcor > -14) and (pxcor < 0) and (pycor < 0) and (pycor > -14)]
set a-cubicle patches with [(pycor mod 2 = 0) and (pxcor mod 2 = 0) and (pxcor >= -16) and (pycor <= 16) and (pxcor < 0) and (pycor > 0)]
set b-cubicle patches with [(pycor mod 2 = 0) and (pxcor mod 2 = 0) and (pxcor <= 16) and (pycor <= 16) and (pxcor > 0) and (pycor > 0)]
set c-cubicle patches with [(pycor mod 2 = 0) and (pxcor mod 2 = 0) and (pxcor <= 16) and (pycor >= -16) and (pxcor > 0) and (pycor < 0)]
set d-cubicle patches with [(pycor mod 2 = 0) and (pxcor mod 2 = 0) and (pxcor >= -16) and (pycor >= -16) and (pxcor < 0) and (pycor < 0)] ]
ask patches with [pcolor = grey] [set cubicle? true]
end
to setup-programmers
create-programmers Number_Of_Programmers [
set shape "person"
ask n-of (Number_Of_Programmers / 4) programmers [set team-name "A"]
ask n-of (Number_Of_Programmers / 4) programmers [set team-name "B"]
ask n-of (Number_Of_Programmers / 4) programmers [set team-name "C"]
ask n-of (Number_Of_Programmers / 4) programmers [set team-name "D"]
; *** WHAT I'M TRYING TO DO WOULD GO HERE ***
ask programmers with [team-name = "A"] [ set color blue] if other turtles-on patch-here [ move-to one-of a-cubicle with [cubicle? = true] set myCubicle patch-here]
ask programmers with [team-name = "B"] [ set color green move-to one-of b-cubicle with [cubicle? = true] set myCubicle patch-here]
ask programmers with [team-name = "C"] [ set color pink move-to one-of c-cubicle with [cubicle? = true] set myCubicle patch-here]
ask programmers with [team-name = "D"] [ set color yellow move-to one-of d-cubicle with [cubicle? = true] set myCubicle patch-here]
ask programmers [set teammates programmers with [color = [color] of myself]]
set projects (list)
set motivation 100
set counter 0
]
end
to setup
clear-all
reset-ticks
setup-environment
setup-programmers
end
to go
lose-or-gain-motivation
socialize
complete-program
tick
end

There are three different ways to create turtle agents in NetLogo. If you want want no more than one turtle in a patch, the easiest way to do so is to select a subset of patches (using n-of XXX patches) and ask them to sprout turtles. In your case, something like this might work:
Replace create-programmers Number_Of_Programmers
with
ask n-of Number_Of_Programmers patches with [cubicle?]
[ sprout-programmers 1

Related

How can I solve the error (you can not use my_ area in a patch context, it is a turtle-only)?

I want the turtles to back again to their areas if batches which I had defined before. The problem is I got an error
you can not use my_ area in a patch context, it is a turtle-only
the button is
to back_from_event
ask turtles with [ area = 1 ] [ move-to one-of patches with [ (not any? other turtles-here) and ( area = my_area ) ] ]
end
the patches defined as below and the turtles were in areas (2,3,4,5) and moved to area 1 and I need them to back again to there areas:
to define_areas
ask patches with [ (pxcor > -3) and (pxcor < 3) and (pycor > -3) and (pycor < 3) ] [ set pcolor white set area 1 ]
ask patches with [ (pxcor > 5 ) and (pxcor < 16 ) and (pycor > 4) and (pycor < 16) ] [ set pcolor white set area 2 ]
ask patches with [ (pxcor < -5 ) and (pxcor > -16 ) and (pycor > 4) and (pycor < 16) ] [ set pcolor white set area 3 ]
ask patches with [ (pxcor < -5 ) and (pxcor > -16 ) and (pycor < -4) and (pycor > -16) ] [ set pcolor white set area 4 ]
ask patches with [ (pxcor > 5 ) and (pxcor < 16 ) and (pycor < -4) and (pycor > -16) ] [ set pcolor white set area 5 ]
end
Okay, this is the same problem as your previous question. Please try to understand the answer rather than simply copy the code. Otherwise, you will keep asking the same question.
You have 'area' as a patch variable and 'my_area' as a turtle variable.
What you need to realise is that turtle to patch is unique because a turtle can only be in one place at a time. Therefore, a turtle can access the variables owned by the patch that it is sitting on without having to specify the patch. So code like this is okay:
ask turtles with [area = 1] [ ]
This is because it is equivalent to:
ask turtles with [ [area] of patch-here = 1] [ ]
However, a patch cannot access a variable owned by a turtle because there may be multiple turtles on the same patch. For example, if you asked a patch to set its pcolor to color and you had red turtle and a blue turtle on the patch, how would it know which color to choose?
Your error says "you can not use my_area in a patch context, it is a turtle-only". That is telling you that you tried to have a patch use the my_area variable but that variable is owned by turtles. So you didn't tell it which turtle to get my_area from.
This is what you have:
to back_from_event
ask turtles with [ area = 1 ]
[ move-to one-of patches with [(not any? other turtles-here) and (area = my_area)]
]
end
I assume you want the area of the patch to be the same as the my_area of the turtle doing the asking. That is what myself is for.
to back_from_event
ask turtles with [ area = 1 ]
[ move-to one-of patches with [(not any? other turtles-here) and (area = [my_area] of myself)]
]
end

NetLogo set patches within a specific range

I'm new to stack overflow and netlogo, in fact this is my first question ever. I thank you all in advance.
In netlogo I have created 4 areas to represent 4 office spaces which coincide with quadrants I-IV:
to setup-environment
ask patches with [ pycor mod 2 = 0 and pxcor <= -16] [ set pcolor grey ]
ask patches with [ pycor mod 2 = 0 and pxcor >= 16] [ set pcolor grey ]
ask patches with [ pxcor mod 2 = 0 and pycor <= -16] [ set pcolor grey ]
ask patches with [ pxcor mod 2 = 0 and pycor >= 16] [ set pcolor grey ]
ask patches with [ pycor = 0] [ set pcolor red ]
ask patches with [ pxcor = 0] [ set pcolor red ]
; THIS PART IN PARTICULAR
ask patches [
set a-space patches with [(pxcor < 0) and (pycor > 0)]
set b-space patches with [(pxcor > 0) and (pycor > 0)]
set c-space patches with [(pxcor > 0) and (pycor < 0)]
set d-space patches with [(pxcor < 0) and (pycor < 0)]
]
This sets up, for example, a-space to be in quadrant II entirely, I need the patches in a-space to be within a certain range. I tried (-14 < pxcor < 0) and (14 > pycor > 0) so that the area is within x = (-14, 0) and y (16, 0), but got this error:
expected this input to be an agent or number or string, but got a
TRUE/FALSE instead
I understand that you can't set patches, but that is not what I'm trying to do here, I'm trying set an area with patches with the range I specify.
Welcome to StackOverflow (and NetLogo). For future questions, please show the specific code that generates the error as part of your example code. However, if I understand your question correctly, you had something like:
set a-space patches with [(-14 < pxcor < 0) and (14 > pycor > 0)]
You can't use compound comparisons such as this in NetLogo. The statement -14 < pxcor < 0 in mathematics is two separate logical statements: -14 < pxcor and pxcor < 0. You must construct them as two statements and use the logical operator and to join them.
Following is a complete model that I think does what you want. Note that as well as the logical structure, I removed your ask patches. The way you have your code set up, each patch sets the variables a-space etc. So, if you have 2500 patches, then those variables are set (to the same value) 2500 times.
globals [a-space b-space c-space d-space]
to setup-environment
ask patches with [ pycor mod 2 = 0 and pxcor <= -16] [ set pcolor grey ]
ask patches with [ pycor mod 2 = 0 and pxcor >= 16] [ set pcolor grey ]
ask patches with [ pxcor mod 2 = 0 and pycor <= -16] [ set pcolor grey ]
ask patches with [ pxcor mod 2 = 0 and pycor >= 16] [ set pcolor grey ]
ask patches with [ pycor = 0] [ set pcolor red ]
ask patches with [ pxcor = 0] [ set pcolor red ]
; THIS PART IN PARTICULAR
set a-space patches with [(-14 < pxcor) and (pxcor < 0) and (pycor > 0) and (pycor < 5)]
ask a-space [set pcolor blue]
end

how to solve "no such plot" error while plotting in netlogo?

I am new to netlogo and facing the following error while plotting the mean of properties associated with patches:
no such plot: " patch 0 0"
error while patch 0 0 running SET-CURRENT-PLOT
called by procedure GO
called by Button 'go'
But patch 0 0 clearly exists and is predefined in the code as follows:
'globals [ k ] ; interaction constant
patches-own [ a b c d' e' ] ; state variables of properties
; a is the Proportion and variety of Blend of land use
; b is the Land uses with supportiveness for complimentary activities
; c is the Vehicular and Pedestrian Intensity
; d is the Intensity of Nodes in urban web
; e' is the Frequency of Enforced Vigilance
to setup
clear-all
set k initial-k
setup-patches
reset-ticks
end
to setup-patches
ask patches [ set pcolor yellow ] ; defines the patches as built up in an area
; to define road patches (horizontal)
ask patches [ if pycor = 0 [ set pcolor grey ] ]
ask patches [ if pxcor = 0 [ set pcolor grey ] ]
ask patches [if pycor = 9 [ set pcolor grey ] ]
ask patches [ if (pycor = 6) and (pxcor < -4 )[ set pcolor grey ] ]
ask patches [ if (pycor = 3) and (pxcor < -4 ) [ set pcolor grey ] ]
ask patches [ if (pycor = 4) and (pxcor > 3 ) [ set pcolor grey ] ]
ask patches [ if (pycor = -6) and (pxcor > 7 ) [ set pcolor grey ] ]
; to define road patches (vertical)
ask patches [ if (pycor > 0) and (pxcor = -10 ) [ set pcolor grey ] ]
ask patches [ if (pycor > 0) and (pxcor = -5 ) [ set pcolor grey ] ]
ask patches [ if (pycor < 0) and (pxcor = -7 ) [ set pcolor grey ] ]
ask patches [ if (pycor < 0) and (pxcor = -3 ) [ set pcolor grey ] ]
ask patches [ if (pycor < -3) and (pxcor = 4 ) [ set pcolor grey ] ]
ask patches [ if (pycor > 3) and (pxcor = 4 ) [ set pcolor grey ] ]
ask patches [ if (pycor > 0) and (pxcor = 7 ) [ set pcolor grey ] ]
; to define nodes as patches
ask patches [ if pxcor = 0 and pycor = 0 [ set pcolor red ] ]
ask patches [ if pxcor = 7 and pycor = 0 [ set pcolor red ] ]
ask patches [ if pxcor = -3 and pycor = 0 [ set pcolor red ] ]
ask patches [ if pxcor = -5 and pycor = 0 [ set pcolor red ] ]
ask patches [ if pxcor = -7 and pycor = 0 [ set pcolor red ] ]
ask patches [ if pxcor = -10 and pycor = 0 [ set pcolor red ] ]
ask patches [ if pxcor = 4 and pycor = 4 [ set pcolor red ] ]
ask patches [ if pxcor = 7 and pycor = 4 [ set pcolor red ] ]
ask patches [ if pxcor = 7 and pycor = 9 [ set pcolor red ] ]
ask patches [ if pxcor = 4 and pycor = 9 [ set pcolor red ] ]
ask patches [ if pxcor = 0 and pycor = 9 [ set pcolor red ] ]
ask patches [ if pxcor = -5 and pycor = 9 [ set pcolor red ] ]
ask patches [ if pxcor = -10 and pycor = 9 [ set pcolor red ] ]
ask patches [ if pxcor = -10 and pycor = 6 [ set pcolor red ] ]
ask patches [ if pxcor = -10 and pycor = 3 [ set pcolor red ] ]
ask patches [ if pxcor = -5 and pycor = 6 [ set pcolor red ] ]
ask patches [ if pxcor = -5 and pycor = 3 [ set pcolor red ] ]
; to set intial values of properties for patches
ask patches with [pcolor = yellow] [ set a random-float 0.9] ; initial a
ask patches with [pcolor = yellow] [ set b random-float 0.9] ; initial b
ask patches with [pcolor = grey] [ set c random-float 0.9] ; initial c
ask patches with [pcolor = red] [ set d' random-float 0.9] ; initial d'
ask patches with [pcolor = grey] [ set e' random-float 0.9] ; initial e'
end
to go
tick
if ticks >= 52 [ stop ]
ask patches with [pcolor = yellow]
[
let fc [c] of one-of patches with [pcolor = grey] ; reports c of any one
grey patch of neighbours
let fe' [e'] of one-of patches with [pcolor = grey] ; reports e' of any one grey patch of neighbours
let fd' [d'] of one-of patches with [pcolor = red] ; reports d' of any one red patch of neighbours
if a < 0.1 [ set a 0.1
if a > 0.9 [ set a 0.9 ] ]
if b < 0.9 [ set b b + (k * a) + (k * fc) + (k * fd')
if b > 0.9 [ set b 0.9 ] ]
if b > 0.1 [ set b b - (k * fe')
if b < 0.1 [ set b 0.1 ] ]
]
ask patch 0 0
[ let fa [a] of one-of patches with [pcolor = yellow]
let fb [b] of one-of patches with [pcolor = yellow]
let fc [c] of one-of patches with [pcolor = grey]
let fe' [e'] of one-of patches with [pcolor = grey]
let eeep (fa + fb + fc + d' + 1 / fe') / 5
let deep (1 / fa + 1 / fb + 1 / fc + 1 / d' + fe') / 5
output-print eeep
output-print deep
set-current-plot " patch 0 0"
set-current-plot-pen "eeep"
plot eeep
set-current-plot-pen "deep"
plot deep
]
end'
I have been trying to solve this for quite sometime and will be grateful for any help.
Thank you!
The patch named patch 0 0 exists. However, that does not mean that the plot named " patch 0 0" exists. You have simply mistyped the name of the plot (perhaps you have the incorrect number of spaces).
I suggest you rename your plot to something that doesn't look like a patch identifier to reduce confusion. Try something like "plot-0-0", renaming both on the interface in the plot dialogue, and also in the code.

turtles change color on particular patch color

this code contains a road setup and turtle creation , after that on the go procedure that makes turtles take the pink path , and am trying to make turtles change color with they get to a red colored patch , but for unknown reason this code is not working and the cars never change their color and stay blue as there creation color
i hope i get some help to do it
turtles-own [
speed
s?
]
to setup-road
clear-all
ask patches [
ifelse pycor < -8 and pycor > -17 [set pcolor black ]
[set pcolor gray - 3 ]
if pycor < -9 and pycor > -15 [ set pcolor gray ]
if pycor > -10 and pycor < -8 and pxcor > 20 and pxcor < 27
[set pcolor white ]
if pycor < -2 and pycor > -4 and pxcor < 20 and pxcor > -24
[set pcolor pink ]
if pycor < -3 and pycor > -10 and pxcor < -22 and pxcor > -24
[set pcolor green ]
if pxcor = 20 and pycor = -3
[set pcolor red
]
]
set-default-shape turtles "car"
create-turtles 2 [
set color blue
set size 2
set xcor random-xcor
set ycor -12
set heading 90
set speed 0.1 + random-float 0.9
set s? true
]
reset-ticks
end
to go
ask turtles [
let car-ahead one-of turtles-on patch-ahead 2
if car-ahead != nobody
[ fd speed]
let gate one-of patches in-radius 5 with [pcolor = green ]
let path one-of patches in-radius 5 with [pcolor = pink ]
if s?
[ ifelse gate != nobody
[ move-to gate
fd speed
ifelse path != nobody
[
move-to path
fd speed
if patch-ahead 1 != nobody and pcolor != pink
[set color green ]
]
[fd speed]]
[fd speed]
]
]
tick
end

netlogo ask pathces color in limited coordinates

I want to learn whether there is any brown patch in this limited coordinates. But this doesn't work. How can I query it?
if not any? pcolor = brown with [ (pxcor >= max-pxcor - boundary-width) or (pxcor <= min-pxcor + boundary-width)][fd 2]
You were almost there, I think. Something like this should work:
if not any? patches with
[ pcolor = brown and
( pxcor >= max-pxcor - boundary-width)
or (pxcor <= min-pxcor + boundary-width) ]
[ fd 2 ]