Netlogo-how to make a turtle turn at a specification? - netlogo

Please see the below Netlogo file. I want the turtles to turn right at WS3 coming from "buffer storage" and the turtles turn left at WS5 coming from "QC Buffer Storage". However, I am getting an error. I found the attached code online and do not understand it 100%. Could someone please help me?
Thank you so much,
John
globals [
IN-free-cabinet
IN-free-drum-a
IN-free-drum-b
new_directions
distance-to-jump
WM-output
WM-output-bad
drum-output
wip
mlt-aux
avg-mlt
]
patches-own[
directions
velocity
processing-time
is-processing?
malfunction
output
ID
bifurcation
]
breed [ machine ]
breed [ drum ]
machine-own [
birth-time
death-time
process-plan
processing
time-of-processing
QC-check
]
drum-own [
birth-time
process-plan
processing
time-of-processing
]
to create-WM [ machine-type ]
;********** Create Cabinet **********
set-default-shape machine "square"
ask patch -12 0 [
ifelse count machine-here = 0 [ set IN-free-cabinet TRUE ] [ set IN-free-cabinet FALSE ]
]
if IN-free-cabinet
[
create-machine 1 [
setxy -12 0
set size 1
set heading 90
set color Blue
set birth-time ticks
set process-plan ["WS1" "WS2" "WS3" "WS4" "WS5" "WS6" "OUT" ]
set processing FALSE
set QC-check "GOOD"
]
]
;********** Create Cabinet **********
;********** Create Drums **********
set-default-shape drum "drum"
ask patch -7 -3 [
ifelse count drum-here = 0 [ set IN-free-drum-a TRUE ] [ set IN-free-drum-a FALSE ]
]
ask patch -3 3 [
ifelse count drum-here = 0 [ set IN-free-drum-b TRUE ] [ set IN-free-drum-b FALSE ]
]
if IN-free-drum-a
[
create-drum 1 [
setxy -7 -3
set size 1
set heading 0
set color Pink
set birth-time ticks
set process-plan ["WS3"]
set processing FALSE
]
]
if IN-free-drum-b
[
create-drum 1 [
setxy -3 3
set size 1
set heading 180
set color Pink
set birth-time ticks
set process-plan ["WS5"]
set processing FALSE
]
]
;********** Create Drum **********
end
to setup
;; (for this model to work with NetLogo's new plotting features,
;; __clear-all-and-reset-ticks should be replaced with clear-all at
;; the beginning of your setup procedure and reset-ticks at the end
;; of the procedure.)
__clear-all-and-reset-ticks
set WM-output 0
set drum-output 0
set avg-mlt 0
set mlt-aux 0
set WM-output-bad 0
;********** Main line **********
ask patch -11 0 [
set pcolor White
set directions [-1 90 -1 -1]
set velocity 1
set processing-time WS1-Processing-Time
set is-processing? FALSE
set malfunction FALSE
set ID "WS1"
set bifurcation FALSE
]
ask patch -10 0 [
set pcolor Yellow
set directions [-1 90 -1 -1]
set velocity 1
set is-processing? FALSE
set malfunction FALSE
set bifurcation FALSE
]
ask patch -9 0 [
set pcolor White
set directions [-1 90 180 -1]
set velocity 1
set processing-time WS2-Processing-Time
set is-processing? FALSE
set malfunction FALSE
set ID "WS2"
set bifurcation FALSE
]
ask patch -8 0 [
set pcolor Yellow
set directions [-1 90 -1 -1]
set velocity 1
set is-processing? FALSE
set malfunction FALSE
set bifurcation FALSE
]
ask patch -7 0 [
set pcolor White
set directions [-1 90 -1 -1]
set velocity 1
set processing-time WS3-Processing-Time
set is-processing? FALSE
set malfunction FALSE
set ID "WS3"
set bifurcation FALSE
]
ask patch -6 0 [
set pcolor Yellow
set directions [-1 90 -1 -1]
set velocity 1
set is-processing? FALSE
set malfunction FALSE
set bifurcation FALSE
]
ask patch -5 0 [
set pcolor White
set directions [-1 90 -1 -1]
set velocity 1
set processing-time WS4-Processing-Time
set is-processing? FALSE
set malfunction FALSE
set ID "WS4"
set bifurcation FALSE
]
ask patch -4 0 [
set pcolor Yellow
set directions [-1 90 -1 -1]
set velocity 1
set is-processing? FALSE
set malfunction FALSE
set bifurcation FALSE
]
ask patch -3 0 [
set pcolor White
set directions [-1 90 -1 -1]
set velocity 1
set processing-time WS5-Processing-Time
set is-processing? FALSE
set malfunction FALSE
set ID "WS5"
set bifurcation FALSE
]
ask patch -2 0 [
set pcolor Yellow
set directions [-1 90 -1 -1]
set velocity 1
set is-processing? FALSE
set malfunction FALSE
set bifurcation FALSE
]
ask patch -1 0 [
set pcolor Yellow
set directions [-1 90 -1 -1]
set velocity 1
set processing-time 0
set is-processing? FALSE
set malfunction FALSE
set ID "WS6"
set bifurcation FALSE
]
ask patch 0 0 [
set pcolor Yellow
set directions [-1 90 180 -1]
set velocity 1
set is-processing? FALSE
set malfunction FALSE
set bifurcation TRUE
]
ask patch 1 0 [
set pcolor Yellow
set directions [-1 90 -1 -1]
set velocity 1
set is-processing? FALSE
set malfunction FALSE
set bifurcation FALSE
]
ask patch 2 0 [
set pcolor Yellow
set directions [-1 90 -1 -1]
set velocity 1
set processing-time 0
set is-processing? FALSE
set malfunction FALSE
set ID "WS7"
set bifurcation FALSE
]
ask patch 3 0 [
set pcolor Yellow
set directions [-1 90 -1 -1]
set velocity 1
set is-processing? FALSE
set malfunction FALSE
set bifurcation FALSE
]
ask patch 4 0 [
set pcolor Yellow
set directions [-1 90 -1 -1]
set velocity 1
set is-processing? FALSE
set malfunction FALSE
set bifurcation FALSE
]
ask patch 5 0 [
set pcolor Yellow
set directions [-1 90 -1 -1]
set velocity 1
set processing-time 0
set is-processing? FALSE
set malfunction FALSE
set ID "WS8"
set bifurcation FALSE
]
ask patch 6 0 [
set pcolor Yellow
set directions [-1 90 -1 -1]
set velocity 1
set is-processing? FALSE
set malfunction FALSE
set bifurcation FALSE
]
ask patch 7 0 [
set pcolor Yellow
set directions [-1 90 -1 -1]
set velocity 1
set processing-time 0
set is-processing? FALSE
set malfunction FALSE
set ID "WS9"
set bifurcation FALSE
]
ask patch 8 0 [
set pcolor Yellow
set directions [-1 90 -1 -1]
set velocity 1
set is-processing? FALSE
set malfunction FALSE
set bifurcation FALSE
]
ask patch 9 0 [
set pcolor Yellow
set directions [-1 90 -1 -1]
set velocity 1
set processing-time 0
set is-processing? FALSE
set malfunction FALSE
set ID "WS10"
set bifurcation FALSE
]
ask patch 10 0 [
set pcolor Yellow
set directions [-1 90 -1 -1]
set velocity 1
set is-processing? FALSE
set malfunction FALSE
set bifurcation FALSE
]
ask patch 11 0 [
set pcolor Green
set directions [-1 -1 -1 -1]
set velocity 1
set is-processing? FALSE
set malfunction FALSE
set output TRUE
set ID "OUT"
set bifurcation FALSE
]
;********** Main line **********
;********** Drum buffer **********
ask patch -3 1 [
set pcolor Yellow
set directions [180 -1 -1 -1]
set velocity 1
set is-processing? FALSE
set malfunction FALSE
set bifurcation FALSE
]
ask patch -3 2 [
set pcolor Yellow
set directions [180 -1 -1 -1]
set velocity 1
set is-processing? FALSE
set malfunction FALSE
set bifurcation FALSE
]
ask patch -3 3 [
set pcolor Green
set directions [180 -1 -1 -1]
set velocity 1
set is-processing? FALSE
set malfunction FALSE
set bifurcation FALSE
]
ask patch -7 -1 [
set pcolor Yellow
set directions [0 -1 -1 -1]
set velocity 1
set is-processing? FALSE
set malfunction FALSE
set bifurcation FALSE
]
ask patch -7 -2 [
set pcolor Yellow
set directions [0 -1 -1 -1]
set velocity 1
set is-processing? FALSE
set malfunction FALSE
set bifurcation FALSE
]
ask patch -7 -3 [
set pcolor Green
set directions [0 -1 -1 -1]
set velocity 1
set is-processing? FALSE
set malfunction FALSE
set bifurcation FALSE
]
;********** Drum buffer **********
;********** Alternative line **********
ask patch 0 -1 [
set pcolor Black
set directions [-1 -1 180 -1]
set velocity 1
set is-processing? FALSE
set malfunction FALSE
set bifurcation FALSE
]
ask patch 0 -2 [
set pcolor Black
set directions [-1 90 -1 -1]
set velocity 2
set is-processing? FALSE
set malfunction FALSE
set bifurcation FALSE
]
ask patch 1 -2 [
set pcolor Black
set directions [-1 90 -1 -1]
set velocity 1
set is-processing? FALSE
set malfunction FALSE
set bifurcation FALSE
]
ask patch 2 -2 [
set pcolor Black
set directions [-1 90 -1 -1]
set velocity 2
set processing-time 20
set is-processing? FALSE
set malfunction FALSE
set ID "WS11"
set bifurcation FALSE
]
ask patch 3 -2 [
set pcolor Black
set directions [-1 90 -1 -1]
set velocity 1
set is-processing? FALSE
set malfunction FALSE
set bifurcation FALSE
]
ask patch 4 -2 [
set pcolor Black
set directions [0 -1 -1 -1]
set velocity 2
set is-processing? FALSE
set malfunction FALSE
set bifurcation FALSE
]
ask patch 4 -1 [
set pcolor Black
set directions [0 -1 -1 -1]
set velocity 1
set is-processing? FALSE
set malfunction FALSE
set bifurcation FALSE
]
;********** Alternative line **********
;********** Line labels **********
ask patch -11 1 [
set plabel "WS1"
]
ask patch -9 1 [
set plabel "WS2"
]
ask patch -7 1 [
set plabel "WS3"
]
ask patch -5 1 [
set plabel "WS4"
]
ask patch -3 1 [
set plabel "WS5"
set plabel-color 25
]
; ask patch -1 1 [
; set plabel "WS6"
; ]
;; ask patch 2 1 [
; set plabel "WS7"
;]
; ask patch 5 1 [
; set plabel "WS8"
; ]
; ask patch 7 1 [
; set plabel "WS9"
; ]
; ask patch 9 1 [
; set plabel "WS10"
; ]
;; ask patch 2 -1 [
;; set plabel "WS11"
;; ]
ask patch 11 0 [
set plabel "OUT"
]
ask patch -6 0 [
set plabel "SCANNER"
set plabel-color 25
]
;********** Line labels ***********
;********** Input Buffer ***********
ask patch -12 0 [
set pcolor Green
set directions [-1 90 -1 -1]
set velocity 1
set is-processing? FALSE
set malfunction FALSE
set bifurcation FALSE
]
;********** Input Buffer ***********
end
to execute-random
;********** Machine movement ***********
ask machine [
ask patch-here[
set distance-to-jump velocity
set new_directions remove -1 directions ]
set heading first new_directions
ifelse not bifurcation [
if not processing and first process-plan != "WS3"[
if not any? machine-on patch-ahead distance-to-jump and not [malfunction] of patch-ahead distance-to-jump[ jump distance-to-jump ]
if first process-plan = ID [
set process-plan remove-item 0 process-plan
set processing TRUE
set is-processing? TRUE
set time-of-processing processing-time
]
]
if not processing and first process-plan = "WS3" and [ID] of patch-ahead distance-to-jump = "WS3" and
; not [malfunction] of patch-ahead distance-to-jump and
not any? machine-on patch-ahead distance-to-jump[
if any? drum-on patch-ahead distance-to-jump [
jump distance-to-jump
set process-plan remove-item 0 process-plan
set processing TRUE
set is-processing? TRUE
set time-of-processing processing-time
set shape "WM"
]
]
if not processing and first process-plan = "WS3" and [ID] of patch-ahead distance-to-jump != "WS3" and
; not [malfunction] of patch-ahead distance-to-jump and
not any? machine-on patch-ahead distance-to-jump[ jump distance-to-jump ]
][ ifelse not any? machine-on patch-ahead distance-to-jump and not [is-processing?] of patch-ahead 2 and not [malfunction] of patch-ahead distance-to-jump [ jump distance-to-jump
set process-plan remove-item 0 process-plan
set process-plan fput "WS7" process-plan
][
if not any? machine-on patch-right-and-ahead 90 2 and ([malfunction] of patch-ahead 2 or any? machine-on patch-ahead 2) [
; if use-W11 [
set heading 180
jump 2
set process-plan remove-item 0 process-plan
set process-plan fput "WS11" process-plan ] ] ]
]
; ]
;********** Machine movement ***********
;********** Drum movement ***********
ask drum [
if not processing and [ID] of patch-here != "WS3"[
ask patch-here[
set distance-to-jump velocity
set new_directions remove -1 directions ]
if not any? drum-on patch-ahead distance-to-jump and ID != "WS3" and not [is-processing?] of patch-ahead distance-to-jump [ jump distance-to-jump ]
]
if any? machine-on patch-here [ die ]
]
;********** Drum movement ***********
end
to resource-processing
ask patches [
if ID = "WS7" [ set processing-time 0 ]
if ID = "WS11" [ set processing-time 0 ]
]
ask machine [
if processing [
set time-of-processing time-of-processing - 1
ask patch-left-and-ahead 90 1 [ set plabel-color Green]
]
]
ask machine [
if processing [
if time-of-processing <= 0 [
ask patch-here [
set is-processing? FALSE
]
ask patch-left-and-ahead 90 1 [ set plabel-color White]
set processing FALSE
if [ID] of patch-here = "WS5" [ ifelse random-float 1 < 0.1 [ set QC-check "BAD" ] [ set QC-check "GOOD" ] ]
]
]
]
; ifelse malfunction-WS7 [ ask patch 2 0 [ set malfunction TRUE ] ]
; [ ask patch 2 0 [ set malfunction FALSE ] ]
end
to count-output
ask machine [
if plabel = "OUT" [
set death-time ticks
ifelse QC-check = "GOOD" [ set WM-output WM-output + 1 ] [ set WM-output-bad WM-output-bad + 1 ]
set mlt-aux (mlt-aux + (death-time - birth-time))
set avg-mlt (mlt-aux / (WM-output + WM-output-bad))
die ]
]
end
to count-drums
ask machine [
if plabel = "SCANNER"
[set drum-output drum-output + 1]
]
end
to resource-ocupation
ask patches [
ifelse any? machine-here [ set is-processing? TRUE ] [ set is-processing? FALSE ]
]
end
to statistics
set-current-plot-pen "WIP"
set wip count machine-on patches + count drum-on patches
plot wip
set-current-plot-pen "avg-mlt"
plot avg-mlt / 10
end
to execute
create-WM[]
resource-ocupation
resource-processing
execute-random
tick
count-drums
count-output
statistics
end
This is my error message:
REMOVE expected input to be a string or list but got the number 0 instead.
error while patch -3 -1 running REMOVE
called by procedure EXECUTE-RANDOM
called by procedure EXECUTE
called by Button 'Run'
org.nlogo.nvm.ArgumentTypeException: REMOVE expected input to be a string or list but got the number 0 instead.
at org.nlogo.prim.etc._remove.report(_remove.java:57)
at org.nlogo.prim._asm_procedureexecuterandom_setobservervariable_61.perform()
at org.nlogo.nvm.Context.runExclusive(Context.java:119)
at org.nlogo.nvm.ExclusiveJob.run(ExclusiveJob.java:57)
at org.nlogo.nvm.Context.runExclusiveJob(Context.java:162)
at org.nlogo.prim._asm_procedureexecuterandom_ask_59.perform(:1)
at org.nlogo.nvm.Context.runExclusive(Context.java:119)
at org.nlogo.nvm.ExclusiveJob.run(ExclusiveJob.java:57)
at org.nlogo.nvm.Context.runExclusiveJob(Context.java:162)
at org.nlogo.prim._asm_procedureexecuterandom_ask_57.perform(:1)
at org.nlogo.nvm.Context.stepConcurrent(Context.java:91)
at org.nlogo.nvm.ConcurrentJob.step(ConcurrentJob.java:82)
at org.nlogo.job.JobThread.org$nlogo$job$JobThread$$runPrimaryJobs(JobThread.scala:143)
at org.nlogo.job.JobThread$$anonfun$run$1.apply$mcV$sp(JobThread.scala:78)
at org.nlogo.job.JobThread$$anonfun$run$1.apply(JobThread.scala:76)
at org.nlogo.job.JobThread$$anonfun$run$1.apply(JobThread.scala:76)
at scala.util.control.Exception$Catch.apply(Exception.scala:88)
at org.nlogo.util.Exceptions$.handling(Exceptions.scala:41)
at org.nlogo.job.JobThread.run(JobThread.scala:75)
NetLogo 5.2.0
main: org.nlogo.app.AppFrame
thread: JobThread
Java HotSpot(TM) 64-Bit Server VM 1.8.0_51 (Oracle Corporation; 1.8.0_51-b16)
operating system: Linux 3.13.0-24-generic (amd64 processor)
Scala version 2.9.2
JOGL: (3D View not initialized)
OpenGL Graphics: (3D View not initialized)
model: headsline
06:45:24.666 SwitchedTabsEvent (org.nlogo.app.Tabs) AWT-EventQueue-0
06:45:24.661 RuntimeErrorEvent (org.nlogo.app.App$$anon$1 (org.nlogo.window.GUIWorkspace)) AWT-EventQueue-0
06:45:24.659 JobRemovedEvent (org.nlogo.app.App$$anon$1 (org.nlogo.window.GUIWorkspace)) JobThread
06:45:24.657 InterfaceGlobalEvent (org.nlogo.app.InterfacePanel$2 (org.nlogo.window.SliderWidget)) AWT-EventQueue-0
06:45:24.657 InterfaceGlobalEvent (org.nlogo.app.InterfacePanel$2 (org.nlogo.window.SliderWidget)) AWT-EventQueue-0
06:45:24.657 InterfaceGlobalEvent (org.nlogo.app.InterfacePanel$2 (org.nlogo.window.SliderWidget)) AWT-EventQueue-0
06:45:24.657 InterfaceGlobalEvent (org.nlogo.app.InterfacePanel$2 (org.nlogo.window.SliderWidget)) AWT-EventQueue-0
06:45:24.657 InterfaceGlobalEvent (org.nlogo.app.InterfacePanel$2 (org.nlogo.window.SliderWidget)) AWT-EventQueue-0
06:45:24.657 PeriodicUpdateEvent (org.nlogo.app.App$$anon$1 (org.nlogo.window.GUIWorkspace)) AWT-EventQueue-0
06:45:24.549 AddJobEvent (org.nlogo.window.ButtonWidget) AWT-EventQueue-0

