Netlogo one-to-one relation - netlogo

I have a netlogo problem for which I can't seem to find a solution, but yet it feels very basic.
I have two types of breeds:
breed [individuals individual]
breed [cars car]
I want to create a link from one individual to one car. So, its a one-one relation. I use this code to do that:
to setup-individuals
create-individuals individuals-number [
set ID 2
set shape "person"
set color yellow
setxy random-xcor random-ycor
set activity ""
set activity_time 0
let rand random 2
ifelse rand = 0
[
set owns-car false
]
[
set owns-car true
create-link-to one-of cars ;; here is the issue
]
]
end
The problem is that if i use "create-link-to one-of cars" there are more than one individuals linked to one car, but I want each individual to have a distinct car. When trying the following command: "create-link-to one-of cars with [my-in-links = 0]" its giving me the following ERROR: "CREATE-LINK-TO expected input to be a turtle but got NOBODY instead." I tried many variations of this command, but its not working.

Your attempted solution of create-link-to one-of cars with [my-in-links = 0] is on the correct path. However, if you look at the NetLogo dictionary, you will see that my-in-links returns an agentset, not an integer giving the number of members of that agentset. So you need to compare to empty rather than compare to the number 0.
This is the code that is syntactically closest to what you have: create-link-to one-of cars with [count my-in-links = 0].
What you really want though is something more like create-link-to one-of cars with [not any? my-in-links]

Related

Is there a way to create a modification of Breed-On that includes specific characteristics?

I want my forager turtles to follow a path laid down by leader turtles, and this path consists of a turtle breed called trail-markers. The forager turtles also lay down trail-markers (to reinforce the trail), so I would like them to check for leader trail-markers on some patch/patches. I know Netlogo includes breeds-on to check for the presence of members of a breed, but I need the foragers to check for blue leader trail-markers. Is there a way to do this?
Here is what I had in mind:
if count (trail-markers with [color = blue]) on patch-ahead 1 > 0 [fd 1]
But I got, error:nothing named on has been defined, which makes sense, since the Netlogo primitive is breeds-on, so I modified it to:
if any? (trail-markers with [color = blue])-on patch-ahead 1 [fd 1]
However, I simply received: error:nothing named -on has been defined instead.
See the code below. I think you just want to use trail-markers-on, but you can simplify by using any? a bit. And you might want to have the custom reporter so you can just check for blue-markers-on if you find that simpler.
breed [ trail-markers trail-marker ]
to example
; I think this is what you want
if count ((trail-markers-on patch-ahead 1) with [color = blue]) > 0 [ forward 1 ]
; this is slightly simpler, using `any?` instead of `count ... > 0`
if any? (trail-markers-on patch-ahead 1) with [color = blue] [ forward 1 ]
; you could also write a custom reporter if you'll need to get this info a lot
if any? blue-markers-on patch-ahead 1 [ forward 1 ]
end
to-report blue-markers-on [p]
report (trail-markers-on p) with [ color = blue ]
end

Operator > and combining it with number and a string in NetLogo

I want a little help. I want turtles to cooperate with 50% probability if their land parcels are > 5.
I am writing as
ifelse random 1 = 0 and land > 5
[set cooperate? true]
[set cooperate? false]
But it gives error that; The > operator can only be used on two numbers, two strings, or two agents of the same type, but not on a string and a number.
how to correct it?
Thanks
There's not quite enough information to diagnose the problem. Is this code inside an ask turtles block with the variable 'land' as a turtles-own attribute? Also, you might want to print off some values of 'land' to make sure you actually have numbers in it.
As you can see from the working example below, there is no error in the code you have provided.
turtles-own [ land cooperate? ]
to testme
clear-all
create-turtles 10
[ set land random 10
ifelse random 1 = 0 and land > 5
[ set cooperate? true ]
[ set cooperate? false ]
]
type "Cooperating: " print count turtles with [cooperate?]
type "Not cooperating: " print count turtles with [not cooperate?]
end

Linking turtles to another turtle with highest value in NetLogo. Limit the number of links

