Using "checkpoints" in netlogo - netlogo

i am designing a program where an aircraft will travel between two points on my "airspace". However, in between those 2 points, i have 5 "checkpoints" where the aircraft will make a decision every time it passes each checkpoint. The code i have written for the checkpoints are as follows :
to setup-areas
ask patches [
ifelse pxcor >= 14
[ set nonedist? true ]
[ set nonedist? false ]
ifelse pxcor > 10.8 and pxcor < 14 and pycor <= -2.5 and pycor > -4.5
[ set twelvedist? true ]
[ set twelvedist? false ]
ifelse pxcor > 7.6 and pxcor <= 10.8 and pycor <= -4.5 and pycor >= -7.5
[ set ninedist? true ]
[ set ninedist? false ]
ifelse pxcor > 4.4 and pxcor <= 7.6 and pycor < -7.5 and pycor >= -8.5
[ set sixdist? true ]
[ set sixdist? false ]
ifelse pxcor > 1.2 and pxcor <= 4.4 and pycor < -10.5 and pycor >= -11.5
[ set threedist? true ]
[ set threedist? false ]
ifelse pxcor >= -2 and pxcor <= 1.2 and pycor < -12.5 and pycor >= -13
[ set onedist? true ]
[ set onedist? false ]
ifelse pxcor < -2 and pycor < -13.5
[ set zerodist? true ]
[ set zerodist? false ]
]
end
The aircraft doest seem to be following the areas it is in. Any help is greatly appreciated!
The code to get the aircraft to execute some action upon entering a specific zone is as follows:
to go
ask aircrafts
[
while [nonedist? = true] [ pre-twelve ]
while [twelvedist? = true] [ twelve-to-nine ]
]
tick
end

I don't have an answer but I do have a diagnostic suggestion.
First, change your setup-areas procedure to make things a bit more visible. The following will mark the patches where the aircraft should do something.
to setup-areas
ask patches [
ifelse pxcor >= 14
[ set nonedist? true set pcolor red]
[ set nonedist? false ]
ifelse pxcor > 10.8 and pxcor < 14 and pycor <= -2.5 and pycor > -4.5
[ set twelvedist? true set pcolor red]
[ set twelvedist? false ]
ifelse pxcor > 7.6 and pxcor <= 10.8 and pycor <= -4.5 and pycor >= -7.5
[ set ninedist? true set pcolor red]
[ set ninedist? false ]
ifelse pxcor > 4.4 and pxcor <= 7.6 and pycor < -7.5 and pycor >= -8.5
[ set sixdist? true set pcolor red]
[ set sixdist? false ]
ifelse pxcor > 1.2 and pxcor <= 4.4 and pycor < -10.5 and pycor >= -11.5
[ set threedist? true set pcolor red]
[ set threedist? false ]
ifelse pxcor >= -2 and pxcor <= 1.2 and pycor < -12.5 and pycor >= -13
[ set onedist? true set pcolor red]
[ set onedist? false ]
ifelse pxcor < -2 and pycor < -13.5
[ set zerodist? true set pcolor red]
[ set zerodist? false ]
]
end
Then change what they should be doing when they hit a checkpoint.
to go
ask aircrafts
[
if [nonedist? = true] [ set color blue ]
if [twelvedist? = true] [ set color blue ]
]
tick
end
This will let you see if the interaction occurs as you expect. Then change your go code back and modify your action code so that, for example:
to pre-twelve
set color blue
type "My height is " print varname ; where varname is whatever is being changed
< then all the code you already have in the pre-twelve procedure>
end
The point is to work out which procedure isn't happening and from there, to work out why it's not happening. There is simply insufficient information in your question to have any chance of working out what's wrong.

Related

Creating a bottleneck obstacle for agents

