Dale Townsend
Dale Townsend

Reputation: 691

Make turtles move to another node depending on their current node

I have a network of nodes connected by links. Agents move randomly around this network, i.e they have an equal probability of going backwards and forwards. Here is my code to move the turtles to a random nearby node:

to start-movement
  let nearest-node min-one-of nodes [distance myself]
  set wlocation nearest-node
  move-to wlocation
end

to move
  ask walkers
  [
    set prevlocation wlocation
    if any? [link-neighbors] of wlocation [
    let new-location one-of [link-neighbors] of wlocation
      move-to new-location
      set wlocation new-location
    ]
  ]
end

When a turtle moves to a node, I would like to check where they came from, then with some probability move them to another node that is not the previous one. For example they move from node 1 to node 2. Node 3 is to the right, node 4 straight and node 5 to the right. I would like something like 'if turtle is on node 2, move to node 3 with p=x, node 4 with p=y, or node 5 with p=z.

Could I do this by accessing the who variable of wlocation and prevlocation?

Upvotes: 2

Views: 317

Answers (1)

JenB
JenB

Reputation: 17678

I think you want something like this. It's unclear how you want to choose the probability values for selecting the next location, so I have done something that favours heading 'up'. To make it clear that nodes and walkers are different types of turtles, I have added explicit breed statements that I assume you have in your code. This is not tested or syntax checked.

The trick is to work out the possible next places to go BEFORE changing the value of prevlocation to the current location. Then you can exclude it from the agentset of potential next places.

breed [nodes node]
breed [walkers walker]
walkers-own [prevlocation wlocation]

to start-movement    ; walker procedure
  set wlocation min-one-of nodes [distance myself]
  move-to wlocation
end

to move
  ask walkers
  [ let targets ([link-neighbors] of wlocation) with [self != [prevlocation] of myself]
    set prevlocation wlocation
    if any? targets
    [ set wlocation ifelse-value random-float 1 < 0.3
        [ max-one-of targets [y-cor] ]
        [ one-of targets ]
      move-to wlocation
    ]
  ]
end

Upvotes: 3

Related Questions