Netlogo, Hatching a different breed - netlogo

I am working on a model which consists of a bunch of nodes which are connected by links. There are two type of agents initially, lets say yellow-bees and collectors. My collectors find a node on which bees are located, travel to it and collect the bees.( The way the model works is when the collector reach the node on which the bee is situated on, the bee is killed and collectors-own variable called, bees-collected will be updated by 1). Next the collector will travel to some node, lets sat node with bee-hive. At this point I want to create a new breed, lets say new variety of bee, i.e red-bees. The problem arises because only the observer can create a new breed and not the collector. Hatching would also not work because it will create more collectors and not a new red bee. Any idea how to solve this ?
breed [yellow-bees yellow-bee]
breed [collectors collector]
breed [red-bees red-bee]
breed [nodes node]
breed [hives hive]
collectors-own [bees-collected current-node]
nodes [hive-present]
to go
...
...
ask collectors[
if current-node = nodes with [hive-present = True][
create-red-bees bees-collected
]
]

Hatching will work if you use the hatch-<breeds> version of the command which, as mentioned in the NetLogo dictionary entry, will create new turtles of the given breed.
Here is an example:
breed [collectors collector]
breed [red-bees red-bee]
to setup
clear-all
create-collectors 10
ask collectors [
hatch-red-bees 1
]
print count red-bees
end

Related

Netlogo : issue with observer context

When running two exact same procedures for specific patches, one of the two is stating an error because "it cannot be run in observer context, it is a patches/turtles context only".
to start
ask patches with [seed = 1 AND age = retirement-age] [retirement]
ask patches with [seed = 1 AND age = death] [successor]
end
The retirement-age is an input from the interface.
The death is a patch property, I tried the startprocedure as well with an input from the interface.
The retirement and death procedure are just a copy-paste of each other, using lists created in other procedures. Only the result is different, see below :
to retirement
;;; other calculations same as successor procedure
ifelse (successor-result = 1) [set age 35 ] [set over-retirement 1]
end
to successor
;;; other calculations same as retirement procedure
ifelse (successor-result = 1) [set age 18] [dispatch-parcels]
end
Running the retirement procedure do not create error, but the successor procedure does create an error.
I did not copy-paste everything because I don't think it is useful as the two procedure are exactly the same. The other calculations use global variables and lists created in other procedures in the observer context.
Here-below is the dispatch-parcels procedure that regards to concerned patches from the beginning.
to dispatch-parcels
;; this regards the patches with seed = 1 that has been processed in the successor procedure. But doesn't work as for updating the age and death of the agent
set seed 0
set age 0
set death 0
;; if there is no successor, the parcels of the farm are distributed along the closest parcels : one farm can distribute its own parcels to multiple different farms
ask patches with [ ID-farm = [ID-farm] of myself]
;; for each parcel, finds the closest parcel and use the ID-farm as the new ID-farm, same for the region in case it changes.
[
set ID-farm ([ID-farm] of (min-one-of patches with [(ID-farm != 0) AND (ID-farm != [ID-farm] of myself)] [distance myself] ))
set region ([region] of (min-one-of patches with [(region != 0) AND (region != [region] of myself)] [distance myself] ))
]
end
I also tried to the successor procedure with the same final command line as from the retirement procedure but I get the same error message. So there should be something "hidden" somewhere about this successor procedure.
Any insight about the likely source of error, what I should more attention to ?
Thanks for your time.
I finally figured out what was wrong. It was so obvious that it became invisible and I'm a bit embarrased.
I'd like to share my answer anyway because it might opened the eyes of tired beginners like me.
In order to test this part of my code, I created a button to only test the successor procedure, which was in observer context, which was not possible with the commands I wrote. That's just it.

NetLogo: Monitor variable of turtle

I have a breed "robots" and I create a single robot with health 0.
breed [robots robot]
create-robots 1 [
set health 0
]
Now I want to track the robot's health during runtime. I've tried many things like
[ health ] of robot 0
But it just doesn't seem to work, the monitor just shows "N/A" like below.
Any idea?
There's nothing wrong with your code (or the bit you've shown anyway). Have you actually run the procedure that the create is inside of? Here's is a complete model:
breed [robots robot]
robots-own [health]
to setup
create-robots 1
[ set health 0
]
end
If you have a monitor with [ health ] of robot 0 it will show N/A initially. As soon as you run the setup procedure (with a button call, or from the command center), it changes to 0.

