How to choose the agents that have different value in the set - netlogo

I would like to change the value of the variable my-inventory-slot of a set of agents but only in the case that value is not repeated (i.e. his value is unique in the set).
breed [ tags tag ]
tags-own [ my-inventory-slot inventoried? ]
breed [ readers reader ]
and
and I have tested
ask my-tags with [ my-inventory-slot != [my-inventory-slot] of self ] [
set inventoried? true
set color red
]
where my-tagsis a reader variable containing the tags around the reader.
The problem is in the selection with [ my-inventory-slot != [my-inventory-slot] of self ] because I have try my-inventory-slot = 5 and the code works fine.

UPDATED
let thistag [ my-inventory-slot ] of one-of my-tags
ask tags with [ my-inventory-slot != thistag ] [
set inventoried? true
set color red
]
UPDATED AGAIN - after downloading code
This is the code that calls the problem procedure:
to go
ask reader 0 [
setup-inventory-in-frame
one-frame-inventory
]
end
So a reader is being asked to run the code. However, readers do not have a variable my-inventory-slot, this is a variable belonging to the tags breed. This is your problem, you need to work out the connection between the reader running the code and the my-inventory-slot variable that you want the match to.
From the chat discussion, what you actually want is the tags belonging to the reader (that is, the variable my-tags) that have unique values of my-inventory-slot. I think this code will do that:
to one-frame-inventory
let frame-time 0
let unique-tags my-tags
let forward-list [ my-inventory-slot ] of my-tags
let reverse-list reverse forward-list
let num-tags count my-tags
ask my-tags
[ if position my-inventory-slot forward-list + position my-inventory-slot reverse-list != num-tags - 1
[ set unique-tags other unique-tags ]
]
ask unique-tags
[ set color red
set inventoried? true
]
end
It is very ugly, so someone else's answer would be good. What this does is strips out the my-inventory-values for the relevant tags into a list. It creates two copies of that list, one in normal order and the other reversed. It identifies non-unique values by finding those with a different position for the first appearance in those two lists. If it's not unique, the relevant tag is removed from the unique-tags agentset.

I have solved the question asking any other my-tags, like this:
let kk my-tags
ask my-tags [
if not any? other kk with [ my-inventory-slot = [my-inventory-slot] of myself ] [
set inventoried? true
set color red
] ]
which is short and clear.

Related

comparing attributes of 2 different turtles breed

I have 2 turtles bread:
globals [ max-applicant ]
breed [ applicants applicant ]
breed [ investors investor ]
applicants-own [ ask-amount applied? industry country-of-origin funded? ]
investors-own [ allowed-industries-list allowed-countries-list no-of-applicants-funded ]
I want to perform a transaction based on whether the investor is allowed to do business with the applicant's industry and country of origin. An Investors can only perform a limited number of transactions based on the max-applicant value
In the code below, I'm trying to select an investor that hasn't reached the max transaction limit and then select an applicant that meets the conditions of the selected investor and fund that applicant:
to close-deal
let investorPerson one-of investors-here with [no-of-applicants-funded < max-applicant]
if investorPerson != nobody [
ask investorPerson [
let applicantPerson one-of applicants-here with [
industry member? [allowed-industries] and country-of-origin member? [allowed-countries] of myself
]
if applicantPerson != nobody [
ask applicantPerson [
set funded? TRUE
]
]
set no-of-applicants-funded no-of-applicants-funded + 1
]
]
end
This code doesn't run. is this the right way to design this operation?
There seem to be two problems:
See here the syntax of member?: it is member? value list, while in your code you have value member? list.
In the first of the two boolean statements where you use member?, you omitted the of myself that should address [allowed-industries].
Combining the two points above, you should have:
let applicantPerson one-of applicants-here with [
(member? industry [allowed-industries] of myself) AND (member? country-of-origin [allowed-countries] of myself)
]
The use of parentheses and the capitalisation of boolean operators are optional, just my own stylistic choice.
Side note: be aware that you are asking your investorPerson to
set no-of-applicants-funded no-of-applicants-funded + 1
outside of the command block that gets executed if there actually is an applicantPerson; i.e. your investors will increment no-of-applicants-funded even when applicantPerson = NOBODY.

How to get the id number of a new turtle just created?

