setting variable in netlogo ifelse function and making turtle to wait in the patch - countdowntimer

I have the following in netlogo
ask m-depts [ ; this check the values of turtles against some set numbers
ifelse
(m- k >= 5) and (m-t >= 3) and (m-c >= 3 ) and (m-s >= 4) and (m-b >= 3) and (m-d >= 0) [
move-to one-of patches with [pcolor = yellow]
] [
ifelse
(m-k >= 5) and (m-t >= 5) and (m-c >= 5 ) and (m-s >= 5) and (m-b >= 3) and (m-d >= 1) [
move-to one-of patches with [pcolor = green]
] [
action-m-depts
] ]]]
]
1st I want to add some conditions (set ……… ) if the turtle will move to yellow and also some other condition if it will move to green eg
If pcolor = yellow [
set m-k m-k + 0.5
set m-t m-t + 1
] ;
If pcolor = green [
set m-k m-k + 0.8
set m-t m-t + 2
] ; etc otherwise do action-m-depts (defined elsewhere)
2nd For the next move (ticks) I want to fix the turtle to wait at the patch eg (for yellow patch, wait for 5 years (5 ticks) and for green patch wait for 2 year (2 ticks). How should I incorporate the two issues in this model?

The code you have for the first part looks good to me. Have you tried it? I say go for it!
The second part is very similar to netlogo: how to make turtles stop for a set number of ticks then continue and Making turtles wait x number of ticks — check out those answers.

Related

Farmer require to change value after discrete time/ ticks in NetLogo

I am writing a model where farmers update profits seasonally and cultivate crops accordingly. In total there will 630 ticks in a year's time period.There are two seasons. First season will take 252 ticks to complete a crop cycle. And other season will take next 378 ticks to complete a crop cycle. For both seasons farmer will calculate her profits. Initially after 252 ticks and then reset some of the variables to start for next season and calculate profits for next 378 ticks. This will go on simultaneously and run for the period of time let's say 10 years or more. Is there anyone who can help in writing the codes.
Below codes are given.
Problem: Ticks mod command will overcalculate or under calculate profits for every season. I want to calculate profits for season 1 from 0 to 252 ticks for season2 from 253 ticks to 630. and again for season1 from 631 to 882 and again for sesson2 till 1260 ticks. adn this will continue.
breed [farmers farmer]
farmers-own [water irrigation-turn]
to setup
clear-all
create-farmers 5
[ set label who
set size 2
]
ask farmer 0 [ setxy min-pxcor + 1 0 set irrigation-turn 0]
ask farmer 1 [ setxy min-pxcor + 1 2 set irrigation-turn 1]
ask farmer 2 [ setxy min-pxcor + 1 4 set irrigation-turn 2]
ask farmer 3 [ setxy min-pxcor + 3 0 set irrigation-turn 3]
ask farmer 4[ setxy min-pxcor + 3 2 set irrigation-turn 4]
ask farmers [ set label who
set size 2]
reset-ticks
end
to go
irrigate
update-profits
tick
end
to irrigate
ask turtles with [irrigation-turn = (ticks mod count farmers)]
[ set color red]
ask turtles with [ irrigation-turn != (ticks mod count farmers)]
[set color blue]
end
to update-profits
if ticks mod 630 = 252 [set-season1profit]
if ticks mod 630 = 378 [ set-season2profits]
end

Releasing two turtles per tick netlogo

I'm currently working on a model where I could simulated pedestrian movement while shopping. So, I have already figure some of things I need but I have been trying to figure out how to release 2 turtles per tick (like a pair) in a certain patch. Both turtles are release at the same time. My code is base from ant lines but it just releases all the num-of-pedestrians in one time and then the turtles start walking. the ticks also start after all turtles are release. I want the turtles to start "walking" as they enter.
Here is my code:
breed [ leadvisitors leadvisitor ]
breed [ visitors visitor ]
to setup
clear-all
setup-visitors
reset-ticks
end
to setup-visitors
create-leadvisitors num-of-pedestrians * 0.1 ;;create 10% of the total number of pedestrians
[
set demand-type "none"
set color black
set size 1
setxy 0 16
set heading 180
set pen-size 1
set destination one-of patches
set wait-time -1
set demand-lvl 0
]
create-visitors (num-of-pedestrians - (num-of-pedestrians * 0.1))
[ set demand-type 0
set size 1
setxy 0 16
set heading 180
set pen-size 1
set destination one-of patches
set wait-time -1
set demand-lvl 1
set attracted? false
] ]
end
to go
if turtles = 0 [ stop ]
ask turtles
[
set-demand-type
have-demand
]
if ticks > 100 [ stop ]
tick
display-labels
end
;;;;; visitor's internal state of demand ;;;;;
to set-demand-type
if demand-type = 0
[ set demand-type "food"
set color red
let target (patches in-cone visitor-vision-depth visitor-view-angle) with [pcolor = red]
]
end
to have-demand
if demand-lvl = 0
[
ifelse wait-time = -1
[ stroll ]
[ set wait-time wait-time - 1
if wait-time = 0
[ stroll ]]
]
if demand-lvl = 1
[ ifelse wait-time = -1
[ stroll
evaluate ]
[ set wait-time wait-time - 1
if wait-time = 0
[ stroll
evaluate ]]
]
end
to stroll
if any? neighbors with [ pcolor = gray - 3 ]
[ die ]
ifelse any? neighbors with [ pcolor = gray or pcolor = orange or pcolor = blue or pcolor = red]
[ facexy exitpt-x exitpt-y ]
[ rt random-float visitor-view-angle lt random-float visitor-view-angle ]
fd walking-speed
end
to evaluate
if any? neighbors with [ pcolor = gray or pcolor = orange or pcolor = blue or pcolor = gray - 4 or pcolor = gray - 3 or pcolor = red + 2]
[ facexy exitpt-x exitpt-y
rt random-float visitor-view-angle lt random-float visitor-view-angle
fd walking-speed ]
let _mycolor color
if any? (patches in-cone visitor-vision-depth visitor-view-angle) with [pcolor = _mycolor]
[ let new-target max-one-of ( patches with [pcolor = _mycolor] in-cone visitor-vision-depth visitor-view-angle) [patch-influence]
let dist-to-new-target min-one-of (patches with [pcolor = _mycolor] in-cone visitor-vision-depth visitor-view-angle) [distance myself]
face new-target
set heading towards new-target
fd walking-speed
set attracted? true
attracted-and-visiting
re-evaluate
]
end
to attracted-and-visiting
if pcolor = red + 2
[ set heading towards one-of patches with [pcolor = red + 2]
fd 0
set patch-popularity patch-popularity + 1
set wait-time avg-waiting-time
if count turtles-here > 0
[ set num-of-visitors num-of-visitors + 1 ]
set plabel num-of-visitors
]
end
to re-evaluate
let _mycolor color
if not any? (patches in-cone visitor-vision-depth visitor-view-angle) with [pcolor = _mycolor]
[ set attracted? false ]
ifelse choose? [set heading towards one-of patches with [pcolor = _mycolor]] [facexy exitpt-x exitpt-y]
end
to-report choose?
report random 2 = 0
end
As a result of our back-and-forth comments, let me take a stab at what you are looking for. I assume that you want to create leadvisitors at the beginning of the run and then add two visitors per tick as the run progresses. If so, then your setup procedure would look something like
globals [num-visitors-created]
to setup
clear-all
set num-visitors-created 0
create-leadvisitors num-of-pedestrians * 0.1 [
set demand-type "none"
set color black
set size 1
setxy 0 16
set heading 180
set pen-size 1
set destination one-of patches
set wait-time -1
set demand-lvl 0
set num-visitors-created num-visitors-created + 1
]
reset-ticks
end
creating your leadvisitors, but no visitors. Note that the global variable num-visitors-created keeps track of the number of leadvisitors and visitors created, being incremented by one each time a leadvisitor or visitor is created.
In your go procedure, you would then create two visitors each tick until the total number of visitors and leadvisitors created reaches num-of-pedestrians. (If there is room for only one new visitor, would you want just one to be created, or do they need to be created in pairs? I'm assuming the latter.) Since you don't want the death of a visitor to open up space for a new one, we test the number created, not the number still alive.
to go
if turtles = 0 [ stop ]
; create a new pair of visitors if there is room.
if num-visitors-created <= (num-of-pedestrians - 2)
create-visitors 2 [
set demand-type 0
set size 1
set heading 180
set pen-size 1
setxy 0 16
set destination one-of patches
set wait-time -1
set demand-lvl 1
set attracted? false
set num-visitors-created num-visitors-created + 1
]
ask turtles
[
set-demand-type
have-demand
]
if ticks > 100 [ stop ]
tick
display-labels
end
(Since it appears that leadvisitors may die as well, do you want to keep a certain minimum number or proportion of leadvisitors? If so, you should open up a new question.)
The code above will create the pair of new visitors on patch 0 16 at each tick. If, however, you want to have the new visitors created on a different patch, say one of the red ones, you could in your go procedure have that patch sprout the new visitors.
ask one-of patches with [pcolor = red] [
sprout-visitors 2 [
set demand-type 0
set size 1
set heading 180
set pen-size 1
set destination one-of patches
set wait-time -1
set demand-lvl 1
set attracted? false
set num-visitors-created num-visitors-created + 1
]
]
Note that here the xy coordinates of the new visitors are not set, so they start on the patch that sprouted them.

how do I place my turtles within a square of 5 x 5 patches and 10 x 10 patches?

I am trying to randomly place my turtles within a square of 5 by 5 patches, I have 2 questions as below:
Is below code correct?
setxy (50 + random 5) (60 + random 5)
How do I make a 10 x 10 patch square?
Your code would place the turtles running it in a 5 X 5 square centered on patches with the bottom-left corner on patch 50 60.
If you want it to be 10 x 10
setxy (50 + random 10) (60 + random 10)
if you want them not to be have to centered on patches use random-float
thus. The patch center coordinates are integers.
setxy (50 + random-float 5) (60 + random-float 5)
If your world is not big enough they will wrap around.

NetLogo: setup-patches created after Tools -> Halt is used?

I my model I have several version of how my world should look like. I implemented this as "chooser" including choices:
"single_tree"
"clustered". My world is 501 * 501 patches.
When I run setup of my both variations, they are not created until I press Tools -> "Halt".
I don't really understand why because on my working model this works fine. Also when I run this "world variations" with basic paramaters - just with [set pcolor ...].
Please what can be bad in my model or what am I doing wrong?
here is the working example - works fine:
to setup-patches ; define patchy landscape
ask patches [
; Single tree
; -------------------------
if world = "single_tree" [
set pcolor green
]
; Clustered trees
; -------------------------
if world = "clustered" [
set pcolor red
]
end
here is little bit more complicated code but I don't see any reason for taking so long time for dispaying..
to setup-patches ; define patchy landscape
ask patches [
; Single tree
; -------------------------
if world = "single_tree" [
ask patches with [pxcor mod 50 = 0 and pycor mod 50 = 0] [
set pcolor red
]
]
; Clustered trees
; -------------------------
if world = "clustered" [
ask patch 0 0 [
ask patches in-radius (2.99 * Grid) with [pxcor mod Grid = 0 and pycor mod Grid = 0] [
set pcolor red
]
]
; ; determine cluster size
ask patches with [pcolor = red] [
ask patches in-radius radius [
set pcolor yellow
]
]
]
end
I really appreciate any suggestions and thank you a lot !
My problem was that I used too many times "ask patches" to "ask patches" to do something...
the fixed code is here - with ask patches only once per if statement:
to setup-patches
if world = "single_tree" [
ask patches with [pxcor mod 50 = 0 and pycor mod 50 = 0] [
set pcolor red
]
]
if world = "clustered" [
ask patch 0 0 [ ask patches in-radius (2.99 * Grid) with [pxcor mod Grid = 0 and pycor mod Grid = 0] [
set pcolor red
]
]
ask patches with [pcolor = red] [
ask patches in-radius 5 [
set pcolor yellow
]
]
]
end

Netlogo, creating obstacle avoidance algorithm

I am simulating pedestrian motion in NetLogo, and am having trouble creating an obstacle avoidance algorithm from scratch. There are algorithms online but they are not suited for moving obstacles (other pedestrians). In addition, my agents are moving from their spawnpoint (point A) to their goal (point B).
This is my NetLogo algorithm:
globals [ wall walkway center dest ]
turtles-own [ gender goal velocity spawnpoint mid turn ]
to setup
clear-all
ask patches[
set wall patches with [
(pxcor > 3 and pycor > 3) or
(pxcor < -3 and pycor > 3) or
(pxcor < -3 and pycor < -3) or
(pxcor > 3 and pycor < -3)
]
set walkway patches with [
(pxcor > -4 and pxcor < 4) or
(pycor > -4 and pycor < 4)
]
set center patch 0 0
]
ask patches [
set pcolor black
]
ask walkway [
set pcolor 9
]
crt population [
set velocity 0.1
set mid 0
set gender random 2
if gender = 0 [set color red]
if gender = 1 [set color blue]
set spawnpoint random 4
if spawnpoint = 0 [ move-to one-of walkway with [not any? turtles-here and (pxcor < -11)]]
if spawnpoint = 1 [ move-to one-of walkway with [not any? turtles-here and (pycor > 11)]]
if spawnpoint = 2 [ move-to one-of walkway with [not any? turtles-here and (pxcor > 11)]]
if spawnpoint = 3 [ move-to one-of walkway with [not any? turtles-here and (pycor < -11)]]
set goal random 4
while [ goal = spawnpoint ] [ set goal random 4 ]
if spawnpoint != 0 and goal = 0 [set goal patch -16 0]
if spawnpoint != 1 and goal = 1 [set goal patch 0 16]
if spawnpoint != 2 and goal = 2 [set goal patch 16 0]
if spawnpoint != 3 and goal = 3 [set goal patch 0 -16]
]
reset-ticks
end
to decelerate
ifelse velocity > 0.01
[ set velocity velocity - 0.01 ]
[ rt 5 ]
end
to accelerate
if velocity < 0.1
[ set velocity velocity + 0.01 ]
end
to go
ask turtles [
ifelse patch-here != goal[
set turn random 2
if distance center < 3 [ set mid 1]
if mid = 0 [ set dest center ]
if mid = 1 [ set dest goal ]
face dest
ifelse any? other turtles-on patches in-cone 1.5 60
[ if any? other turtles-on patches in-cone 1.5 60
[ bk velocity
rt 90 ] ]
[ accelerate
face dest
fd velocity ]
]
[ die ]
]
end
The simulated environment of this simulation is an intersection:
http://imgur.com/nQzhA7g,R5ZYJrp#0
(sorry, I need 10 rep to post images :( )
Image 1 shows the state of the environment after setup. Image 2 shows what happens after the agents move to their goal (goal != their spawnpoint). The agents facing the different directions shows the agents which made its way past the clutter of agents in the center and are now on the way to their goal. The agents in the center, however, are stuck there because of my algorithm. The simulation is more problematic when there are more number of agents, which means they will just clutter in the center of the environment and just stutter when moving.
I based my algorithm on http://files.bookboon.com/ai/Vision-Cone-Example-2.html . Forgive my algorithm, I started programming in NetLogo a week ago and until now I still don't have the proper mindset in programming in it. I'm sure there's a better way to implement what I have in mind, but alas I am frustrated upon trying many implementations that came to my mind (but never got close to the real thing).
P.S: This is my first post/question in StackOverflow! I hope my question (and my way of asking) isn't bad.
Here is the simplest working version I could come up with:
turtles-own [ goal dest velocity ]
to setup
clear-all
let walkway-color white - 1
ask patches [
set pcolor ifelse-value (abs pxcor < 4 or abs pycor < 4) [ walkway-color ] [ black ]
]
let goals (patch-set patch -16 0 patch 0 16 patch 16 0 patch 0 -16)
ask n-of population patches with [ pcolor = walkway-color and distance patch 0 0 > 10 ] [
sprout 1 [
set velocity 0.1
set color one-of [ red blue ] ; representing gender
set dest patch 0 0 ; first head towards center
set goal one-of goals with [ distance myself > 10 ]
]
]
reset-ticks
end
to go
ask turtles [
if patch-here = goal [ die ] ; the rest will not execute
if dest = patch 0 0 and distance patch 0 0 < 3 [ set dest goal ]
face dest
if-else any? other turtles in-cone 1.5 60
[ rt 5
bk velocity ]
[ fd velocity ]
]
tick
end
Aside from the fact that I completely rewrote your setup procedure, it's not that different from your own version. I think your main problem was backing before turning: since you face dest again at the beginning of the next go cycle, your rt was basically useless.

Resources