Auction implementation similar to Dining Philosophers

I am trying to implement an auction concept in netlogo - it is similar to the dining philosophers problem program.
My program deals with computers and processors that correspond to philosophers and forks in the dining philosopher program. In the philosophers program a user needs 2 forks to eat but in computers and processors, one computer needs one processor to work.
The states defined in my program are IDLE, NEED, USING, corresponding to THINKING, HUNGRY, EATING in the philosopher program.
Currently, my program goes to the point where all computers state changes to NEED. I am having issues in acquiring of server for the computers.
The code snippet is -
// ... lines of code for declaration etc
.
.
.
to update
if state = "IDLE" [
if random-float 1.0 < hungry-chance [
set state "NEED"
]
stop
]
if state = "USING" [
set total-used (total-used + 1)
if random-float 1.0 < full-chance
[ release-servers ]
set state "IDLE"
stop
]
if state = "NEED"
[ acquire-servers ]
if we've got both forks, eat.
if got? servers
[ set state "USING" ]
stop
end
//lines of code in between
.
.
to acquire-servers ;; philosopher procedure
ask servers [
if [owner] of servers = nobody[
set owner myself
move-to owner
set heading [heading] of owner
]
]
end
// lines of code at end
First, you need to change your if for testing state into ifelse and remove the stop commands. That way, the computer will find its correct state and do the relevant procedures and then not look at the other states.
Second, the acquire-servers procedure currently assigns all the servers to the first computer that needs one. You probably want a procedure that looks more like:
to acquire-one-server
let candidates servers with [ owner = nobody ]
if any? candidates
[ ask one-of candidates
[ set owner myself
move-to owner
set heading [heading] of owner
]
]
end

NetLogo - selecting agent from agentset

I have a model of male and female animals interacting, with males competing with each other for access to females. When a dispersing male challenges a resident male (i.e., male-to-challenge) and loses, I’d like to have the dispersing male ‘remember’ who he lost to. I accomplish this with set dominant-males (turtle-set dominant-males male-to-challenge) at the end of the procedure. Then at the beginning of the procedure in the next time step, the dispersing male won't challenge the same dominant-male again. I thought that would be easy enough with:
; identify those males owning nearby females:
let owner-males-of-nearby-fem turtle-set [males-in-my-territory] of breeding-females with [member? self (owned-nearby-females)]
; identify those males who have not been challenged before:
let unchallenged-males owner-males-of-nearby-fem with [not member? self dominant-males]
; select one of the unchallenged males to challenge:
let male-to-challenge one-of unchallenged-males
However, I often find that the unchallenged-males are the same ones that had been challenged before and won (i.e., dominant-males), even though those males should not have been selected in the first place. I use print statements to verify this and included a simple error message using the following:
if [self] of unchallenged-males = [self] of dominant-males
[
user-message "this is wrong!"
]
I thought this would be easy but I've been stumped most of the day on this. Any help would be really appreciated.
You are testing against dominant-males of owner-males-of-nearby-fem instead of the challenger. Try changing dominant-males to [dominant-males] of myself.

Turtles own after change of breed

I have a code in which juveniles (juvis juvi) become adults (adults adult). The current way I have it is that juvis change breed to adults at a set age (determined by a turtles own, j-age). However I have a turtles own for adults (a-age) which I want to be Zero when the juvis change breed to adults. Does this happen automatically, or do I have to add code along the lines of "set a-age 0". I have tried putting that in in various guises and places but always get errors. Current code is as follows:
to emergence
ask juvis
[if j-age = (juvenile-period * 24) [set breed adults]]
end
any advice is appreciated. thank you.
When you change breed, any variables owned by the current breed that were not owned by the previous breed are set to 0.
You can test this yourself--just add a SHOW command to show the variable you are curious about!