face to minimum heading difference - netlogo

How can I face or rotate or set the heading of an agent (turtle for example) to that an element of a patch-set which needs minimum rotation.
So the agent has a initial heading, and we have a patch-set (for example 5 patches in a cone), and I would like to face the agent that one which is at minimum in angle difference. I don't want to use patch-ahead because it can be one patch backward if that is the only one.
I tried some combinations with these commands:
min-one-of towards myself self heading subtract-headings towardsxy face - 180
Thank you in advance.

Are you having trouble getting the heading differences with subtract headings? Then you can try this:
to-report abs-hdiff [#t #p]
let _current [heading] of #t
let _new [towards #p] of #t
report abs (subtract-headings _current _new)
end
For example:
to test
ca
ask n-of 5 patches [set pcolor red]
let _patches (patches with [pcolor = red])
crt 1
ask turtle 0 [
hatch 1 [pen-down fd 10 die] ;just to see old heading
face min-one-of _patches [abs-hdiff myself self]
]
end

Related

Getting error "There is no agent for MYSELF to refer to" after new procedure is added, but worked before. (Netlogo)

I am trying to simulate an F1 race where the cars go around different tracks and need to pit when their tyres have degraded enough. They stick to the patches that are the track color (black). I have the code where I get the cars to follow the track shown below:
breed[cars car]
cars-own[tyre-level
tyre-type
tyre-total
speed
maxSpeed
draftSpeed
speed
]
;part of the setup
create-cars num-cars [
setxy xcord ycord
set shape "car top"
set size 5
set heading head
set tyre-level one-of (list soft_tyre_durability medium_tyre_durability hard_tyre_durability)
if tyre-level = soft_tyre_durability [set tyre-type "Soft"]
if tyre-level = medium_tyre_durability [set tyre-type "Medium"]
if tyre-level = hard_tyre_durability [set tyre-type "Hard"]
if tyre-level = soft_tyre_durability [set tyre-total soft_tyre_durability]
if tyre-level = medium_tyre_durability [set tyre-total medium_tyre_durability]
if tyre-level = hard_tyre_durability [set tyre-total hard_tyre_durability]
set maxSpeed 30
set draftSpeed 50
]
end
to move ;; turtle procedure
ask cars [face-chosen-neighbor
fd 0.5
set tyre-level tyre-level - 0.01]
end
to face-chosen-neighbor ;; turtle procedure
;; turtle faces the patch that requires the least change in
;; heading to face
let closest-border-patch min-one-of different-colored-neighbors [abs turn-amount]
rt [turn-amount * track-help] of closest-border-patch
end
;; computes the turn the calling turtle would have to make to face this patch
to-report turn-amount ;; patch procedure
let this-patch self
report [subtract-headings (towards this-patch) heading] of myself
end
to-report different-colored-neighbors ;; patch procedure
;; report neighbors that are a different color than me
report neighbors in-cone 2 180 with [pcolor = [pcolor] of myself] ;; report patches or neighbors in cone
end
This runs okay. However I now change the move procedure and add a speed procedure for the accelerating and decelerating - they slow down on turns and speed up if they aren't. They also have an increased max speed if drafting another car.
to move ;; turtle procedure
ask cars [face-chosen-neighbor
control-speed
fd speed / 200
set tyre-level tyre-level - 0.01]
end
to control-speed
let drafting any? other cars in-cone 4 130
ifelse drafting = nobody [set maxSpeed maxSpeed * ( (tyre-level + 50 ) / tyre-total)] [set maxSpeed draftSpeed * ( (tyre-level + 50 ) / tyre-total)]
if turn-amount * track-help > 60 [set speed speed - (deceleration * turn-amount * 0.5)]
let car-ahead any? other cars in-cone 1 130
ifelse car-ahead = nobody [
ifelse speed < maxSpeed [set speed speed + acceleration] [set speed speed - deceleration]
]
[
ifelse [speed] of car-ahead >= maxSpeed [
set speed maxSpeed
set speed speed - deceleration
] [
;try to overtake
ifelse [pcolor] of patch-left-and-ahead 90 1 = [pcolor] of myself and not any? turtles-on patch-left-and-ahead 90 1
[move-to patch-left-and-ahead 90 1] [
ifelse [pcolor] of patch-right-and-ahead 90 1 = [pcolor] of myself and not any? turtles-on patch-right-and-ahead 90 1
[move-to patch-right-and-ahead 90 1] [
set speed [speed] of car-ahead
set speed speed - deceleration]
]
]
]
end
Now if I run this, I get the error "There is no agent for MYSELF to refer to". I assume its got to do with the different definitions of self and myself. I have tried my best to understand it all and have scoured the forums. I'm not sure if I still understand it correctly.
Any help will be greatly appreciated!
I suspect that the error occurs in the lines where you look at the [pcolor] of myself. self and myself can be confusing. Here you have asked cars to look at the color of the patches that they are on, so it is self that you want. Agents can directly determine the color of the patch they are on, in a sense, the patch's pcolor is one of the agent's built-in variables, like its own color, so in fact, you could just ask the car to look at pcolor. Since an agent, in this case a car, can be on only one patch at a time, there is no confusion as to which patch is being referred to. Equivalently, you could use [pcolor] of patch-here.
myself would be used if the car asked the patch to then look at one of the car's variables. E.g., if the car asked the patch to do something with the tyre-type of the car who asked, it would be [tyre-type] of myself.

Minimum distance and interference between turtles

I am doing boarding processes model with anti-Covid measures. I would like to know if any of you could help me.
I would like to enforce that the minimum distance between turtles will be 4 patches.
I would like to identify if there is an interference, that is, that a previous turtle occupies a seat that prevents a new agent from passing through.
I don't know how to insert this in my code:
to go
; stop when all the pessengers have found a seat
if not any? turtles with [seated? = false]
[stop]
set counter counter + 1
(foreach (sort turtles with [seated? = false] ) [a ->
ask a [
; check if we have reached the correct row of seats
ifelse [seat-row] of patch-at 1 -1 = assigned-seat-row
[
; only seat the passenger if he/she has stored the luggage OR if we don't take luggages into account
ifelse luggage-store-ticks = 0 or storing-luggage-takes-time = false
[
let seat patch-at 1 -1
set xcor [pxcor] of seat
set ycor assigned-seat-number
set seated? true
]
[
set luggage-store-ticks luggage-store-ticks - 1
]
]
[
let passenger-ahead one-of turtles-on patch-ahead 1
ifelse passenger-ahead != nobody
[
set speed [speed] of passenger-ahead
if xcor != airplane-door-x-cor
[fd speed]
]
[
set speed default-speed
fd speed
]
]
]
])
end
You could extend your existing patch-ahead 1 to also look at the patches 2, 3 and 4 ahead. But I think it would be easiest to use in-cone 5 10 or similar. That will look ahead in a cone shape, 10 degrees on either side of the heading and a distance of 5. So you could do something like:
let potential-blockers turtles in-cone 5 10
let blocker min-one-of potential-blockers [distance myself]
That should (not tested) find the closest turtle approximately in front and name it "blocker" so that you can do things like check if it's far enough away, match speed (see the basic traffic model in the NetLogo models library)

netlogo: have patches calculate influence value using reporter

I'm modeling a cityscape for looking at movement, where patches equate to buildings and have different influence values. I need to use a reporter function that calculates the value of each patch with something like:
(its own influence) plus (the influence of neighbors) divided by the
distance to a specific turtle (max-one-of distance myself)
The turtle will then move towards the patch with the highest influence value, but along a defined street.
I'm new to using netlogo and have gotten completely stuck.
I've included a portion of what I have so far, but I can't figure out how to write the reporter function that will compute each patches influence value (in-cone) so that the turtles can then move towards the best option.
to setup-influence-field
ask patches with [pcolor = green] [set influence commercial-influence]
ask patches with [pcolor = orange] [set influence production-influence]
ask patches with [pcolor = yellow] [set influence domestic-influence]
ask patches with [pcolor = pink] [set influence religious-influence]
ask patches with [pcolor = blue] [set influence public-influence]
end
to go
move-serapis
end
to move-serapis
ask serapis [set-procession-heading]
repeat 2 [ ask serapis [ fd .25 ] display ]
tick
end
;;;;; the reporter values are need for this part of the code so that the turtles (serapis) can move towards the patches with the highest influence value;;;;
to set-procession-heading
let direction patches in-cone 4 40 with [influence-field > 0]
if any? influence-field
[face max-one-of influence-field] ;;;; face towards the highest computed influence value
ifelse any? patches with [pcolor = black] in-cone 1 25
[process]
end
Any help would be greatly appreciated!
I don't think this is completely correct and I can't test it, but it should start you off and maybe someone here can fix it if you let us know the errors.
to set-procession-heading
let direction-targets patches in-cone 4 40 with [influence-field > 0]
if any? direction-targets
[ face max-one-of direction-targets [ influence-amount self ] ]
end
to-report influence-amount [ target-node ]
report ( [ influence-field ] + sum [ influence-field] of neighbors ) / distance target-node
end
What I have done is set up a separate procedure to report the results of your calculation. That procedure takes an argument (named target-node) because you need to be able to pass the identity of the turtle being influenced. Once you have that procedure, then you can simply pass the agent-set of potential directions to the calculation procedure and pick the one with the largest value.

Test if the nearest turtle is in-radius

I try to ask to my turtles: if the nearest turtle is in radius lower to "distance-min" (a global), so get out!
But the following code doesn't work, and I really don't understand why.
to go
ask boeufs [get-out]
tick
end
to get-out
let x min-one-of boeufs in-radius distance-min [distance myself]
ifelse (x != nobody) [
face x
rt 180
fd 1
set color red
]
[
wiggle
]
end
It seems like if: x equals to the turtle himself …
the condition is always true, even with only 1 turtle in the world, why? :/
You want to use other. All it does is take an agent set and remove the current agent from it. So your code becomes:
let x min-one-of other boeufs in-radius distance-min [distance myself]
other boeufs won't include the turtle itself, so it will never be reported by this.

On netlogo, what command do I use to make a turtle stop if the patch it wants to move to is a certain color

I'm making a maze on netlogo and I want to do it so that once it tries to walk into the violet lines, it'll stay on its own patch instead of moving forward. What command would that be? I tried bk 1 to reverse the fd 1 but it doesn't work all the time
You can undo your step like this:
ask turtles [
fd 1
if pcolor = violet [fd -1]
]
Or you can check ahead of time as Marzy answered. Basically it's the difference of asking for forgiveness vs permission :-)
I hope this example answer your questions:
turtles-own [target]
to setup
clear-all
reset-ticks
ask n-of 100 patches [
set pcolor red
]
create-turtles 1
[ move-to one-of patches with [pcolor != red]
set heading 90
set target one-of patches with [pcolor != red]
ask target
[
set pcolor green
]
]
end
to go
ask turtles
[ifelse pcolor != green
[
ifelse [pcolor] of patch-ahead 1 != red
[
Your-Move-Function
]
[
Your-Bounce-Function
]
leave-a-trail
]
[stop
print ticks
]
]
tick
end
to Your-Move-Function
let t target
face min-one-of all-possible-moves [distance t]
fd 1
end
to Your-Bounce-Function
let t target
face min-one-of all-possible-moves [distance t]
end
to-report all-possible-moves
report patches in-radius 1 with [pcolor != red and distance myself <= 1 and distance myself > 0 and plabel = "" ]
end
to leave-a-trail
ask patch-here [set plabel ticks]
end
This is how it works:
Random patches are colored Red to show walls or obstacles, one turtle is created in a random location with a random target which is colored green:
I have used a variable to store all available patches which turtle can step on , but since I have considered a target for the turtle, turtle chooses the one patch which is closest to the target, and since I have noticed in some cases it might go in circle I have asked the turtle to leave tick number which is its move number as a plabel, you can use a variable for that for specifying if that path was already selected or not.