I am a beginner with NetLogo and I am trying to ask some turtles (students from different social classes) to link to other turtles (schools). What I would like is for the working class students to look for the school with the highest achievement which is at the same not expensive and has not reached the max number of links allowed. If the desired school has reached the max number of links allowed I want the student to look for the next school with the highest achievement which has not reached the max number of links allowed and so on.
This is the code. I get the following message "ANY? expected input to be an agentset but got the turtle (school 1) instead."
breed [schools school]
breed [upperclass upperclass-student]
breed [workingclass workingclass-student]
upperclass-own [achievement enrolled? target]
workingclass-own [achievement enrolled? target]
schools-own [schoolachievement expensive? ]
to setup
clear-all
set-default-shape schools "house"
set-default-shape upperclass "person"
set-default-shape workingclass "person"
ask patches [ set pcolor 8 ]
create-schools num-of-schools [ setxy random-xcor random-ycor set schoolachievement random-normal 5 1
set expensive? one-of [ true false ] ]
create-upperclass num-of-upperclass [ set color white setxy random-xcor random-ycor set achievement
random-normal 5 1 ] ;Students from upper class have higher achievement
create-workingclass num-of-workingclass [ set color red setxy random-xcor random-ycor set achievement
random-normal 4 1 ]
end
to go
ask workingclass [
choose-school ]
end
to choose-school
if breed = workingclass [
set target one-of schools with-max [ schoolachievement ] with [ expensive? = false ] ]
if any? target with [ count link-neighbors < max-link-count ] [
create-link-with target ]
set enrolled? TRUE
end
Your problem is the difference between an agent and an agentset, which is a somewhat subtle problem. The with-max returns an agentset - a set of agents (in this case turtles). That agentset can have 0 members, 1 member, 2+ members but is a set even if it is empty. However, the one-of selects one agent from the agentset and returns it as an agent, not an agentset. That is, NetLogo knows anything that is returned by one-of must be exactly one agent. At this point, primitives that are for agentsets (like any?) will throw an error unless they can also be used on individual agents.
So, back to your code. I like the readability of checking whether there are viable schools and then selecting one, which is what I think you meant to do. That would be:
to choose-school
if breed = workingclass
[ set targets schools with-max [ schoolachievement ] with [ expensive? = false ]
set candidates targets with [ count link-neighbors < max-link-count ]
if any? candidates
[ create-link-with one-of candidates
set enrolled? TRUE
]
]
end
Note that I also changed to targets instead of target, which is one way to keep track of whether something is an agent or an agentset.
The other way you could do this and keep it as an agent is:
to choose-school
if breed = workingclass [
set target one-of schools with-max [ schoolachievement ] with [ expensive? = false ] ]
if target != nobody and [count link-neighbors] of target < max-link-count [
create-link-with target ]
set enrolled? TRUE
end
So you can use nobody instead of any? but you can't also use with in that line because the with is really a filter on a set.
I also think you have a bracketing issue - I assume you want set enrolled? TRUE inside the brackets. I left it in the second fix, but changed in the first error (as well as changing bracket position convention to make the code block structure more visible)

How to 'track' a turtle?

