⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pedestrian code.txt

📁 本程序用NetLogo开放
💻 TXT
字号:
globals [ clock screenArea numPedestrians average-speed time ]           

breeds [pedestrians ]

pedestrians-own [ v_speed             ;; speed of pedestrians [ 2 cells/time step (5%) , 3 cells/time step (90%) , 4 cells/time step(5%)) 
                  left_vail? right_vail?
                  current_lane? left_lane? right_lane? 
                  gap  gap_opp gap_same                       
                  gap_current gap_current_same  gap_current_opp
                  gap_left gap_left_same gap_left_opp
                  gap_right gap_right_same gap_right_opp 
                  speed
                ]
                 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;SETUP PROCEDURES;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to setup
  ca
  draw-color-patches
  set screenArea (screen-edge-x  * screen-edge-y  * 4)
  set-default-shape pedestrians "person"  
  set numPedestrians int(screenArea * (%DensityPedestrians / 100))
  create-pedestrians (numPedestrians)
  ask pedestrians [ setPedestrians ]
end

to draw-color-patches
  ask patches
    [ 
      ifelse ( pxcor + pycor ) mod 2 = 0
        [ set pcolor white - 1  ]
        [ set pcolor white ] 
    ]       
end 

to setPedestrians
   locals [randDirection  randSpeed]   
   set xcor int (random (screen-size-x ))
   set ycor int (random (screen-size-y ))
          
   set randDirection random-float 1
   ifelse randDirection < 0.52
      [ set heading 90 ]
      [ set heading 270 ]
     
   set randSpeed random-float 1        
   ;; set initial speed : 5% 2 cells/time step; 90% 3 cells/time step; 5% 4 cells/time step
   ;; set initial direction : 50% left direction , 50% right direction
   ;; set color according to their initial speed and initial advancing direction
   ifelse heading = 270
       [ ifelse randSpeed <= 0.05      
            [ set v_speed  2
              set color ( orange ) 
            ]
            [ ifelse (randSpeed > 0.05) and (randSpeed <= 0.95)
                  [  set v_speed 3
                     set color  yellow           
                   ]
                   [ if (randSpeed > 0.95) and (randSpeed <= 1)
                       [ set v_speed 4
                         set color ( magenta )
                       ]
                   ]
               ]
        ]
        [ ifelse randSpeed <= 0.05      
              [  set v_speed  2
                 set color  (cyan)
               ]
               [  ifelse (randSpeed > 0.05) and (randSpeed <= 0.95)
                    [ set v_speed 3    
                      set color blue  ;; going to the right and 2 cells/time step,blue
                    ]
                    [ if (randSpeed > 0.95) and (randSpeed <= 1)
                       [ set v_speed 4
                         set color (green)  
                       ]
                   ]
               ]
         ]
    set speed v_speed 
         
    ;; make sure no two cars are on the same patch
    loop
       [ ifelse any other-turtles-here
          [ fd 1 ]
          [ stop ]
       ]
end

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;GO PROCEDURE;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to go
   ifelse clock < stopAt
     [   initialize
         lane_assign
         lane_switch
         speed_assign
         forward_move                         
         set clock (clock + 1)
         set average-speed ((sum values-from turtles [speed]) / numPedestrians )
         set-current-plot "pedestrians"
         set-current-plot-pen "average"
         plot average-speed    
      ] 
      [stop] 
end

to initialize
   ask pedestrians
         [
            set left_vail? true
            set right_vail? true
            set gap 0
            set gap_opp 0
            set gap_same 0
            set gap_current 0
            set gap_left 0
            set gap_right 0
            set current_lane? false
            set left_lane? false
            set right_lane? false
         ]
end

to lane_assign
  check_lane
  ask pedestrians
    [    computate_current_gaps
         if left_vail? [computate_left_gaps]
         if right_vail? [computate_right_gaps]
         set gap max(list gap_current gap_left gap_right)
         ifelse (gap_current = 1) and (gap_current_same = 1)
            [ set current_lane? true]
            [ ifelse (gap_left = 1) and (gap_left_same = 1)
                [ set left_lane? true]
                [ ifelse (gap_right = 1) and (gap_right_same = 1)
                    [set right_lane? true]
                    [choose_lane]
                 ]
            ]      
    ]
end

to lane_switch
   ask pedestrians
       [
           if left_lane?
               [ set ycor (ycor + 1) ]
           if right_lane?
               [ set ycor (ycor - 1) ] 
       ]
end

