Netlogo:移动海龟并计算距离

时间:2017-12-06 08:39:52

标签: netlogo

我目前正在做关于路径规划的项目。

所以我计划的步骤是这样的:

  1. 点击“设置”以排列所有坐标
  2. 点击“开始”创建移动海龟
  3. 点击“go”使移动的乌龟移动到最近的坐标(标有“x”)
  4. 这意味着,在步骤4中,动龟已经计算出每个坐标的距离。
  5. 这里我附上了界面和编码

    The interface

    to setup
     clear-all
     set-default-shape turtles "x"
     create-turtles 9
     ask turtles[set color red]
    
     ask turtle 0[setxy 0 15]
     ask turtle 1[setxy 4 15]
     ask turtle 2[setxy -4 15]
     ask turtle 3[setxy 0 12]
     ask turtle 4[setxy 4 12]
     ask turtle 5[setxy -4 12]
     ask turtle 6[setxy 0 9]
     ask turtle 7[setxy 4 9]
     ask turtle 8[setxy -4 9]
    end
    
    to start
     set-default-shape turtles "airplane"
     create-turtles 1
    
     ask turtle 9[setxy 0 -15]
    end
    

1 个答案:

答案 0 :(得分:1)

请勿忘记距离取决于您的拓扑结构。

to setup
  clear-all
  let targets [
    [0 15] [4 15] [-4 15]
    [0 12] [4 12] [-4 12]
    [0 9] [4 9] [-4 9]
  ]
  foreach targets [xy -> ask patch item 0 xy item 1 xy [
    sprout 1 [set shape "x" set color red]
    ]
  ]
  create-turtles 1 [ set shape "airplane" setxy 0 -15]
end

编辑:如果您将所有其他海龟视为符合条件的目标,那么一旦您将turtle 9移至目标,它就会留在那里。如果你不想这样,你说同一地点的乌龟不符合条件:

to move
  ask turtle 9 [move-to min-one-of eligibles [distance myself]]
end

to-report eligibles
  report turtles with [0 < distance myself]
end