How to remove duplicate items in two lists in NetLogo?

Viewed 55

I'm having a hard time removing duplicates from two lists. I have code that the turtles walk through and export the coordinates of the patch they passed. I would like to remove the duplicated lines that are followed. For example: on one tick the turtle went through the patch (0 0) in sequence it just turned and continued in the patch (0 0) then went to the patch (0 1) and then back to the patch (0 0). Then I would like to know if there is a way to remove the repeated data in sequence.

In the example the .csv file would be:

patch 0 0

patch 0 0

patch 0 1

patch 0 0

I would like the .csv file:

patch 0 0

patch 0 1

patch 0 0

It is possible? If yes, how can I do it?

extensions [ csv ]
globals [Outputlist outifle]
turtles-own [ pxcor-list pycor-list mytimer ]


to setup
  clear-all
  set Outputlist [ [ "id" "pxcor" "pycor" "mytimer" ] ]
  crt 10
    [
      setxy random-xcor random-ycor
      set size 0.5
      set pxcor-list (list pxcor)
      set pycor-list (list pycor)
      set mytimer [0]
      pen-down
  ]
  let pcolors [ ]
  set pcolors [ 1 10 ]
  ask patches [ set pcolor item (random 2) pcolors ]
   reset-ticks
end


to go
  if ticks = 4 [
    ask turtles [ output ]
    stop
  ]
  move
  tick
end


to move
  ask turtles [
    rt random 360
    fd 1
    register-coordinates
  ]
end


to register-coordinates
  set pxcor-list lput pxcor pxcor-list 
  set pycor-list lput pycor pycor-list
  set mytimer lput ticks mytimer
end


To output
  let cur-who-list n-values ( length pxcor-list ) [ who ]
  ;    print cur-who-list
  ( foreach cur-who-list pxcor-list pycor-list mytimer
    [
      [ a b c d ] ->
      let to-append ( list a b c d )
      set Outputlist lput to-append Outputlist
    ]
  )
  ; Export the list to csv
  csv:to-file (word outifle ".csv") Outputlist
end

Thanks in advance

1 Answers

You can achieve this by making turtles evaluate a condition before adding their new coordinates to the lists of coordinates. In the condition, turtles will have to evaluate if the current location is different from the latest registered location; only if that's the case turtles will add the new coordinates to the lists:

to move
  ask turtles [
    rt random 360
    fd 1
    
    if (pxcor != last pxcor-list) or (pycor != last pycor-list) [
      register-coordinates
    ]
  ]
end

An alternative: you can ask turtles to take note of their current patch before moving, and check if the patch is the same after they moved:

to move
  ask turtles [
    let origin patch-here
    
    rt random 360
    fd 1
    
    if (origin != patch-here) [
      register-coordinates
    ]
  ]
end

Maybe this second approach is preferable because, although turtles create a local variable, they will only ever have to evaluate one condition (instead of possibly two conditions, in the first solution), and because turtles will not have to access lists to evaluate the condition. It might be speculation, but this means that the second approach could be faster.

Related