to speed_assign
    locals [p_exchg]
    ask pedestrians
       [         
          computate_gaps
          set speed gap
          if ( ((gap = 0 ) or ( gap = 1)) and (gap = gap_opp))
             [ set p_exchg random-float 1
               ifelse p_exchg < 0.5
                    [ set speed (gap + 1) ]
                    [ set speed 0 ]
              ]
       ]
end

to forward_move
   ask pedestrians
   [
      fd speed 
   ]
end

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;SUBPROCEDURE;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

to check_lane
    locals [rand xpos ypos]
    ask pedestrians
      [    set xpos xcor
           set ypos ycor             
           ;; check adjacent cells                   
           ifelse ycor = (0 - screen-edge-y)
              [ set right_vail? false ]
              [ ifelse (any pedestrians with [(xcor = xpos) and (ycor = (ypos - 1))])
                  [ set right_vail? false ]
                  [ set right_vail? true ]                    
              ]  
      ]      
  ask pedestrians
      [    set xpos xcor
           set ypos ycor 
           ifelse ycor = screen-edge-y
               [ set left_vail? false ]
               [ ifelse (any pedestrians with [(xcor = xpos) and (ycor = (ypos + 1))])
                     [ set left_vail? false ]
                     [ set left_vail? true]
               ]
      ]                  
end

to choose_lane
     locals [rand]
     ifelse (gap = gap_current)
         [ set current_lane? true ]
         [ ifelse (gap = gap_left) and (gap_left = gap_right) and (gap != gap_current)
               [ set rand random-float 1
                  ifelse rand < 0.5 
                      [ set left_lane? true ]                                                    
                      [ set right_lane? true ] ;;right
                ]
                [ ifelse ( gap = gap_left) and (gap != gap_right) and (gap != gap_current)
                     [ set left_lane? true  ]
                     [ if (gap = gap_right) and (gap != gap_left) and (gap != gap_current)
                          [set right_lane? true ]                        
                     ]
                 ] 
          ]                                
end

to computate_gaps
     locals [ walker_same walker_opp xpos ypos xpos1 xpos2 numCell_opp numCell_same]
     set numCell_opp 0
     set numCell_same 0
     set xpos xcor
     set ypos ycor
     
     if heading = 90
          [  ifelse any pedestrians with [(xcor <= ( xpos + 8)) and (xcor > xpos) and (ycor = ypos) and ( heading = 90)]
                 [ set xpos1 min values-from (pedestrians with [(xcor <= ( xpos + 8)) and (xcor > xpos) and (ycor = ypos) and ( heading = 90)]) [xcor]
                   set gap_same int(abs(xcor - xpos1) - 1)
                 ]
                 [ set gap_same 8]
             ifelse any pedestrians with [(xcor <= ( xpos + 8)) and (xcor > xpos) and (ycor = ypos) and ( heading = 270)]
                 [ set xpos2 min values-from (pedestrians with [(xcor <= ( xpos + 8)) and (xcor > xpos) and (ycor = ypos) and ( heading = 270)]) [xcor]
                   set gap_opp int (0.5 * (abs(xcor - xpos2) - 1))
                 ]  
                 [ set gap_opp 4]        
          ]
     if heading = 270
           [  ifelse any pedestrians with [(xcor >= ( xpos - 8)) and (xcor < xpos) and (ycor = ypos) and ( heading = 270)]
                  [ set xpos1 max values-from (pedestrians with [(xcor >= ( xpos - 8)) and (xcor < xpos) and (ycor = ypos) and ( heading = 270)]) [xcor]
                    set gap_same int(abs(xcor - xpos1) - 1)
                   ]
                   [ set gap_same 8]
              ifelse any pedestrians with [(xcor >= ( xpos - 8)) and (xcor < xpos) and (ycor = ypos) and ( heading = 90)]
                  [ set xpos2 max values-from (pedestrians with [(xcor >= ( xpos - 8)) and (xcor < xpos) and (ycor = ypos) and ( heading = 90)]) [xcor]
                    set gap_opp int( 0.5 * (abs(xcor - xpos2) - 1))
                  ] 
                  [ set gap_opp 4]    
            ]
            
       set gap min(list gap_opp gap_same v_speed) 
    
end