I am trying to create a patch that will be a bottleneck structure in the middle of a path where the turtles have to go through.
I have created the parabolas but I would like to add a slider so that the size of the bottleneck can be changed but I don't know how to create a code that will move it.
Here is the code (I am very bad with the maths of the parabola so I had help with the top one and I just manually created the down one)
to setup-road
;; patch procedure
;; colour patches to paint a horizontal road
let bound (max-pycor / 3)
ifelse (pycor < (max-pycor - bound)) and (pycor > (min-pycor + bound))
[ set pcolor red - 3 ]
[ set pcolor green ]
set upper-road-bound (max-pycor - bound - 2 )
set lower-road-bound (min-pycor + bound + 2 )
setup-top-parabola-on-road
setup-down-parabola-on-road
end
;; I want to be able to expand the parabolas so that the opening between them will be wider as well as narrow according a slider.
to setup-top-parabola-on-road
ask patches with
[pxcor > -25 and pxcor < 25] [
if pycor > ((pxcor ^ 2) / 20 + 10)
[ set pcolor green ]
]
end
to setup-down-parabola-on-road
ask patches with [pxcor >= -4 and pxcor <= 4 and pycor = -11][set pcolor green]
ask patches with [pxcor >= -5 and pxcor <= 5 and pycor = -12][set pcolor green]
ask patches with [pxcor >= -6 and pxcor <= 6 and pycor = -13][set pcolor green]
ask patches with [pxcor >= -7 and pxcor <= 7 and pycor = -14][set pcolor green]
ask patches with [pxcor >= -8 and pxcor <= 8 and pycor = -15][set pcolor green]
ask patches with [pxcor >= -9 and pxcor <= 9 and pycor = -16][set pcolor green]
ask patches with [pxcor >= -10 and pxcor <= 10 and pycor = -17][set pcolor green]
ask patches with [pxcor >= -11 and pxcor <= 11 and pycor = -18][set pcolor green]
ask patches with [pxcor >= -12 and pxcor <= 12 and pycor = -19][set pcolor green]
ask patches with [pxcor >= -13 and pxcor <= 13 and pycor = -20][set pcolor green]
ask patches with [pxcor >= -14 and pxcor <= 14 and pycor = -21][set pcolor green]
ask patches with [pxcor >= -15 and pxcor <= 15 and pycor = -22][set pcolor green]
ask patches with [pxcor >= -16 and pxcor <= 16 and pycor = -23][set pcolor green]
ask patches with [pxcor >= -17 and pxcor <= 17 and pycor = -24][set pcolor green]
ask patches with [pxcor >= -18 and pxcor <= 18 and pycor = -25][set pcolor green]
end
Can anyone help?
Here you go.
This assumes you have a button called setup
and a slider for variable "width" that ranges from 0 to 11.
I added an "ask patches [ ]" in near the top where you are setting red and green.
to setup
clear-all
setup-road
reset-ticks
end
to setup-road
;; patch procedure
;; colour patches to paint a horizontal road
let bound (max-pycor / 3)
ask patches [
ifelse (pycor < (max-pycor - bound)) and (pycor > (min-pycor + bound))
[ set pcolor red - 3 ]
[ set pcolor green ]
]
let upper-road-bound (max-pycor - bound - 2 )
let lower-road-bound (min-pycor + bound + 2 )
setup-top-parabola-on-road
setup-down-parabola-on-road
end
;; I want to be able to expand the parabolas so that the opening between them will be wider as well as narrow according a slider.
to setup-top-parabola-on-road
ask patches with
[pxcor > -25 and pxcor < 25] [
if pycor > ((pxcor ^ 2) / 20 + width)
[ set pcolor blue ]
]
end
to setup-down-parabola-on-road
ask patches with
[pxcor > -25 and pxcor < 25] [
if pycor < (-1 * (pxcor ^ 2) / 20) - width + 1
[ set pcolor yellow ]
]
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

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

How to make turtles move around patches with specific rules?

