When one variable of a patch reaches a certain value (e.g. age = 75 ), I redirect the patch to another procedure (called retirement) where I want to select all the patches that share the same value of the variable Farm_ID so the procedure is applied to all the patches.
I'm looking for a command which mean "the same as", I also considered to use the to-report and report procedure to report the pxcor and pycor of the original patch and then ask the other patches with the same ID_Farm as the patch pxcor pycor
But I feel like I'm missing something in the essence of using Netlogo (I'm a newbie).
to start-simulation
reset-timer
tick-advance 70
;; increase the farmer's age
ask patches with [seed = 1] [set age age + 1]
ask patches [
if age = 75 [ retirement ]
]
;; other stuffs
end
to retirement
ask patches with [ ID_Farm = ???]
;; procedure to change the variable ID_Farm depending on the closest patches with a different ID_Farm
end
You need myself (see here), and more specifically you need to use variable = [variable] of myself.
See a minimal and reproducible example below:
to setup
clear-all
ask patches [
ifelse (pxcor > 0)
[set pcolor lime]
[set pcolor orange]
]
end
to go
ask one-of patches [
type "I am the chosen patch. My color is " type (ifelse-value (pcolor = 25) ["orange"] (pcolor = 65) ["lime"] ["cyan"]) print ". I and all patches having my same color will become cyan."
operate-on-patches-with-my-same-color
]
end
to operate-on-patches-with-my-same-color
ask patches with [pcolor = [pcolor] of myself] [
set pcolor cyan
]
end
When you check the myself entry in the NetLogo Dictionary, note the difference between self and myself. At the beginning it may be confusing, but as you get more and more used with how NetLogo works it will become clear.
Related
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
How can I build a syntax for an "ifelse" statement that has a turtle in a specified patch and has the turtle determine if the turtle's color is the specified color? The following is sample code, but it won't work. Incidentally, I would like to build it using "max-pxcor - 1".
ifelse count turtles-on patch (max-pxcor - 1) with [color = red] = 1[ ;The following code is omitted
(1) The patch X Y notation selects a specific patch but you have not provided a pycor. I assume you therefore want any patch with the correct value for pxcor. How many of those patches are coloured red? (2) If you are not selecting a patch with its identifier, then you are working with a patchset rather than a patch, even if there is only one patch in that set. If you need to get a patch from a patchset, you need one-of.
If you actually want to identify that patch so you can do something with it, then this approach separates the patches from the turtles so is reasonably readable:
let wanted-patch one-of patches with [pxcor = max-pxcor - 1 and color = red]
ifelse count turtles-on wanted-patch = 1
[
But you could also do:
ifelse any? patches with [pxcor = max-pxcor - 1 and
color = red and
count turtles-here = 1]
[
Of, if you genuinely want the patchset so the total number of turtles on all the right hand column red patches is 1 (so 1 on one patch and 0 on others), then:
ifelse count (turtles-on patches with [pxcor = max-pxcor - 1 and color = red]) = 1
[
For this last one, the () are optional, for readability.
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.
I am new at net logo and I want to write a model based on tragedy of the commons in the case of an overfished pond. The purpose is to find an equilibrium between fishers and fishes based on an economic model with demand and supply. If there are less fishers, more fishes will be in the pond, then after a certain time (ticks) the number of fishers increases and less fishes will be in the pond. Maybe set like a number of fishes per day that can be fished. Thus, the solution is to find a convenient number of fishers as the fishes can still reproduce. I want to have a box in the interface where I can type in a number and see what happens with the number of fishes.
I have no idea how to set this up. I hope to hear from you :)
I started with this code:
breed [fishers fisher]
breed [fishes fish]
to setup
clear-all
reset-ticks
ask patches [set pcolor blue ] ;; lake/pond in form of a rectangle in color
ask patches [ if pxcor > 8 [ set pcolor green ]]
ask patches [ if pycor > 8 [ set pcolor green ]]
ask patches [ if pycor < -8 [ set pcolor green ]]
ask patches [ if pxcor < -8 [ set pcolor green ]]
ask one-of patches with [ pcolor = blue ] [ sprout 20 [set shape "fish" set color pink set size 1.5 ]] ;; creates fishes
ask one-of patches with [ pcolor = green ] [ sprout 2 [set shape "person" set color black set size 3 ] ] ;; creates fishers
end
to go
tick
;;fishes
ask turtles with [ shape = "fish" and color = pink ]
[ right random 360 forward 1
if [pcolor] of patch-ahead 1 = green [ right 180 fd 1 ]]
;; fishers
ask turtles with [ shape = "person" and color = black]
[;right random 360 forward 1
if any? patches with [pcolor = blue]
[set heading towards one-of patches with [pcolor = blue] forward 1]
if [pcolor] of patch-ahead 1 = blue [ right 180 fd 2 ]]
ask turtles with [shape = "person" and color = black]
[if any? turtles with [shape = "fish" and color = pink] in-radius 2
[ask turtles with [shape = "fish" and color = pink] in-radius 2 [die]]]
end
Firstly, I suggest you look through existing models in the Netlogo library (Wolf-sheep-predation model may help). You roughly have the right idea in your current code, but you should look at other models to improve. You've already set your different breeds of turtles, but you should also set up their respective shapes under 'setup'. This would help you a great deal later - instead of calling for
ask turtles with [ shape = "fish"...]
you can simply
ask fishes [do sth...]
For that 'box at the interface', you can have a slider at the interface determining the number of fishers you want your run to start with. With another slider, you can set the fishing pressure in your simulated run (i.e. how many fish each fisher will catch) and I suppose you can also consider how this changes when population of fish decreases.
Finally, for a model like yours, you can observe the supply and demand trend by plotting the curves of no. of fishers over time and no. of fishes over time. Again, look at the wolf-sheep-predation model to have an idea of how to do this.
I can't give you more than this I'm afraid since I'm no pro myself but hope this helps a little. Hope someone else would be able to give you a clearer idea.
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.