Netlogo. Update an history list when turtles birth and - netlogo

I will be very grateful if someone could give me any kind of advice about a problem that I have in my code. I am working on a model with four kind of agents. Each type of agents uses a different strategy. These strategies are Prisoner Dilemma variations. Two of these strategies (tit for tat and unforgiving) need to record (in a list) who was the partner of interaction, and depending if that partner defected or not, will be the behave (cooperative or defect) of the agent in the next encounter with the same partner. Agents move randomly through the world. The problem come when agents die or birth according of their resources level. At this point these strategies that require to record who was the previous partner and how was his behave (cooperative or defect) do not work. This is the message that show up.
I am really appreciate any support!!!!.
number of initial turtles is 42, but two ticks later the number is 52
Below is a code that reproduces the error. In order to run it, it is necessary to create the sliders: n-tit-for-tat-com; n-unforgiving-com; inc-ecological-resources; energy-consume-companies:
globals
[
;;Number of companies with each strategy¨
num-tit-for-tat-com
num-unforgiving-com
;;number of interactions by each strategy
num-tit-for-tat-com-games
num-unforgiving-com-games
;; Total score of all companies playing each strategy
tit-for-tat-com-score
unforgiving-com-score
; Total companies en each ticks
num-companies
]
breed [companies company]
;;create companies variables
companies-own [
score-com
score-com-round
strategy-com
defect-now?-com
partner-defected?-com ;;action of the partner
partnered?-com ;;am I partnered?
partner-com ;;WHO of my partner (nobody if not partnered)
partner-history-com ;;a list containing information about past interactions with other turtles (indexed by WHO values)
earnings-tit-for-tat-Com
earnings-unforgiving-Com
earnings-com
tit-for-tat-com-score-round
unforgiving-com-score-round
]
patches-own
[
resources-amount ;; resources amount in each patches
resources-to ;; aux variable to count resources taken
]
;;;Setup Procedures;;;
to setup
clear-all
setup-patches
setup-companies ;;setup the turtles and distribute them randomly
reset-ticks
end
; Setup patches
;===============
to setup-patches
ask patches [set resources-amount random 4] ;; patches represent ecological system. Resources are distributed randomly. [0,1,2 or 3] 0 -> non resources; 3 -> plenty of resources
ask patches [
set pcolor (ifelse-value
resources-amount = 0 [49]
resources-amount = 1 [58]
resources-amount = 2 [67]
resources-amount = 3 [65]
[63] ;; this case never happen. Because 4 means integer number in range [0 to 3]
)]
end
to setup-companies
make-companies ;;create the appropriate number of turtles playing each strategy
setup-common-variables-com ;;sets the variables that all turtles share
end
;;create the appropriate number of turtles playing each strategy
to make-companies
create-companies n-tit-for-tat-com
[ set strategy-com "tit-for-tat-com"
set color 135
set earnings-tit-for-tat-Com random 4 + 1 ;;randomly initial resources in the range [ 1 - 4 ]. It is sum + 1 to enforce never inicial resource = 0
]
create-companies n-unforgiving-com
[ set strategy-com "unforgiving-com"
set color 4
set earnings-unforgiving-Com random 4 + 1 ;;randomly initial resources in the range [ 1 - 4 ]. It is sum + 1 to enforce never inicial resource = 0
]
end
; Setup common variables
;¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨
;;set the variables that all turtles share
to setup-common-variables-com
ask companies [
set shape "pentagon"
set size 1.3
set tit-for-tat-com-score-round 0
set unforgiving-com-score-round 0
set score-com 0
set partnered?-com false
set partner-com nobody
setxy random-xcor random-ycor
]
end
;;;Runtime Procedures;;;
to go
ask companies [birth-die-com]
store-initial-companies-counts ;;record the number of turtles for each strategy
setup-history-lists-com
clear-last-round
ask companies [ partner-up-com ] ;;have turtles try to find a partner
let partnered-com companies with [ partnered?-com ]
ask partnered-com [ select-strategy-com ] ;;all partnered turtles select action
ask partnered-com [ play-a-round-com ]
adjust-patches
tick
end
;Birth-die-com ; According to the level of resources companies can die or birth
;======================
to birth-die-com
(ifelse
strategy-com = "tit-for-tat-com"
[
set earnings-com earnings-tit-for-tat-com
(if earnings-com > 2 * energy-consume-companies [
hatch 1 [set earnings-tit-for-tat-com random 2 + 1 ] ;;randomly initial resources in the range [ 1 - 2 ]. It is sum + 1 to enforce never inicial resource = 0
set earnings-com (earnings-com - energy-consume-companies )])
( if earnings-com < energy-consume-companies [ die ])
]
strategy-com = "unforgiving-com"
[
set earnings-com earnings-unforgiving-com
(if earnings-com > 2 * energy-consume-companies [
hatch 1 [set earnings-unforgiving-com random 2 + 1 ] ;;randomly initial resources in the range [ 1 - 2 ]. It is sum + 1 to enforce never inicial resource = 0
set earnings-com (earnings-com - energy-consume-companies )])
( if earnings-com < energy-consume-companies [ die ])
]
)
end
;;STORE INITIAL COMPANIES¨
;===========================
;;record the number of turtles at the begining of each tick
;;The number of turtles of each strategy is used when calculating average payoffs.
to store-initial-companies-counts
set num-tit-for-tat-com (count companies with [strategy-com = "tit-for-tat-com"])
set num-unforgiving-com (count companies with [strategy-com = "unforgiving-com"])
end
;Setup-History-Lists-com;; initialize PARTNER-HISTORY list in all turtles
;========================
to setup-history-lists-com
set num-companies (count companies)
let default-history-com [] ;;initialize the DEFAULT-HISTORY variable to be a list
;create a list with NUM-TURTLE elements for storing partner histories
repeat (count companies) [ set default-history-com (fput false default-history-com) ]
;give each turtle a copy of this list for tracking partner histories
ask companies [ set partner-history-com default-history-com ]
end
;Clear-last-round
;================
to clear-last-round
let partnered-com companies with [ partnered?-com ]
ask partnered-com [ release-partners-com ]
end
; release partner between companies and turn around
;¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨
to release-partners-com
set partnered?-com false
set partner-com nobody
rt 180
set label ""
end
;;have turtles try to find a partner
;;Since other turtles that have already executed partner-up may have
;;caused the turtle executing partner-up to be partnered,
;;a check is needed to make sure the calling turtle isn't partnered.
;;partner-up. Find partner
;===========================
to partner-up-com ;;turtle procedure
if (not partnered?-com) [ ;;make sure still not partnered
rt (random-float 90 - random-float 90) fd 1 ;;move around randomly
set partner-com one-of other (companies in-radius 1 ) with [ not partnered?-com ]
if partner-com != nobody [ ;;if successful grabbing a partner, partner up
set partnered?-com true
set heading 270 ;;face partner
ask partner-com [
set partnered?-com true
set partner-com myself
set heading 90
]
]
]
end
;Select stratey company
;¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨
;;choose an action based upon the strategy being played
to select-strategy-com ;;turtle procedure
if strategy-com = "tit-for-tat-com" [ tit-for-tat-com ]
if strategy-com = "unforgiving-com" [ unforgiving-com ]
end
;Play a round-companies
;¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨
to play-a-round-com ;;turtle procedure
get-payoff-com ;;calculate the payoff for this round
update-history-com ;;store the results for next time
end
; get-payoff-com
;¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨
;;calculate the payoff for this round and
;;display a label with that payoff.
;; Strategy-companies
;Turtles l l
;Action l C l D
;__________l________ l__________
; C l 3 l -3
;----------l---------l----------
; D l 5 l -2
to get-payoff-com
set partner-defected?-com [defect-now?-com] of partner-com
ifelse partner-defected?-com [
ifelse defect-now?-com
[ set score-com (score-com + 1 ) set label 1 set score-com-round 1 ]
[set score-com (score-com + 0) set label 0 set score-com-round 0]
]
[ifelse defect-now?-com
[set score-com (score-com + 5) set label 5 set score-com-round 5]
[set score-com (score-com + 3) set label 3 set score-com-round 3]
]
end
;Update-history
;¨¨¨¨¨¨¨¨¨¨¨¨¨¨
;;update PARTNER-HISTORY based upon the strategy being played
to update-history-com
if strategy-com = "tit-for-tat-com" [ tit-for-tat-com-history-update ]
if strategy-com = "unforgiving-com" [ unforgiving-com-history-update ]
end
;;;Strategies;;;
; Tit-for-tat-com
;¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨
to tit-for-tat-com
set num-tit-for-tat-com-games num-tit-for-tat-com-games + 1
set partner-defected?-com item ([who] of partner-com) partner-history-com
ifelse (partner-defected?-com)
[set defect-now?-com true]
[set defect-now?-com false]
ask companies in-radius 1 [
ask patches in-radius 1 [set resources-to (resources-amount * 1)]
ask patches in-radius 1 [set resources-amount (resources-amount - resources-to)]
set tit-for-tat-com-score-round ( tit-for-tat-com-score-round + score-com-round )
set earnings-tit-for-tat-com (earnings-tit-for-tat-Com + ((sum [ resources-to ] of patches in-radius 1)/ 2) + tit-for-tat-com-score-round - energy-consume-companies )
ask patches in-radius 1 [set resources-to 0 ]
set tit-for-tat-com-score-round 0
]
end
;Tit-for-tat-com-history-update¨
;¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨
to tit-for-tat-com-history-update
set partner-history-com
(replace-item ([who] of partner-com) partner-history-com partner-defected?-com)
end
;unforgiving-com ; Works similar to tit-for-tat, but once the another defect, always defect
;¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨
to unforgiving-com
set num-unforgiving-com-games num-unforgiving-com-games + 1
set partner-defected?-com item ([who] of partner-com) partner-history-com
ifelse (partner-defected?-com)
[set defect-now?-com true]
[set defect-now?-com false]
ask companies in-radius 1 [
ask patches in-radius 1 [set resources-to (resources-amount * 1)]
ask patches in-radius 1 [set resources-amount (resources-amount - resources-to)]
set unforgiving-com-score-round ( unforgiving-com-score-round + score-com-round )
set earnings-unforgiving-com (earnings-unforgiving-Com + ((sum [ resources-to ] of patches in-radius 1)/ 2) + unforgiving-com-score-round - energy-consume-companies)
ask patches in-radius 1 [set resources-to 0 ]
set unforgiving-com-score-round 0
]
end
;unforgiving-com-history-update
;¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨
to unforgiving-com-history-update
if partner-defected?-com [
set partner-history-com
(replace-item ([who] of partner-com) partner-history-com partner-defected?-com)
]
end
;; Adjusting Patches
;; Put limits
to adjust-patches
ask patches [ set resources-amount resources-amount + random-float Inc-Ecological-resources ] ;; Resources are renewable. They grow up again between [0-3). Use Slider
ask patches [ ;; Resources range [0-3)
set resources-amount (ifelse-value
resources-amount < 0 [0] ;;
resources-amount > 3 [3] ;;
[resources-amount]
)
]
;; Patches are colored according their level
ask patches [
set pcolor (ifelse-value
resources-amount < 0 [ black ]
resources-amount = 0 [ 38 ]
resources-amount > 0 and resources-amount <= 0.5 [ 49 ]
resources-amount > 0.5 and resources-amount <= 2 [ 67 ]
resources-amount > 2 and resources-amount <= 3 [ 65 ]
resources-amount > 3 and resources-amount <= 4 [ 63 ]
[ white ])
]
end