I am new to Netlogo, and have some questions. That would be great if you could help me.
I would like to create some fruit flies moving around a tree that is made up by green patches. Fruit flies are attracted to the tree. They will turn back to the tree if fruit flies move away certain distance (such as 5 grids) from the tree.
In the beginning, they will not stop on the green patches because they have enough energy. As time passed, they will loss their energy. They will find the closest green patch, and stay on it for certain time once their energy reaches 0. They gain energy after that, and they only can do short hops from the bottom branch to the top one (3 hops). Flies will move back to the bottom branch when they are on the top part of tree. I think I need to do a WHILE loop, but I have no idea how to do that. Please look at my codes.
breed [flies fly]
breed [suns sun]
turtles-own [energy]
flies-own [count-down]
to setup
clear-all
setup-suns
setup-flies
setup-patches
reset-ticks
end
to setup-suns
;; Create the sun
set-default-shape suns "sun"
create-suns 1 [
setxy max-pxcor - 3
max-pycor - 3
set color yellow
set size 7
]
end
to setup-flies
set-default-shape flies "bee 2"
create-flies number-fly [ set color white setxy random-xcor random-ycor set count-down 15]
end
to setup-patches
;; Create sky and grass
ask patches
[ set pcolor blue ]
ask patches with [pycor < min-pycor + 2]
[ set pcolor 66 ]
;; Create trunk and branches
ask patches with [ pxcor = -15 and pycor <= 0 ] [ set pcolor brown ]
ask patches with [ pxcor = -15 and pycor < 8 and pycor > 0] [ set pcolor lime ]
ask patches with [ pxcor = pycor - 15 and pycor <= 5 and pycor > 0 ] [ set pcolor lime ]
ask patches with [ pxcor = (- pycor) - 15 and pycor <= 5 and pycor > 0 ] [ set pcolor lime ]
ask patches with [ pxcor = pycor - 8 and pycor <= 2 and pxcor > -15 ] [ set pcolor lime ]
ask patches with [ pxcor = (- pycor) - 22 and pycor <= 2 and pxcor < -15 ] [ set pcolor lime ]
ask patches with [ pxcor = pycor - 1 and pycor <= -1 and pxcor > -15 ] [ set pcolor lime ]
ask patches with [ pxcor = (- pycor) - 29 and pycor <= -1 and pxcor < -15 ] [ set pcolor lime ]
ask patches with [ pxcor = 15 and pycor <= 0 ] [ set pcolor brown ]
ask patches with [ pxcor = 15 and pycor < 8 and pycor > 0] [ set pcolor lime ]
ask patches with [ pxcor = pycor + 15 and pycor <= 5 and pycor > 0 ] [ set pcolor lime ]
ask patches with [ pxcor = (- pycor) + 15 and pycor <= 5 and pycor > 0 ] [ set pcolor lime ]
ask patches with [ pxcor = pycor + 22 and pycor <= 2 and pxcor > 15 ] [ set pcolor lime ]
ask patches with [ pxcor = (- pycor) + 8 and pycor <= 2 and pxcor < 15 ] [ set pcolor lime ]
ask patches with [ pxcor = pycor + 29 and pycor <= -1 and pxcor > 15 ] [ set pcolor lime ]
ask patches with [ pxcor = (- pycor) + 1 and pycor <= -1 and pxcor < 15 ] [ set pcolor lime ]
ask patches with [ pxcor = -26 and pycor = -3 ] [ set pcolor red ]
ask patches with [ pxcor = -10 and pycor = 5 ] [ set pcolor red ]
ask patches with [ pxcor = 21 and pycor = -1 ] [ set pcolor red ]
end
to go
move-flies
tick
end
to move-flies
ask flies [
set energy 6
ifelse energy > 10 [
;rt random 50 lt random 50 jump random-float 1 ;
let nearest-leaf min-one-of (patches with [pcolor = lime] ) [distance myself] ; Find the closest leaf within 5 grids - long range search.
if is-patch? nearest-leaf [ ; If it is a leaf, and flies will be attracted to the leaf.
face nearest-leaf
;set heading 45
;fd distance nearest-leaf
rt random 50 lt random 50 jump random-float 5 ; Protential resources make flies move actively (fast movement).
;move-to nearest-leaf ;
]
]
[
ifelse pcolor != lime
[ rt random 50 lt random 50 jump random-float 1 ; Initialization - flies fly around to search their resources.
continue]
[ stay ]
]
]
end
to continue
let nearest-leaf min-one-of (patches in-radius 1 with [pcolor = lime] ) [distance myself] ; Find the closest leaf within 2 grids - short hops.
ifelse is-patch? nearest-leaf [ ; If it is a leaf, and flies will be attracted to the leaf.
face nearest-leaf
fd random distance nearest-leaf
;ask patches in-radius 1 [set pcolor red]
;rt random 50 lt random 50 jump random-float 5 ; Protential resources make flies move actively (fast movement).
;move-to nearest-leaf ;
]
[
let turn-back min-one-of (patches with [pcolor = lime] ) [distance myself] ;
;set heading 180
face turn-back
jump random-float 5 ]
move-up ;Flies tend to move up through all branches by short hops. Need a while loop.
; Move down if they reach the top of tree
let canopy patch-at-heading-and-distance 0 1
let canopy-left patch-left-and-ahead 45 1
let canopy-right patch-right-and-ahead 45 1
if canopy != lime or canopy-left != lime or canopy-right != lime
[
move-down
]
end
to move-up
ask flies [
set heading one-of [0 30 -30]
]
end
to move-down
ask flies [
set heading one-of [180 120 -120]
]
end
to stay
set count-down count-down - 1 ;;decrement timer
set label count-down
if count-down = 0
[
rt random 50 lt random 50 jump random-float 1
set label ""
reset-count-down ;;it's another procedure
]
end
to reset-count-down
set count-down 30
end
I am sorry if you are confused by my codes. I appreciate your help. Thanks.
Kind Regards,
Ming
From your description, I think you almost certainly don't want a while loop. In a NetLogo model's go procedure, you're only specifying what happens in a single tick. So you would only use while if need it to express something that happens all in a single instant, not a process that unfolds over multiple ticks.
(Agree with Frank this is too much code to post to have a good chance of getting help. It would take quite a while for me to read and study this much code, let alone try to help you with it. In this answer I've tried to extract and address a single aspect.)

How to let some of the turtles move again?

In this model all it does is turtle will find a seat(red patch for available and yellow for taken). And once the seats are all occupied it all will stop.
Now how to make some of the turtles move again? Like if it is seated it will move again and try go another place or it will go out.
breed [kids kid]
breed [adults adult]
breed [oldies old]
kids-own [step]
adults-own [step]
oldies-own [step]
turtles-own [seated?]
to setup
__clear-all-and-reset-ticks
ask patches [setup-world]
ask patches with [pcolor = red ][set plabel count turtles-here]
set-default-shape turtles "person"
create-kids number-of-kids
create-adults number-of-adults
create-oldies number-of-oldies
ask kids[
set color green
set size 1
setxy -10 0
set heading random-float 90
rt 45 - random-float 90]
ask adults[
set color orange
set size 1
setxy -10 0
set heading random-float 45
rt 45 - random-float 90]
ask oldies[
set color blue
set size 1
setxy -10 0
set heading random-float 45
rt 45 - random-float 90]
end
to setup-world
set pcolor white
if ( pxcor = 10 ) and ( pycor < 10 and pycor > -11 ) [ set pcolor brown ]
if ( pxcor = -10 ) and ( pycor < 10 and pycor > 1 ) [ set pcolor brown ]
if ( pxcor = -10 ) and ( pycor < -1 and pycor > -11 ) [ set pcolor brown ]
if ( pycor = 10 ) and ( pxcor < 11 and pxcor > -11 ) [ set pcolor brown ]
if ( pycor = -10 ) and ( pxcor < 10 and pxcor > -11 ) [ set pcolor brown ]
if ( pxcor = 8 ) and ( pycor < 8 and pycor > 2 ) [ set pcolor red ]
if ( pxcor = 8 ) and ( pycor < -2 and pycor > -8 ) [ set pcolor red ]
end
to go
if count patches
with [pcolor = yellow and any? other turtles-here] = 10
[stop]
ask kids with [seated? = 0][
rt random 10
fd 2
if pcolor = red and not any? other turtles-here [
move-to patch-here
set seated? true
set pcolor yellow
]
]
ask adults with [seated? = 0]
[
rt random 10
fd 1.5
if pcolor = red and not any? other turtles-here[
move-to patch-here
set seated? true
set pcolor yellow
]
]
ask oldies with [seated? = 0]
[
rt random 10
fd 1
if pcolor = red and not any? other turtles-here[
move-to patch-here
set seated? true
set pcolor yellow
]
]
tick
end
Just the way you asked non-seated turtles by ask ... with [Seated? = 0] you can ask others by ask ... with [Seated? = 1]
However, I will suspect that they will go in a loop, because they are close to the bench ans bench is empty, so as soon as they stand up they will sit on the same spot again. Maybe you can have a memory [] which stores last few moves and if there is a seated position, say in last 10 items it will choose not to sit. I am not sure what are your requirements, this is just an example.
ask adults with [seated? = 1]
[
; Stand up set seated? = 0
; go around
]
ask kids with [seated? = 1]
[
; Stand up set seated? = 0
; go around
]
ask oldies with [seated? = 1]
[
; Stand up set seated? = 0
; go around
]
Update:
I have changed your code, its not much different from your code, but it has memory function to make sure turtles move around after leaving a bench and are not going to sit on same bench again and to some degree avoid walls (you need to improve the avoiding since step size is different for each agent):
breed [kids kid]
breed [adults adult]
breed [oldies old]
Globals [out-of-boundry]
turtles-own [seated? memory step]
to setup
clear-all
reset-ticks
ask patches [setup-world]
ask patches with [pcolor = red ][set plabel count turtles-here]
set out-of-boundry patches with [ pycor > 10 or pxcor < -10 or pxcor > 10 or pycor < -10 or pcolor = brown]
set-default-shape turtles "person"
create-kids 5 [
set memory []
set seated? false
set color green
set size 1
setxy -10 0
set heading random-float 90
rt 45 - random-float 90
set step 2
]
create-adults 5 [
set memory []
set seated? false
set color orange
set size 1
setxy -10 0
set heading random-float 45
rt 45 - random-float 90
set step 1.5
]
create-oldies 5 [
set memory []
set seated? false
set step 1
set color blue
set size 1
setxy -10 0
set heading random-float 45
rt 45 - random-float 90
]
end
to setup-world
set pcolor white
if ( pxcor = 10 ) and ( pycor < 10 and pycor > -11 ) [ set pcolor brown ]
if ( pxcor = -10 ) and ( pycor < 10 and pycor > 1 ) [ set pcolor brown ]
if ( pxcor = -10 ) and ( pycor < -1 and pycor > -11 ) [ set pcolor brown ]
if ( pycor = 10 ) and ( pxcor < 11 and pxcor > -11 ) [ set pcolor brown ]
if ( pycor = -10 ) and ( pxcor < 10 and pxcor > -11 ) [ set pcolor brown ]
if ( pxcor = 8 ) and ( pycor < 8 and pycor > 2 ) [ set pcolor red ]
if ( pxcor = 8 ) and ( pycor < -2 and pycor > -8 ) [ set pcolor red ]
end
to go
if count patches with [pcolor = yellow and any? other turtles-here] = 10 [stop]
ask turtles [
set memory lput Seated? memory
restrict-memory
ifelse seated?
[stand-up ]
[ move-in-the-brown-area step
sit]
]
tick
end
to move-in-the-brown-area [step-size]
ifelse not member? patch-ahead step-size out-of-boundry [
fd step-size
rt random 10
]
[ face one-of patches with [pcolor = white]
fd step-size
]
If member? patch-here out-of-boundry[
let target patch -10 0
face target
fd step-size]
end
to restrict-memory
;assume your memory-limit is 5
let memory-limit 5
if length memory >= memory-limit
[ set memory but-first memory ]
end
to stand-up
if seated?
[ set seated? false
set pcolor red
fd 1
]
end
to sit
if pcolor = red and not any? other turtles-here with [not member? true memory][
move-to patch-here
set seated? true
set pcolor yellow
]
end