How can you break ask in netlogo - netlogo

How can you break ask in netlogo?
For example..agents are moving and suddenly some condition has satifised. Now you don't want ask agents to execute(move) and stop ask. And pass control to next line after ask.
Something similar exists in procedures called "stop". But not for ask. Is there a way around?

Well, stop does break out of ask. From the documentation of stop:
This agent exits immediately from the enclosing procedure, ask, or ask-like construct (e.g. crt, hatch, sprout). Only the current procedure stops, not all execution for the agent.
So you can do things like:
ask turtles [
if stop-condition? [ stop ]
move-to one-of patches ; or whatever
]
print "done!"
...and as soon as stop-condition? becomes true, the execution will break out of the ask block and "done!" will be printed.

While I really hesitate to contradict Nicholas, I feel his answer is this case is misleading. Lack of a real break statement is a serious shortcoming of NetLogo. Inside an ask, a stop is more like a continue than a break. Example: executing the following code will always print 50.
to test
let ctr 0
crt 100 [set color blue]
ask n-of 50 turtles [set color red]
ask turtles [
if (color = red) [ stop ]
set ctr (1 + ctr)
]
print ctr
end

Related

How to kill and regenerate turtles every set number of ticks

The model I am working on simulates a group of workers working on a team project. I am trying to add a on/off switch titled "replacement." The intended outcomes are:
When it is on:
30% of existing turtles of breed employees die, and equal number of new turtles are introduced.
When it is off:
No turtles die, and all turtles generated at setup will work on the project from start to finish.
The purpose of killing off the turtles is the turtle-own variable "skills." I am trying to simulate a scenario where people resign from a company and new people are hired, which will result in a change of skill level.
If it is easier to change the skill level of 30% of turtles every 20 ticks instead of killing/regenerating, that would be perfect, but I was unable to figure it out.
My attempt at this problem is shown below. It runs without any errors, but I confirmed that it's not killing/respawning 30% of turtles every set number of ticks. Any helpful guidance will be much appreciated.
to go
if all? patches [ workload = 0 ] [ stop ]
ifelse replacement [hire] [continue]
recolor
tick
end
to hire
if ticks > 0 and ticks mod 20 = 0 [
ask n-of (count employees * 0.30) employees [die]
create-turtles number_of_workers * 0.30 [
set breed employees
setxy random-xcor random-ycor
set shape "person"
set color black
set size 1
set skills random-float 1]
]
ask employees [move]
ask leaders [move]
end
to continue
ask employees [move]
ask leaders [move]
end
I ended up finding the answer on my own. Removing "ticks > 0 and" from the hire function solved it :)
From your description of the problem, I am not sure why removing the if ticks > 0 and solved it. I am wondering if you perhaps had the "replacement" variable set to false, and the hire procedure would not have been called.
Regardless, you also asked about just changing the skill set every 20 ticks. You could do this:
to go
if all? patches [ workload = 0 ] [ stop ]
if replacement? and ticks mod 20 = 0 [hire]
move-people
recolor
tick
end
to hire
ask n-of (count employees * 0.30) employees
[ set breed employees
set skills random-float 1
]
end
to move-people
ask employees [move]
ask leaders [move]
end
I also reorganised slightly in a way that is intended to make it easier to debug. You had the moving in both the "hire" and "continue" procedures. Clearly you want that to always happen, so that is now explicit in the "go" procedure (and I changed the name to be more descriptive).
Now the "hire" procedure simply changes the skills of existing employees. Note, however, that any other variables (such as leader, size, colour and position) are not affected. If you go this approach, you may want to indicate in some way that they are new hires.
The "hire" procedure is now clearly called when the "replacement?" switch is on and also a multiple of 20 ticks. If you have part of the condition in the calling procedure ("go") and part in the procedure itself ("hire") then it's easy to forget the existence of whichever half you are not looking at.

In Netlogo, how to measure turtle or patch own variables in behavior space in the "measure runs using these reporters" space

I am running my Netlogo model in behavior space. In my model, I created a turtles-own variable called consumption-rate. I want to export the consumption-rate of each turtle for every tick of my run. From my understanding of behavior space, I would somehow put consumption-rate in the box that says "Measure runs using these reporters" in order for it to be exported, but I keep getting different errors every time I try. For instance I mostly get an error that says "Experiment aborted due to syntax error: Expected reporter". I also need to export a patches-own variable I created called quality for every patch in my model at each tick, and I have the same issue. All the examples for this part of behavior space online just show "count turtles" or something similar. Am I able to export a turtle or patch variable there? If so, what code would I use?
I took a shot into the blue and tried using primitives like "show consumption-rate" or "report consumption-rate". I am unsure of the format of code I would even begin to use to give me those exports. Any advice or help? I also tried just typing in "consumption-rate" or "quality" in the "Measure runs using these reporters" box, but got an error saying I can't use a turtle or patch variable in an observer context, how can I make those into the observer context? Anyway around that?
patches-own [ quality ]
turtles-own [ consumption-rate ]
to setup-patches
ask patches
[set quality (2 + random 8)
set pcolor scale-color green quality 1 10 ]
end
to Go
ask turtles
[ calculate-consumption ]
end
to calculate-consumption
set consumption-rate ( [ quality ] of patch-here ) / ( strength-of-competition * count turtles-here )
end
You have a conceptual mismatch. There is no problem in exporting a turtle or patch variable in BehaviorSpace, but you haven't told NetLogo which variable to export. You need to specify whether it's that variable for all turtles, or only some turtles or whatever.
Here's a modified version of your code so that it is complete and self-contained.
globals [strength-of-competition]
patches-own [ quality ]
turtles-own [ consumption-rate ]
to setup
set strength-of-competition 0.4
ask patches
[ set quality (2 + random 8)
set pcolor scale-color green quality 1 10
]
create-turtles 300 [setxy random-xcor random-ycor]
end
to go
ask turtles
[ calculate-consumption ]
end
to calculate-consumption
set consumption-rate quality / ( strength-of-competition * count turtles-here )
end
Run this with a BehaviourSpace setup that has [consumption-rate] of turtles as the reporter. Also put 2 in the time limit. You will get the output you requested.
A good tip for working with BehaviorSpace when you are new to it, is to set up a monitor on your interface for each value that you want to save in your output. Get the monitors showing what you want to export and then simply take the code that you ended up with and put it in the reporter box. The advantage of doing the monitor step is that it forces you to get your thinking right without going down the rabbit hole of whether it is a BehaviorSpace issue.

