i've encountered a Problem with my NetLogo Model. While I'm trying to give a certain number of Turtles (here "Akteure") a certain Vallue, which I want to set first constant on 1 (one-of) and later control the amount with a slider, I only revieve random numbers, often higher then for example the one-of limitation I've set in the code below
clear-all
reset-ticks
set radius max-pxcor - 2
create-ordered-Akteure number-of-Akteure[
fd max-pxcor - 5
rt 90
set size 3
set shape "person"
set color white
set Information1 false
set Information2 false
set Macht_neutral true
ask one-of Akteure [set information1 true]
if Information1 [set color green]
ask one-of Akteure [set Information2 true]
if Information2 [set Color Red]
ask one-of Akteure [set shape "kings"]
if shape = "Kings" [set Macht_hoch true]
ask one-of Akteure [set shape "Bauer"]
if shape = "Bauer" [set Macht_gering true]
]```
What am I ignoring, where is my mistake?
Jan
I think one problem is, that you set these values within the create-ordered-Akteure block, so that number-of-Akteure-times one random Akteur will be set e.g. to Bauer. Just close the block before ask one-of ... and join the set code blocks:
create-ordered-Akteure number-of-Akteure
[
fd max-pxcor - 5
rt 90
set size 3
set shape "person"
set color white
set Information1 false
set Information2 false
set Macht_neutral true
]
ask one-of Akteure
[
set information1 true
set color green
]
ask one-of Akteure
[
set Information2 true
set Color Red
]
ask one-of Akteure
[
set shape "kings"
set Macht_hoch true
]
ask one-of Akteure
[
set shape "Bauer"
set Macht_gering true
]
I prefere to open a block within a new line, so that I can easily see, which block is closed, but that is probably just a style question
Related
I am trying to model infection between mosquito and human, when setting different states for human it works but for mosquito it does not work. this is the code I used to set mosquito agent to infected
ask one-of turtles [set infected? true]
if infected? [ set color blue]
The complete code is
breed [human humans]
breed [mosquito mosquitoes]
turtles-own
[
infected? ;; If true, the person is infected
recovered? ;; If true, the person has lived through an infection.
susceptible? ;; Tracks whether the person was initially susceptible
]
;;; SETUP PROCEDURES
to setup
clear-all
setup-human
setup-mosquito
reset-ticks
end
to setup-human
create-turtles 100
[
setxy random-xcor random-ycor
set recovered? false
set infected? false
set susceptible? true
set shape "person"
set color white
set size 1
ask one-of turtles[set infected? true]
if infected? [set color red]
]
end
to setup-mosquito
create-turtles 10
[
setxy random-xcor random-ycor
set susceptible? true
set infected? false
set shape "bug"
set color brown
set size 1
ask one-of turtles [set infected? true]
if infected? [ set color blue]
]
end
to go
ask turtles
[
set heading random 360
forward random 2
]
tick
end
The bracketing is unclear in your code, I can't see what the if statement is within. But if the goal is to do several actions when you infect the mosquito, then put them all together like:
ask one-of turtles
[ set infected? true
set color blue
]
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.
I'm currently working on a zombie apocalypse simulation in NetLogo 2D. In my current simulation, when the zombies collide with humans the humans have a 100% chance to turn into a zombie. I would like to change this so that I can use a slider to set the conversion rate - for example if I have a rate of 50% on my slider, then when the zombies collide with humans there's a 50% chance they would turn into a zombie, otherwise kill the attacking zombie.
This is how I have currently setup my project, the way I've done this so far is to detect when they collide, and set the humans health to -1, and then made an if statement that says if the health is below 1, to make them a zombie.
I would appreciate any help in the right direction as I've spent time pondering about this and haven't come up with any solution.
breed[humans person]
breed[zombies zombie]
globals[rad]
humans-own[zombies_seen zombies_hit humans_speed per_vis_rad per_vis_ang health]
zombies-own[zombies_speed humans_around_me closest_humans]
patches-own[solid]
to setup_world
clear-all
reset-ticks
set rad 1
ask patches[
set pcolor green
]
create-humans number_of_humans [
setxy random-xcor random-ycor
set color blue
set size 5
set shape "person"
set humans_speed 1 + random-float 0.1
set health 100
adjust_vision_cone
]
ask humans[
ask patches in-radius 5[
set pcolor yellow
]
]
create-zombies number_of_zombies [
setxy random-xcor random-ycor
set color red
set size 4
set shape "person"
set zombies_speed 0.5
]
ask zombies[
move-to one-of patches with [pcolor = green]
]
ask humans[
ask patches in-radius 5[
set pcolor green
]
]
draw_solid_patches
end
to draw_solid_patches
ask patches [
set solid false
]
ask n-of 100 patches with [pcolor = green][
set pcolor brown
set solid true
]
end
to detect_wall
if[solid] of patch-ahead 1 = true[
right 90
left 90
]
end
to run_model
Every 0.01 [
make_humans_move
make_zombies_move
tick
reset_patch_colour
if not any? humans [stop]
if ticks = 500000 [stop]
]
end
to make_humans_move
ask humans[
if health > 1 [
show_visualisations
right 45
left 45
let have_seen_zombies human_function
detect_wall
forward humans_speed
]
if health < 1 [
; ; ; this is where it looks at the health of the humans,
; ; ; and checks to see if they have been hit by a zombie,
; ; ; because their health would be below 1.
set breed zombies
set color red
set size 4
set shape "person"
set zombies_speed 0.5
]
]
end
to make_zombies_move
ask zombies[
detect_wall
right 45
left 45
smell_humans
detect_wall
forward zombies_speed
]
end
to smell_humans
if any? humans in-radius 10 [
set heading towards one-of humans in-radius 10]
detect_wall
end
to show_visualisations
if show_vis_cone = true [
ask patches in-cone per_vis_rad per_vis_ang with [pcolor = green] [
set pcolor orange
]
]
end
to reset_patch_colour
ask patches with [pcolor = orange] [
set pcolor green
]
end
to adjust_vision_cone
set per_vis_rad 10 + random 10
set per_vis_ang 90
end
to-report human_function
let seen [false]
let hit [false]
ask zombies in-cone per_vis_rad per_vis_ang [
set seen true
]
ask zombies in-radius rad [
set hit true
]
ifelse seen = true [
set zombies_seen zombies_seen + 1
right 180
][
right (random zwr - (zwr / 2))
]
if hit = true [
set zombies_hit zombies_hit + 1
set color black
set health -1
; ; ; this is where I make the human into a zombie,
; ; ; I need to have a method here that will refer to
; ; ; the convert_probability variable.
]
report seen
end
Making minimal adjustments to your code you could substitute the set health -1 to
if random 100 < conversion_probability [ set health 0 ]
where conversion_probability is a slider going from 0 to 100, as you said in the question.
I also want to point out that you could detect walls like this
to detect_wall
if [solid] of patch-ahead 2
[
face one-of patches in-radius 2 with [not solid]
]
end
doing left x and then right x only makes the turtle turn to the left and then face back where it was originally facing, unless it sees a zombie it won't change the behavior of the humans all that much.
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
I have two sets of agents: retailerA and retailerB. Previously, I used breed to create them, but as I wanted to create a random network in which it was asking all the turtles and randomly choosing some to be connected, I decided it is better to keep turtles in the random network code (in the library) and do this change:
create-turtles 130 ; 100 of which are retailerA and 30 are retialerB.
Now I want each set of agents to have a different display. RetailerA will be scattered randomly while retailerB should be shown in a circle. However, the following code does not do what I want.
to A
create-turtles 100
set retailerA? true
set shape "person"
setxy random-xcor random-ycor
end
to B
create-turtles 30 [
set retailerB? true
set shape "person"
set color gray
]
ask turtles with [retailerB?] [
layout-circle turtles with [retailerB?] (max-pxcor - 5)
]
end
This treats all the turtles to have a circle display.
Thanks,
When you create a turtles-own variable, the default value is 0. So, if you do
create-turtles 100 [
set retailerA? true
set shape "person"
setxy random-xcor random-ycor
]
You have correctly set retailerA? to true, but since you did not set retailerB? for this agentset, they will all have a value of 0 for retailerB?. So, if you try to evaluate a true/false expression using with (eg turtles with [retailerB?]...) but some of the turtles return a value of 0 instead of either true or false, NetLogo doesn't know what to do. To fix this, you could either explicitly set those variables in the setup procedure, like this:
to spawn-a
create-turtles 100 [
set retailerA? true
set retailerB? false
set shape "person"
setxy random-xcor random-ycor
]
end
or you could explicitly say with [retailerB? = true].
Additionally, check out the syntax for layout-circle- you don't need to ask turtles with that primitive. To fix your issues, then, you'd need something like:
turtles-own [ retailerA? retailerB? ]
to setup
ca
spawn-a
spawn-b
reset-ticks
end
to spawn-a
create-turtles 100 [
set retailerA? true
set shape "person"
setxy random-xcor random-ycor
]
end
to spawn-b
create-turtles 30 [
set retailerB? true
set shape "person"
set color gray
]
layout-circle turtles with [retailerB? = true] (max-pxcor - 5)
end