J.Chen, I don't have an answer to the question you asked, but the help that you really need is about how to start debugging a NetLogo Program. David Merino is right that we don't have enough information to debug the program for you, but you should take more steps first. StackOverflow is not a "debug my program" site. It's a site for questions that you have after you've tried to work through the problem and can't find a solution. At that point you can post a more specific question.
Here are some tips for debugging the program you're experimenting with:
The error message begins:
REMOVE expected input to be a string or list but got the number 0 instead.
error while patch -3 -1 running REMOVE
called by procedure EXECUTE-RANDOM
called by procedure EXECUTE
called by Button 'Run'
That means that the error is caused by an instance of the remove function. In NetLogo, you can use the Find command on the Edit menu to search for "remove". Better yet, search for "remove ", i.e. with a space after it. There are two instances of the remove command in the program, so you know that one of them is causing the problem.
Next, the error message tells you that the remove function was called by the procedure execute-random. This is going to be a user-defined procedure. Its definition will start with to execute-random. It turns out that both instances of remove are in the execute-random procedure, so you now have to figure out which one is causing the problem. If one of the removes was in execute-random, and the other wasn't, you could ignore the other one for now.
Now, the error message says that "REMOVE expected input to be a string or list but got the number 0 instead." If you look at the documentation for remove in the NetLogo dictionary, you see that its second argument is supposed to be a list or a string. The remove commands in the source code both try to remove the number -1 from something called directions. Since -1 is not a character that could be in a string, directions is probably supposed to be a list. However, the error message tells you that sometimes, direction contains the number 0 instead of a list.
What I would do at this point, probably, is to insert a print directions statement into the source code just before the line containing remove. It might also be useful to add print self to print out the patch. I'm not sure what to do next, because I don't understand the program, and I can't run it without the GUI elements.
However, here is one tip that's not at all obvious: All variables start out with the value 0. directions is defined near the top of the source code as a patches-own variable. What I suspect is that for at least one patch, this variable is not getting initialized to have a list as its value before one of the remove lines is executed, and that that's why directions has the value 0.

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?