Game of Life , NetLogo i Extend my neighbors to 24 cells using in-radius

Here is the code :
to new_neighbor
set my-neighbors (other patches) in-radius 2
end
to go
ask patches
[ set live-neighbors count my-neighbors with [living?] ]
;; Starting a new "ask patches" here ensures that all the patches
;; finish executing the first ask before any of them start executing
;; the second ask. This keeps all the patches in synch with each other,
;; so the births and deaths at each generation all happen in lockstep.
ask patches
[ ifelse live-neighbors = 3
[ cell-birth ]
[ if live-neighbors != 2
[ cell-death ] ] ]
tick
end
Here is the error:
COUNT expected input to be an agentset but got the number 0 instead.
Error while patch 30 -31 running COUNT
called by procedure GO
called by Button 'go-once'
I just want to extend my neighbors to 24 cell. Nothing else. I want program to check not first 8 cells ring but 24.
Thank you.
Look in the Models Library (File menu). In the Code Examples section, there is one called 'Moore & von Neumann Example'. That does exactly what you want.
For your error, there were no patches in my-neighbors. Your code doesn't include any section that actually sets the my-neighbors variable, so it's unclear why it was empty. That is, what bit of code actually calls the procedure new_neighbor?

Move turtles to vacant patch ahead in NetLogo

I'm struggling to work out how to tell my turtles to move forward 1 onto a vacant patch, once they have already been turned around in a procedure called "turn-turtle".
let ahead patch-ahead 1
let vacant-ahead ahead with [not any? turtles-here ] ;;this line needs fixing
if any? turtles
[turn-turtle if vacant-ahead [fd 1]]
It produces this error, which I understand, but can't work out how to fix.
WITH expected input to be an agentset but got the patch (patch 1 -2) instead.
error while solute 2 running WITH
called by procedure MOVE-TURTLE
called by procedure GO
called by Button 'go'
Just replying to some comments in a more readable fashion:
Sorry I don't think I explained it very well, as I've just taken a tiny bit of my code out. Maybe this makes more sense.
to go
ask turtles
[move-turtle]
end
to move-turtle
turn-turtle
if (not any? turtles-on patch-ahead 1)
[fd 1]
end
So I just want this code to move the turtles that have been turned with "turn-turtle" to an empty patch ahead 1, preferably taking up the entire patch, like if they were "sprouted". Thanks!
Sounds like you want to do something a bit different, if you really want ahead to be a single patch. So perhaps
to move
ifelse (any? turtles-on patch-ahead 1) [
turn-turtle
][
fd 1
]
end

Is there an else command in netlogo?

I am trying to create a program in netlogo where there are blocks that come down the screen and when their y-coordinate reaches a certain value they reverse their direction and move in the opposite way.
So far I was able to make them move in one direction and then switch directions when they reach the critical y-coordinate value, but once they take one step in the reverse direction it glitches and they get stuck moving one step forward and one step backward.
I wanted to know if there was an else command in netlogo so I could specify that if the while command wasn't fulfilled it could reverse its direction and move without glitching.
Here is my code.
to maze
while [abs pycor < 16 ] [fd 1 wait .1]
bk 1 wait .1
end
There is no separate else keyword in NetLogo, but the ifelse command allows you to specify two blocks: one that is executed if the condition is true, and another (the "else" block) that is executed if the condition is false.
It seems, however, like you should rethink your general approach to the problem. Turtles in NetLogo always face in a particular direction, and you could take advantage of that: instead of having them "back up", you could have them turn around.
Also, it's generally ill-advised to try to do things in a while loop. If you want your turtles to repeat a behavior, a "forever button" is usually the way to go.
In the following example, you should call the go procedure from a forever button:
to setup
clear-all
ask patches with [ pycor = max-pycor - 1 ] [
sprout 1 [
set heading 180 ; head down
]
]
reset-ticks
end
to go
ask turtles [
if abs pycor = max-pycor [
rt 180 ; turn around!
]
fd 1
]
tick
end
This probably doesn't achieve exactly what you wanted, but there is a good chance that you can modify it to fit your needs.
Also note that this will work better using tick-based updates.