Netlogo - find the closest agent based on Manhattan distance

与世无争的帅哥 提交于 2019-12-13 20:17:38

问题


I am modelling a big warehouse operations (see below pic).

I have implemented a vertex on each patch (green dots) and link them so that I could compute the shortest path (using dijkstra algorithm) from each vertex to all other vertices, stored in a table (dictionary) of each vertex. This process is done during setup stage and is quite time-consuming. Then the yellow inventory cells (rectangle) will issue the demand request for task fulfillment.

The forklift truck (some in the path) is assuming to stay put on one of the vertex (node) when it's not busy. When get the demand request, it will ask its current standing vertex (node) to get the starting_node and the end_node of the inventory cell it is going to. Then the forklift ask the starting_node to get the shortest path from its table to get the shortest path (combination of nodes) and drive to the end_node (the yellow cell).

Currently, the yellow cells just randomly pick free forklift truck in the warehouse. As a further development, I want to add the function that allows the yellow cell to identify the free truck that is closest to it based on the actual distance (not the euclidean distance built in Netlogo). As there are many trucks so it's not good to use "foreach" to loop through all available trucks and compute the actual distance using above method. You could use primitive "distance myself" to locate the truck quickly but that's not accurate. Is there a good way to identify the closest truck with a faster computational method?


回答1:


I don't know if this would work with your current setup, but you may also find the nw extension helpful. For example, try this setup (I apologize for the length, just approximating your world):

extensions [ nw ]

breed [ nodes node ]
breed [ trucks truck ]
breed [ cells cell ]

to setup
  ca

  resize-world -29 29 -14 14

  ask patches with [ ( pxcor mod 5 = 0 or pycor = 0 ) ] [
    set pcolor grey 
    sprout-nodes 1 [
      set size 0.5
      set shape "circle"
      set color green
    ]
  ]

  ask nodes [
    create-links-with turtles-on neighbors4 [
      set color green  
    ]
  ]

  ask n-of 5 nodes [
    hatch 1 [
      set size 1
      set color red
      set breed trucks 
      set shape "car"
    ]
  ]

  ask n-of 2 trucks [
    set color blue
  ]

  ask one-of nodes [
    ask one-of neighbors with [ pcolor = black ] [
      sprout-cells 1 [
        set size 1.5
        set shape "box"
        set color yellow
      ]
    ]
  ]
  reset-ticks
end

Gives a simple world like this:

You can use the nw extension to calculate the distances on the fly, rather than storing them in a per-cell table, and have the forklifts (trucks, here) follow the shortest path. More details in comments:

to summon-nearest-truck 
  ; Treat blue trucks as 'empty' and ready to go to a cell
  let ready-trucks trucks with [ color = blue ]

  ; Get the nodes associated with those ready trucks
  let truck-proxies turtle-set map [ i -> [nodes-on patch-here] of i ] sort ready-trucks

  ; Randomly ask one of the cells to
  ask one-of cells [

    ; Choose one of the neighboring road nodes as a proxy for distance calculation
    let node-proxy one-of nodes-on neighbors4 

    ; Get the node proxy to:
    ask node-proxy [
      ; Find the nearest (by network distance) trucks, and select the node
      ; located on the same patch as that truck
      let my-truck-proxy min-one-of truck-proxies [length ( nw:turtles-on-path-to myself) ]

      ; Get that truck-proxy node to generate the path to the original asking node
      ; and have the appropriate truck follow that path
      ask my-truck-proxy [
        ; Generate the path to follow
        let path-to-follow nw:turtles-on-path-to myself

        ; Highlight that path for visualization
        ask turtle-set path-to-follow [
          set color orange
        ]

        ; Get the truck ready to move
        let ready-truck one-of trucks-here with [ color = blue ]
        ask ready-truck[
          set color yellow
        ]

        ; Move that truck along the path-to-follow
        ask ready-truck [
          foreach path-to-follow [
            n ->
            face n
            move-to n
            ; For visualization only
            wait 0.1
          ]
        ]
      ]
    ]
  ]
end

This has the yellow box summon the nearest truck based on the length of nw:turtles-on-path-to that is returned. That truck follows the path to the summoning node:



来源:https://stackoverflow.com/questions/55294301/netlogo-find-the-closest-agent-based-on-manhattan-distance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!