I would like to track a turtle created as follows:
time 0: turtle 1 creates a new turtle - object - using hatch-
time > 0: the object created is added to the list of a new turtle, turtle 2;
time > 0: turtle 2 wants to share this object with a new turtle, turtle 3. The object is then added to turtle 3's list ...
The turtle to track is the object. I thought about visit a path/link or changing color to the turtle created (for example, ask this-object [ set color red ]), but I am hiding the turtle, so it does not make sense.
Do you have any idea on how I can track the path of this turtle within the network? (you can think of a car that has an accident, but the driver does not stop, continuing driving and it has a new accident ...
What I would like to track are not only the accidents, but also the cars that have caused them, if it possible.
I hope you can help me.
Thanks
You have now asked essentially the same question at least 10 times in slightly different ways. It's clear that none of the answers have answered your question, but it's also clear that you aren't understanding any of the answers. While I would normally ask you to post what you have tried so far, it's probably best to start from the beginning.
Here is a complete model that does what you want. I have put print statements at key points so that you can see that it does what you want.
breed [people person]
people-own
[ my-objects
]
breed [objects object]
objects-own
[ my-people
]
to setup
clear-all
create-people 5
[ setxy random-xcor random-ycor
set color red
set my-objects []
]
ask one-of people
[ hatch-objects 1
[ set color blue
set my-people (list myself)
let child self
ask myself [ set my-objects (list child) ]
]
]
reset-ticks
end
to go
ask one-of objects [move-object]
tick
end
to move-object
let target one-of people
while [target = first my-people] [set target one-of people]
let this-object self
ask target
[ type self type " Objects old: " print my-objects
set my-objects fput this-object my-objects
type self type " Objects new: " print my-objects
]
type self type " Owners old: " print my-people
set my-people fput target my-people
move-to target
type self type " Owners new: " print my-people
end
If you want to make any progress on your code, you need to completely understand this model first. Don't just copy the relevant bits into your code and try to amend. Instead, make a new NetLogo model for this code and understand every line - what it does, how it works. Once you understand it, you can then use the concepts and approach in your own model.

Selecting an item from a list in Netlogo

I'd need to pick an object in a bag containing 20 elements with attributes c (color) and s (size). Both color and size are numbers (e.g. c= {red = 256, black = 0, ... } = {256, 0, ...}).
As in Python I'd use random.choice in numpy library, I found on the web that the corresponding function in Netlogo is the extension rnd.
Struggling along a possible solution, I did
Edited:
breed[people person]
people-own
[
ball
size
color
bag
]
to setup
create-people 5
[ set color gray
setxy random-xcor random-ycor
]
ask people[
set bag [ ] ; 0 items
]
end
To create the balls:
to create-balls
set color random 300 ; color
set size random-float 5 ; size
let this-ball self
ask one-of people [ ; ask one of people to put the ball created into the bag
set bag fput this-ball bag ; add ball to the bag
]
end
The code below should include the part of drawing:
to draw
ask one-of people [
rnd:weighted-one-of bag [ ] ; I do not know what I'd write in the brackets
]
end
As you can easily see, I've many doubts about how to implement the code.
How can I select one item from the bag depending on its size (or color)?
Can you please help me out with it?
Here is a complete model that creates people and balls as turtle agents and has 30 of the balls get chosen weighted by their size. It then opens an inspect window for the person who has chosen the most balls.
extensions [rnd]
breed [people person]
people-own [ my-balls ]
breed [balls ball]
balls-own [ chosen? ]
to setup
clear-all
create-people 20
[ setxy random-xcor random-ycor
set my-balls (turtle-set nobody)
]
create-balls 50
[ hide-turtle
set size one-of [1 2 3 4 5]
set color one-of [red white blue yellow]
set chosen? false
]
repeat 30 [draw-ball]
inspect max-one-of people [count my-balls]
end
to draw-ball
ask one-of people
[ let bag-of-balls balls with [not chosen?]
let choice rnd:weighted-one-of bag-of-balls [size]
ask choice [set chosen? true]
set my-balls (turtle-set my-balls choice)
]
end
Some things to notice:
There are NO lists in this code. There are situations where you should use lists. Common uses include memory where the order is important (eg you only want to keep track of the last 5 other people seen) or where the same agent can appear multiple times. And the list commands are very powerful. However, unless you need a list then you should use agentsets.
Each person has their own bag called 'my-balls' that contains the balls they select. That is initialised as a turtle-list as part of the setup.
I used a variable called 'chosen?' that is owned by each ball to track whether it is still in the bag for the next person to choose. Then the bag-of-balls is created only as all the balls not yet chosen.
The code for weighted random choice (when choosing from agentsets) simply has the name of the variable holding the weight as the reporter, but you could use some function such as rnd:weighted-one-of bag-of-balls [size ^ 2] if you wanted a more complicated weighting scheme.