Search code examples
indexingdistancenetlogoclosesttargeting

how to move turtles to sequential targets and stop at endpoints


For clarification on this question, here is the original question with background information.

I have been attempting to make one agentset (ships) move towards another agentset (target-port) and once the ships arrive, move on to the next target (shortest distance away). What i have so far although its not working correctly:

ask ships   
 [ if distance target-port = 0
 [ set target-port min-one-of ports with [who != myself] [distance myself]
   face target-port ]
  ifelse distance target-port < 1
 [ move-to target-port ]
 [ fd 1 ] 
 ]

I would also like to have the ships stop moving once they've met the end of the path. (i.e port 0 or 2 in this specific scenario).

Here is an example of the entire code to make it easier to understand:

breed [ships ship]
breed[ports port]
ships-own [current-port target-port]

to setup 
clear-all

let index 0  
create-ports 3 
[    
let loc item index [ [4 -4] [ 9 5] [ -11 11] ] 
 setxy (item 0 loc) (item 1 loc) 
 set index index + 1  
 set shape "circle" 
 set size 5
 set color red - 1]    

ask ports
[let s who
 set label ( word "    Port    "  s )
 hatch-ships 1 

[ set current-port s
  set size 10
  set color red
  pen-down  
  set pen-size 1
  Set target-port min-one-of ports with [ who != s] [distance myself]   
  set heading towards target-port
  set label (word "target " target-port)         
  ] ]

  reset-ticks 
  end

 to go 
 ask ships   
 [ if distance target-port = 0
 [ set target-port min-one-of ports with [who != myself] [distance myself]
 face target-port ]
 ifelse distance target-port < 1
 [ move-to target-port ]
 [ fd 1 ] 
 ]
 end

Any help would be greatly appreciated. Thanks


Solution

  • For the first part of your question, you just need to use other.

    ask ships [
      if (distance target-port = 0) [
        let _otherports no-turtles
        ask target-port [set _otherports (other ports)]
        set target-port min-one-of _otherports [distance myself]
        face target-port
      ]
      ifelse (distance target-port < 1) [
        move-to target-port
      ][
        fd 1
      ]
    ]
    

    You do not really provide enough info to answer the rest of your question, since your code forces ships to pick a new target once they arrive at their current target. One way to proceed is to give a ship a list of ports to visit, in order, and then stop when you reach the end of the list.