I am struggling with developing of a Agent-based model for human migration. I created a network with different countries and calculated the reputation per country based on several parameters now I want to say that from the batch of the refugees in point XY should refugees go based on the country reputation to the specific country.
Can you please help how to write the code that the agents(refugees) will be distributed in the network based on the country reputation?
I set the country reputation as a parameter for the agent country and also included for each country the reputation.
This is my Code:
extensions [ nw ]
breed [countries country]
breed [nodes node]
breed [refugees refugee]
breed [houses house]
create-houses 1 [setxy 49 6 ]
create-countries 1 [setxy 44 5 set reputation 0.007]
create-countries 1 [setxy 43 13 set reputation 0.028]
create-countries 1 [setxy 46 16 set reputation 0.008]
create-countries 1 [setxy 39 16 set reputation 0]
create-countries 1 [setxy 34 18 set reputation 0.001]
create-countries 1 [setxy 32 18 set reputation 0.001]
create-countries 1 [setxy 37.5 21 set reputation 0.024]
create-countries 1 [setxy 36 22 set reputation 0.001]
create-countries 1 [setxy 34 22 set reputation 0.088]
create-countries 1 [setxy 32.5 25.5 set reputation 0.004]
create-countries 1 [setxy 25 26 set reputation 0.462]
create-countries 1 [setxy 40 29 set reputation 0.007]
create-countries 1 [setxy 22 28 set reputation 0.032]
create-countries 1 [setxy 23 31 set reputation 0.075]
create-countries 1 [setxy 31 35 set reputation 0.049]
create-countries 1 [setxy 37 42 set reputation 0.211]
create-countries 1 [setxy 44 33 set reputation 0.001]
create-countries 1 [setxy 42 37 set reputation 0.001]
create-countries 1 [setxy 42 41 set reputation 0.001]
set-default-shape refugees "refugee"
create-refugees 10 [
set location house 0
move-to location]
Thank you very much!
The code you have provided is pieces of different procedures that makes it very hard to understand what you are actually asking. And you mention network a few times but give no detail about what is actually connected in your network.
From your comments, I think that you want each refugee to be initially located in a random country, with the random selection weighted proportional to the country's reputation. If so, here is a complete model that does this. It uses the weighted-one-of primitive from the rnd extension, which randomly selects weighted by whatever attribute you specify (in this case, reputation). I have no idea how houses and countries relate to each other and have ignored this, and have moved a refugee to a country instead of a house.
extensions [ rnd ]
breed [countries country]
breed [refugees refugee]
countries-own [reputation]
refugees-own [location]
to setup
create-countries 1 [setxy 44 5 set reputation 0.007]
create-countries 1 [setxy 43 13 set reputation 0.028]
create-refugees 10
[ set location rnd:weighted-one-of countries [reputation]
move-to location
]
end
Related
I would like to change the minimum coordinate unit of the patch space, which is set to exist only one turtle per patch space, to less than one, and monitor it. We want to set the minimum spatial unit to the same tick size n as in tick-advance (e.g. n=0.1). Here is a piece of code, but it doesn't work. Can anyone help me? Thanks in advance.
globals [n] ; n<=1 i.e. n=0.1
; omitted
ask turtles [forward n]
tick-advance n
I find myself in the same boat as Matteo- I don't know that what you're looking to do is possible / makes sense in the context of NetLogo. The coordinates of patches are fixed (ie, one patch is one unit) but arbitrary (1 in Netlogo can mean 1 m or 1 km, depending on the model). In other words, a patch's coordinates are discrete, while turtles can move around in continuous space. So you can, of course, have a turtle wander around in step sizes of 1/10:
globals [n]
to setup
ca
set n 0.1
crt 10
reset-ticks
end
to go
ask turtles [
forward n
]
tick-advance n
end
After one run of go above, you could conceivably have a turtle at coordinates [xcor = 0.1, ycor = 0.1], although it would still be on patch 0 0 since pxcor values are integers.
It seems that what you are actually needing to do is not coming across as needed- can you edit your question to provide a little more detail / context? Perhaps knowing the why behind your need to model in this way will help askers get you pointed in the right direction. I personally am curious about:
Why you are using tick-advance instead of just tick
How you have implemented your one-turtle-per-patch restrictions- in other words, can you show a Minimal Reproducible Example? That may prompt other ways to approach what you're after.
Here is an example world with time tied to ticks:
globals [ seconds ]
to setup
ca
set seconds 0
resize-world 0 50 0 50
ask patches with [
floor (pxcor / 10) mod 2 + floor (pycor / 10) mod 2 = 1
] [
set pcolor white
]
crt 10
reset-ticks
end
to go
ask turtles [
fd 1
]
set seconds precision (seconds + 0.1) 2
if seconds mod 1 = 0 [print ( word "It has been " seconds " seconds.")]
tick
end
OBJECTIVE: My plan is to run my model for ~60 ticks, and use the outcome of that run (i.e. the changes to the patches) as the starting point for all future runs. The idea behind this is that the first 60 ticks simulate a landscape from the past up until today (without any policy interventions). Then, from today on, I want to explore a range of policy scenarios, all starting with the same base conditions.
QUESTION: Do you know if there is a smart way to take stock of / save the outcomes of a run so that I can use them as a starting point for future runs, or do I need to assess the conditions after 60 ticks manually and then build an alternative setup-button that replicates those conditions?
I agree with Charles that export-world and import-world should work.
An alternative (see code below ) would be to use a fixed random seed for your alternative setup for the first 60 ticks, then change to a run-specific random seed, which would also work on a web-based run. ( I suspect export-world doesn't work over the web. )
Here's an example of changing the random seed mid-flight. Be sure to save the random seed before you define the new random seed or everything will always be the same!
Load this code and hit setup and go buttons multiple times and you can confirm it's working.
globals [ variable-random-seed fixed-ticks]
to setup
clear-all
set variable-random-seed random 999999999 ;; nine nines works
random-seed 123456789 ;; any fixed number to use for low ticks
set fixed-ticks 10 ;; or 60 in your case
print " ----------- fixed ------------- ===== -------- varies by run ----------- "
reset-ticks
end
to go
if ticks > 20 [ print "\n" stop ]
write random 100
if ticks = fixed-ticks [ write "=====" random-seed variable-random-seed ]
tick
end
Sample output of three runs
----------- fixed ------------- ===== -------- varies by run
66 68 42 59 14 1 34 20 3 15 86 "=====" 1 80 87 54 85 51 37 53 94 69
----------- fixed ------------- ===== -------- varies by run
66 68 42 59 14 1 34 20 3 15 86 "=====" 94 72 60 26 18 90 65 50 65 18
----------- fixed ------------- ===== -------- varies by run
66 68 42 59 14 1 34 20 3 15 86 "=====" 23 93 75 68 17 44 17 30 99 94
I am having trouble with summing lists of all turtles and am not sure where I am going wrong.
For instance, in the following code I would like to show the total number of apples among all the turtles. Every time I run the code I get a value of 0, when it should in theory be 100. I am not sure what the error is. Any assistance would be much appreciated!
turtles-own [ apples ]
to test
clear-all
create-turtles 5 [
set apples []
set apples lput 20 apples
]
show sum [apples] of turtles
end
The following code and test should sum all the numbers in all the turtles' lists.
turtles-own [ apples ]
to test
clear-all
create-turtles 5 [
set apples []
set apples lput 20 apples
set apples lput 10 apples
]
ask one-of turtles [set apples lput 30 apples]
show sum map sum [apples] of turtles ; here's the key command
end
map sum [apples] of turtles sums the number in each turtle's list and puts those sums in a list, and then the first sum adds them all up. I made one turtle's list longer than the others' just to be sure that all the lists do not need to be the same length.
Hope this helps,
Charles
I want to modell the interactions of different tree species in a forest. To do so, I also have to simulate the growth/spread of the forest.
There I face the two following problems:
I want the trees to reach a minimum age from which on they can hatch a new tree each year. But I only know how to make them reproduce every (e.g.) 20 years.
There is also a set age at which the trees are chopped town. The problem is, that when this age is reached, all trees of one breed are chopped down, even if their age should actually be less than their harvest-age.
Here are the relevant parts of my code:
to go
ask oaks [set age ticks]
end
to set-harvest-age
ask oaks [set harvest-age 240]
end
to spread
ask oaks [
if (age mod 30 = 0) and (count oaks > 1) [hatch-oaks 1 [setxy random-xcor random-ycor set age 1]]]
end
to chop-down
ask oaks [if (age >= harvest-age) [die]]
end
The "set age 1" in "spread" does not seem to work. Maybe someone of you has an idea.
Thank you!
I think your main problem is the process order here. Every time go is called, all oaks will set their age to the current ticks. That includes any new saplings that you've hatched, so even if their age was 1 when they hatched, those saplings would instantly be set to the same age as all other oaks (which is just the number of ticks. Instead, you should use your oaks-own (or whatever species you want) variable to track the age of each individual turtle by incrementing it every tick rather than setting it to the ticks.
Additionally, it's probably better to use go or a similarly named procedure to act as the scheduler to call all the other relevant procedures. For example, check out these setup chunks:
breed [ oaks oak ]
oaks-own [ age harvest-age ]
to setup
ca
spawn-oaks
reset-ticks
end
to spawn-oaks ; setup procedure
create-oaks 10 [
set shape "tree"
set color green
setxy random-xcor random-ycor
; Set the harvest age
set harvest-age 100
; Randomly choose the age of the first generation
; to be somewhere between 50 and 75
set age 50 + random 25
]
end
This creates 10 oaks with a an age randomly between 50 and 75. It also sets their harvest age. Now, use a procedure to increase every oak's individual age by one each tick:
to get-older ; Oak procedure
set age age + 1
end
Then, something to have them start creating saplings when they reach maturity. I've included an if any? other oaks-here qualifier so that the population size doesn't immediately explode (as saplings can only survive on a patch without an established oak), but you would limit that growth in whatever way makes sense for your model.
to spread ; Oak procedure
; Get living, mature oaks to spead saplings nearby
; only some years (to avoid population explosion)
if age > 30 and random 50 < 5 [
hatch 1 [
; set sapling age to zero, and have it
; move away from its parent randomly
set age 0
rt random 360
fd random 5
if any? other oaks-here [
die
]
]
]
end
Finally, your chop-down procedure should actually work without changing now that the age issue is sorted out:
to chop-down ; Oak procedure
if age >= harvest-age [
die
]
end
Now all that's needed is to use go to call those procedures in the correct order:
to go
; Use the go procedure to schedule subprocedures
ask oaks [
get-older
spread
chop-down
]
tick
end
Bit of a silly example but hopefully will get you pointed in the right direction!
I want to have specific 'who' (labels) of the patches.I have attached a picture for it. How can I set it? Is it setting manual 'who' of patch numbers? or Is there any other way of doing it?
Thanks
picture for patch identifiers
If you don't care about the specific "spiral" sequence in your example, you can use a variant of Luck's solution that takes advantage of NetLogo's default patch sort order:
patches-own [ id ]
to setup
clear-all
(foreach (sort patches) (range count patches) [ [p n] ->
ask p [ set id n ]
])
ask patches [ set plabel id ]
end
Or you could use a slightly different sort order specified with sort-by.
That being said, I don't know what your requirements are, but I would question the idea of having a specific id for patches. Most things in NetLogo can be done without ever refering to an agent's id. The who number itself is a relic of very old NetLogo versions and should almost never be used in modern code.
If you want to refer to a specific patch, refer to it by its coordinates, for example: patch -2 4.
If you want to store a patch for future reference, store a reference to the patch itself, not some kind of id. For example: ask turtles [ set my-patch one-of patches ].
A patch doesn't have a who since that is a turtle-only variable; patches have coordinates instead. However, you could give patches their own id number and use that. If all you need is a 5 x 5 world (as in your picture), you could accomplish what you want in Netlogo code. For example, see the code below (and note that I changed the world settings to those seen here). However, if you want a world with more patches, I would recommend reading your desired patch values from a file.
globals [
idlist
ordered_patches
]
patches-own [
id
]
to setup
set idlist [ 9 10 11 12 13 24 1 2 3 14 23 8 0 4 15 22 7 6 5 16 21 20 19 18 17 ]
set ordered_patches sort patches
end
to assign-ids
( foreach ordered_patches idlist [
[ o_patch _id ] ->
ask o_patch [
set id _id
]
]
)
ask patches [
set plabel id
]
end