I have the following code where I hatch a new agent,
to t-of-slowdown [ es-poi ]
if we-look > 0 [
set we-look (we-look - 1)
if (we-look <= 0) [
if es-poi and (not any? events-here) [
hatch-events 1 [
set color green
set size 5
set is-poi? true
set new-poi true
let m [[ end2 ] of cur-link] of myself
move-to m ]
set events-x ([who] of events-here)
show events-x
set we-poi-var va-geometric (1 / 1500) + we-ticks poi
set sera-poi false
]
set impregna true
set color red
set seguir true
set we-look random-normal 120 20 ;time to watch an event
]
]
end
which is run in a turtle context (walkers breed)
A walker is moving by a 'link' (another procedure which calls this one), and when a counter is <0,
this code generates a new event (events breed) and places it in the same place where the walker is (cur-link is the current walker link).
After that, the walker must get the id number of the new event
set events-x ([who] of events-here)
The problem here is that variable events-x get an empty list []. The next time the walker passes by the same event it does get the number-id of the event.
Something must be wrong but I can not guess what it is.
I would appreciate very much if someone could take a look and point me some help.
Regards
You could:
let child-who -1
hatch-events 1 [
...
set child-who who
...
]
set events-x child-who
Or:
hatch-events 1 [
...
let child-who who
ask myself [ set events-x my-who ]
...
]
Both of these are a bit clunky, sadly. The second one avoids needing to initialize child-who to a meaningless value, but it requires using myself, a primitive that is likely to mystify the reader.
You could avoid both problems with:
let parent self
hatch-events 1 [
...
let child-who who
ask parent [ set events-x child-who ]
...
]
(But note that using who numbers at all, for anything, is rarely the best and most idiomatic solution to any problem. It's almost always better to store a reference to the turtle itself.)

NetLogo: create a dynamic number of breeds

how can i create a dynamic number of breeds at runtime?
I want my user to be able to choose the amount of breeds.
I thought about something like
to setup_breeds
let j 1
while[j <= n_groups][
breed[j]
]
end
where n_groups is the number of breeds whick is taken from a slider.
But unfortunatly i cannot use breed inside a method...
Any ideas?
Thanks!
You need to explicitly declare each breed with the breed keyword, so the short answer is: no, you can't have a dynamic number of breeds.
But do you really need actual NetLogo breeds? The main purpose of having different breeds is to have different variables for each breed. If that is not case, perhaps you can get away with having a group-id turtle variable. To create a certain number of turtles for each of n_groups, you could just do something like:
turtles-own [ group-id ]
to setup
clear-all
let n_groups 10
let n-turtles-per-group 5
foreach n-values n_groups [ ? ] [
create-turtles 10 [ set group-id ? ]
]
ask turtles [ set label group-id ]
; do something with only turtles of, e.g., group 2:
ask turtles with [ group-id = 2 ] [
fd 5
]
end
If you think you really need breeds, edit your question to tell us why, and we'll see if we can find a solution for you.
Side note:
I used foreach n-values n_groups [ ? ] to loop through your n groups. That's the equivalent of:
let i 0
while [ i < n_groups ] [
set i i + 1
]
...but arguably more "NetLogo-ish".

How to link actions of agents?

I am trying to program the simulation with 3 sets of agents - A,B,C.
The point is that the agents from the set A can choose to DO the action or NOT.
If they decide to NOT do the action, the simulation stops.
When they decide to DO the action, simulation continues to the next step, where the agents from the set B can also decide to DO the action or not. The same is here.
And the agents from the set C, can also decide to DO the action or NOT, but here, the simulation in both cases stops.
Here is my code:
ask turttles [
if breed = set A [ ifeslse do?= false [ set lazy]
stop]
[ if breed = set B [ ifelse do1?= false [ set lazy]
stop]
[ask other turtles [ if breed = set C [ ifelse do 2? = false [ set lazy
stop] ]
[set done
stop] ]
]
]
]
The code does not work very good,I need somehing to link these three step, because when I export-world, I got data only from the first step
If you do stop inside of an ask, it won't cause the whole simulation to stop. It will only stop the current turtle from executing the rest of the ask.
I think you want something more like:
globals [done?]
to setup
...
set done? false
...
end
to go
if done? [ stop ]
ifelse ...
[ ask A [ do-action ] ]
[ set done? true ]
ifelse ...
[ ask B [ do-action ] ]
[ set done? true ]
ifelse ...
[ ask C [ do-action ] ]
[ set done? true ]
...
end
But I'm guessing somewhat, since it's difficult to tell from your description what your actual intentions are. (Especially since you haven't included your real code — the coede in your question wouldn't get past the compiler.)

NetLogo nw extension: how to save the links reported by `nw:weighted-path-to`?

I want to save the links of the shortest path between the source and destination , so thagt i can change their color to red ie the links' color. But theres is no primitive to save the links
the code is:
ask nodes with [label = "Source" ]
[
show nw:weighted-path-to turtle nodenumberdestination "bandwidth"
]
can somebody tell me how to save the links reported by the nw primitive used above, so as to change their color to red in the graph?
I'm not entirely sure what you mean by saving the links, but you can store the list of links in a variable. So, if you have a turtles-own variable path-to-destination, you can just do
ask nodes with [label = "Source" ] [
set path-to-destination nw:weighted-path-to turtle nodenumberdestination "bandwidth"
]
Alternatively, you can just store the list of links in a local variable if you don't need to do anything with them later:
ask nodes with [label = "Source" ] [
let path-to-destination nw:weighted-path-to turtle nodenumberdestination "bandwidth"
]
As for turning them red, nw:weighted-path-to returns a list of links, so we can just loop through that list ask each to become red. Extending the previous code, that looks like:
ask nodes with [label = "Source" ] [
let path-to-destination nw:weighted-path-to turtle nodenumberdestination "bandwidth"
foreach path-to-destination [ ask ? [ set color red ] ]
]