to computate_current_gaps
     locals [ walker_same walker_opp xpos ypos xpos1 xpos2 numCell_opp numCell_same]
     set xpos xcor
     set ypos ycor
     
     if heading = 90
          [  ifelse any pedestrians with [(xcor <= ( xpos + 8)) and (xcor > xpos) and (ycor = ypos) and ( heading = 90)]
                 [ set xpos1 min values-from (pedestrians with [(xcor <= ( xpos + 8)) and (xcor > xpos) and (ycor = ypos) and ( heading = 90)]) [xcor]
                   set gap_current_same int(abs(xcor - xpos1) - 1)
                 ]
                 [ set gap_current_same 8]
             ifelse any pedestrians with [(xcor <= ( xpos + 8)) and (xcor > xpos) and (ycor = ypos) and ( heading = 270)] 
                 [ set gap_current_opp 0]
                 [ set gap_current_opp 4]        
          ]
     if heading = 270
           [  ifelse any pedestrians with [(xcor >= ( xpos - 8)) and (xcor < xpos) and (ycor = ypos) and ( heading = 270)]
                  [ set xpos1 max values-from (pedestrians with [(xcor >= ( xpos - 8)) and (xcor < xpos) and (ycor = ypos) and ( heading = 270)]) [xcor]
                    set gap_current_same int(abs(xcor - xpos1) - 1)
                   ]
                   [ set gap_current_same 8]
              ifelse any pedestrians with [(xcor >= ( xpos - 8)) and (xcor < xpos) and (ycor = ypos) and ( heading = 90)]
                   [ set gap_current_opp 0]
                   [ set gap_current_opp 4]    
            ]
            
       set gap_current min(list gap_current_opp gap_current_same v_speed) 
    
end

to computate_left_gaps
     locals [ xpos ypos xpos1 xpos2]
     set xpos xcor
     set ypos (ycor + 1)
     
     if heading = 90
          [  ifelse any pedestrians with [(xcor <= ( xpos + 8)) and (xcor > xpos) and (ycor = ypos) and ( heading = 90)]
                 [ set xpos1 min values-from (pedestrians with [(xcor <= ( xpos + 8)) and (xcor > xpos) and (ycor = ypos) and ( heading = 90)]) [xcor]
                   set gap_left_same int(abs(xcor - xpos1) - 1)
                 ]
                 [ set gap_left_same 8]
             ifelse any pedestrians with [(xcor <= ( xpos + 8)) and (xcor > xpos) and (ycor = ypos) and ( heading = 270)] 
                 [ set gap_left_opp 0]
                 [ set gap_left_opp 4]        
          ]
     if heading = 270
           [  ifelse any pedestrians with [(xcor >= ( xpos - 8)) and (xcor < xpos) and (ycor = ypos) and ( heading = 270)]
                  [ set xpos1 max values-from (pedestrians with [(xcor >= ( xpos - 8)) and (xcor < xpos) and (ycor = ypos) and ( heading = 270)]) [xcor]
                    set gap_left_same int(abs(xcor - xpos1) - 1)
                   ]
                   [ set gap_left_same 8]
              ifelse any pedestrians with [(xcor >= ( xpos - 8)) and (xcor < xpos) and (ycor = ypos) and ( heading = 90)]
                   [ set gap_left_opp 0] 
                   [ set gap_left_opp 4]    
            ]
            
       set gap_left min(list gap_left_opp gap_left_same v_speed) 
    
end

to computate_right_gaps
     locals [xpos ypos xpos1 xpos2]
     set xpos xcor
     set ypos (ycor - 1)
     
     if heading = 90
          [  ifelse any pedestrians with [(xcor <= ( xpos + 8)) and (xcor > xpos) and (ycor = ypos) and ( heading = 90)]
                 [ set xpos1 min values-from (pedestrians with [(xcor <= ( xpos + 8)) and (xcor > xpos) and (ycor = ypos) and ( heading = 90)]) [xcor]
                   set gap_right_same int(abs(xcor - xpos1) - 1)
                 ]
                 [ set gap_right_same 8]
             ifelse any pedestrians with [(xcor <= ( xpos + 8)) and (xcor > xpos) and (ycor = ypos) and ( heading = 270)]
                 [ set gap_right_opp 0] 
                 [ set gap_right_opp 4]        
          ]
     if heading = 270
           [  ifelse any pedestrians with [(xcor >= ( xpos - 8)) and (xcor < xpos) and (ycor = ypos) and ( heading = 270)]
                  [ set xpos1 max values-from (pedestrians with [(xcor >= ( xpos - 8)) and (xcor < xpos) and (ycor = ypos) and ( heading = 270)]) [xcor]
                    set gap_right_same int(abs(xcor - xpos1) - 1)
                   ]
                   [ set gap_right_same 8]
              ifelse any pedestrians with [(xcor >= ( xpos - 8)) and (xcor < xpos) and (ycor = ypos) and ( heading = 90)]
                  [ set gap_right_opp 0]
                  [ set gap_right_opp 4]     
            ]
            
       set gap_right min(list gap_right_opp gap_right_same v_speed)    
end

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -