Wall agents in netlogo - netlogo

I am trying to simulate a room with opening door. Instead of using patches I wish to use agents.
The earlier approach I followed was first creating a box with a gap and then distributing agents on it.
But it didn't seem give the results.
(Netlogo Sprouting turtles at regular intervals
Netlogo Sprouting turtles spaced at less than one patch)
Above is roughly sketched diagram.
Note:
An easier to do it would to sprout turtles at each patch. But I don't require so. Instead I wish to make turtles small and sprout one more than at a patch.
Thanks.
Question: distribute turtles along the sides of a rectangle (with a gap as shown in fig) and distribution can be varied depending upon the desired density of turtles.

The following does the job in case someone in future faces such a issue:
set breadth-patches patches with[(pycor > (-(breadth)) and pycor < breadth and pxcor = lengthrec) or(pycor > (-(breadth)) and pycor < breadth and pxcor = (-(lengthrec))) ]
set length-patches patches with[(pxcor > (-(lengthrec)) and pxcor < lengthrec and pycor = (-(breadth - 1))) or (pxcor > (-(lengthrec)) and pxcor < lengthrec and pycor = (breadth - 1))]
set gap-patches patches with [pxcor > (gap * (-1)) and pxcor < gap and pycor =(breadth - 1)]
set length-patches length-patches with [not member? self gap-patches]
ask breadth-patches[
sprout-walls 1[set color 2
set size 0.5 set heading 180 fd 0.25
if-else(pxcor < 0)[set heading 90][set heading 270] fd 0.25]
sprout-walls 1[set color 2
set size 0.5 set heading 360 fd 0.25
if-else(pxcor < 0)[set heading 90][set heading 270] fd 0.25]
]
ask length-patches[
sprout-walls 1[set color 2
set size 0.5 set heading 90 fd 0.25
if-else(pycor < 0)[set heading 180][set heading 0] fd 0.25
]
sprout-walls 1[set color 2
set size 0.5 set heading 270 fd 0.25
if-else(pycor < 0)[set heading 180][set heading 0] fd 0.25
]
]
end

Related

Netlogo divide world into upper and lower

Good afternoon everyone:
Currently I am working in a program in Netlogo and I want to divide the world in upper-quadrant and lower-quadrant and ask turtles to move to the upper-quadrant. I have figured out how to divide the world in four quadrant from a previous question answered here, but I don't know how to divide it in two.
Thank you very much for your help
ask patches with [ pxcor <= max-pxcor and pxcor > 0 and pycor > 0]
[
set pcolor red
set quadrant 1
]
ask patches with [ pxcor >= min-pxcor and pxcor < 0 and pycor > 0]
[
set pcolor blue
set quadrant 2
]
ask patches with [ pxcor <= max-pxcor and pxcor > 0 and pycor < 0]
[
set pcolor green
set quadrant 3
]
ask patches with [ pxcor >= min-pxcor and pxcor < 0 and pycor < 0]
[
set pcolor yellow
set quadrant 4
]
Given that you are interested in a lower and an upper quadrant, you only need to look at y coordinates. The specific condition depends on where your world's origin (i.e. coordinates [0;0]) is.
If your world's origin is in the default position, which is the center, then do:
patches-own [
quadrant
]
to setup
clear-all
ask patches [
ifelse (pycor > 0)
[set quadrant 1]
[set quadrant 2]
]
end
If your world's origin is in a corner (e.g. I assume bottom left corner in this case), just do:
patches-own [
quadrant
]
to setup
clear-all
ask patches [
ifelse (pycor > max-pycor / 2)
[set quadrant 1]
[set quadrant 2]
]
end
If you don't know in advance where your world's origin will be, or if your world's origin is in a less common place than the two examples above, you can take a more generalised approach that fits any situation:
patches-own [
quadrant
]
to setup
clear-all
let y-extent (max-pycor - min-pycor + 1)
ask patches [
ifelse (pycor > y-extent / 2)
[set quadrant 1]
[set quadrant 2]
]
end