Related

Netlogo - code adds a tick and then stops

With this model I need the code for the first year (tick = 0) to be different to the remaining 4. I've run the code below and the first tick runs ok, it then ticks and stops - none of the tick = 1 code seems to be running.
globals [num_agents difference year leader_test ]
breed [tasks task]
breed [managers manager ]
tasks-own [requirement leadership matched ]
managers-own [ability wealth matched requirement task_leader]
to setup
clear-all
set num_years 5
set tolerance 5
set num_agents 100
create-tasks num_agents [
set shape "box"
set leadership one-of [10 20 30 40 50 60 70 80 90 100]
ifelse who < 50 [setxy 0 who set color blue][setxy 45 (who - 50) set color blue]
set heading 90
set requirement who + 100
set matched 0
]
create-managers num_agents [
setxy random 30 + 10 random 50
set shape "person" set color green set heading 270
set ability (who - num_agents + 100)
set wealth 0 set matched 0
]
reset-ticks
end
to go
;;first year -different to remaining
ifelse ticks < 1 [
ask managers with [matched = 0]
[show ticks
move-to one-of tasks with [matched = 0]
fd -1
set requirement [requirement] of one-of tasks-on patch-ahead 1
set task_leader [leadership] of one-of tasks-on patch-ahead 1
set difference abs(requirement - ability)
set matched 1
set wealth (requirement)
show wealth
show task_leader
ask tasks-on patch-ahead 1 [set matched 1 set shape "arrow" set heading 0]
if difference > tolerance [set color red ask tasks-on patch-ahead 1 [set shape "circle" ] ]]
]
; years 2 - num_years
[
ask managers [
if ability > (requirement + tolerance) [
ask tasks-on patch-ahead 1 [set matched 0 set shape "box" ]
setxy random 30 + 10 random 50
set shape "person" set color green set heading 270 set matched 0
]
]
ask managers with [matched = 1]
[ set leader_test random 100
if ability < (requirement - tolerance) [
if leader_test <= task_leader
[;;leader should make correct decision and fire manager
ask tasks-on patch-ahead 1 [set matched 0 set shape "butterfly" ]
setxy random 30 + 10 random 50
set shape "person" set color green set heading 270 set matched 0]
]
]
]
ask managers with [matched = 0]
[move-to one-of tasks with [matched = 0]
fd -1
set matched 1]
ask managers with [matched = 1][
set requirement [requirement] of one-of tasks-on patch-ahead 1
set task_leader [leadership] of one-of tasks-on patch-ahead 1
set difference abs(requirement - ability)
set wealth (wealth + requirement)
ask tasks-on patch-ahead 1 [set matched 1 set shape "arrow" set heading 0]
if difference > tolerance [set color red ask tasks-on patch-ahead 1 [set shape "circle" ]
]
]
ifelse ticks > (num_years ) [
stop] [tick ]
I have had problems with ticks and stop before - there is obviously something I'm not getting.
Per LeirsW
It runs just fine for me. You are using a forever button right?