Repeat command and use of variable by turn in Netlogo

I am stuck with repeat command. NetLog codes' examples couldn't help anymore. I want turtles to pick the value by turn assigned through "who" number and then move a step forward. I want turtles to repeat the same task 10 times. I appreciate any help.
I run the commands error appears in as;
Can't find element 3 of the list [0 1 0], which is only of length 3.
error while turtle 0 running ITEM
called by (anonymous command: [ ?1 -> ask turtles with [ who = ?1 ] [ if item ticks turn = 1 [ fd 1 ] ] ])
called by procedure GO
called by Button 'Go'*
Here are the codes
turtles-own [turn]
To setup
ca
create-turtles 2
[move-to one-of patches
]
reset-ticks
end
to go
tick
define-turn
foreach [ 0 1]
[ ?1 ->
ask turtles with [who = ?1]
[ if (item ticks turn) = 1 [fd 1 ] ]]
end
to define-turn
Ask turtle 0 [ repeat 10 [ set turn [0 1 0] ]]
Ask turtle 1 [repeat 10 [ set turn [0 0 1] ]]
end

Create a gradient for a turtle swarm

I created a rectangular turtle grid using a total of 1000 turtles.
let in-shape patches with [ pxcor >= -50 and pxcor <= 50 and pycor >= -5 and pycor <= 5 ]
ask in-shape [ sprout 1 ]
Now I need to create a sort of gradient, that will give a sense of distance to my turtle swarm. Given a "seed" with value 0, it emits a message with its value 0 in a certain talk_radius.
The turtles inside this talk_radius compute their distance to the seed. If the distance is less than a fixed value (called gradient_distance) the turtle will assume 1 as gradient_value, emitting a message with its value.
The other turtles do the same. So everyone will take the value x + 1, where x is the lowest value of the turtle within the gradient_distance, as shown in the picture
This is the relative algorithm in pseudo code:
loop
if gradient seed = TRUE then // check if robot is designated as gradient source
gradient value(self) = 0
else
gradient value(self) = GRADIENT MAX
for all neighbors n do
if measured distance(n) < G then // G represents the gradient-distance and GRADIENT MAX is infinity
if gradient value(n) < gradient value(self) then
gradient value(self) = gradient value(n)
gradient value(self) = gradient value(self) + 1
transmit gradient value(self)
And that's my implementation in netlogo:
globals [talk_radius gradient_max gradient_distance]
turtles-own [gradient_seed gradient_value]
to setup
ca
resize-world -60 60 -20 20
crt 1000
let in-shape patches with [ pxcor >= -50 and pxcor <= 50 and pycor >= -5 and pycor <= 5 ]
ask in-shape [ sprout 1 ]
set talk_radius 4
set gradient_max 100000
set gradient_distance 1
ask turtles
[set shape "circle"]
ask turtles-on patch -50 5
[set gradient_seed true]
end
to gradient-formation
while [true]
[
ask turtles
[
ifelse (gradient_seed = true)
[
set gradient_value 0
]
[
set gradient_value gradient_max
set color scale-color green gradient_value 0 120
ask (other turtles) in-radius talk_radius with [distance myself <= gradient_distance] ;; i consider all the turtle in talk_radius having the right gradient_distance
[
let a ([gradient_value] of self) ;; "of self" is not necessary but helped me for a better comprehension
if (a < ([gradient_value] of myself))
[
ask myself [set gradient_value a]
]
]
set gradient_value (gradient_value + 1)
]
set color scale-color green gradient_value 0 120
]
]
end
I used a scale-color in order to have a feedback of what i have done, as you can see in the image.
And now the problem: instead of let a ([gradient_value] of self), i tried set a ([gradient_value] of self) adding a to the turtle variable (I added a in the turtle-own list on top).
I thought the result would have been the same, but instead i got a constantly increasing gradient_value for every turtle as you can see in the image(the color white denotes a very high gradient_value).
Why this difference? Thank you in advance and sorry for the long question.
EDITED EXTENSIVELY in response to discussion that refined the problem
First, I would like to start with a simpler version of the code. I believe this is exactly the same as yours without the while[true]. I removed the extra 1000 turtles you are creating, and separated the ifelse on whether a seed into two separate ask statements, for clarity. I also moved the colouring until after the value calculation is complete.
globals [talk_radius gradient_max gradient_distance]
turtles-own [gradient_seed? gradient_value]
to setup
clear-all
resize-world -60 60 -20 20
let in-shape patches with [ pxcor >= -50 and pxcor <= 50 and pycor >= -5 and pycor <= 5 ]
ask in-shape
[ sprout 1
[ set shape "circle"
set gradient_seed? false
]
]
set talk_radius 4
set gradient_max 100000
set gradient_distance 1
repeat 10 [ gradient-formation ]
end
to gradient-formation
ask turtles-on patch -50 5
[ set gradient_seed? true
set gradient_value 0
]
ask turtles with [not gradient_seed?]
[ set gradient_value gradient_max
ask (other turtles) in-radius talk_radius with [distance myself <= gradient_distance]
[ let my-gradval ([gradient_value] of self)
if my-gradval < [gradient_value] of myself
[ ask myself [set gradient_value my-gradval]
]
]
set gradient_value (gradient_value + 1)
]
ask turtles [set color scale-color green gradient_value 0 120 ]
end
There is a conceptual issue here. Until a turtle has its gradient_value calculated, it is 0. This means a large number of turtles will have a 0 turtle nearby and then have their own gradient_value as 1. It does not produce a colour gradient. To get around this, you need to run the gradient-formation several times. The approach in your code of while [true] introduces an infinite loop. Instead, you can repeat an arbitrary number of times (10 in the code above).
The problem with let versus set + turtles-won is that the set with turtles-own creates 1000 copies of gradient_value - one for each turtle. The let version creates a (temporary) global variable that all turtles access. So when you use set, you are setting it for that turtle, not as a general access number. I think what is happening is that the line set gradient_value my-gradval is accessing the wrong turtle's copy of my-gradval.
But, from the discussion, the purpose of the code that is causing the problem is to find a local minimum. There is a much more direct way of doing that.
to gradient-formation
ask turtles-on patch -50 5
[ set gradient_seed? true
set gradient_value 0
]
ask turtles with [not gradient_seed?]
[ set gradient_value 1 + min [gradient_value] of
other turtles in-radius min (list talk_radius gradient_distance)
]
ask turtles [set color scale-color green gradient_value 0 120 ]
end
ADDED a minimum working example to show the differences.
This is the let (global variable) version
turtles-own [testval]
to testme
clear-all
create-turtles 500
[ setxy random-xcor random-ycor
set color blue
set testval 1 + random 10
]
ask one-of turtles
[ set color red
inspect self
type "testval of asking turtle is " print testval
ask turtles-on neighbors
[ set color yellow
let my-testval [testval] of self ;; creates a temp global variable
type "my-testval is " print my-testval
if my-testval < [testval] of myself
[ ask myself
[ set testval my-testval ;; copies the global variable value
]
]
]
]
end
This is the set (turtle attribute) version
turtles-own [testval my-testval]
to testme
clear-all
create-turtles 500
[ setxy random-xcor random-ycor
set color blue
set testval 1 + random 10
]
ask one-of turtles
[ set color red
inspect self
type "testval of asking turtle is " print testval
ask turtles-on neighbors
[ set color yellow
set my-testval [testval] of self
type "my-testval is " print my-testval
if my-testval < [testval] of myself
[ ask myself
[ set testval my-testval ;; copies value from one attribute to other
]
]
]
]
end

