In an «ask turtles» method: How can I read a variable of a patch in a relative angle and distance from a turtle? - simulation

I want [turtles with [shape = "sheep"]] to move either to the lefthand side or the righthand side, depending on how many [attractive (green) patches] are around a certain relative patch.
How the turtles with [shape = "sheep"] should count the patches (yellow)
The (incorrect) code looks like this:
to move-sheep
ask turtles with [shape = "sheep"] [
right random (16 + (count neighbors4 with [pcolor = green] patch-right-and-ahead 60 2)^ 2)
left random (16 + (count neighbors4 with [pcolor = green] patch-right-and-ahead -60 2)^ 2)
forward 1
#(some other commands…)
]
end
Thank you ^^

It would be much easier if you actually told us what the problem is. You say your code is wrong, but not how you know it's wrong. Is it reporting an error (and, if so, what is the error and which line is reporting it)? Is the moving turtle going the wrong way?
Regardless, the easiest way to approach this is to do something smaller before trying to move. If you simply count the number of green patches and print that out, you can test the code. Once you introduce movement, how will you tell if it counted correctly?
I am still not entirely sure what you are asking. But I think this code might help you diagnose your problem. It tells you what the count is around the target patches, so you can see if it is doing the correct count. Once you know the counting works, you can then modify for movement.
to testme
clear-all
ask patches [set pcolor one-of [yellow green]]
create-turtles 1 [set heading 30 set color black]
ask one-of turtles
[ ask patch-right-and-ahead 60 2 [set pcolor red]
type "Right: "
print count ([neighbors4] of patch-right-and-ahead 60 2) with [pcolor = green]
ask patch-right-and-ahead -60 2 [set pcolor red]
type "Left: "
print count ([neighbors4] of patch-right-and-ahead -60 2) with [pcolor = green]
]
end

Related

Netlogo - how to count turtles number around a specific turtle

