netlogo: have patches calculate influence value using reporter - netlogo

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.

Related

How to divide world in regions of specific size?

I am having troubles building my model: I am trying to build an agricultural area, where a specific number of farmers have lands of different sizes (also specific, not random). In the farmers' proprieties, there are fields (yellow patches) and forest (green patches).
Here's a bit of code:
breed [Smallfarmers Smallfarmer] Smallfarmers-own [property]
breed [Mediumfarmers Mediumfarmer]
Mediumfarmers-own [property]
to setup
ca
loop [
repeat 50[
create-Smallfarmers 1 [
set property [patches in-radius-nowrap 1.5] of one-of patches with [pcolor = black]
if all? (patch-set [neighbors] of property) [pcolor = black] [
ask n-of 2 property [set pcolor green]
ask property with [pcolor != green] [set pcolor yellow]]]]
repeat 10[
create-Mediumfarmers 1 [
set property [patches in-radius-nowrap 2.9] of one-of patches with [pcolor = black]
if all? (patch-set [neighbors] of property) [pcolor = black] [
ask n-of 6 property [set pcolor green]
ask property with [pcolor != green] [set pcolor yellow]]]]
stop]
end
With this code I have these problems:
not all the farmers are created.
the properties are floating in the black space, far apart.
How can I improve (or completely revolutionize) my code?
There are several distinct issues with this. The first are general NetLogo bits:
Using in-radius-nowrap suggests to me that you have the world wrapping and don't really want it to wrap since it is land and it doesn't make sense to wrap. You can turn off wrapping with the settings button (top right) on the interface, where you also set the number of patches in the world.
If you want to create (say) 10 farmers, you don't need to do repeat 10 [create-farmers 1 ..., you just create 10 at once. All the code in the [ ] that sets up the property will happen for each farmer because it is part of the create block.
neighbors is already a patchset, but I suspect you want to include the central patch as well
Your specific problem about too few properties too far apart - In fact, all the farmers and their properties are being created but their properties are not being coloured. Your code creates a farmer and gives them property, but only colours it if the property does not overlap.
So to fix this, you need to create the farm only where there is space. It is probably easier to create the larger farms first, and then the smaller farms can fill in the gaps. Looking at your code though, all the farms are the same size (3x3) and the difference is how many fields are green or yellow. Here is some code that creates farms (notice also that it puts the farmer on the farm).
breed [farmers farmer]
farmers-own
[ property
]
to setup
clear-all
create-farmers 10
[ let potential-farms patches with [all? (patch-set self neighbors) [pcolor = black]]
ifelse any? potential-farms
[ move-to one-of potential-farms
set property (patch-set patch-here neighbors)
ask n-of 2 property [set pcolor green]
ask property with [pcolor != green] [set pcolor yellow]
]
[ print "No space available" ]
]
end

NetLogo: select a patch with neighbors having certain qualities?

I want to let my turtle move to certain patch and make a "splotch". The central patch = my turtle location, can be selected randomly, however satisfy two conditions:
has to be in a certain distance from turtle's actual position
has to surrounded (neighbors in certain radius) by patches with specific quality
The reason is to create kind of "buffers" around my turtle's position, with aim to obstruct the close proximity of my clumps.
Please, how can I satisfy these two conditions?
As far, I have:
to go
ask turtles [
; select one of patches in specific distance, and
; surrounded by patches with no magenta color
let aaa one-of patches with [distance myself > 3 and
all? neighbors with [pcolor != magenta]]
; how to write this condition above ??
; and how replace "neighbors" by "in-radius"??
move-to aaa
ask neighbors [ ; create clump of magenta patches
set pcolor magenta ]
ask patch-here [ ; set central patch to magenta
set pcolor magenta ]
]
You're almost there; you just need to reread the documentation for all? and any?.
let _candidates patches with [distance myself > 3]
set _candidates _candidates with [
not any? (patches in-radius 3 with [pcolor = magenta])
]
let aaa one-of _candidates
If it is possible that there will be no candidates, you should guard against that.

How to ask turtles to place in a desired area, on Netlogo?

I want to create turtles, which they place in a desired area with random coordinate:
they should place in the white area and in middle of it in a line. in other words, in the top regtangle, their xcor should be random and their ycor is 10. in the right regtangle, their ycor should be random and their xcor is 10 and so on.
When you create turtles, you can give them instructions such as their location. For example:
create-turtles 1 [ set ycor 10 ]
Alternatively, you can sprout the turtles from the relevant patches and their location will already be set. For example:
ask n-of 5 patches with [pcolor > 1] [ sprout 1 ]
to place-on-color [#color]
let _patches (patches with [pcolor = #color])
ask turtles [
move-to one-of (_patches with [not any? turtles-here])
]
end
Add error checking if you may have too many turtles. (Or remove the unique occupancy constraint if you don't want it.)

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.