how to see the color identification signal lights? - netlogo

layout intersections with traffic lights. I want the car to stop when the red lights and the lights go xanh.thi what I should use for car algorithm can recognize the color of the lights
I have used light layout algorithm as follows
to draw-stop-light
ifelse stop-light = "north"
[
ask patch -7 12 [set pcolor green ]
ask patch 10 10 [ set pcolor red ]
ask patch -10 -10 [ set pcolor red ]
]
[
ask patch -7 12 [ set pcolor red ]
ask patch 10 10 [ set pcolor green ]
ask patch -10 -10 [ set pcolor green ]
]
end

This is really a question better suited to the NetLogo users group here than Stack Overflow since you are asking how to program a poorly defined specification. For example, the users group may be able to point you to a similar model. Stack Overflow is really about fixing specific problems with your code. However, here's something to get you started until the question gets deleted.
Break down what you want to happen into very small steps and then code each step. The steps will be something like:
When the car gets near the intersection, check to see what colour the traffic light is
If the light is red, remember to stop at the intersection
Stop at the intersection if required
If at the intersection, wait until the light turns green then go
Do NOT try to write the whole thing. Instead, write one bit at a time and test is to see if it works. Here is some approximate code for the first bit:
ask cars
[ ifelse heading = 180 and ycor > 20 and ycor < 25 and not stop-flag
[ if [ pcolor of patch -7 12 ] = red [ set stop-flag true ] ]
[ ... (cars from other directions)
]
ask cars
[ forward speed
if stop-flag and ... (on intersection patches)
[ ... (move backwards to be outside the intersection)
set stop-flag false
]
]

patch-right-and-ahead (http://ccl.northwestern.edu/netlogo/docs/dictionary.html#patch-lr-and-ahead) could be useful here. Perhaps something like:
if [pcolor] of patch-right-and-ahead 90 1 = red
[ set speed 0 ]

Related

How to find the average distance from turtles that fulfill a certain condition?

I want to move the current turtle one step closer to the others that fulfill a certain condition (e.g. have color = green).
I am doing this the hard way (because I don't know any better), by trying to calculate the the average distance of the current turtle from all others that fulfill the condition, and calculate the average from x+1, x-1, y+1, y-1. Then whichever is the smallest would indicate the direction of the move. Not very elegant, I know, and limits movements to horizontal and vertical, but I couldn't come up with anything better (the only other idea that struck me was to calculate average x and y coordinates of all turtles that fulfill the condition and move the current turtle towards that, but that seemed even more ridiculous to me)
Problem is that even with my clumsy solution, I am not getting anywhere, since I am struggling with how to calculate the average distance from the "green" turtles.
If you want to calculate the mean distance, you can have the asking turtle call mean and [distance myself].
With this setup:
to setup
ca
crt 10 [
set color green
move-to one-of patches with [ pxcor < 0 ]
]
crt 1 [
set color red
move-to one-of patches with [ pxcor > 10 ]
]
reset-ticks
end
Calling the function below will have the red turtle print out first all distances between itself and all green turtles, then the mean of those distances:
to calc-mean-distance
ask turtles with [ color = red ] [
print [ distance myself ] of turtles with [ color = green ]
print mean [ distance myself ] of turtles with [ color = green ]
]
end
Beyond that, I'm not 100% sure what you're trying to do- are you hoping to move the asking turtle towards the nearest turtle that meets some condition? If so, this might work for you:
to go
ask turtles with [ color = red ] [
let target min-one-of ( turtles with [ color = green ] ) [ distance myself ]
face target
ifelse distance target > 1 [
fd 1
] [
move-to target
]
]
tick
end
If you want the asking turtle to move instead towards the geographic center of those turtles that meet a condition, you could indeed get the mean x and y coordinates of those turtles as you describe, then have the asking turtle move towards that point:
to go
let central-x mean [ xcor ] of turtles with [ color = green ]
let central-y mean [ ycor ] of turtles with [ color = green ]
ask turtles with [ color = red ] [
facexy central-x central-y
ifelse distancexy central-x central-y > 1 [
fd 1
] [
setxy central-x central-y
]
]
tick
end
If those aren't quite what you're trying to achieve, feel free to leave a comment for clarification!

How to check if a patch remains a state for a certain ticks\time?

Based on the “sheep eat grass” model. The patches are changing color with the agents move. I want to check if a patch has remain a centain color for a while (such as 5 ticks). If a patch has stayed in a centain color for a centain times without any change it will turn into black.
I try to use countup but it didn’t work. I need a accumulated state. Thanks a lot
If pcolor=green[
Ifelse countup>=5[
Set pcolor black
Set countup 0]
[set countup countup+1]]
Can you give a little more detail as to what is going wrong with the code you've shown? For example, with this setup:
patches-own [ time-spent-green ]
to setup
ca
crt 3
reset-ticks
end
Something very similar to your example works fine for me:
to go
ask turtles [
rt random 61 - 30
fd 1
ask patch-here [
set pcolor green
]
]
ask patches with [ pcolor = green ] [
ifelse time-spent-green >= 5 [
set pcolor black
set time-spent-green 0
] [
set time-spent-green time-spent-green + 1
]
]
tick
end
Where the patches stay green for 5 ticks then turn back to black.

How to manually adjust wrapping topology of netlogo interface?

Netlogo only provides four topology options that dictates what turtles and patches do when they reach the any given edge of the netlogo world or interface, namely, box, torus, horizontal wrapping and vertical wrapping ... Is there a way to adjust these options so that just one edge is wrapped? Or three edges are wrapped for instance?
Nigus- I do remember, I never forget a Corgi! Anyway, as Jen mentioned there is no built-in for this, but you can build it into your turtle movement rules. For example, with this setup:
to setup
ca
crt 10 [ pd ]
reset-ticks
end
If you want your turtles to treat a boundary as closed, you could have them check their patch-ahead by whatever their movement speed is (example speed of 1, here) and do a simple math operation to see if they are 'allowed' to wrap at that boundary. To have the left boundary closed, try:
to left-closed ; turtle procedure
ask turtles [
let target patch-ahead 1
if ( ( [pxcor] of target ) - pxcor ) <= 1 [
fd 1
]
]
tick
end
To have the right boundary closed, you can just do the opposite:
to right-closed ; turtle procedure
ask turtles [
let target patch-ahead 1
if ( pxcor - ( [pxcor] of target ) ) <= 1 [
fd 1
]
]
tick
end
Obviously, this is a very simple example and would need some work to massage it to fit your current movement rules.
Edit:
To show this in action, check out this example code. First, a modified setup that specifies where turtles should spawn and also creates a 'wall' of red patches:
to setup
ca
ask patches with [ pxcor = 5 ] [
set pcolor red
]
ask patch -5 0 [
sprout 10 [
pd
]
]
reset-ticks
end
Now, a modified version of the right-closed procedure above that turns it into a to-report:
to-report closed-border-right? [ target-patch ]
report ( pxcor - ( [pxcor] of patch-ahead 1 ) ) <= 1
end
Now, we should expect that turtles should not be able to cross the red wall. They should also not be able to travel off the right border, but they should be able to cross the left border. So, if the turtles are free to wander, using this movement procedure:
to move-example
ask turtles [
rt random 61 - 30
let target patch-ahead 1
if closed-border-right? target and [pcolor] of target != red [
fd 1
]
]
tick
end
We should expect them to eventually get 'trapped' between the unwrapped border and the red wall- and this happens as we expect, once the turtles make their way through the left border, they cannot get back:

To build patch clusters at large spatial scales

I used the code from How to create cluster patches that do not overlap between them to build patches as shown in the first figure below.
Here is the code :
to make-cluster
loop [
let cluster [patches in-radius (2 + random-float 2)] of one-of patches
if all? (patch-set [neighbors] of cluster) [pcolor = black] [
ask cluster [ set pcolor green ]
stop ] ]
clear-all repeat 20 [ make-cluster ]
When I use this code in a large spatial extent (i.e. 1000 x 1000 patches with patch size = 1 pixel), green patches are like circles (see the second figure below).
How can I have patches as shown in the first figure ?
Thank you very much for your help.
If your goal is to simply have heterogeneous regions (rather than specifically blocky, symmetric things), you might play around with some of the answers here: Creating a random shape (blob) of a given area in NetLogo
Frank's solution and my first solution will probably run pretty slow on that large of a world. I just added a solution that should scale to a world of your size. I've put it here too for convenience:
to make-blob [ area x y ]
let blob-maker nobody
crt 1 [ set blob-maker self setxy x y ]
let border patch-set [ patch-here ] of blob-maker
repeat area [
ask blob-maker [
ask min-one-of border [ distance myself ] [
set pcolor green
set border (patch-set border neighbors4) with [ pcolor = black ]
]
rt random 360
fd .8
]
]
ask blob-maker [ die ]
end
That said, if you like the blockiness, it's often the case that models with a large number of patches in a blocky formation can be reworked into models with a smaller number of patches that behave quite similarly. For example, one strategy is to scale down the size and movements of the turtles so that the world is still relatively large to them.

NetLogo Turtle position

I'm really new at programming in NetLogo and i need a little help. I have an assignment and i did most of it. The thing left to do is to make robot walk in labyrinth. Robot can walk only on a black patches (violet patches represent the obstacles).
So, the thing i need help with is to position robot in the center of the labyrinth - i must do it with "patch-here" (...i did it little bit differently in procedure "stvori-agenta") and mark that patch on which robot stands as black. So, afterwards i could write procedures for robots movements only on a black patches.
Here is the code:
breed [robots robot]
to crtaj-zidove
ask patches with
[
( pxcor = max-pxcor)
or (pxcor = min-pxcor)
or ( pycor = max-pycor)
or (pycor = min-pycor) ]
[ set pcolor violet]
end
to labirint
ask n-of 15 patches with [ pcolor != violet ] [
set pcolor violet]
end
to stvori-agenta
set-default-shape robots "robot"
ask patch 5 5 [ sprout-robots 1 ]
ask turtles [
set heading 0
set color grey
]
end
to setup
clear-all
crtaj-zidove
labirint
stvori-agenta
end
This will make the robot turn the patch it is standing on black:
ask robots [ set pcolor black ]
You say you must use patch-here. That isn't actually necessary, since turtles have direct access to the patches they are standing on. But you could also write it as:
ask robots [ ask patch-here [ set pcolor black ] ]
It does the same thing.