Reputation: 1
I have three issues (full code at the end):
ISSUE #1:
in "change-color" section usage of elseif vs two if statement yields two different results.
to change-color
ask n-of 1 turtles with [not(recently-changed?)] [
ifelse (color = white)
[set color red
set recently-changed? true]
[set color white
set recently-changed? true]
]
end
vs
to change-color
ask n-of 1 turtles with [not(recently-changed?)] [
if (color = white)
[set color red
set recently-changed? true]
if (color = red)
[set color white
set recently-changed? true]
]
end
shouldn't both of these yield the same result? But the one that uses ifelse results what I want.
To be specific, I want to ask 1 out of 5 total turtles to change color. If the the turtle color is red it changes to white and if it is white then the color changes to red. This is exactly what I get when I use elseif.
However when I use two if statements, for whatever reason Netlogo first ensures that all the red turtles become white.
ISSUE #2:
If the color of a turtle was recently changed then I don't want it to be changed for 3 days
to change-after
ask turtles with [recently-changed?]
[set shape "circle"
let t3 0
set t3 t3 + 1
if t3 >= 3
[set recently-changed? false
set shape "arrow"]
]
end
but it looks like this will allow color change after 2 days. Why?
ISSUE #3:
I want recently changed to change shape to circle and after 3 days I want them to change back to arrow shape. But as the simulation progresses, everyone changes into a circle and nobody seems to revert back to arrow shape like intended. Why?
FULL CODE:
turtles-own
[
recently-changed?
]
to setup
clear-all
reset-ticks
create-turtles 10
[
set color red
setxy random-pxcor random-pycor
set recently-changed? false
ask n-of 5 turtles [set color white]
]
end
to go
change-color
change-after
tick
end
to change-color
ask n-of 1 turtles with [not(recently-changed?)]
[
ifelse (color = white)
[set color red
set recently-changed? true]
;if (color = red)
[set color white
set recently-changed? true
]
]
end
to change-after
ask turtles with [recently-changed?]
[set shape "circle"
let t3 0
set t3 t3 + 1
if t3 >= 3
[set recently-changed? false
set shape "arrow"]
]
end
WHAT I AM TRYING TO DO:
Upvotes: 0
Views: 61
Reputation: 2305
In the scenario with the two if statements, you first pick out a turtle that has not recently been changed. You check if it is white and if it is, you ask it to become red. Then you check if it is red and ask it to become white.
The problem here is that the two if statements are completed after eachother so the initially white turtle that is changed to red will change back to white again. I suggest you just stick with the ifelse here.
If you still want to use if
statements, you can include the stop
primitive to immediately exit the enclosing procedure once you have changed a color:
ask n-of 1 turtles with [not(recently-changed?)] [
if (color = white)
[set color red
set recently-changed? true
stop]
if (color = red)
[set color white
set recently-changed? true]
]
On to your question about the shapes. In the change-after procedure, you are creating a local variable called t3 with the value 0 and increasing its value with 1. The problem is that local variables cease to exist as soon as the procedure they are in ends. They are not meant to be used over multiple ticks. Instead, you should make it a turtles variable
turtles-own
[
recently-changed?
t3
]
Then you set t3
to 0 within the color change procedure (I moved both that and the change to recently-changed?
outside of the ifelse command blocks since it happens regardless of which option is chosen)
ask n-of 1 turtles with [not(recently-changed?)]
[
ifelse (color = white)
[set color red] [set color white]
set recently-changed? true
set t3 0
]
And then finally count up in the change-after procedure
to change-after
ask turtles with [recently-changed?]
[set shape "circle"
set t3 t3 + 1
if t3 >= 3
[set recently-changed? false
set shape "default"]
]
end
Another problem I spotted with your code is in the setup procedure. I noticed that the number of white and red turtles doesn't seem to be as intended. The problem is in this piece of code:
create-turtles 10
[
set color red
setxy random-pxcor random-pycor
set recently-changed? false
ask n-of 5 turtles [set color white]
]
You are creating 10 turtles and giving each one of them the command to change the color of 5 random turtles to white, so that command is carried out 10 times. Instead, just put the command outside the brackets
create-turtles 10
[
set color red
setxy random-pxcor random-pycor
set recently-changed? false
]
ask n-of 5 turtles [set color white]
As a last tip: I see you are using n-of 1
a few times. You could also use one-of
there.
Upvotes: 0