I'm actively trying to reprogram the sample model of Traffic 2 lanes but with my own addition, I have added what looks like a foothpath with people at the bottom, but when I run the code it adds the 4 people required sometimes on the same patch. How do I fix this?
to make-people
create-people 4 [setup-turtles]
end
to setup-turtles ;;Turtle Procedure
set shape "person"
let y-coordinates (list -8 -7 -6 -5)
let remove-index random length y-coordinates
set ycor item remove-index y-coordinates
set y-coordinates remove-item remove-index y-coordinates
set xcor 19
end
The rest of the code is the same as the sample model in Netlogo under social science called Traffic 2 Lanes, with a breed of people he only other difference.
The problem is that each person is defining again the y-coordinates list for their own creation. The list does not carry over from the creation of one person to the next, so one person's removing one of the items from the list will not have any affect on the list that the next person defines anew when they are created. The easiest way around this is to define y-coordinates as a global variable so that each person will work on the same list. Thus when one person eliminates a coordinate, the next person will get that shortened list. Try
breed [people person]
globals [y-coordinates]
to make-people
set y-coordinates (list -8 -7 -6 -5)
create-people 4 [setup-turtles]
end
to setup-turtles ;;Turtle Procedure
set shape "person"
let remove-index random length y-coordinates
set ycor item remove-index y-coordinates
set y-coordinates remove-item remove-index y-coordinates
set xcor 19
show y-coordinates
end
The show statement will show you that the y-coordinates list is indeed being shortened by each new turtle.
Related
I have each patch holding two lists: directions and magnitudes. Both lists contain values that are computed based on nearby turtles:
directions contains the headings from the patch to the various turtles (so basically it is just the turtle's heading rotated by 180°);
magnitudes contains a value that is directly proportional to the turtles' mass and inversely proportional to the distance between the patch and the turtles;
The items of the two lists are coupled in their order, i.e. the first item of directions and the first item of magnitudes are computed based on the same turtle, the two second items are computed based on another turtle and so on.
What I want to achieve is to come up with two single patch-own values, my-direction and my-magnitude, representing in some way the weighted average of directions, where the weights are magnitudes.
To put it another way, I am thinking of this in terms of vectors: turtles are exerting on the patch a force that can be represented as a vector, always pointing in the direction of the turtle and with a certain intensity (the magnitude). The resulting vector (represented by my-direction and my-magnitude) should be the resulting average of these forces.
I have seen this question. It does not address the issue of a weighted average; however it mentions the concept of circular mean. I've delved into it a bit, but I'm not sure how to apply it to my case and even if to apply it: does it still apply even with the formulation of the problem in terms of vectors?
I've seen this question/answer on SE Mathematics where it is said that the average vector can be found by averaging x- and y-coordinates of the initial vectors. In my case, ideally all the pairs of values in the two lists form a different vector with origin in the patch at issue, with heading found in directions and length found in magnitude. I suspect I can find the coordinates of each vector by multiplying its sine and cosine by its magnitude, however at this point I'd use some guidance as I might be overcomplicating things (either from the maths perspective or the NetLogo perspective).
This one below is a reduced version of the code that brings to the point where target-patches (not focusing on all patches, in order to make it quicker) have the two lists populated.
globals [
target-patches
]
turtles-own [
mass
reach
]
patches-own [
my-direction
my-magnitude
directions-list
magnitudes-list
]
to setup
clear-all
set target-patches n-of 10 patches
ask target-patches [
set directions-list (list)
set magnitudes-list (list)
set pcolor yellow + 3
]
create-turtles 10 [
move-to one-of patches with [(not any? turtles-here) AND (not member? self target-patches)]
set mass (random 11) + 5
set reach (mass * 0.8)
set size (mass / 8)
set shape "circle"
]
populate-lists
end
to populate-lists
ask turtles [
let relevant-targets (target-patches in-radius reach)
ask relevant-targets [
set directions-list lput (towards myself) (directions-list)
set magnitudes-list lput (magnitude-based-on-distance) (magnitudes-list)
]
]
end
to-report magnitude-based-on-distance
report [mass] of myself / (distance myself * 1.2)
end
Your initial instinct is right. You can do something like
LET my-dx mean (map direction magnitude [ theta scalar -> scalar * sin theta ])
(I may have that map and anonymous syntax wrong please edit)
And do the same for my-dy using cos (edit: or maybe negative cos?)
patch-at my-dx my-dy is one way of getting the patch.
You can also do (atan my-dx my-dy) to gets the new direction
And distancexy my-dx my-dy to get the magnitude
Don’t remember if patch can use distancexy or patch-at, but hopefully so. Otherwise you have to use a helper turtle, and do the math yourself.
I think I did a orbital mechanics toy model once that did something like this. It’s hiding on turtlezero.com.
I have a NetLogo model. each turtle has two attributes, "closeness" and "deviation_from_oracle". Now let's say there are 1000 agents in the model. The question is, how can I plot the "closeness" against "deviation_from_oracle" ?
It would also be helpful if I can get a csv file from NetLogo that has the value of closeness and deviaiton_from_oracle of all turtles after for example 1500 steps.
I definitely agree with Hugh_Kelley regarding using Behaviorspace to output your values (or custom export functions that might make for easier data cleanup if you're looking to report values for a large dynamic number of turtles- depends on your comfort with your statistical software of choice).
If you do need to plot something on the interface to show your users or something, you may find the plotxy function does what you need. For example, you'll need a plot on the interface called "plot 1" and a single blank pen in that plot called "pen-0".
You can control that plot either by manually setting up its x and y extent or by using the set-plot-... commands as in this setup:
to setup
ca
crt 10
set-current-plot "plot 1"
set-current-plot-pen "pen-0"
set-plot-pen-mode 2
set-plot-x-range 0 17
set-plot-y-range 0 25
reset-ticks
end
If you need to have a value plotted for each of your turtles, you can get the turtles to call plotxy for whatever values you're looking to plot- here I just use their absolute x coordinate and distance to the center as an example:
to go
ask turtles [
rt random 61 - 30
fd 1
set-plot-pen-color color
plotxy ( abs xcor ) distance patch 0 0
]
tick
end
This gives output like:
Where each point was plotted by an individual turtle.
If you want instead some reported mean value, have the observer call plotxy instead- another example that plots the average distance to other turtles and the average distance to center:
to go
plotxy mean-closeness-to-others mean-distance-center
ask turtles [
rt random 61 - 30
fd 1
]
tick
end
to-report mean-closeness-to-others
report mean [ mean map distance sort other turtles ] of turtles
end
to-report mean-distance-center
report mean [ distancexy 0 0 ] of turtles
end
For an output like:
In my model, the number of turtle is dynamic based on the value defined by the user using a slider. The slider can take values between 2 and 10. Each turtle has its own set of co-ordinates and characteristics, hence i used the following code to create them.
create-parties 1
[set color red set label-color red set label who + 1 set size 3 setxy party1-left-right party1-lib-con ]
create-parties 1
[set color green set label-color red set label who + 1 set size 3 setxy party2-left-right party2-lib-con ]
if Num-of-parties >= 3
[ create-parties 1
[set color blue set label-color red set label who + 1 set size 3 setxy party3-left-right party3-lib-con ] ]
I have repeated the above till Num-of-parties =10.
In one of the modules i have created a condition where if a certain value of the turtle reaches 0 it will die.
In the later part of the model i have used set-current-plot to create a chart using the below code:
set-current-plot "Voter Support"
set-current-plot-pen "Party1"
plot 100 * [my-size] of turtle 0 / sum[votes-with-benefit] of patches
set-current-plot-pen "Party2"
plot 100 * [my-size] of turtle 1 / sum[votes-with-benefit] of patches
if Num-of-parties >= 3 [ set-current-plot-pen "Party3"
plot 100 * [my-size] of turtle 2 / sum[votes-with-benefit] of patches ]
so on and so forth for all ten possible turtles.
The problem is if the user has defined 5 turtles and turtle 3 dies at tick 10, then chart portion of the code is throwing an error since there is no turtle 3 but num-of-turtles slider defined by the user has a value of 5.
Please advise on how to solve this. Thanks, appreciate the help.
Regards
When writing model code, you should try to apply the DRY principle: Don't Repeat Yourself. Creating each turtle separately and then trying to do something with each of them by addressing them separately as turtle 0, turtle 1, etc. will lead to all sorts of problems. What you're experiencing with plotting is only the tip of the iceberg.
Fortunately, NetLogo gives you all the facilities needed to deal with a "dynamic" number of turtles. ask is the primitive you will use most often for this, but there are plenty of other primitives that deal with whole agentsets. You can read more about agentsets in the programming guide.
In the case of plotting, you can ask each of your parties to create a "temporary plot pen". We'll use the who number to give a unique name to each of these pens. (That's one of the very few legitimate uses of the who number in NetLogo.)
Put this code in the "Plot setup commands" field of your plot:
ask parties [
create-temporary-plot-pen (word "Party" (who + 1))
set-plot-pen-color color ; set the pen to the color of the party
]
(Note that you won't need the plot pens that you previously defined anymore: you can just delete them. New plot pens will be dynamically created every time you set up your plot.)
To do the actual plotting, we can use very similar code. Put this code in the "Plot update commands" field of your plot:
ask parties [
set-current-plot-pen (word "Party" (who + 1))
plot 100 * my-size / sum [ votes-with-benefit ] of patches
]
My simulation space is a total of 1024 patches. I need to randomly place 113 turtles within the middle area of my simulation space which is only 78 patches. Does anyone have any input on how to do this and make sure that the turtles are only in that specified area? Thanks!
Positions are simply numbers so you can set xcor and ycor in appropriate ranges by using the random-float generator and rescaling as required. For example, set xcor 3 + random-float 5 will locate the turtle with an x coordinate in the interval from 3 to 8
I'm doing a school project in Netlogo for the first time.
I need to use a table that saves the turtle's position as key and a value. The especific turtle has a breed (pacman). I've tried:
set tbl table:make
set xx [xcor] of pacman 10
set yy [ycor] of pacman 10
table:put tbl [xx yy] 1
//(the value 1 is not important)
Why doesn't it work this way?
If I make:
table:put tbl [-2 -5] 1 //(-5 and -2 are the xcor e ycor of pacman 10)
it works. But the pacman changes its position so I need to save each position in the table.
Thank you
http://ccl.northwestern.edu/netlogo/docs/faq.html#listexpectedconstant says:
If a list contains only constants, you can write it down just by putting square brackets around it, like [1 2 3].
If you want your list to contain items that may vary at runtime, the list cannot be written down directly. Instead, you build it using the list primitive.