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

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.

Related

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)

In an «ask turtles» method: How can I read a variable of a patch in a relative angle and distance from a turtle?

I want [turtles with [shape = "sheep"]] to move either to the lefthand side or the righthand side, depending on how many [attractive (green) patches] are around a certain relative patch.
How the turtles with [shape = "sheep"] should count the patches (yellow)
The (incorrect) code looks like this:
to move-sheep
ask turtles with [shape = "sheep"] [
right random (16 + (count neighbors4 with [pcolor = green] patch-right-and-ahead 60 2)^ 2)
left random (16 + (count neighbors4 with [pcolor = green] patch-right-and-ahead -60 2)^ 2)
forward 1
#(some other commands…)
]
end
Thank you ^^
It would be much easier if you actually told us what the problem is. You say your code is wrong, but not how you know it's wrong. Is it reporting an error (and, if so, what is the error and which line is reporting it)? Is the moving turtle going the wrong way?
Regardless, the easiest way to approach this is to do something smaller before trying to move. If you simply count the number of green patches and print that out, you can test the code. Once you introduce movement, how will you tell if it counted correctly?
I am still not entirely sure what you are asking. But I think this code might help you diagnose your problem. It tells you what the count is around the target patches, so you can see if it is doing the correct count. Once you know the counting works, you can then modify for movement.
to testme
clear-all
ask patches [set pcolor one-of [yellow green]]
create-turtles 1 [set heading 30 set color black]
ask one-of turtles
[ ask patch-right-and-ahead 60 2 [set pcolor red]
type "Right: "
print count ([neighbors4] of patch-right-and-ahead 60 2) with [pcolor = green]
ask patch-right-and-ahead -60 2 [set pcolor red]
type "Left: "
print count ([neighbors4] of patch-right-and-ahead -60 2) with [pcolor = green]
]
end

Netlogo: How to make turtles stop moving when a condition (suitable patch is found) is met?

I'm trying to construct a model that simulates patterns of housing selection in a given area.
I am asking turtles to seek a settlement patch in radius-3 that offers the most resource, if one is found, they should settle there and stop moving; otherwise, they should move to somewhere else and stop moving. However, no matter what I do, no turtle seems to stop; every turtle is constantly moving around regardless of whether a settlement is found.
Below is my seek-settlement module under "go". Please advise on how to make a turtle stop moving if a condition is met (i.e. a suitable settlement is found)? I tried sticking “stop” in various places but with no difference.
to seek-settlement-1
ask turtles with [wealth >= com]
[
ifelse any? patches in-radius 3 with [pcolor = green and count turtles-here = 0]
[ ;;; if there are any neighboring empty green patches with resource larger than wealth of the turtle
move-to max-one-of patches in-radius 3 with [pcolor = green and count turtles-here = 0] [resource] ;+ (0.5 * count turtles in-radius 3 with [color = blue])]
set settlement patch-here ;;; move to the one with the most utility (defined as an even combination of resource and number of blue turtles in radius 1) and settle there
stop ;;; this stop appears to make no difference
]
[right random 360
forward 3
set wealth (wealth - com)
stop] ;;; this stop appears to make no difference
if settlement != nobody
[
set wealth (wealth - com + (0.5 * [resource] of settlement)) ;;; update the turtle's wealth by subtracting the cost of movement and adding 20% of the resource of the patch here
ask settlement
[set resource (0.5 * resource)] ;;; update the settlement patch's resource by subtracting 20%
stop ;;; this stop appears to make no difference
]
]
end
Please review the documentation of stop:
http://ccl.northwestern.edu/netlogo/docs/dict/stop.html
The stop will merely exit your ask.
Your code does not match your description. Trying to follow your description, I get something like the following:
globals [com]
patches-own [resource]
turtles-own [wealth settlement]
to setup
ca
set com 10
crt 50 [
set wealth random 500
set settlement nobody
setxy random-xcor random-ycor
]
ask patches [
if random-float 1 < 0.2 [set resource 100 set pcolor green]
]
end
to go
;you can change the filter any way you wish
ask turtles with [wealth >= com and settlement = nobody] [seek-settlement]
end
to seek-settlement
let _candidates (patches in-radius 3 with [pcolor = green and count turtles-here = 0])
if any? _candidates [
set settlement max-one-of _candidates [resource]
move-to settlement
]
ifelse (settlement = nobody) [ ;this conditional shd probably be in a separate proc
right random 360
forward 3
set wealth (wealth - com)
][
set wealth (wealth - com + (0.5 * [resource] of settlement))
ask settlement [set resource (0.5 * resource)]
]
end

face to minimum heading difference

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

turtles overshooting their target and getting stuck in infinite movement loop

I am modeling tax evasion. My model has stationary traders (one breed of turtles) and customers (another breed).
The traders go towards the cheapest trader in their vicinity.
As long as my model has less than 10 traders, I have not experienced this problem. But as I ramp it up to over 20, one of the traders seem to, by chance, be at a set of coordinates that make every customer get stuck in an infinite movement loop where they move forward by 1, but overshoot the target, turn around, and overshoot again etc.
I can get less problems by decreasing forward movement to say 0.001 instead of 1, but the problem will still occur eventually.
Is there a quick fix to this problem? I can imagine a solution where an ifelse makes them jump directly into the traders coordinates when within range 1 or something, but is there an easier way?
I have tried implementing moving towards nearest trader given distance - as I suggested above, but now the customers get stuck in conga lines in groups in random locations without a trader
Here is the code regarding movement:
to find_food
ifelse ( num-traders-close < 2 )
[nearest_food]
[choose-cheapest]
end
to nearest_food
let nearest-food min-one-of (traders )[distance myself]
let cf-dist distance min-one-of traders [distance myself]
ifelse closest-trader > 1
[face nearest-food
fd 1]
[face nearest-food
fd cf-dist]
end
to choose-cheapest
let cheapest-food min-one-of traders [price]
let cf-dist distance min-one-of traders [distance myself]
ifelse closest-trader > 1
[face cheapest-food
fd 1]
[face cheapest-food
fd cf-dist ]
end
In the code in your other question , I have tried to follow your own code, but you can do this as well :
instead of finding one traders , you always check one-of close traders to that customer, and cheapest food and nearest food are customer property.
Breed [Customers Customer]
Breed [Traders trader]
Customers-own
[cheapest-food nearest-food traders-close]
traders-own [price]
to setup
random-seed 234523432
clear-all
Create-traders 10 [move-to one-of patches set price random 100 set shape "house" set color white ]
create-Customers 50 [move-to one-of patches set shape "person" ]
reset-ticks
end
to go
ask customers
[
set-customers
find_food
]
tick
end
to set-customers
rt random 100
fd 1
set traders-close traders with [distance myself < 5]
set nearest-food min-one-of (traders-close )[distance myself]
set cheapest-food min-one-of traders-close [price]
end
to find_food
ifelse ( count traders-close < 2 )
[ifelse nearest-food != nobody
[Move-to nearest-food ]
[Move-to min-one-of Traders [Distance myself]]
]
[ ifelse cheapest-food != nobody
[Move-to cheapest-food]
[Move-to min-one-of Traders with [Distance myself < 5][price]]
]
end