如何使用链接原语

时间:2016-12-16 11:25:24

标签: netlogo

我使用链接原语创建了路径网络,然后创建了在这些链接上行走的海龟(我的代码中的人)。我的代码运行正常。在此代码中,每次调用设置模块时都会创建链接节点的随机网络,同时在随机节点上创建海龟。他们在调用程序时开始行走。

现在我想创建两个或更多固定路径网络,例如网络A和网络B,然后在网络A的任何一个网络中创建人员,其中包含所有人,而网络B则没有人在其上,当设置被调用。如何才能做到这一点?由于在当前代码中,人们正在两个网络中创建,如附图中所示,以供参考。我也在编写完整的代码,以便您理解。

enter image description here

breed [ nodes node ]
breed [ people person ]

people-own [ to-node cur-link previous-link speed cur-location]

to setup
    clear-all
    ask patches [set pcolor pink]

    setup-nodeslinks
    setup-people

   reset-ticks
end

to setup-nodeslinks
   set-default-shape nodes "circle"
   ;; create a random network
   create-nodes 30 [ set color blue ]
   ask nodes [ create-link-with one-of other nodes ]
   ;; lay it out so links are not overlapping
   repeat 500 [ layout ]
   ;; leave space around the edges
   ask nodes [ setxy 0.95 * xcor 0.95 * ycor ]
end

to layout
   layout-spring nodes links 0.5 2 1
end

;; put some "people" turtles on the network

to setup-people
   set-default-shape people "person"
   let people-size 1
   let max-speed  (max-speed-km/h / 36) / 8.775
   let min-speed  max-speed * (1 - speed-variation) 
   create-people num-people [
      set size people-size ;; use meters-per-patch??
      set color yellow
      set speed min-speed + random-float (max-speed - min-speed)
      let l one-of links
      set-next-person-link l [end1] of l
      set previous-link l
    ]
 end

 to set-next-person-link [l n] ;;  proc
   set cur-link l
   move-to n
   set cur-location n
    ifelse n = [end1] of l [set to-node [end2] of l] [set to-node [end1] of  l]
   face to-node
end

to go
   ask people [move-people speed]

   tick 
end

to move-people [spd] ;; people move proc
    let distfromnode distance to-node
    ifelse distfromnode > spd
    [
        ask one-of people-here[if any? other people-here with [precision heading 3 = [precision heading 3] of myself]
                           [ let d min-one-of people-here [[distancexy xcor ycor] of myself]
                             set speed [speed] of d
                           ]
                         ]
         forward spd
     ]
     [ routedirection
       move-people spd - distfromnode ]
 end

  to routedirection
      set previous-link cur-link
      let nextlinks [my-links] of to-node
      ifelse count nextlinks = 1 [ set-next-person-link one-of nextlinks to- node ]
                            [ set-next-person-link one-of nextlinks with  [self != [cur-link] of myself] to-node ]
  end

0 个答案:

没有答案