James
James

Reputation: 91

Tell agent to not cross a road in Netlogo model

I'm trying to add a condition that doesn't allow an agent to cross over a road. The road patches are set to color red. I can't seem to figure out how to get this condition to work. I ultimately want the agent to turn around if the road is in the patch ahead. Here is my net logo code so far.

    to go
      ask turtles [
     move
      ]
        tick
      if ticks >= 60 [stop]
    end

    to move
  ifelse random-float 1 < q
  [
    ifelse random-float 1 < w
    [let target-patch max-one-of neighbors [veg-suitability]
    face target-patch]
    [let target-patch max-one-of neighbors [pelev]
    face target-patch]
  ]
  [
    ifelse [pcolor] of patch-ahead 1 = red
    [lt random-float 180]
    move-to one-of neighbors
    ldd-normal
  ]

end

     to ldd-normal
      let ldd-distance (ldd-scale)
      fd ldd-distance

    end

Upvotes: 0

Views: 182

Answers (1)

JenB
JenB

Reputation: 17678

The logic of your move procedure is a bit confused I think. First you have a random chance to either move to a patch with a higher value of a variable of interest (with the uphill primitive) or, if the random draw fails, it moves to a random neighbour. If you don't want it to move onto a red patch then you need to test if the patch that is chosen is red, but you just move it without checking.

After you have moved the turtle, you then check the colour of patch-ahead. Your problem here is that patch-ahead depends on the direction the turtle is facing, which has nothing to do with the direction it has already been moving. You either make it turn (though it may not turn enough) OR move forward. So it never actually moves away.

I can't give you an actual answer because I don't know what your logic is supposed to be. But you could look at structures like:

move-to one-of neighbors with [pcolor != red]

Or, if there are enough red patches that it is possible that there aren't any non-red neighbours (which would cause an error if you tried to move to one), you could use:

let okay-patches neighbors with [pcolor != red]
if any? okay-patches [move-to one-of okay-patches]

Another option is that you only meant to face rather than move to the patch in the first sections of code, then test whether it is facing a red patch and turn around if it is.

Upvotes: 2

Related Questions