Netlogo:同时创建/迭代多个列表

时间:2016-02-24 20:13:31

标签: netlogo simultaneous

我有一个简单的Astar搜索模型,可以找到指定目的地的最佳路径。它适用于所有海龟 - 但是,它一次只能这样做。我希望通过让所有海龟同时在搜索空间(find-a-path)中创建/迭代来拥有多只海龟(船只)时加快我的模拟速度。

下面我附上了我称之为代码的部分代码。有没有办法可以修改我当前的代码来支持这个?非常感谢任何帮助,谢谢。

to find-shortest-path-to-destination
      place-turtles
      label-destination
      ask ships
      [ 
        set path find-a-path current-waypoint target-waypoint
        set optimal-path path
        set current-path path  
        output-show (word  "Shortest Path Length: " length optimal-path " (" "LP: " land-prox "," " LPW: " land-prox-weight ")")
      ]

    end

    to-report find-a-path [ source-patch destination-patch] 

      ; initialize all variables to default values
      let search-done? false
      let search-path []
      let current-patch 0
      set open []
      set closed []  

      ; add source patch in the open list
      set open lput source-patch open

      ; loop until we reach the destination or the open list becomes empty
      while [ search-done? != true]
      [    
        ifelse length open != 0
        [
          ; sort the patches in open list in increasing order of their f() values
          set open sort-by [[f] of ?1 < [f] of ?2] open

          ; take the first patch in the open list
          ; as the current patch (which is currently being explored (n))
          ; and remove it from the open list
          set current-patch item 0 open 
          set open remove-item 0 open

          ; add the current patch to the closed list
          set closed lput current-patch closed

          ; explore the neighbours of the current patch
          ask current-patch
          [         
            ; if any of the neighbours is the destination stop the search process
            ifelse any? neighbors with [ (pxcor = [ pxcor ] of destination-patch) and (pycor = [pycor] of destination-patch)]
            [
              set search-done? true
            ]
            [
              ; the neighbors should not be obstacles or already explored patches (part of the closed list)          
              ask neighbors with [ elev <= min-depth and (not member? self closed) and (self != parent-patch)  ]     
              [
                ; the neighbors to be explored should also not be the source or 
                ; destination patches or already a part of the open list (unexplored patches list)
                if not member? self open and self != source-patch and self != destination-patch
                [
                  set pcolor gray + 1 

                  ; add the eligible patch to the open list
                  set open lput self open

                  ; update the path finding variables of the eligible patch
                  set parent-patch current-patch 
                  set g [g] of parent-patch  + 1
                  set h distance destination-patch
                  set f (g + h)     
                ]

              ]
            ]
            if self != source-patch
            [

            ]
          ]
        ]
        [
          ; if a path is not found (search is incomplete) and the open list is exhausted 
          ; display a user message and report an empty search path list.
          user-message( "A path from the source to the destination does not exist." )
          report []
        ]
      ]

      ; if a path is found (search completed) add the current patch 
      ; (node adjacent to the destination) to the search path.
      set search-path lput current-patch search-path

      ; trace the search path from the current patch 
      ; all the way to the source patch using the parent patch
      ; variable which was set during the search for every patch that was explored
      let temp first search-path
      while [ temp != source-patch ]
      [
        ask temp
        [
          set pcolor white
        ]
        set search-path lput [parent-patch] of temp search-path 
        set temp [parent-patch] of temp
      ]

      ; add the destination patch to the front of the search path
      set search-path fput destination-patch search-path

      ; reverse the search path so that it starts from a patch adjacent to the
      ; source patch and ends at the destination patch
      set search-path reverse search-path  

      ; report the search path
      report search-path
    end

0 个答案:

没有答案