I have defined a blah? as a property of a turtle under turtles-own.
now in the to go i want turtles with blah? property to do something and I want turtles without blah? property to do something slightly different.
For example:
ask turtles with [blah?]
[forward 2]
ask turtles with [not blah?] ; this won't work but i am trying to figure out how to do this
[forward 1]
so ask turtles with [not blah?] won't work; how can I do that>
For what I can guess, the problem must be of the sort that you define blah? as TRUE for the turtles whose blah? is true, but you do not define it as FALSE for the turtles whose blah? is false. This would result in some turtles having blah? = TRUE, and some others having blah? = 0 (because 0 is the default value in NetLogo for all custom variables).
This would cause not to give an error, because not expects a boolean value but it finds a 0 instead.
The solution is to define blah? for all turtles, and not only for those who should have it as true:
turtles-own [
blah?
]
to setup
clear-all
create-turtles world-height [
setxy (min-pxcor) (who + min-pycor)
set heading 90
ifelse (random 2 < 1)
[set blah? TRUE
set color yellow]
[set blah? FALSE
set color cyan]
]
end
to go
ask turtles with [blah?] [
forward 2
]
ask turtles with [not blah?] [
forward 1
]
end
A suggestion: it would probably be better to use one ask command-block coupled with ifelse (overall: n turtles being asked, n conditions being evaluated), rather than two ask command-blocks both using with (overall: n turtles being asked but 2n conditions being evaluated).
The go procedure would look like:
to go
ask turtles [
ifelse (blah?)
[forward 2]
[forward 1]
]
end
Related
For an assignment in one of my classes we need to model disease spread and also have the turtles, whether sick or healthy, reproduce when a male and a female end up on the same patch, and it's also based on a slider with probability of reproduction. We're supposed to have the gender assigned randomly at birth so that the reproduction works properly. Any idea how to do this?
This is my code so far:
to setup
clear-all
ifelse netlogo-web? [set max-turtles 300] [set max-turtles 300]
create-healthy-cows Population-Size [ set shape "cow"
set color lime
set infected? false
set disease? false
ask n-of (random Population-Size) turtles [set gender-male? true]
set size 3
setxy random-xcor random-ycor
set age random 200
set label-color black ]
end
and also:
to check-reproduction
ask turtles [if gender-male? = false [
ask turtles [if any? turtles-here with [gender-male? = true]
[reproduce-cows] ]]]
end
to reproduce-cows
ask turtles [ if max-turtles < 300 [
ask turtles [if age >= 50 [if (random-float 100 < Reproduction-Rate)
[hatch 1
[set color blue] ]]]]]
end
Also I have gender-male? set as a turtles-own.
Have a look at one-of and see if you can come up with a solution. It's important to learn what you're doing and why, and not just ask for the answer, especially when its for a class assignment.
I've never seen or heard of NetLogo and it took me less than 5 minutes to come up with a solution and then find a second, better one.. Especially if this is an assignment you should be googling and researching instead of asking to be spoonfed!
I want to select the network neighbours filtered by a link attribute rather than a turtle attribute. For example, this might be selecting only the closest friends based on a strength score on the friendship link. I could do this by having different link breeds for close friends, but this would require constantly changing breeds as the condition was or was not required.
Below is an example with a boolean condition.
links-own [flag?]
to testme
clear-all
ask patches [set pcolor white]
create-turtles 20
[ setxy 0.95 * random-xcor 0.95 * random-ycor
set color black
]
ask n-of 3 turtles [set color red]
repeat 40
[ ask one-of turtles
[ create-link-with one-of other turtles
[ set flag? random-float 1 < 0.4
set color ifelse-value flag? [red] [gray]
]
]
]
; colour-neighbours
colour-neighbours2
end
to colour-neighbours
ask turtles with [color = red ]
[ ask my-links with [flag?]
[ ask other-end [ set color blue ]
]
]
end
to colour-neighbours2
ask turtles with [color = red ]
[ ask turtle-set [ other-end ] of my-links with [flag?]
[ set color blue ]
]
end
I am currently doing the equivalent of colour-neighbours, but it involves stepping through several contexts. The colour-neighbours2 version is conceptually closer because it is referring directly to the network neighbours. However, because of the of, I get a list of neighbours that I then have to convert to an agentset.
This is for teaching and, while both work, they seem very convoluted when compared to the unconditional network neighbourhood with the link-neighbors primitive. That is, if I didn't care about the flag, I could simply say ask link-neighbors [ set color blue ].
Is there a more direct way to identify network neighbours conditional on a link attribute?
You already cover most possibilities. Another way to do it would be:
to colour-neighbours3
foreach [ other-end ] of my-links with [ flag? ] [ t ->
ask t [ set color blue ]
]
end
I would avoid colour-neighbours2 because, as you stated, it requires the conversion from a list to an agentset. Whether you should use colour-neighbours or colour-neighbours3 is, I think, a matter of personal preference.
Just to add a more-convoluted (worse?) option that uses an agentset:
to colour-neighbours4
ask turtles with [ color = red ] [
let flag-links my-links with [flag?]
ask link-neighbors with [ any? my-links with [ member? self flag-links ] ] [
set color blue
]
]
end
Maybe i worded the question wrong but what i want to do in the code is when another turtle meets another turtle they create links with each other
I know its
to go
tick
make-circle
move
if ticks >= timer1 [stop]
end
to move
ask turtles
[
create-links-with other turtles ;here is where i want to put the code
set heading random 360 fd 1]
create-network
end
to create-network
ask links [
set thickness 0.01 * counter
if [patch-here] of end1 = [patch-here] of end2
[set counter (counter + 1)]
]
end
but im not sure how to word it correctly to link when they meet how do i do that
Establish a breed variable for your counts. Then create a link between all turtles that are on the same patch with other turtles-here. Then increment that count variable for when the others turtles have met the original calling turtle. I'll note that I increment the count variable by .5 because each turtle in the link will increment it (there's 2 turtles, so .5 * 2 = 1).
links-own [meets]
to setup
clear-all
crt 100 [setxy random-xcor random-ycor ]
end
to go
ask turtles [fd 1]
ask turtles [rt random 90]
ask turtles [ create-links-with other turtles-here]
ask turtles [ ask other turtles-here [ask link-with myself [ set meets meets + .5]]]
end
I want to hatch a turtle after two different colors of turtles overlap.
to interact
if any? other turtles-here
[
birth
]
;detect interaction
end
to birth
ask turtles
[
hatch random 5 [ fd 1 ]
]
end
I would like to hatch a turtle that was an average color of the two parent turtles that interacted.
something like.
to birth
ask turtles
[ hatch random 5
[ let color be sum of previous turtles color sum / 2
fd 1 ] ]
end
also any tips on what I might be misunderstanding about netlogo syntax might be appreciated.
This might not be exactly what you're looking for, but if the parents are the only ones on that patch when they give birth then this block should do the trick.
to birth
let Q mean [color] of turtles-here
ask one-of turtles-here
[hatch random 5
[
set color Q
fd 1
]
]
end
I'm not sure if you'd need to make the offspring it's own breed though to tell them to change their color and move, or if this will work... If this doesn't work then:
breed[offsprings offspring]
breed[parents parent]
to birth
let Q mean [color] of parents-here
ask one-of parents-here
[hatch-offsprings random 5 ]
ask offsprings-here
[
set color Q
fd 1
]
end
I have two turtle breeds who populate each sides of the window and then only move round in there own side.
The problem I am having is that I want to constantly check to see if one singular instance of a turtle from each breed are both on the same y coordinate. And if this returns true i want both of those turtles to stop, but for all other turtles from each breed to carry on moving. I know you can identify a turtle by there unique ID but i don't know how to use this and how to use the correct syntax.
The best way to describe this in pseudo code would be
ask turtles [
if breed1 turtle ycor = breed2 turtle ycor
[ stop breed1 turtle and breed2 turtle ] ]
UPDATE
Tried getting the code to work but still nothing happening. Not sure if it is the way the procedure is wrote or the number I have chosen for the threshold.
to move-turtles
ask turtles [
if not any? turtles with [ breed != [ breed ] of myself and abs (ycor - [ycor] of myself) < 1 ]
[
ask redteam with [pcolor = green - 3] [
right random 360
forward 1
]
ask redteam with [pcolor != green - 3] [
back 1
]
ask blueteam with [pcolor = green - 2] [
right random 360
forward 1
]
ask blueteam with [pcolor != green - 2] [
back 1
]]
]
end
Note that "same coordinate" is actually somewhat ambiguous. If one turtles ycor is 5.0000001 and another's is 5.0000000, are they at the same coordinate? Because of this, you should check to see if their coordinates are within a certain amount of each other.
Also, the best way to stop moving is to simply not move. So, here is a possible go procedure that would do what you want:
to go
ask turtles [
if not any? turtles with [ breed != [ breed ] of myself and abs (ycor - [ ycor ] of myself) < threshold ] [
move ;; replace with your move procedure or code
]
]
end
Here, each turtle checks to see if there are any turtles of a different breed who's ycor is within threshold of their own ycor. If there are not, then it moves. Otherwise, it does nothing.
The myself stuff is the most confusing part here, so I recommend reading the docs.