In the image below, the black box represents a patch. I wish to create agents of size-
(Patch-size/8)
and distribute them at the top of the patch as such.
Also, possible to to create a code such that it takes :
Patch-Size as input and distributes them accordingly.
Previous Code Used:
My previous approach sprout at the centre and moves them to align with the desired positions but it is considerably length and not effective if I wish to variate number of agents.
`
Ok, so, forget about patch-size for a moment, as we are going to be working with unit patches, without regard to the number of pixels.
I'm assuming the number of turtles is variable, and that we want the turtles to fit exactly inside the width of the patch, and with the tops of the turtles aligned with the top edge of the patch.
If the number of turtles is constant, or the maximum number is constant, and/or the size is constant, then the code can be a little more simple, as we can avoid recalculating some things.
Anyway, for C turtles:
The gap between the turtles is 1 / C
The "half-gap" is also useful, that's gap / 2
This is also the size of the turtles
Hence, the turtles will definitely just fit across the patch
The ycor of the turtles will be pxyor + .5 - half-gap
Since gap is the size, this puts the turtles along the top
The xcor will be pxcor - .5 + gap * N - half-gap
N is the number of the current turtle. So, the xcor varies from a half-gap from the left edge to a half-gap from the right edge.
Here, pxcor - .5 shifts the turtle center all the way to the left edge
Then, + gap * N shifts the turtle over N turtle widths,
Then, - half-gap shifts the turtle back one half turtle size.
This makes sure the first and last turtles are just touching the edge.
So, let's do it:
to align-inside-at-top ;; patch procedure
let counter count turtles-here ;; we will use this as a count-down, after using it in some calculations
if counter > 0 ;; could assume there are turtles, but we are not.
[ let gap 1 / counter ;; size of turtles, gap between turtles
let half-gap gap / 2 ;; half-size of turtles
let ytop pycor + .5 - half-gap
let xleft pxcor - .5 - half-gap
ask turtles-here
[ set size gap
set ycor ytop
set xcor xleft + gap * counter
set counter counter - 1 ;; so we're placing them from right to left
]
]
end
Hope this helps!
keep in mind no matter the patch-size patches are always 1 step across.
( pxcor - .5, pxcor + .5) X (pycor - .5, pycor +.5)
Patch-size is a sort of zoom it doesn't normally effect a simulation.
With that said and assuming you are doing this as some sort of demonstration or visualization. Anything you do for aesthetics tends to slow things down but here you go.
To line-up
Let c count turtles-here
Ask turtles-here
[
Set ycor pycor - .45
Set xcor pxcor + .45 - c / patch-size
Let c c - 1
]
End
If turtles on a patch > patch size it will mess up.
Related
I would need to visualize the below turtles (from three different breeds) in a squared grid where links are only between turtles from the same breed, except for breeds types2 and types3, that can be also linked to each other.
So what I would like to have is a 2D grid where the number of turtles per each type is
40% of type1
40% of type2
20% of type3
(in total 100 turtles).
set-default-shape types1 "circle"
set-default-shape types2 "circle"
set-default-shape types3 "triangle"
ask n-of 100 patches [ sprout-types1 1 ]
ask n-of (100 * 0.4) types1 [set breed types2]
ask n-of (100 * 0.2) types1 [set breed types3]
the values are ok but the turtles are 'free' in the world, not displayed on a grid.
How can I display them into a grid and link them based on the above conditions?
This answer Different types of turtles in a lattice grid has provided some help on this, but the resize of the grid and the number of turtles are not the expected ones.
You should resize the world before creating agents.
That is, from the perspective of the code's workflow: if you want to have n agents, then n is first and foremost the number of patches. Then, once this is the case, all patches will sprout.
You need the resize-world command.
You mentioned that you want to have 100 turtles, that is 100 patches, that is a 10x10 world.
This means that you could do:
to setup
clear-all
resize-world 0 9 0 9 ; This creates a 10x10 world.
set-patch-size 30
ask patches [
sprout 1 [
set shape "circle"
set size 0.5
]
]
end
The code above works as long as you are happy for your world to be the size of exactly 100 patches, given that the size = 100 is hard-coded.
You might want to think about some way to accomodate a change in the number of agents.
For example, the approach below works as long as the number of agents is the perfect square of an integer:
globals [
n-agents
]
to setup
clear-all
set n-agents 100
let side-length n-agents ^ (1 / 2) - 1
resize-world 0 side-length 0 side-length
set-patch-size 30
ask patches [
sprout 1 [
set shape "circle"
set size 0.5
]
]
end
After all the point is that the shape of the world in NetLogo can only be a square or a rectangle; i.e. you cannot have a NetLogo world that is made of a prime number of patches (only exception being a world whose world-height and/or world-width equal 1).
So, in order to have your code be the most accomodating to changes in n, you could come up with more elaborated steps that resize the world based on n so that it gives you n patches even when n is not a perfect square; but for example, unless you are happy to have a monodimensional world, you can never have 53 patches. However, since you are talking of grids, I think this shouldn't be a problem for you.
My code is about 2 car in one road, where both cars will change their heading (turn to left) if they meet head-to-head, and go back to the center of the road (which is pycor = 0) and turtles can't walk outside the road,
My problem is I don't know how to make the turtles move back to the center, I tried to use cohesion from flocking, but I don't know the right way. Here is my code:
globals
[initialHead too-close-distance]
turtles-own
[speed
top-speed]
to setup
clear-all
ask patches [setup-road]
setup-cars
reset-ticks
end
to go
move
end
to setup-road
if pycor < 2 and pycor > -2 [ set pcolor white
]
end
to setup-cars
create-turtles 1
[
setxy -15 0
set color red
set size 1.2
set initialHead 90
set speed 0.5
set top-speed 0.5 + random-float 0.5
]
create-turtles 1
[
setxy 15 -0
set color blue
set size 1.2
set initialHead 270
set speed 0.5
set top-speed 0.5 + random-float 0.5
]
end
to move
ask turtles
[
speed-up-car
avoid
forward speed]
end
to avoid
slow-down-car
let visibility (patches in-cone 7 50)
let center pycor = 0
let too-near one-of other turtles-on visibility
ifelse too-near != nobody
[turn-away ([heading] of too-near) max-separate-turn
[;need to turn back to center]
[ fd speed]
end
to turn-away [new-heading max-turn]
turn-at-most (subtract-headings heading new-heading) max-turn
end
to turn-at-most [turn max-turn] ;; turtle procedure
ifelse abs turn > max-turn
[ ifelse turn > 0
[ rt max-turn ]
[ lt max-turn ] ]
[ rt turn ]
end
to speed-up-car
set speed (speed + acceleration)
if speed > top-speed [ set speed top-speed ]
end
to slow-down-car
set speed (speed - deceleration)
if speed < 0 [ set speed deceleration ]
end
Here's some basic logic that I think will work for passing for the case of 2 cars heading opposite directions. The logic gets much more complex if you have many cars.
I'll leave it to you to write NetLogo code.
Basically, you need a state-transition plans which keeps track of what state each car is in, so it can decide what to do next and what state that will put it in.
Suppose you have states ["driving", "turning-away", "passing", "turning-back" ], and you know if you have conflicting traffic. You also know your y-position so you know how far off the center you are.
You need to figure out logically what all the possible and relevant transitions are and the rules that determine which one to take given what state you are currently in.
It would be interesting (!) to model the states as nodes and the transitions as links, but let's leave that for another time.
Basically you will end up with a long list of if-else clauses, which you can evaluate to decide what the next-state will be, and then after everything is evaluated, you can do what it takes to move to that state. Rather than building a full dynamic model of motion, let's simplify to say that passing involves turning to 10 degrees from the original heading for a bit, then by 30 degrees for a while, then by 10 degrees for a while longer, then back to 0 degrees when happily off the road. Returning to the center-line is just the reverse process. This would probably look ok to a user.
Here's some pseudocode:
if driving and there's no conflict, keep driving.
if driving [
if there's a conflict
[ set next-state "turning-away"
if off-road by short distance [set offset-angle 10]
if off-road by medium distance [ set offset-angle 30]
if off-road by large distance [ set offset-angle 10]
if off-road entirely [ set offset-angle 0 set state "passing"]
]
otherwise [ set next-state "driving"]
and move forward based on speed and heading.
if in the middle of turning away the conflict goes away, i don't know what you want to do. Probably completing move to the passing location is simplest to code.
if passing and there's still a conflict, keep on passing
if passing and there's no conflict anymore, reverse the moves based on distance off-road to get back on the road.
if in the middle of turning back there is a new conflict, i don't know what you want to do. Probably change to turning-away and pick a heading based on distance from the centerline as above.
Anyway, let me know what you think of that approach. I don't see how to make it much simpler.
I want some of my turtles to leave at trace of the steps they make. I want them to change the color of the patches they pass by on their move. Similar to what the "pen-down" command does - just with the effect, that the patches change color. It is esay for me to change the color of the patch, the turtle reach - but I want all the patches colored - like if you were walking on a lawn, and the grass under you momentaneouesly turns red :-) Nut just the steps with every tick, but the continous route.
Is there a way? I would be glad to se a small coded example. Thanks very much - this homepage does a great job.
Magellancruiser
Here is one basic solution. The key is realizing that instead of having the turtle do something like forward (random 10) + 1 you can instead have it go forward 1 a number of times equal to (random 10) + 1. Because the distances are based on path sizes (1 = 1 patch across), if you color the patches as you go forward 1 at a time, you should "draw" the color on the patch.
turtles-own [ my-color ]
to setup
clear-all
create-turtles 10 [
set my-color color ; the turtles will have random colors, store them to use later
set color white ; but they're easier to see if they're white
]
reset-ticks
end
to go
ask one-of turtles [
left (random 50) - 25 ; wiggle a bit to not just go in a straight line
let d (random 10) + 1 ; the turtle will move 1 to 10 steps
repeat d [
forward 1
set pcolor my-color ; the turtle can directly set the patch's pcolor variable to its own
]
]
tick
end
You can either use setup and go from the command center or add buttons for them.
I'm having some issues with the in-cone command in Netlogo. I am trying to identify the sum / mean of all the patch variables directly in front of my turtles current location (ie the sum of all the variables it crosses). However, this only appears to be working when my turtle is at the center of a patch (co-ordinates are integers not decimals), which also means I can only move my turtles at right angles. I'm yet to find any other questions pertaining to the same issue on Stackoverflow or elsewhere. So if anyone could offer some insight, I'd be greatly appreciative.
Below is the simple sample code. And I've annotated where making the changes causes this to not work.
Cheers
Paul
turtles-own [value]
patches-own [value-test]
to Set-Up
ca
reset-ticks
ask patches [if pycor > 150 [set value-test 1]]
ask patches [if pxcor > 150 [set value-test 1]]
ask patches [if value-test = 1 [set pcolor red]]
create-turtles 1
ask turtles[
;It works when the turtle is created at the origin (0 0), or at defined coordinates (but not random-xcor random-ycor)
;setxy random-xcor random-ycor
set value 0
set size 10
set color yellow]
end
to go
ask turtles[
;heading has to be 0, 90, 180, or 270.
set heading 270]
ask turtles[
let test mean [value-test] of patches in-cone 10 1
print test
print xcor
print ycor
ask patches in-cone 10 1 [set pcolor blue]
forward 10]
end
in-cone is not the right tool for the job. Unfortunately, NetLogo doesn't have a primitive that looks ahead in a straight line. It does, however, have patch-ahead, which reports a single patch at a given distance. We can use that to build something similar to what your looking for:
to-report patches-ahead [ dist step ]
report patch-set map patch-ahead n-values (dist / step) [ step + ? * step ]
end
This code may look puzzling at first, but what it does it actually quite simple:
It uses n-values to build a list of incrementing values: n-values (dist / step) [ step + ? * step ]. For example, if dist was 1 and step was 0.2, you'd get [0.2 0.4 0.6 0.8 1]. These values represent the distances at which we are going to be looking for a patch.
It uses map to call patch-ahead for each of values in the list and build a list of patches. Note that this list can contain duplicate patches, especially if step is small, since patch-ahead 0.1 and patch-ahead 0.2, for example, may very well be the same patch.
It uses patch-set to turn that list in a proper agentset of patches, without duplicates.
(You could achieve the same thing with a while loop and an incrementing counter, but the code would be longer, more error prone, and much less elegant.)
To use it, just replace instances of patches in-cone 10 1 in your code by something like patches-ahead 10 0.1.
You will notice that there is a trade-off between precision and speed: the smaller step is, the less likely it is to "skip" the corner of a patch, but the longer it will take to run. But I'm sure that you can find a value that works well for you.
Nicolas has a much better answer solving the problem of looking in a straight line but if you simply what look at the patch directly ahead use patch-ahead 1 it works at all angles and coordinates and is much faster than in-cone.
Completely an aside but probably the reason you found this bug is because your cone was set to 1 degree wide and 10 patches long. Long narrow cones tend to break up.
let pop area * density
distribute-turtles
to distribute-turtles [ pop box ]
if (pop > count box) [ error "Box can't hold all turtles!" ]
ask n-of pop box [sprout-inboxturtles 1[
set color blue
set exit true ;ignore
set alpha alpha-exit ;ignore
set beta beta-exit ;ignore
set size 0.9
]
]
end
The above code distributes turtles on patches.
box-patches
pop-number of turtles
I will create turtles such in accordance to the density within the box such that:
a.) no 2 turtles have the same centre, therefore there could more than 2 turtles per patch but shouldn't have the same centre.
The turtles already have random headings so if you have them go forward a random float less than .5 they will have unique coordinates.
fd random-float .5
Aesthetically the turtle size should be small enough to see each.
Remember turtles-here will report all turtles on the patch.