I have two function (reporter) calls in NetLogo:
[ask patches [set p distros [150 140 130 120 110 100 90 80 70 60 50] [4 9 10 13 4 3 2 2 1 1 1]]]
[ask patches [set p distros [150 140 130 120 110 100 90 80 70 60 50] [1 1 1 2 2 3 4 13 10 9 4]]]
that I would like to read in from a CSV file:
150 140 130 120 110 100 90 80 70 60 50
4 9 10 13 4 3 2 2 1 1 1
150 140 130 120 110 100 90 80 70 60 50
1 1 1 2 2 3 4 13 10 9 4
Something like:
[ask patches [set p distros [row1] [row2]]]
[ask patches [set p distros [row3] [row4]]]
Is there an easy way to do this please?
Got it I think. Add the csv extension at the top, then:
file-open "/path/to/myfile.csv"
let row1 csv:from-row file-read-line
let row2 csv:from-row file-read-line
let row3 csv:from-row file-read-line
let row4 csv:from-row file-read-line
[ask patches [set p distros row1 row2]]
[ask patches [set p distros row3 row4]]
NetLogo should know to remove quote marks when reading in the data.
Related
Using NetLogo 5.3.1, i'm trying to set up BehaviorSpace so that all its model runs start after exactly the same 500-tick warmup period. However, the results are not intuitive to me.
For illustrative purposes, I will use the 'Flocking.nlogo' model in the model library. Below is the model code, with 2 lines of code added to the end of the setup which saves the model's state after 500 ticks.
turtles-own [
flockmates ;; agentset of nearby turtles
nearest-neighbor ;; closest one of our flockmates
]
to setup
clear-all
create-turtles population
[ set color yellow - 2 + random 7 ;; random shades look nice
set size 1.5 ;; easier to see
setxy random-xcor random-ycor
set flockmates no-turtles ]
reset-ticks
; Now execute a 500-tick warm-up period and save the model's state
repeat 500 [ go ]
export-world "Flocking-after-500ticks.csv"
end
to go
ask turtles [ flock ]
;; the following line is used to make the turtles
;; animate more smoothly.
repeat 5 [ ask turtles [ fd 0.2 ] display ]
;; for greater efficiency, at the expense of smooth
;; animation, substitute the following line instead:
;; ask turtles [ fd 1 ]
tick
end
to flock ;; turtle procedure
find-flockmates
if any? flockmates
[ find-nearest-neighbor
ifelse distance nearest-neighbor < minimum-separation
[ separate ]
[ align
cohere ] ]
end
to find-flockmates ;; turtle procedure
set flockmates other turtles in-radius vision
end
to find-nearest-neighbor ;; turtle procedure
set nearest-neighbor min-one-of flockmates [distance myself]
end
;;; SEPARATE
to separate ;; turtle procedure
turn-away ([heading] of nearest-neighbor) max-separate-turn
end
;;; ALIGN
to align ;; turtle procedure
turn-towards average-flockmate-heading max-align-turn
end
to-report average-flockmate-heading ;; turtle procedure
;; We can't just average the heading variables here.
;; For example, the average of 1 and 359 should be 0,
;; not 180. So we have to use trigonometry.
let x-component sum [dx] of flockmates
let y-component sum [dy] of flockmates
ifelse x-component = 0 and y-component = 0
[ report heading ]
[ report atan x-component y-component ]
end
;;; COHERE
to cohere ;; turtle procedure
turn-towards average-heading-towards-flockmates max-cohere-turn
end
to-report average-heading-towards-flockmates ;; turtle procedure
;; "towards myself" gives us the heading from the other turtle
;; to me, but we want the heading from me to the other turtle,
;; so we add 180
let x-component mean [sin (towards myself + 180)] of flockmates
let y-component mean [cos (towards myself + 180)] of flockmates
ifelse x-component = 0 and y-component = 0
[ report heading ]
[ report atan x-component y-component ]
end
;;; HELPER PROCEDURES
to turn-towards [new-heading max-turn] ;; turtle procedure
turn-at-most (subtract-headings new-heading heading) max-turn
end
to turn-away [new-heading max-turn] ;; turtle procedure
turn-at-most (subtract-headings heading new-heading) max-turn
end
;; turn right by "turn" degrees (or left if "turn" is negative),
;; but never turn more than "max-turn" degrees
to turn-at-most [turn max-turn] ;; turtle procedure
ifelse abs turn > max-turn
[ ifelse turn > 0
[ rt max-turn ]
[ lt max-turn ] ]
[ rt turn ]
end
; Copyright 1998 Uri Wilensky.
; See Info tab for full copyright and license.
The BehaviorSpace window looks like this:
The added 2 lines of code, which saves the model's state after 500 ticks, come from the answer to question 6 in Chapter 9 in Railsback & Grimm 2012: Agent-based and individual-based modeling (1st edition). The answer continues by stating the next step: "Then, in BehaviorSpace, change the "Setup commands" to just import the saved world and run 1000 more ticks".
I did this, and then imported the file into R to summarise the data by calculating the mean and SD of number of flockmates at tick 100, 200, 300, 400, and 500. Below the R code:
df <- read.csv("ibm_table_output-test.csv", skip = 6)
df1 <- df %>%
rename(run_number = X.run.number.,
time_step = X.step.,
mean_flockmates = mean..count.flockmates..of.turtles
) %>%
select(run_number,
time_step,
mean_flockmates,
vision) %>%
arrange(run_number,
time_step) %>%
filter(time_step == 100 |
time_step == 200 |
time_step == 300 |
time_step == 400 |
time_step == 500)
df1_long <- melt(df1, # Apply melt function
id.vars = c("run_number", "time_step","vision"))
# Calculate a summary table
df1.summ <- df1_long %>%
group_by(time_step, vision) %>%
summarise(avg = mean(value),
sd = sd(value))
The output is as follows:
# A tibble: 15 × 4
# Groups: time_step [5]
time_step vision avg sd
<int> <int> <dbl> <dbl>
1 100 1 8.34 0
2 100 2 8.34 0
3 100 3 8.34 0
4 200 1 7.83 0
5 200 2 7.83 0
6 200 3 7.83 0
7 300 1 7.95 0
8 300 2 7.95 0
9 300 3 7.95 0
10 400 1 7.45 0
11 400 2 7.45 0
12 400 3 7.45 0
13 500 1 7.92 0
14 500 2 7.92 0
15 500 3 7.92 0
To me this output doesn't make sense.
My question is why is the average number of flockmates the same across different vision levels within the same time_step group? And why are the SDs all 0? In other words, why do the model runs produce identical outputs? I thought that initiating a burnin period would initiate identical starting positions for all simulations, but create different mean and SD values for each run because of different random numbers used? Or am I misunderstanding?
EDIT: The reason why the SDs are 0 is because there is no variation in mean values, but I don't understand why there is no variation. Below is the df1_long data frame:
run_number time_step vision variable value
1 1 100 1 mean_flockmates 8.340000
2 1 200 1 mean_flockmates 7.833333
3 1 300 1 mean_flockmates 7.953333
4 1 400 1 mean_flockmates 7.446667
5 1 500 1 mean_flockmates 7.920000
6 2 100 1 mean_flockmates 8.340000
7 2 200 1 mean_flockmates 7.833333
8 2 300 1 mean_flockmates 7.953333
9 2 400 1 mean_flockmates 7.446667
10 2 500 1 mean_flockmates 7.920000
11 3 100 2 mean_flockmates 8.340000
12 3 200 2 mean_flockmates 7.833333
13 3 300 2 mean_flockmates 7.953333
14 3 400 2 mean_flockmates 7.446667
15 3 500 2 mean_flockmates 7.920000
16 4 100 2 mean_flockmates 8.340000
17 4 200 2 mean_flockmates 7.833333
18 4 300 2 mean_flockmates 7.953333
19 4 400 2 mean_flockmates 7.446667
20 4 500 2 mean_flockmates 7.920000
21 5 100 3 mean_flockmates 8.340000
22 5 200 3 mean_flockmates 7.833333
23 5 300 3 mean_flockmates 7.953333
24 5 400 3 mean_flockmates 7.446667
25 5 500 3 mean_flockmates 7.920000
26 6 100 3 mean_flockmates 8.340000
27 6 200 3 mean_flockmates 7.833333
28 6 300 3 mean_flockmates 7.953333
29 6 400 3 mean_flockmates 7.446667
30 6 500 3 mean_flockmates 7.920000
My understanding is that you're running setup once, manually, and then running your BehaviorSpace experiment. The problem you will have with that is that the random number generator seed is including in the export-world data that you generate once by running the setup procedure. Then when you call import-world in the Setup commands: of each experiment run you will get that RNG seed imported as well. The export actually includes the full state of the RNG, but thinking of it as being the same seed is close enough.
LeirsW is correct that Flocking (as most NetLogo models, and probably the original one you had the problem with) is totally deterministic. So the outcome will be the same with the same RNG seed each time.
The fix is easy, add a second line to your BehaviorSpace experiment Setup commands: after the import-world that runs random-seed new-seed. This will make sure each model run has a new, unique RNG seed to use for the rest of its run.
The simple answer to the lack of variation is that the flocking model (after setup) is as good as 100% deterministic. There are some elements in the code that at first sight you might think have some stochasticity to them but that sochasticity doesn't have any effect.
The order in which turtles flock and move is stochastic, but they all choose how they want to move before any of them do so, which results in this stochasticity having no effect on the outcome.
The other part that you would expect carries some stochasticity is set nearest-neighbor min-one-of flockmates [distance myself], when two birds are at the exact same distance away from the focal bird. But all their locations have floating point numbers in both x and y, so I assume that the chance that you will have a tie in distances is also extremely low.
I am creating an urban environment that is robust from interruption for my university project.
The problem I am facing is that I am not sure how to make each turtle follow a specific network line. The black line represents the electricity cable and the blue line represent the underground water pipe. I am not sure how to create a turtle to follow the network line to be consumed by the house or the big buildings. Also, please bear in mind that I only just started learning Netlogo so any improvements in the coding itself are much appreciated.
This is the current code for the urban environment model. Also, is it even possible for a turtle to follow a specific line?
breed [ houses house ]
breed [ big-buildings big-building ]
breed [ commercial-buildings commercial-building ]
breed [ water-pumping-stations water-pumping-station ]
breed [ sewage-plant-stations sewage-plant-station ]
breed [ electricity-power-stations electricity-power-station ]
breed [ overhead-cable-stations overhead-cable-station ]
to setup
clear-all
reset-ticks
setup-landscape
setup-network
if (show-grid = true ) [
draw-grid ;for the switch to hide or show the gride
]
if (show-eletricity = true) [
draw-electricity ; for the swtich to hide or show the gride
]
if (show-sewage = true) [
draw-sewage ; for the swtich to hide or show the gride
]
if (show-water = true) [
draw-water ; for the swtich to hide or show the gride
]
end
to setup-network
draw-networks ; designing lines and colour coordinate them to show what each network are.
end
to draw-networks ; list of networks that has been drawn.
draw-electricity
draw-sewage
draw-water
end
to draw-water
create-turtles 1 [ ;FOR THE HOUSE AND BIG BUILDINGS
setxy 37 -18
set heading 90
set color blue
set pen-size 2.5
pen-down
left 180
fd 10
right 90
fd 9
right 90
fd 3
right 90
fd 3
back 6
fd 3
right 90
fd 7
right 90
fd 3
back 6
fd 3
left 90
fd 7
right 90
fd 3
back 6
fd 3
left 90
fd 3
right 90
fd 19
right 90
fd 3
right 90
fd 4
back 9
fd 5
left 90
fd 4
right 90
fd 8
back 8
left 90
fd 3
right 90
fd 4
back 9
fd 5
left 90
fd 4
right 90
fd 8
back 8
left 90
fd 3
right 90
fd 4
back 9
die
]
create-turtles 1 [ ;FOR THE COMMERICAL BUILDING
setxy -37 -18
set heading 90
set color blue
set pen-size 2.5
pen-down
left 90
fd 14
right 90
fd 12
right 90
fd 3
back 6
fd 3
left 90
fd 10
left 90
fd 3
back 6
fd 3
right 90
fd 10
right 90
fd 3
back 6
fd 3
die
]
end
to draw-sewage
create-turtles 1 [ ;FOR HOUSE AND BIG BUILDINGS
setxy 38 -1
set heading 90
set color green
set pen-size 2.5
pen-down
right 90
fd 8
right 90
fd 8
right 90
fd 3
back 6
fd 3
left 90
fd 7
right 90
fd 3
back 6
fd 3
left 90
fd 7
right 90
fd 3
back 6
fd 3
left 90
fd 3
right 90
fd 19
right 90
fd 3
right 90
fd 4
back 9
fd 5
left 90
fd 4
right 90
fd 8
back 8
left 90
fd 3
right 90
fd 4
back 9
fd 5
left 90
fd 4
right 90
fd 8
back 8
left 90
fd 3
right 90
fd 4
back 9
die
]
create-turtles 1 [ ;FOR COMMERICAL BUILDING
setxy -18 -18
set heading 90
set color green
set pen-size 2.5
pen-down
right 180
back 7
right 90
fd 7
right 90
fd 9
left 90
fd 7
left 90
fd 3
right 90
fd 3
back 6
fd 3
left 90
fd 10
right 90
fd 3
back 6
fd 3
left 90
fd 10
right 90
fd 3
back 6
fd 3
die
]
end
to draw-electricity
;HOUSE
create-turtles 1 [ ;house 1
setxy -38 18
set heading 90
set color black
set pen-size 2.5
pen-down
fd 54
right 90
fd 3
die
]
create-turtles 1 [ ;house 2
setxy -38 18
set heading 90
set color black
set pen-size 2.5
pen-down
forward 61
right 90
fd 3
die
]
create-turtles 1 [ ; house 3
setxy -38 18
set heading 90
set color black
set pen-size 2.5
pen-down
forward 68
right 90
fd 3
die
]
create-turtles 1 [ ; house 4
setxy -38 18
set heading 90
set color black
set pen-size 2.5
pen-down
forward 70
right 90
fd 8
right 90
fd 2
left -90
back 4
die
]
create-turtles 1 [ ; house 5
setxy -38 18
set heading 90
set color black
set pen-size 2.5
pen-down
forward 70
right 90
fd 8
right 90
fd 9
left -90
back 4
die
]
create-turtles 1 [ ; house 6
setxy -38 18
set heading 90
set color black
set pen-size 2.5
pen-down
forward 70
right 90
fd 8
right 90
fd 16
left -90
back 4
die
]
create-turtles 1 [ ; house 7
setxy -38 18
set heading 90
set color black
set pen-size 2.5
pen-down
forward 70
right 90
fd 18
left 90
back 5
right 90
back 2
die
]
create-turtles 1 [ ; house 8
setxy -38 18
set heading 90
set color black
set pen-size 2.5
pen-down
forward 70
right 90
fd 18
left 90
back 12
right 90
back 2
die
]
; BIG BUILDINGS ROW
create-turtles 1 [ ; building 1
setxy -38 18
set heading 90
set color black
set pen-size 2.5
pen-down
forward 70
right 90
fd 8
right 90
fd 19
left -90
back 19
right 90
fd 3
right 90
back 3
die
]
create-turtles 1 [ ; building 2
setxy -38 18
set heading 90
set color black
set pen-size 2.5
pen-down
forward 70
right 90
fd 8
right 90
fd 19
left -90
back 19
right 90
fd 10
right 90
back 3
die
]
create-turtles 1 [ ; building 3
setxy -38 18
set heading 90
set color black
set pen-size 2.5
pen-down
forward 70
right 90
fd 8
right 90
fd 19
left -90
back 19
right 90
fd 17
right 90
back 3
die
]
create-turtles 1 [ ; building 4
setxy -38 18
set heading 90
set color black
set pen-size 2.5
pen-down
forward 70
right 90
fd 8
right 90
fd 19
left -90
back 19
right 90
fd 3
right 90
fd 3
die
]
create-turtles 1 [ ; building 5
setxy -38 18
set heading 90
set color black
set pen-size 2.5
pen-down
forward 70
right 90
fd 8
right 90
fd 19
left -90
back 19
right 90
fd 10
right 90
fd 3
die
]
create-turtles 1 [ ; building 6
setxy -38 18
set heading 90
set color black
set pen-size 2.5
pen-down
forward 70
right 90
fd 8
right 90
fd 19
left -90
back 19
right 90
fd 17
right 90
fd 3
die
]
;COMMERICAL BUILDINGS
create-turtles 1 [ ;COMMERICAL BUILDINGS 1
setxy -38 18
set heading 90
set color black
set pen-size 2.5
pen-down
right 90
fd 22
left 90
fd 33
die
]
create-turtles 1 [ ;COMMERICAL BUILDINGS 2
setxy -25 -4
set heading 90
set color black
set pen-size 2.5
pen-down
right 90
fd 2
die
]
create-turtles 1 [ ;COMMERICAL BUILDINGS 3
setxy -25 -4
set heading 90
set color black
set pen-size 2.5
pen-down
right 90
back 3
die
]
create-turtles 1 [ ;COMMERICAL BUILDINGS 4
setxy -15 -4
set heading 90
set color black
set pen-size 2.5
pen-down
right 90
fd 2
die
]
create-turtles 1 [ ;COMMERICAL BUILDINGS 5
setxy -15 -4
set heading 90
set color black
set pen-size 2.5
pen-down
right 90
back 3
die
]
create-turtles 1 [ ;COMMERICAL BUILDINGS 6
setxy -5 -4
set heading 90
set color black
set pen-size 2.5
pen-down
right 90
fd 2
die
]
create-turtles 1 [ ;COMMERICAL BUILDINGS 7
setxy -5 -4
set heading 90
set color black
set pen-size 2.5
pen-down
right 90
back 3
die
]
create-turtles 1 [ ;COMMERICAL BUILDINGS 8
setxy -25 -4
set heading 90
set color black
set pen-size 2.5
pen-down
right 90
fd 2
die
]
end
to setup-landscape
ask patches [
set pcolor pink
]
draw-entities
end
to draw-entities
draw-houses
draw-big-buildings
draw-water-pumping-stations
draw-sewage-plant-stations
draw-electricity-power-stations
draw-commercial-buildings
end
to draw-houses ; setting house as stationary not going to move at all
ask patch 30 15 [ sprout-houses 1 ]
ask patch 23 15 [ sprout-houses 1 ]
ask patch 16 15 [ sprout-houses 1 ]
ask patch 30 6 [ sprout-houses 1 ]
ask patch 23 6 [ sprout-houses 1 ]
ask patch 16 6 [ sprout-houses 1 ]
ask patch 20 2 [ sprout-houses 1 ]
ask patch 27 2 [ sprout-houses 1 ]
ask houses [
set size 1.5
set shape "house"
set color red
]
end
to draw-big-buildings ; setting big buildings as stationary not going to move at all
ask patch 30 -6 [ sprout-big-buildings 2 ]
ask patch 23 -6 [ sprout-big-buildings 2 ]
ask patch 16 -6 [ sprout-big-buildings 2 ]
ask patch 30 -12 [ sprout-big-buildings 3 ]
ask patch 23 -12 [ sprout-big-buildings 3 ]
ask patch 16 -12 [ sprout-big-buildings 3 ]
ask big-buildings [
set size 2
set shape "bigbuildings"
set color white
]
end
to draw-water-pumping-stations ; setting water pumping statoins as stationary not going to move at all
ask patch 37 -18 [ sprout-water-pumping-stations 3 ]
ask patch -37 -18 [ sprout-water-pumping-stations 3 ] ;FOR THE COMMERICAL BUILDINGS
ask water-pumping-stations [
set size 2
set shape "producewater"
set color blue
]
end
to draw-sewage-plant-stations ; setting sewage plant station as stationary not going to move at all
ask patch 38 -1 [ sprout-sewage-plant-stations 4 ]
ask patch -18 -18 [sprout-sewage-plant-stations 4] ;FOR THE COMMERICAL BUILDING
ask sewage-plant-stations [
set size 2
set shape "sewage"
set color green
]
end
to draw-electricity-power-stations ; setting eletricity power station as stationary not going to move at all
ask patch -38 18 [ sprout-electricity-power-stations 5]
ask electricity-power-stations [
set size 2
set shape "electrcitystation"
set color 44
]
end
to draw-commercial-buildings ; setting commerical buildings as stationary not going to move at all
ask patch -5 -1 [ sprout-commercial-buildings 7 ]
ask patch -5 -6 [ sprout-commercial-buildings 7 ]
ask patch -15 -6 [ sprout-commercial-buildings 7 ]
ask patch -15 -1 [ sprout-commercial-buildings 7 ]
ask patch -25 -1 [ sprout-commercial-buildings 7 ]
ask patch -25 -6 [ sprout-commercial-buildings 7 ]
ask commercial-buildings [
set size 2
set shape "commercialbuilding"
set color orange
]
end
to draw-grid ; allows me to pinpoint where each turtle is properly ( helps me to debug the code)
ask patches [
sprout 1 [
set heading 0
set color black
set pen-size 1
forward 3
right 90
pen-down
repeat 8 [
forward 3 right 90 fd 3
]
die
]
]
end
to go
end
This is the image of what my model currently looks like and NetLogo whole image is what I imagine how my model will work as I haven't started doing the go procedure.
The model can be found in the following GitHub repo:
https://github.com/omoaka1738/Netlogo-help.git
So, my comment above aside, drawing the network so it can be traversed is a whole other thing. You don't want to get bogged down in having to make a path-seeking model, on top of what you are already trying to do. So it's important to not just draw the lines, but give them information to make using them easier. This is why you've really got to think about your model, all its parts, how they interact (not in terms of netlogo, but just like, in terms of if it was a game, what are the rules?)
First question:
Do we really need to model the “physical” location of the wire/pipe or do we just need to know what connects to what?
Maybe we are modeling the effect of breaks in the physical lines/pipes, so we do ned to model the pipes and lines.
Next: do we really need to model each step that the water/elec takes in the pipe? That’s not how electricity works. It’s only sort of how water works. That’s mostly about pressure.
Instead, we can “cook” or “preload” the wires and pipes with information about where the stuff is coming from, and where it goes. This lets the model know what it needs to know without us doing extra work while the model is running.
So:
First: instead of PD and FD etc, we can use turtles to be the pipes and wires, or possibly, just the terminals, access points and joints. If we need to, the pipes too.
Each member will know:
what services it provides, like water or electric
Whether it is a terminal, in-line access point, joint, or pipe (if we need to model pipes)
An indicator of which connection leads to the source, created while we create the network.
Note that we can also "pre-calculate" the path from the source to any destination by recording the steps to trace backwards from the destination to the source, then reversing the steps. Store this map in the destination.
The more I think about it, the more this looks like a job for linked turtles. Then you don't need to mark or create turtles for the "pipes" just for the terminals, bends, joints, and access points.
So consider:
a breed of turtle, maybe called [nodes node].
This is common jargon for networks (aka graphs), they have nodes and edges.
add a breed of directed links called [ edges edge](common jargon again)
nodes will own:
service -- will be "w" or "e" (for exampleor watever you want)
Source — contains the turtle that is at the source
Input-direction (or something) l—-contains the node that is in the direction towards the source
maybe some other stuff.
We can reuse some of your line-drawing code, and maybe make it more consider and easier to copy and change.
POSTING THIS ANSWER FOR NOW
will com back and add on letter
Add some comments to this answer to give more info about what the model is doing.
I´ve in through this in previous posts and i got a new problem. Im trying to abandoment a restoration processes. At the beginning orange and bue flow swiftly, but as soon as they interact non win and just change between them over and over. In the code, I stablished that if a patch is orange and it sees at least 3 blues it should change to blue, and i didnt stablish that if a blue patch is surrounded by orange it changes, however it happens. The idea is that at set up I have 3 color, and with some sliders (that would be in - if reforestado >= 3 [if random 100 <= SLIDER]) the patches changes and ultimetly blue (75) and a little bit of green (66) should win showing restauraton
This code can be copy and pasted and it should work, ignore the red patches that appear. Please set vertical limits to the world.
Why isnt the orange loosing agains the blue?
Thanks in advance.
patches-own
[bordear
abandono
reforestado
potrerizado
temperatura
humedad
dosel
]
breed [ potreros potrero ]
breed [ bordes borde ]
breed [ bosques bosque ]
to setup
resize-world 0 90 0 60
clear-all
ask patches with [
pxcor <= 30 and
pxcor >= min-pxcor and
pycor <= 60 and
pycor >= min-pycor ] [
set pcolor 35
set temperatura 26.5
set dosel 1
set humedad 90.4
]
;Potrero
ask patches with [
pxcor <= 60 and
pxcor >= 30 and
pycor <= 60 and
pycor >= min-pycor ] [
set pcolor 44
set temperatura 29
set dosel 84.3
set humedad 79.3;
]
;Borde
ask patches with [
pxcor <= 90 and
pxcor >= 60 and
pycor <= 60 and
pycor >= min-pycor ] [
set pcolor 66
set temperatura 26.3
set dosel 85.2
set humedad 94 ;
]
;Bosque
;se establece la forma de la rana
; Se establecen las condiciones de las ranas, tamaño color y lugar de aparicion. La energia sera igual en todas las ranas en el set up.
create-potreros 50
[ set size 3 ;; easier to see
set color yellow
setxy random xcor random ycor
move-to one-of patches with [pcolor = 35]
set heading random 45 + 45
set energia 50
] ;; red = not carrying food
;Potrero
reset-ticks
end
to go
ask potreros [
if energia < 50 [descansar-potrero]
if energia >= 50 and energia <= 80 [move-potrero]
if energia > 80 [ if ticks mod 50 = 0 [reproducirse]]
set heading random 270
fd 1
set energia energia - 2
morir]
ask patches [ restauracion ]
ask patches [ deforestacion ]
ask patches [abandonacion]
ask patches [borderizacion]
if ticks >= 500 [stop]
tick
end
to restauracion
if pcolor = 44 or pcolor = 25 or pcolor = 35[
set reforestado count neighbors with [pcolor = 66 or pcolor = 75] ]
if reforestado >= 3 [if random 100 <= 100 [
set pcolor 75
set temperatura 24.4 + random 3
set humedad 91 + random 9
set dosel 82 + random 5 ]
]
end
to deforestacion
if pcolor = 44 or pcolor = 25 or pcolor = 75 or pcolor = 66 [
set potrerizado count neighbors with [pcolor = 35] ]
if potrerizado >= 3 [if random 100 <= 0 [
set pcolor 35
set temperatura 22.7 + random 5
set humedad 84.5 + random 10
set dosel 0 + random 10]
]
end
to abandonacion
if pcolor = 35 [
set abandono count neighbors with [pcolor = 44 or pcolor = 25] ]
if abandono >= 3 [if random 100 <= 50 [
set pcolor 25
set temperatura 24.6 + random 8
set humedad 68 + random 14
set dosel 79 + random 9]
]
end
to borderizacion
if pcolor = 66 [
set bordear count neighbors with [pcolor = 44] ]
if bordear >= 3 [if random 100 <= 50 [
set temperatura 24 + random 6
set humedad 80 + random 20
set dosel 80 + random 5]
]
end
to move-potrero
ask neighbors in-radius 2
[if temperatura >= 28.6 or temperatura <= 22.6 or dosel >= 5 or humedad <= 84
[ set pcolor red
;facexy random 30 random 60
;fd 5
;set energia energia - 10]]
;[ask potreros [descansar-potrero]]
]]
end
to descansar-potrero
ifelse pcolor = 35 [
set energia energia + 6]
[set energia energia + 1]
end
to reproducirse
if energia > 80 [ if random 100 > 60 [set energia energia - 70
hatch 1 [ rt random-float 360 fd 2
set energia energia / 3]]]
end
to morir
if energia <= 1 [die]
end ```
Right now, once your patches set abandono to some value in to abandonacion, they never reset it unless their pcolor is returned to 35. So, you have all patches evaluating the if abandono >=3... code. For a quick fix to see what I mean, change your to abandonacion chunk to:
to abandonacion
set abandono 0
if pcolor = 35 [
set abandono count neighbors with [pcolor = 44 or pcolor = 25] ]
if abandono >= 3 [if random 100 <= 50 [
set pcolor 25
set temperatura 24.6 + random 8
set humedad 68 + random 14
set dosel 79 + random 9]
]
end
The above simply resets the abandono to 0 right before evaluating the abandano count to ensure that all patches are starting "fresh." There are other ways to address this, but how you actually go about it may depend on if you are tracking the number of abandono patches every tick.
I have the following code which creates a grid of agents and place links between pairs if they are in a distance of interference.
breed [ readers reader ]
undirected-link-breed [ rris rri ]
globals [
interf-rri-radius
num-readers
distance-var-x
distance-var-y
readers-per-row
readers-per-column
num-checkouts
]
to setup
ca
setup-globals
ask patches [ set pcolor blue - 3 ]
spawn-by-row-col
reset-ticks
end
to setup-globals
set interf-rri-radius 1005
set num-readers 40
set distance-var-x 12
set distance-var-y 22
set readers-per-row 8
set readers-per-column 5
set num-checkouts 0
end
to spawn-by-row-col
let half-step 0.5 * distance-var-x
let d-vals ( range ( min-pxcor + half-step ) ( min-pxcor + (readers-per-row * distance-var-x)) distance-var-x )
let dc-vals ( range ( min-pxcor + half-step ) ( min-pycor + (readers-per-column * distance-var-y)) distance-var-y )
show dc-vals
; Create an empty list to build into
let possible-coords []
; For each possible vertical value, map all horizontal values in order and
; combine these into an ordered list starting at the lowest px and py coords
foreach dc-vals [
d ->
set possible-coords ( sentence possible-coords map [ i -> (list i d) ] d-vals )
]
show (word "possible-coords = " possible-coords)
; Use the number of readers to sublist the possible coordinates, and
; create a turtle at each of the coordinate combinations left.
let max-positions length possible-coords
if max-positions > (num-readers + num-checkouts) [ set max-positions (num-readers + num-checkouts) ]
let use-coords sublist possible-coords num-checkouts max-positions
foreach use-coords [
coords ->
create-readers 1 [
setxy item 0 coords item 1 coords
set shape "square 2"
set size 2
set color 15
]
]
ask readers [ create-rris-with other readers in-radius (interf-rri-radius / 10) ]
end
The neighbors of reader 0 are
show [sort [who] of rri-neighbors] of reader 0
[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39]
However, the distance between reader 0 and reader 39 is
show [distance reader 0] of reader 39
121.6552506059644
and the connection radius is 1005/10= 100.5 so they should not be connected with an rri link.
Notice, I use a origin-centered world of -50 to 50 in X and Y dimensions.
I have tested the code in previous Netlogo version 6.0.4 and the reader 39 is not a neighbour of the reader 0.
I do not know what can be the problem. I believe is the new version but I would like to be sure.
In case anyone else runs into this, there is a confirmed bug with in-radius in the NetLogo 6.1.0 release, and the details are in the bug report on GitHub.
The issue only affects in-radius when used in a non-wrapped world, and only when used on turtles, and only when used with a radius that's a significant % of the world width. If you're using a world with wrapping, or using in-radius with patches, or using a small radius relative to the world size, your data will be correct and you don't need the below workaround.
As a workaround if your model is affected, you can use a simple user-defined NetLogo procedure in your model until the fix is published. It's not going to be super-fast if your having turtles calculate in-radius many times per tick, but if you have few turtles or are just using it during setup, it should be okay:
to-report temp-in-radius [agentset r]
report agentset with [ distance myself <= r ]
end
Then, instead of create-rris-with other readers in-radius (interf-rri-radius / 10) you would do create-rris-with (temp-in-radius other readers (interf-rri-radius / 10)).
Or more generally, instead of something like count other turtles in-radius 5 would become count temp-in-radius (other turtles) 5.
I am creating an patient-surgeon-operation bed model, wherein I need to show surgeons lined up on the left side of patch awaiting to enter operation room in the center and the patients awaiting in the queue from the right side.
I want the surgeons and patients to be located on the patch as per their who number
S1 S2 S3 --> Operation room < -- P1 P2 P3
I use the below query, I am not sure where to incorporate the who number
to lineup-patients
LET gapp 10
LET directions
[45 90 230 180 45 90 230 180 45 90 45 90 230 180 45 90 230 180 45 90 45 90 ]
LET jj 0 ; counter / index
REPEAT initial-number-patients
[ create-PATIENTS 1
[ SETXY (0 + jj * gapp) 20
set shape "person"
SET size 1.2
SET label who
SET label-color black
SET heading item jj directions
]
SET jj jj + 1
ASK patients [
MOVE-TO ONE-OF PATCHES WITH [ PCOLOR = yellow ]
] ]
END
You have a move-to after you line them up. And it always moves all existing patients. To keep things cleaner, write a separate lineup proc.
to lineup [#patients #patch #gap]
let _x ([pxcor] of #patch)
let _y ([pycor] of #patch)
let _xqs n-values (count #patients) [[n] -> _x + n * #gap]
(foreach sort #patients _xqs [
[p x] -> ask p [setxy x _y]
])
end
You can test this with a new instance of NetLogo as follows:
to test
ca
crt 20
lineup turtles one-of patches 0.5
end