How to divide world in regions of specific size? - netlogo

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

Related

How to change color of n number of turtles out of total in a specific region?

I am working on an infectious disease model, I've created a world with two regions. The specified number of people get initialized in their specific region.
Now, I need to initially infect the same number of people in each region with the disease using the initially_infected global variable. e.g 10 people infected in one region and 10 in the other region.
The infection should be indicated by changing color of turtles to red.
I've tried using "n-of" but it is not working correctly as want to restrict the initially infected people in their respective regions.
to setup_agents
create-humans green_population [
move-to one-of patches with [pcolor = green and not any? humans-here]
set shape "green person"
set color yellow
set size 1
set green_antibodies 0
]
ask n-of initially_infected humans [set color red]
create-humans blue_population [
move-to one-of patches with [pcolor = blue and not any? humans-here]
set shape "blue person"
set color yellow
set size 1
set blue_antibodies 0
]
ask n-of initially_infected humans [set color red]
end
Your code is not working because you are asking humans in general (by using ask n-of initially_infected humans [set color red]), and not based on where they are.
You can tackle this in a variety of pretty much equivalent ways. To do it in the tidies way, you should ask yourself what is the main programming feature that univocally differentiates these two groups: their shape? the color of the patche they are on? should they hold a turtles-own variable differentiating them? should they be separate breeds?
Whatever you choose this feature to be, use that feature in the ask n-of ... statement.
In the fully-reproducible example below, I just use the turtles' color as the distinguishing element:
breed [humans human]
globals [
green_population
blue_population
initially_infected
]
humans-own [
green_antibodies
starting_region
]
to setup
clear-all
ask patches [
ifelse (pxcor > 0)
[set pcolor green]
[set pcolor blue]
]
set green_population 300
set blue_population 270
set initially_infected 10
setup_agents
end
to setup_agents
create-humans green_population [
move-to one-of patches with [(pcolor = green) AND (not any? humans-here)]
set color green + 1
]
create-humans blue_population [
move-to one-of patches with [(pcolor = blue) AND (not any? humans-here)]
set color blue + 1
]
ask n-of initially_infected humans with [color = green + 1] [
set color red
]
ask n-of initially_infected humans with [color = blue + 1] [
set color red
]
end
Perhaps a more elegant option would be to create a little procedure that takes the populations' color and the quantity to infect as inputs. Modifying only the relevant part of the code, it would be:
to setup_agents
create-humans green_population [
move-to one-of patches with [(pcolor = green) AND (not any? humans-here)]
set color green + 1
]
create-humans blue_population [
move-to one-of patches with [(pcolor = blue) AND (not any? humans-here)]
set color blue + 1
]
infect-humans (green + 1) initially_infected
infect-humans (blue + 1) initially_infected
end
to infect-humans [hue quantity]
ask n-of quantity humans with [color = hue] [
set color red
]
end
In my example I used the humans' colors as distinguishing factor, but you can easily adapt this to any other thing you might want to use.

How to spawn turtles a certain amount of patches away from each other

I am trying to spawn turtles 5 patches away from each other but I'm not sure how, right now they all spawn on green patches (I don't want them to spawn on brown ones) and I'm not sure how exactly you control the distance between the spawning of turtles, thanks.
breed [ humans person ]
breed [ zombies zombie ]
to setup_world
clear-all
reset-ticks
ask patches [
set pcolor green
]
ask n-of 100 patches [
set pcolor brown
]
ask n-of 15 patches with [pcolor != brown][sprout-humans 1 [set size 5
set color blue
set shape "person"]]
ask n-of 5 patches with [pcolor != brown][sprout-zombies 1 [set size 4
set color red
set shape "person"]]
end
Have you read this question: NetLogo Create turtle at regular distance from each other?
Anyway, I thought that showing you some working functions would be helpful, here I made two alternatives, sprout-distanced1 and sprout-distanced2, you can test them both by alternating which line is commented; I also added a slider called Min-Distance to control the turtles spacing.
sprout-distanced1 uses the keyword carefully with is basically a try-else block, it's there in case that the turtle doesn't find a patch distanced enough to move to, in which case rather than sending a warning the turtle will stay where it is and print its distance to the closest turtle.
sprout-distanced2 uses a while loop, in case that the turtle doesn't find a place to move to that is at least Min-Distance from another turtle it will reduce the minimum radius by a small amount until it can distance itself from other turtles, if it had to move to a patch where it is less than Min-Distance away from other turtles it will log the distance at the Command Center.
breed [ humans person ]
breed [ zombies zombie ]
to setup_world
clear-all
reset-ticks
ask patches
[
set pcolor green
]
ask n-of 100 patches
[
set pcolor brown
]
ask n-of 15 patches with [pcolor != brown]
[
sprout-humans 1
[
set size 5
set color blue
set shape "person"
;sprout-distanced1
sprout-distanced2
]
]
ask n-of 5 patches with [pcolor != brown]
[
sprout-zombies 1
[
set size 4
set color red
set shape "person"
;sprout-distanced1
sprout-distanced2
]
]
end
to sprout-distanced1
carefully
[
; try to move at least Min-Distance away from other turtles
move-to one-of patches with [not any? other turtles in-radius Min-Distance]
]
[
; if can't move Min-Distance away from other turtles
; stay put and log the min distance to other turtle, just for reference
show distance min-one-of other turtles [distance myself]
setxy random-xcor random-ycor
sprout-distanced1
]
end
to sprout-distanced2
let min-dist Min-Distance
let moved? FALSE
while [not moved? and min-dist > 0]
[
; can distance it self somewhere?
ifelse any? patches with [not any? other turtles in-radius min-dist]
[
; if yes, go there
move-to one-of patches with [not any? other turtles in-radius min-dist]
set moved? TRUE
; if had to reduce the distancing radious log it
if moved? and min-dist < Min-Distance
[
show distance min-one-of other turtles [distance myself]
]
]
[
; no where to go, reduce the distancing radious
set min-dist min-dist - 0.1
]
]
end
Choose whichever suits better your model.

set a demand and supply curve for the model tragedy of the commons in the case of an overfished pond

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.

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.)

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.