Netlogo: How can I obtain the accumulate value in Netlogo?

I already confirmed the information on the link.
However I could not apply this.
The following is a part of the sample model program.
I wanted to accumulate the number of turtles, however I could not accumulate it with the sample program.
I probably need your advice. Thank you.
globals [num-turtles cumulative-sum average-time number-dead ]
turtles-own [count-up]
to setup
clear-all
set num-turtles 5
reset-ticks
end
to go
if count turtles < num-turtles
[ ask patch 0 0
[ sprout 1
[ set count-up 0 ]
]
]
set cumulative-sum cumulative-sum + 1 ;I would like to calculate the integral value here, but this syntax is not a cumulative value.
ask (turtles-on patch 0 0)
[
set cumulative-sum count turtles-here
]
set average-time ifelse-value (number-dead = 0)
[ 0 ][(cumulative-sum) / (number-dead)]
if (count turtles > 0) [
ask min-one-of turtles [who] [
if count-up >= 6 [
set number-dead number-dead + 1
die
]
]
]
ask (turtles-on patch 0 0)
[ set count-up count-up + 1
]
tick
end
that's much better, thanks- I can now run the code no problem. However, I still don't think I understand what you are wanting cumulative-sum to actually count. Are you just looking for the total number of turtles, including both those still alive and the ones that have died? If so, I think it's just a matter of moving your set cumulative-sum cumulative-sum + 1 line. For example:
EDIT:
Ok I think I understand now from your comment. Try this:
globals [num-turtles cumulative-sum average-time number-dead ]
turtles-own [count-up]
to setup
clear-all
set num-turtles 5
reset-ticks
end
to go
if count turtles < num-turtles [
ask patch 0 0 [
sprout 1 [
set count-up 0
]
]
]
if (count turtles > 0) [
ask min-one-of turtles [who] [
if count-up >= 6 [
set number-dead number-dead + 1
die
]
]
]
ask turtles-on patch 0 0 [
set count-up count-up + 1
]
set cumulative-sum cumulative-sum + count turtles
set average-time ifelse-value (number-dead = 0) [0] [(cumulative-sum) / (number-dead)]
tick
end

