1

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!

Koolade786
  • 11
  • 2

1 Answers1

1

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.

Charles
  • 3,888
  • 11
  • 13