I need to "do-something-special" if around yellow turtle there is at least 3 blue turtles . Is the code bellow correct?
I tried
ask turtles with [color = yellow]
[
if count turtles in-radius 1 with [color = blue] >= 3
[do-something-special]
]
do-something-special should remove (disappear) 3 of blue turtles and current yellow one
Did I do the location of relevant turtles correctly and how do I kill them after I find them?
Hannah's answer is good but the linked example won't fully fix your problem. Since you will be using the set of close agents twice (once to count and once to potentially remove some), you should also create an agentset for efficiency reasons (you don't want to create it twice). Here is a full solution.
ask turtles with [color = yellow]
[ let near-blue turtles in-radius 1 with [color = blue]
if count near-blue >= 3
[ ask n-of 3 near-blue [die]
die
]
]
Also, if you don't care about the exactness of the radius, an alternative to turtles in-radius 1 would be turtles-on (patch-set neighbors patch-here), which is all the turtles on the neighbouring and same patches to wherever your asker turtle is sitting.
At the moment your code counts the amount of turtles that are blue in the radius of one patch around the yellow turtle. If the amount of blue turtles is bigger/equal 3 the yellow turtles die if you use the "die" command instead of "do-something-special". So it looks as follows.
ask turtles with [color = yellow]
[
if count turtles in-radius 1 with [color = blue] >= 3
[die]
]
Maybe you can merge the code with the following example and then kill the neighbors.

netlogo: have patches calculate influence value using reporter

I'm modeling a cityscape for looking at movement, where patches equate to buildings and have different influence values. I need to use a reporter function that calculates the value of each patch with something like:
(its own influence) plus (the influence of neighbors) divided by the
distance to a specific turtle (max-one-of distance myself)
The turtle will then move towards the patch with the highest influence value, but along a defined street.
I'm new to using netlogo and have gotten completely stuck.
I've included a portion of what I have so far, but I can't figure out how to write the reporter function that will compute each patches influence value (in-cone) so that the turtles can then move towards the best option.
to setup-influence-field
ask patches with [pcolor = green] [set influence commercial-influence]
ask patches with [pcolor = orange] [set influence production-influence]
ask patches with [pcolor = yellow] [set influence domestic-influence]
ask patches with [pcolor = pink] [set influence religious-influence]
ask patches with [pcolor = blue] [set influence public-influence]
end
to go
move-serapis
end
to move-serapis
ask serapis [set-procession-heading]
repeat 2 [ ask serapis [ fd .25 ] display ]
tick
end
;;;;; the reporter values are need for this part of the code so that the turtles (serapis) can move towards the patches with the highest influence value;;;;
to set-procession-heading
let direction patches in-cone 4 40 with [influence-field > 0]
if any? influence-field
[face max-one-of influence-field] ;;;; face towards the highest computed influence value
ifelse any? patches with [pcolor = black] in-cone 1 25
[process]
end
Any help would be greatly appreciated!
I don't think this is completely correct and I can't test it, but it should start you off and maybe someone here can fix it if you let us know the errors.
to set-procession-heading
let direction-targets patches in-cone 4 40 with [influence-field > 0]
if any? direction-targets
[ face max-one-of direction-targets [ influence-amount self ] ]
end
to-report influence-amount [ target-node ]
report ( [ influence-field ] + sum [ influence-field] of neighbors ) / distance target-node
end
What I have done is set up a separate procedure to report the results of your calculation. That procedure takes an argument (named target-node) because you need to be able to pass the identity of the turtle being influenced. Once you have that procedure, then you can simply pass the agent-set of potential directions to the calculation procedure and pick the one with the largest value.

How to make simulation run faster in netlogo instead of using Slider bar near to view update

Is there any code to make simulation run faster in netlogo, instead of using slider bar near to setting? What my code need to do is to simulate the crowd behavior, it's work fine if the number of turtles around 100,however when I increase the number up to 300-800 turtles, simulation take very long to finish. Each tick also take very long to count from 0 to 1 and next until all turtles die. one thing that I suspect cause slow simulation is when ask turtles to evacuate. without evacuate rule, everything went smoothly even set a maximum number of turtles. is there other way to write evacuate rule, so that it can run faster? thanks.
to go
ask turtles [wander fd 0.01]
if emergency? = true [move]
if all? turtles [ pcolor = red ] ;stops simuation
[stop]
tick
end
to wander
[ do..something]
end
to move
set time-to-evacuate time-to-evacuate + 1
ask turtles [avoid-obstacles fd 0.1]
ask turtles [follow-leader fd 0.1]
ask turtles [flock fd 0.1]
ask turtles with [pcolor != red] [evacuate fd 0.1]
ask turtles with [pcolor = red][die]
end
to evacuate
ask turtles with [color = black ]
[let beings-seen patches in-cone 10 135 with [pcolor = red]
if any? beings-seen
[ let target one-of beings-seen
face target]]
ask turtles with [color = white ]
[let beings-seen patches in-cone 5 135 with [pcolor = red]
if any? beings-seen
[ let target one-of beings-seen
face target]]
end
to avoid-obstacles
[do something]
end
to follow-leader
[do something]
end
to flock
[do something]
end
In your move procedure you have:
ask turtles with [pcolor != red] [ evacuate ... ]
And then in evacuate you have:
ask turtles with [color = black] [ ... ]
evacuate is already being run by all of the non-red turtles, so you've got every non-red turtle asking every black turtle to do something at every time tick.
I don't think you intended that.
I have to guess a bit at your intent, but I think if in evacuate you replace the ask with an if:
if color = black [ ... ]
that's probably closer to what you meant.

On netlogo, what command do I use to make a turtle stop if the patch it wants to move to is a certain color

I'm making a maze on netlogo and I want to do it so that once it tries to walk into the violet lines, it'll stay on its own patch instead of moving forward. What command would that be? I tried bk 1 to reverse the fd 1 but it doesn't work all the time
You can undo your step like this:
ask turtles [
fd 1
if pcolor = violet [fd -1]
]
Or you can check ahead of time as Marzy answered. Basically it's the difference of asking for forgiveness vs permission :-)
I hope this example answer your questions:
turtles-own [target]
to setup
clear-all
reset-ticks
ask n-of 100 patches [
set pcolor red
]
create-turtles 1
[ move-to one-of patches with [pcolor != red]
set heading 90
set target one-of patches with [pcolor != red]
ask target
[
set pcolor green
]
]
end
to go
ask turtles
[ifelse pcolor != green
[
ifelse [pcolor] of patch-ahead 1 != red
[
Your-Move-Function
]
[
Your-Bounce-Function
]
leave-a-trail
]
[stop
print ticks
]
]
tick
end
to Your-Move-Function
let t target
face min-one-of all-possible-moves [distance t]
fd 1
end
to Your-Bounce-Function
let t target
face min-one-of all-possible-moves [distance t]
end
to-report all-possible-moves
report patches in-radius 1 with [pcolor != red and distance myself <= 1 and distance myself > 0 and plabel = "" ]
end
to leave-a-trail
ask patch-here [set plabel ticks]
end
This is how it works:
Random patches are colored Red to show walls or obstacles, one turtle is created in a random location with a random target which is colored green:
I have used a variable to store all available patches which turtle can step on , but since I have considered a target for the turtle, turtle chooses the one patch which is closest to the target, and since I have noticed in some cases it might go in circle I have asked the turtle to leave tick number which is its move number as a plabel, you can use a variable for that for specifying if that path was already selected or not.

netlogo move turtle nearest patch

I want to move my turtle to the nearest red or green color patch in its vision.I tried this code but it doesnt move.What is wrong?
while [collectedDirt = 5]
[
ask turtle 0 [
let nearest-patch min-one-of (patches with [pcolor = red or pcolor = green] in-cone 15 20)[distancemyself]
face nearest-patch
fd distance nearest-patch
]
set collectedDirt collectedDirt + 1
search-dirt ;; research whether there is red patch in-cone because of new position
]
You might want to provide us with a little bit more context, but my guess would be that you need something like while [collectedDirt < 5] instead of while [collectedDirt = 5]. If this block is the only way your turtles can "collect dirt", the code probably never even gets executed...
Edit:
You also might want to add a condition in case there is no red/green patch in the cone of vision:
if is-patch? nearest-patch [
face nearest-patch
fd distance nearest-patch
set collectedDirt collectedDirt + 1
]