How do I translate my code from Netlogo to Netlogo Web? ('TO or TO-REPORT expected' error)

I'd like to use Netlogo Web to open my Netlogo (Desktop version) model, but it does not seem to work. I get a 'TO or TO-REPORT expected' error when I try to upload my model. Not sure what I'm doing wrong. I've inserted the code below. It is supposed to model a cellular process, in which cells divide, filaments of cells branch, and cells change shape as they grow older (procedures 'divide', 'branch', 'transform'). Help please?
to setup
clear-all
setup-turtles
reset-ticks
ask turtles [ set size 1 ]
end
to setup-turtles
create-threads 1
ask turtles [
setxy random-xcor random-ycor
set shape "line"
set color 65
]
end
turtles-own [age]
to go
add-age
divide
branch
transform
tick
ask turtles [ set size 1 ]
end
breed [threads thread]
breed [cylinders cylinder]
breed [circles circle]
to add-age
ask turtles [
set age age + 1
ifelse show-age?
[ set label age ]
[ set label "" ]
]
end
to divide
ask turtles [
if breed = threads or breed = cylinders [
ifelse random 100 < 50
[if (not any? other turtles-on patch-ahead 1) and
(not any? other turtles-on patch-right-and-ahead 10 1) and
(not any? other turtles-on patch-left-and-ahead 10 1)
[hatch 1
[let turn-degree (random(20) - 10)
rt turn-degree
fd 1
set age age - 1
]
]
]
[if (not any? other turtles-on patch-right-and-ahead 180 1) and
(not any? other turtles-on patch-right-and-ahead 170 1) and
(not any? other turtles-on patch-right-and-ahead 190 1)
[hatch 1
[let turn-degree (170 + random(20))
rt turn-degree
fd 1
set age age - 1
]
]
]
]]
end
to branch
ask circles [
if random 1000 < 2[
ifelse random 100 < 50
[if not any? other turtles-on patch-right-and-ahead 90 1
[hatch-threads 1
[right 90
fd 1
set age 0
set shape "line"]
]
]
[if not any? other turtles-on patch-left-and-ahead 90 1
[hatch-threads 1
[left 90
fd 1
set age 0
set shape "line"]
]
]
]]
end
to transform
ask threads[
if (age > 50) and (random 100 < 50)[
set breed cylinders
set shape "cylinder1"
]
]
ask cylinders[
if (age > 100) and (random 100 < 50) [
set breed circles
set shape "circle"
]
]
end
NetLogo Web is stricter about order of declaration (and so will be future versions of NetLogo Desktop).
Just move your turtles-own and breed statements to the top of your code and you should be all right.