NetLogo Battle Simulation: Placing soldiers in between specific coordinates?

I'm new to NetLogo and I'm unsure of how to place 300 spartans in a narrow row. Let's say an area of 2x5 patches, turtles overlapping one another. I have tried using sprout, this achieved the specific coordinate requirements but the turtles are only one per patch.. Here is some code I have.
ask patches with [pxcor > 0 and pycor > -2 and pycor < 2]
[ sprout 1 [ set color red ] ]
or
to setup-spartans
create-spartans 300
set-default-shape turtles "person"
ask spartans
[ setxy random-xcor -3 ;; makes only a single row and goes across entire screen
;; (I need it to be in a specific area)
set heading 180
set color red ]
end
How about something like this?
create-spartans 300 [
set xcor -2 + random-float 5
set ycor -1 + random-float 2
]

How to move turtles on a game board simulation?

I'm creating a board game simulation similar to monopoly, where pieces are moving around the board. My turtle acts as the user's game piece and moves around the border of the world. The border is set up in alternating colors. I have a button that generates the number of steps the turtle moves. Those steps are stored as dice-num, a global variable. However, when the turtle lands on the patches before the patch (16 -16) (the bottom right-hand corner of the board) and the number of steps it needs to move exceeds 1, I can't turn the turtle's heading halfway to move up the board. As a result it only moves back to the beginning of the board.
I've tried to treat each case separately:
;if the turtle lands on the patch before the corner
ask turtle 0 [if pycor = min-pycor and pxcor = max-pxcor - 1
[setxy max-pxcor min-pycor
set dice-num dice-num - 1
show dice-num
]
]
;dice-num refers to number of steps the turtle moves
Heres my code so far:
to setup
board
end
to go
dice-roll
end
to board
ask patches [if pxcor = max-pxcor or pycor = max-pycor or pxcor = min-
pxcor or pycor = min-pycor
[set pcolor blue]]
ask patches [if pycor = max-pycor or pycor = min-pycor
[if pxcor mod 2 = 0
[set pcolor orange]]]
ask patches [if pxcor = max-pxcor or pxcor = min-pxcor
[if pycor mod 2 = 0
[set pcolor orange]]]
ask patch min-pxcor min-pycor [set pcolor green]
end
to dice-roll
set dice [1 2 3 4 5 6]
set dice-num one-of dice
user-message (word "You rolled: " dice-num)
ask turtle 0 [
fd dice-num
]
;allows the turtle to turn if it lands on a corner
ask turtle 0 [if ycor = min-pycor and xcor = max-pxcor [set heading 0]
if xcor = max-pxcor and ycor = max-pycor [set heading 270]
if xcor = min-pxcor and ycor = max-pycor [set heading 180]
;add a statement to end game once player rereaches the green patch
]
;if the turtle lands on the patch before the corner
ask turtle 0 [if pycor = min-pycor and pxcor = max-pxcor - 1
[setxy max-pxcor min-pycor
set dice-num dice-num - 1
show dice-num
]
]
end
I expect the output to be the turtle to start moving up the right hand side of the board once it lands on the patch (15 -16) and recieves a dice-num greater than 1. However, when the turtle does land on the patch (15 -16) and the dice-num is greater than 1 it simply moves back to the beginning of the board.
Okay, this was simply too long for comments so I had to make some guesses. I expect the problem was that you were forwarding the full amount of the die roll after checking location. This would mean you have to test all the potential patches that could reach the corner. Instead, it is easier to move one patch and test location before moving the next patch. This is a cleaned up version of your code that implements this move and check process.
to setup
clear-all
board
ask one-of patches with [pcolor = green]
[ sprout 1
[ set color white
set heading 90
forward 1
]
]
end
to go
let die-num dice-roll
ask one-of turtles [ move die-num ]
end
to board
ask patches with [ pxcor = max-pxcor or pxcor = min-pxcor ]
[ set pcolor ifelse-value (pycor mod 2 = 0) [orange][blue] ]
ask patches with [ pycor = max-pycor or pycor = min-pycor ]
[ set pcolor ifelse-value (pxcor mod 2 = 0) [orange][blue] ]
ask patch min-pxcor min-pycor [set pcolor green]
end
to-report dice-roll
let dice-num one-of [1 2 3 4 5 6]
user-message (word "You rolled: " dice-num)
report dice-num
end
to move [#roll]
while [ #roll > 0 ]
[ if pxcor = max-pxcor and pycor = max-pycor [set heading one-of [180 270] ]
if pxcor = max-pxcor and pycor = min-pycor [set heading one-of [0 270] ]
if pxcor = min-pxcor and pycor = max-pycor [set heading one-of [180 90] ]
if pxcor = min-pxcor and pycor = min-pycor [ stop ]
fd 1
set #roll #roll - 1
]
end
The alternative is simply to add the die roll to the current location and see if it goes past. If it does, calculate where you want to get to.

NetLogo how to count turtles in a coordinate

im doing an evacuation model which will compare the time to take if ull take the nearest exit or go with the crowd. What i have done so far is to simulate the" nearest exit"
to go
ask turtles [sideway]
end
to sideway
if pxcor < 0 [ set heading 270 ]
if pxcor > 0 [ set heading 90 ]
end
this is just apseudocode. now my question is that how to count the number of turtles in pxcor >0 and pxcore<0?
example
if pxcor > 0
counter ++;
count turtles with [ pxcor > 0 ]
turtles is just all the turtles. turtles with [ pxcor > 0 ] creates a new set that contains just the turtles from turtles for which pxcor > 0 is true. count just counts the number of agents in a set.

NetLogo: Error in avoiding walls and closed gates

I have this code in which turtles are suppose to change direction when they encounter walls and closed gates.
It is okay at the beginning but then it will give this message.
OF expected input to be a turtle agentset or patch agentset or turtle or patch but got NOBODY instead.
error while turtle 259 running OF
called by procedure GO
called by Button 'Go'
I can send the whole model.
if state = 1 [fd speed
ifelse [pcolor] of patch-at-heading-and-distance 0 1 = black or [pcolor] of patch-at-heading-and-distance 0 1 = red
[ lt random-float 90]
[fd 1]
; ifelse [pcolor] of patch-ahead 1 = blue
; [ lt random-float 360 ] ;; We see a blue patch in front of us. Turn a random amount.
; [ fd 1 ]
if (pxcor >= 89 and pxcor <= 90) and (pycor > 5 and pycor < 10) [facexy (87 + random 3) 25 fd speed]
;if (pxcor >= -10 and pxcor <= 1) and (pycor >= 6 and pycor <= 23 ) [facexy ( 7 + random 3) (26 + random 3) fd speed]
if (pxcor >= 85 and pxcor <= 90) and (pycor = 26) [
let choice random 2
fd speed
if choice = 1 [leftbench]
if choice = 0 [facexy (87 + random 3) 76]
]
if (pxcor >= 83 and pxcor <= 92) and (pycor >= 75 and pycor <= 77) [rightbench fd speed]
if pcolor = brown and not any? other turtles-here
[ move-to patch-here
set seated? true
set pcolor orange
]
]
Does your model have wrapping at the world edges turned off in one or both directions?
If so, then your code has to handle the possibility that sometimes this patch:
patch-at-heading-and-distance 0 1
won't exist, if the turtle is near a world edge. If that happens, then patch-at-heading-at-distance will return nobody, and when you try and do:
[pcolor] of patch-at-heading-and-distance 0 1 = black
you get the error, because no pcolor can be retrieved from a nonexistent patch.
The typical way to code around this is to do something like:
let target patch-at-heading-and-distance 0 1
if is-patch? target and ...
Finally, I wonder whether you really mean patch-at-heading-and-distance 0 1. It's equivalent to patch-at 0 1, which refers to the patch north of the turtle; not sure why you'd want that. If you want to test the patch ahead of the turtle (the patch the turtle will land in if it uses fd), use patch-ahead.