Is there a way to set patch color for multiple patches with a single line of code in NetLogo?

Right now i am using
ask patch 1 1 [set pcolor grey]
ask patch 1 -1 [set pcolor grey]
ask patch -1 1 [set pcolor grey]
ask patch -1 -1 [set pcolor grey]
Is there a way to set patch color for multiple patches with a single line of code in NetLogo?
Simplest way:
ask (patch-set patch 1 1 patch 1 -1 patch -1 1 patch -1 -1) [ set pcolor grey ]
(The following has been updated in response to Seth's comment:)
If this is being run from the observer (that is, it's not being run by any agent) you can use at-points like so:
ask patches at-points [[1 1] [1 -1] [-1 1] [-1 -1]] [ set pcolor grey ]
However, if this code is run by a turtle or a patch, the list of coordinates will be treated as relative to that agent. So patches at-points [[1 0] [0 1]] will give the patch to the right and the patch above the current agent. You can easily make a version that uses absolute coordinates:
to-report patches-at-coords [ coordinates ]
report (patch-set map [patch first ? last ?] coordinates)
end
and then use it like so:
ask patches-at-coords [[1 1] [1 -1] [-1 1] [-1 -1]] [ set pcolor grey ]
Regardless, unless you're doing this a lot, I'd go with the first method.

NetLogo, How can I make the agent move is a specific area and then colour the area while it moving without stucking?

the problem
In this code I try to make the agent to move in the yellow area which is = 47, and to colour the patches to be "red", but the problem is that the agent stuck in the red coloured area.
I want the agent to move and to colour until all the patches become red, it is like cleaning.
Please guys, I need help please....
;;leftcheck and rightcheck has been declared in globals
if (behaviour = "RCleaner")
[
ifelse any? patches in-cone 2 10 with [pcolor = 47]
[ fd random-float 0.03 ]
[
rt 2
ifelse any? patches in-cone 2 10 with [pcolor = 47]
[ set rightcheck true ] [ set rightcheck false ]
lt 2
ifelse any? patches in-cone 2 10 with [pcolor = 47]
[ set leftcheck true ] [ set leftcheck false ]
rt 2
if leftcheck [ lt 1 ]
if rightcheck [ rt 1 ]
fd 0.01
]
set heading heading + random 1 - random 1
ask patch-here [ set pcolor red]
]