Netlogo: simulation of cellular differentiation pattern?

I am trying to simulate the differentiation pattern of a simple organism. This is how I'd like my breeds and variables to work:
Breeds: vegetatives and heterocysts. Vegetatives can divide, heterocysts can't. Vegetatives can become heterocysts. Ideally, once a heterocyst is formed, the closer a vegetative is to it, the less likely it is for it to become a heterocyst in turn.
Variables:
age: + 1 per tick, - 1 for newly-hatched turtles
bump: A means by which to displace all the turtles located 'ahead' of a newly-hatched turtle, so they don't overlap. I imagined the system a bit like a Newton's Cradle (http://s.hswstatic.com/gif/newtons-cradle-1.jpg)
pro: promoter. Accumulates partially randomly. Once it reaches a certain value ('concentration'), a vegetative would change breed to become a heterocyst. Value decreased by inh.
proL: label for pro, with rounded values.
inh: inhibitor. Ideally this value should form a 'gradient' (highest in turtles near heterocysts, lowest further away).
The obvious problem that I can see is that I get a lot of contiguous heterocysts. Which is sort of what I've been trying to avoid. But I can't see what went wrong...Please help?
to setup
clear-all
setup-turtles
reset-ticks
ask turtles [ set size 1 ]
end
to setup-turtles
create-vegetatives 1
ask turtles [
setxy random-xcor random-ycor
set shape "circle"
set color 65]
end
to go
divide
add-age
move
differentiate
tick
end
turtles-own [age
bump
inh
pro
proL]
breed [vegetatives vegetative]
breed [heterocysts heterocyst]
to add-age
ask turtles [
set age age + 1
ifelse show-age?
[ set label age ]
[ set label "" ]
]
end
to divide
ask vegetatives [
if random 100 < 2 [
hatch 1[
set bump 1
set age age - 1
set inh 0
]
]]
end
;;Trying to get only one turtle per patch, making the others move
to move
ask turtles[
while [bump = 1] [
ifelse not any? turtles-on patch-right-and-ahead 180 1[
rt 180
fd 1
set bump 0
if any? other turtles-here[
ask other turtles-here
[set bump 1]
]
]
[fd 1
set bump 0
if any? other turtles-here[
ask other turtles-here[
set bump 1]
]
]
]]
end
to differentiate
ask turtles[
set pro (pro - inh + (random 2))
set proL round pro
ifelse show-proL?
[ set label proL ]
[ set label "" ]
create-links-with other turtles-on patch-ahead 1
create-links-with other turtles-on patch-right-and-ahead 180 1
if breed = vegetatives [
if any? link-neighbors[
ifelse any? link-neighbors with [breed = heterocysts]
[]
[set inh mean [inh] of link-neighbors]
]
if any? vegetatives with [pro > 50]
[ask vegetatives with [pro > 50]
[set breed heterocysts
set color brown
set shape "circle"
if any? link-neighbors[
ask link-neighbors with [breed != heterocysts]
[set inh 2]]
]]
]]
clear-links
end

Avoid visited patch netlogo

Sir i am working on a project and time to time i use to post questions here related to that problem. I have created a simulation scenario i which turtles (robots) move around in the space and also each robot keep track of its visited patches. On the movement if patch-ahead 1 is visited patch then i need to turn the robot to 45 degree left and check again for patch-ahead until it checks all 8 neighbors if any one of the 8 is UN-visited then it should move to that patch and continue its exploration. But if all 8 are visited then it should move to patch which is in front of current heading after checking the all 8 neighbors no matter it is visited.
Here is the piece of code i am using.
breed [robots robot ]
robots-own[ state memory ]
patches-own [ is-obstacle? ]
to setup
__clear-all-and-reset-ticks
create-robots num [
set memory (list patch-here)
]
draw-obstacles
ask patches [if pxcor = 0 and pycor = 0 [ set pcolor black ]]
end
to draw-obstacles
ask patches with [ pxcor mod 6 = 0 and pycor mod 6 = 0 ] [set pcolor red set is-obstacle? true]
; set pcolor red
ask patches [ ifelse pcolor = red [ set is-obstacle? true ][ set is-obstacle? false ] ]
ask patches with [ count neighbors != 8 ] [ set pcolor red set is-obstacle? true ]
end
to make-obstacle
if mouse-down?
[ ask-concurrent patches
[ if ((abs (pxcor - mouse-xcor)) < 1) and ((abs (pycor - mouse-ycor)) < 1)
[set pcolor red]]
]
end
to remove-obs
if mouse-down?
[ ask-concurrent patches
[ if ((abs (pxcor - mouse-xcor)) < 1) and ((abs (pycor - mouse-ycor)) < 1)
[set pcolor black]]
]
end
to go
ask patches [if ( pcolor = red )[set is-obstacle? true]]
ask patches [if ( pcolor = black )[set is-obstacle? false]]
ask-concurrent robots ; wanderers instructions
[
rt random-float rate-of-random-turn
lt (rate-of-random-turn / 2)
set-state
move-robots
]
tick
end
to move-robots ;;turtle proc
if (not member? state ["disperse" "explore"]) [
error "Unknown state"
]
if (state = "disperse") [
disperse
]
if (state = "explore") [
explore
]
end
to set-state ;;turtle proc
ifelse (any? other turtles in-radius 1) [
set state "disperse"
] [
set state "explore"
]
end
to disperse ;;turtle proc
avoid-obstacle
move1
; move-to one-of patch-set [neighbors] of neighbors
end
to explore
move
;search-open-room
avoid-obstacle
;move-to one-of neighbors
end
to move
fd speed
set memory lput patch-here memory
if ( (member? patch-ahead 1 memory) or ([is-obstacle?] of patch-ahead 1 ) )
[ lt random 45
]
end
to move1
fd speed
end
to avoid-obstacle
set memory lput patch-here memory
if ([is-obstacle?] of patch-ahead 1 )
[
ifelse [is-obstacle?] of patch-at-heading-and-distance (heading - 5) 1
[
ifelse [is-obstacle?] of patch-at-heading-and-distance (heading + 5) 1
[
ifelse random 1 = 0
[ rt 40 ]
[ lt 40 ]
]
[ rt 60 ]
]
[lt 60]
]
end
to search-open-room
ask robots[
ifelse ([is-obstacle?] of patches in-cone 2 150 )
[ rt 45 ] [ move ]
]
end
But in the move procedure i am just able to lt random 45. How to change it according to above mentioned scenario. I tried many with while loop and repeat statement but code does not seems to be working for me.
You could do it using while or repeat, but I think this a case where recursion works well.
The idea is to have a procedure that keeps on calling itself until the desired state is achieved:
to turn-until-free [ n ]
let target ifelse-value (patch-ahead 1 = patch-here)
[ patch-ahead 2 ]
[ patch-ahead 1 ]
let seen? member? target memory
let obstacle? [ is-obstacle? ] of target
if-else n < 8
[ if seen? or obstacle? [ lt 45 turn-until-free n + 1 ] ]
[ if obstacle? [ lt 45 turn-until-free n + 1 ] ]
end
The n parameter represents the number of calls that we've already made to the procedure (or, in other words, the number of neighbors that we've checked so far). When you call the procedure for the first time, you start with n = 0:
to move
turn-until-free 0
fd 1
set memory lput patch-here memory
ask patch-here [ set pcolor black + 2 ] ; just to show what's visited
end
A couple of things that were not part of your original specification:
It can happen that patch-ahead 1 is the same patch that the robot is already on. A patch has sides of length 1, but its diagonal is a bit longer (√2). So if a robot is in the bottom left corner, for example, and facing towards the top right, patch-ahead 1 = patch-here will be true. In these cases, we look a bit further and set the target to patch-ahead 2.
It can happen that, after checking all 8 neighbors, you end up facing an obstacle. If that's the case, you need to keep on turning until you're clear of the obstacle. And as a matter of fact, doing this takes care of obstacle avoidance nicely, and you might be able to get rid of the avoid-obstacle procedure in your code.
Edit:
Here is the code needed (in addition to the two procedures above) for a fully working example:
breed [ robots robot ]
robots-own [ memory ]
patches-own [ is-obstacle? ]
to setup
ca
ask patches [ set is-obstacle? false ]
ask patches with [ pxcor mod 6 = 0 and pycor mod 6 = 0 ] [
set is-obstacle? true
set pcolor red
]
ask n-of 5 patches with [ not is-obstacle? ] [
sprout-robots 1 [ set memory [] ]
]
reset-ticks
end
to go
ask robots [ move ]
tick
end
If you are having trouble with runtime errors or other unwanted behavior, I suggest you start from this and add back whatever else you had in your simulation one piece at a time. Then, you can see exactly where the problem comes from.
I also changed if-else n < 7 for if-else n < 8 in the turn-until-free procedure above. This way, the robot comes back to its original heading if all neighbors are explored instead of turning a bit right. This avoids going in circles once the whole territory is explored.