I would like to ask turtles in the car? to stop moving once they change colour. This is the code I tried but the output does not change.
let moving (turtles) with [ car? ]
if any? moving
[ ask moving [stop] ]
I also tried this
if any? turtles with [ car?]
[ stop ]
which stops the entire simulation once there's a turtle with car?. Car? is a turtle-own.
I would like (turtles) with [ car? ] to not move at all.
Any advise is greatly appreciated.
Turtles only move when you tell them to, it's not like you start moving them and they continue to do so until you tell them not to. So presumably you have some code somewhere that actually moves turtles that looks something like:
ask turtles with [car?] [move]
Instead, only move the ones you want, something like:
ask turtles with [car? and color = yellow] [move]
Alternatively, when you change their colour, you can also set car? false if the change in colour means that the person is walking or similar
Related
The final part of my design involves me recording down anytime a car breed drives into or in netlogo terms, is on the same patch or X and Y coordinate as the people breed as they navigate across the edge of the screen. Had this been java I could've done something like
if Car.xPostion == Person.xPostion
(Do something...)
But unfortunately I do not know how to do the same in NetLogo, all I've been able to do so far is just ask the two breeds by giving every turtle a boolean variable called movable and setting them to true and the rest to false, is there anyway I can check the two coordinates of two different turtles on Netlogo? This is all I 've been able to do so far.
to record-accidents
ask turtles with [movable? = true]
[
]
If you tried something like your java approach, it would fail because turtle positions are continuous and floating numbers are nearly always not equal.
If I have understood your question correctly, you have given a boolean variable called movable? set to true for cars and false for all other breeds. You don't need to do this, turtles know their own breed so you can do ask cars.
To answer your specific question, there are several ways to approach it depending on the perspective (sort of, which agent is in charge).
You could identify patches where there are accidents:, which is the answer to your question in the title (about identifying patches with two breeds).
let accident-locations patches with [any? people-here and any? cars-here]
if any? accident-locations
[ ask accident-locations
[ <do something>
But you can also take a turtle perspective. You could start from pedestrians who have been hit. This takes advantage of the fact that turtles can automatically access the patch variables (like turtles-here) for the patch where they are located:
let hit people with [any? cars-here]
if any? hit
[ ask hit...
or from cars:
let hitters cars with [any? people-here]
if any? hitters
[ ask hitters...
For example, I have 10 turtles at one patch, how do I move a specific turtle (turtle with [color = red]) to the top?
Thank you for your help!
I will assume that the question is about the "z-order" of the turtles and that "moving a turtle to the top", means "have it painted on top of the other ones".
There are two factors that determine the painting order in NetLogo: breeds, and ẁho numbers. Breeds have precedence. As per the Breeds section in the Programming Guide:
The order in which breeds are declared is also the order in which they are layered in the view. So breeds defined later will appear on top of breeds defined earlier;
Turtles within the same breed are painted in their order of creation (inferable in NetLogo by their who number): the older ones are painted first, and the newer ones are painted on top.
The order of creation is not modifiable, but if nothing in your code is holding on to turtle references or who numbers (the latter being inadvisable anyway), you could use hatch to create a clone of a turtle and then kill the old one immediately. For example:
to setup
clear-all
create-ordered-turtles 10 [ set size 10 ]
ask turtles with [ color = red ] [
hatch 1
die
]
end
The last line would bring all the red turtles (only one in this case) on top.
What if you can't do that, for some reason? Then you can use breeds:
breed [ background-turtles background-turtle ]
breed [ foreground-turtles foreground-turtle ]
to setup
clear-all
create-ordered-background-turtles 10 [ set size 10 ]
ask turtles with [ color = red ] [
set breed foreground-turtles
]
end
You would need as many breeds as you want "layers" of turtles. This may or may not be convenient. The best approach will depend on your specific use case.
There is some ambiguity.
To move the turtle to the top of the patch
Set ycor pycor + .5
To move it to the top of the view
Set ycor max-pycor
To make it the top of the stack in the photoshop kind of way. Not so easy.
turtles are displayed in order of their who ids. Who ids can not be changed. So if you want red to be on top either create it last or have it swap values with the turtle on top. Sorry.
Imagine a city with streets that people move around the city and streets. How can i say to the turtles that move just in a certain ways or don't move in some ways(patches)?
Some relevant models in the Code Examples section of the Models Library that I suggest you look at and study:
Look Ahead Example: turtles look ahead of them before moving so they don't step on blue patches
Wall Following Example: turtles treat brown patches as "walls", walking alongside them
In Look Ahead Example, the crucial snippet of turtle code is:
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 ] ;; Otherwise, it is safe to move forward.
In Wall Following Example, the behavior of the turtles is more complicated, so the code is a more complicated, too.
Hello i will try to be quick
I have a room with a fire that expands , and i have two exits , all i want to do is say to agents that if a door is blocked by fire then to go to the other one. i came up with something like this but not result.
to doorblock
show count neighbors with [pcolor = 77] ;; the patch color of the two doors
end
;;to go
ask smarts [ ;;smarts are the agents inside the room that need to get oout
if [ doorblock > 5 ]
[ set target one-of sexits]] ;;sexits is the other door
Anyone got a better idea? Thanks
OK, so if I understood correctly, you want your agents to take a look at the door that is their current target, check if that door has more than 5 fire agents around it, and choose another target door if that is the case.
If your fire agents are just red turtles (with no specific breed), you probably want something like this:
ask smarts [
if count ([ turtles-on neighbors ] of target) with [ color = red ] > 5 [
if-else ([ breed ] of target = sexits )
[ set target one-of nexits ]
[ set target one-of sexits ]
]
]
The key primitives here are:
neighbors, that will give you the patches around a turtle (the patches around target, in this case)
turtles-on, that will give you the turtles that are on members of a patch set (here, that will be the turtles that are on the patches that are the neighbors of target)
and finally, with allows you to get only the turtles from an agentset that satisfy some condition (here, we use it to get only the red turtles that represent the fires).
You should also try to understand the of primitive.
And I guessed you wanted to assign a new target that was of a different breed than the previous one (south if north, north if south) but that's up to you.
I am using an existing model in netlogo called Chemical Equilibrium and am adding some more code. I want to add turtles (catalyst) which have no effect on the reaction/other turtles but speeds up the FORWARD reaction, which has been defined as follows:
to react-forward [t]
ask t [ set color red ]
set color green
rt random-float 360
jump 2
end
I was thinking that I should put a switch and a slider, make the turtles into whitemols or I do a turtles-own [catalyst] and then define that like I have done with temperature and pressure. I tried the following but it didnt work.
turtles-own [speed catalyst]
crt whitemols
[ set color white
randomize
set speed 1
]
I know the above code is incorrect but am not sure how to code this particular feature.
There are many ways to do this, of course. I can't tell what is going on in your program from the little snipped you include.
One way would be to have the catalyst be of a different breed:
breed [catalysts catalyst]
breed [chemical-x chemical-x]
;and so on
;then the forward reaction is sped up by the existence of catalysts
to react-forward
let num-catalysts count catalysts
;speed up by num-catalysts
;...
end