I'm working on making my program interact with an overarching framework that transfers information with lists.
I have a breed called 'people' and I export information about them with
to srti-lists
ask people [ foreach [self] of people [
set traits-list (list (who) (color) (heading)(xcor)(ycor)(shape)]]
set master-list [traits-list] of people
end
It works great for exporting that information. What I'm having trouble with is creating or updating people with information that I then receive in the form of a master-list.
I've been approaching it as probably a foreach problem, but the problem is that while that lets me execute commands for every item of the master-list, I haven't figured out how to then access the individual nested items.
So, say:
foreach master-list
[create person
set who item 0 master-list
etc. The problem is that syntax would create a person and then set the who as an entire sublist. Omitting the list throws up an error, and selecting the items more specifically is untenable since it'd be a list of variable length.
Any ideas how to iteratively select items from nested lists? Is foreach even the right approach?
I can't test it but this looks wrong to me:
to srti-lists
ask people [ foreach [self] of people [
set traits-list (list (who) (color) (heading)(xcor)(ycor)(shape)]]
set master-list [traits-list] of people
end
ask already loops through the people, so this looks like it is assigning to each turtle the list of traits for all turtles, and then the master-list is a list of those lists. You probably want:
to srti-lists
ask people [
set traits-list (list (who) (color) (heading)(xcor)(ycor)(shape)]
set master-list [traits-list] of people
end
On the setting of traits, the who number cannot be set - it is assigned automatically when the turtle is created and can never be changed. For example, try the following complete (broken) model and you will get an error:
to testme
create-turtles 5
ask one-of turtles [set who 10 ]
end
So I don't know what you mean when you say that who is assigned an entire sublist - who is always an integer and assigned sequentially.
Concerning your stated question - your syntax set who item 0 master-list would be find if you were trying to set a variable that you could actually set.
Related
I'm trying to simulate a car production factory and trying to keep separate lists of turtles who are on jobs and who are not on jobs.
I have a turtles-own variable: is-on-job?
I want to have a agent set of turtles not on jobs not-working-turtles
I want to have a agent set of turtles on jobs working-turtles
Whenever a turtle is on a job, is there a way to remove that specific turtle from the not-working-turtles agentset and add it to the working-turtles agentset?
Also on a different topic, is there a way to sort turtles based on distance to a specific patch and then if the closest turtle is on a job then go to the second turtle and so on?
You can create your two agentsets as:
let not-working-turtles turtles with [not is-on-job?]
let working-turtles turtles with [is-on-job?]
To move a turtle from one to the other, you will need code along the lines of:
to make-me-work
let free one-of not-working-turtles
ask free
[ set is-on-job? true
set not-working-turtles other not-working-turtles
]
set working-turtles (turtle-set working-turtles free)
end
Here I used the very convenient other primitive to remove the turtle doing the asking. You are more likely to want to do this as a procedure that takes arguments and pass the turtle to the procedure rather than randomly select one within the procedure, but the code would be similar.
On your question about choosing the closest, you don't need to sort. Instead, look at min-one-of in the NetLogo dictionary. If you can't work it out, please do a new question (StackOverflow should be only one question per question) with the relevant code.
I am trying to implement a basic function but I'm out of practice with Haskell and struggling so would really appreciate some help. My question is specifically how to select a section of a list by index. I know how to in other languages but have been struggling
[ x | x <- graph, x!! > 5 && x!! <10 ]
I have been fiddling around with basic list comprehension similar to what is above, and while I know that isn't right I was hoping a similarly simple solution would be available.
If anyone wants more information or felt like helping on the further question I have included more information below, thanks!
type Node = Int
type Branch = [Node]
type Graph= [Node]
next :: Branch -> Graph -> [Branch]
This is the individual question for the "next" function
This is the general set up information but most importantly that the graph is represented as a flattened adjacency matric
Apologies for the two pictures but it seemed the best way to convey the information.
As pointed out in the comments !! does not give you the index of a value in the way it seems you expect. It is just an infix for getting an element of a list.
There is no way to get the index of x like this in Haskell since the x object doesn't keep track of where it is.
To fix this we can make a list of objects that do keep track of where they were. This can be achieved with zip.
zip [0..] graph
This creates a list of tuples each containing their index and the value in graph.
So you can write your list comprehensions as
[ x | (index, x) <- zip [0..] graph, index > 5, index < 10 ]
Now this is not going to be terribly fast since it still needs to go through every element of the list despite the fact that we know no element after the 11th will be used. For speed we would want to use a combination of take and drop.
drop 5 (take 10 graph)
However if we wanted to do some other selections (e.g. all even indexes), we can still go back to the list comprehension.
In this case, you could drop 5 <&> take 4. As in drop 5 x & take 4. Drop skips the first few elements and take leaves out all but the first few left after the drop.
In Netlogo, I have turtles-own lists, which means I set a turtle's variable to be a list. Each tick, another value is added to the list. After a few thousand ticks, these lists are quite long... and the problem arises that I can't open the agent monitor in the GUI any more because it takes too long to load the list.
reproducible code:
breed [persons person]
turtles-own [examplelist]
to setup
clear-all
reset-ticks
create-persons 1 [setxy 0 0]
ask turtles [set examplelist []]
end
to go
ask turtles [set examplelist lput ticks examplelist]
tick
end
I would need the agent monitor to watch another turtle-own variable; I don't need to watch the lists (they are just used to do a calculation every 8760 ticks).
Is there maybe a possibility, to e.g. hide the list from the agent monitor? Or do I need to handle the lists as global variables instead? Being quite unhandy as I would need to create and name separate lists for every turtle...
I can see three options:
1/ If you are creating a modelling framework, I assume that your user cannot actually code in NetLogo. This means that you have to predefine the scenarios for them anyway (for example, they could choose the calculation), so you only need to have the possible calculations stored instead of all the input values to those calculations.
2/ It is not clear from your question why any user would open an inspect window or otherwise access the individual turtle. If the user doesn't need it directly, instead of adding all this information to the turtles, you could export it to a file, adding a line each tick. The user would do the analysis of the simulation in R or Excel or whatever.
3/ You could create a shadow turtle for every turtle. This is not something I would recommend, but the idea is that the shadow turtle has a subset of variables (not the lists) and the variable values it does have are identical to the turtle it is shadowing. The limited set of variables version of the turtle is the one that would accessible to monitor.
Let's say I have a list of turtles and each turtle in this list has a own numeric variable foo. How can I elegantly extract the turtle with the lowest value stored in foo without iterating the whole list?
Thank you in advance!
Eric.
If you want the turtle in the list with the lowest value of foo, then you can sort the list of turtles by the foo value for each turtle and then pick off the first turtle in the list. For NetLogo v6.0 that would be (assuming foo is a turtle-own variable and turtle-list is your list of turtles):
first sort-by[[t1 t2] -> [foo] of t1 < [foo] of t2] turtle-list
Charles
it depends a bit on the context (how is the list structured, who owns it, how is it built and how frequently), but in general I'd save the list in it's own global/turtle-owned variable then use the primitive 'min-one-of' to query it. If you give a reproducible example I can help you apply this to code, but something to the effect of
globals [foo-list]
;make foo-list;
let small_foo min-one-of [foo-list]
show small_foo
When you say you have a 'list' of turtles, do you actually mean the data type list, or do you really have an agentset (which is the more usual NetLogo construction of a group of turtles)? If you have an agentset, look at the dictionary for the primitive with-min. You will want some construction like let small_foo agentsetname with-min [foo]
I have a problem in Netlogo, where i should check if a neighbor patch of a specific patch is already colored black.
This is how i thought it could work:
ifelse [ask patch xcoord-temp ycoord-temp [ask patches in-radius rnd_radius [pcolor = black]]]?
[print "true"]
[print "false"]
Error Message is: Expected a TRUE/FALSE here, rather than a list or block.
xcoord-temp and ycoord-temp are calculated coordinates.
rnd_radius is a random radius between 1 and 15.
So does someone has an idea how to solve this?
Thanks!
Regards,
John
Okay first, the reporter patch receives an identificator that's linked to a patch. If you want to ask something to a patch at a certain position you want to use [patch-at]1.
Second, I don't understand the use of question mark in your code ?.
Third, by using [pcolor = black] the compiler understands you want to set rather than get.
Lastly, you can use the word of to access values of an agent.
So I would put it out like this:
to myprocedure
ask patch-at xcoord-temp ycoord-temp
[
check-color
]
end
to check-color
ifelse [pcolor] of (patches in-radius rnd_radius) = black
[print "true"]
[print "false"]
end
Note than I'm using an extra procedure that may not be complete, perhaps it would have to receive the temporal coordinates you use; unless these coordinates are global variables that change overtime, then you can use the procedures like that.
Please tell us if this helps you, I don't have NetLogo installed right now, I did that based on the dictionary.
Edit:
As stated in the comments
[pcolor] of (patches in-radius rnd_radius) will return a list, thus it can never equal black.
You'll have to ask for an specific neighbor of the patch or perhaps use one-of in the list resulting from the previous statement..
The simplest and least verbose way would be to write a reporter procedure like this
to-report any-black-neighbors-in-radius? [r]
report any? patches in-radius r with [pcolor = black]
end
You can then ask a patch to run this procedure using r as a parameter, and either print the value, or use it for something else. Like this:
ask one-of patches [print any-black-neighbors-in-radius 5]
If you want to ask a particular patch to run it, just ask that patch using patch, like so:
ask patch 5 -5 [print any-black-neighbors-in-radius 3]