Pierfederico Gardino

Simulation models for economics

Project work on

"Would be city and pedestrians."

 

The applet requires Java 5 or higher. Java must be enabled in your browser settings. Mac users must have Mac OS X 10.4 or higher. Windows and Linux users may obtain the latest Java from Oracle's Java site.


Here you can find an introduction to the Would be city and pedestrians application.


powered by NetLogo

view/download model file: wuold_be_city_and_pedestrians.nlogo

WHAT IS IT?

This model represents a would-be city in which cars wait for allowing people to cross the road, pedestrians cross just in precise places.

HOW TO USE IT

Click on the SETUP button to set up the roads, trees, offices, schools, people and cars. Set the number of people and cars through the sliders.

Click on the GO button to make move cars and people.

HOW IT WORKS

Cars move along the streets, people walk randomly. When a pedestrian crosses the street a car stops to enalbe she to pass beyond, then all cars continue moving on their path.

THINGS TO NOTICE

The colour of the patches of the streets may change, it depends on the people: once that a person is over a grey patch it affects its colour by changing it, it is useful as a signal for the cars. If a person crosses outside four fixed points it becomes red, she wounds herself.

THINGS TO TRY

The model works with any number of agents, it holds for both cars and people whose figure can be regulated with the sliders. Every time the system is setup with the SETUP button the agents are posed in different places but after a few whiles the final outcome will be the same: street patches change colour, cars wait, people cross the streets.

NETLOGO FEATURES

The wait command for the patches is used to allow the watcher to better see the colour change for the grey patches.

RELATED MODELS

“Traffic Grid” for the functioning of the breaks of the cars.

CODE

breed [schools school]
breed [trees tree]
breed [cars car]
breed [offices office]
breed [people person]
globals [#red #white]

to setup
  ca
  reset-ticks
  setup-roads
  setup-schools
  setup-trees
  setup-offices
  setup-people
  setup-cars

end
  
to setup-roads                                                       ;coordinate per creare quattro incroci 
  ask patches with [ pycor >= -8 and pycor < -7]
  [set pcolor grey]
  ask patches with [ pycor >= 8 and pycor < 9]
  [set pcolor grey]
  ask patches with [ pxcor >= -8 and pxcor < -7]
  [set pcolor grey]
  ask patches with [ pxcor >= 8 and pxcor < 9]
  [set pcolor grey]
  
end 

to setup-schools
  set-default-shape schools "house"
  create-schools 2
  
  ask schools
    [ set size 4
      set color yellow]
    
    ask school 0
      [ setxy -6 -12]
      
      ask school 1
        [ setxy 12 -5]
   
end 

to setup-trees
  set-default-shape trees "tree"
  create-trees 2
  
  ask trees 
     [ set size 2
       set color green]
     
     ask tree 2
     [ setxy -6 -14]
     
     ask tree 3
     [ setxy 14 -5]
     
end

to setup-offices
 set-default-shape offices "house"
  create-offices 2
  
   ask offices
    [ set size 3
      set color blue]
      
      ask office 4 
      [ setxy -10 1]
        
      ask office 5 
      [ setxy -1 10]
end
  
to setup-people 
 set-default-shape people "person"
  create-people howManyPeople
  
  ask people
    [set size 1
      set color white
      setxy random-pxcor random-pycor]
 
end

to setup-cars                          
  
 set-default-shape cars "car"
  create-cars howManyCars
   [set size 1  
    set color blue
    move-to one-of patches with [pcolor = grey]
    ]
   
   ask cars                                            ;; le auto posizionate sulle strade verticali si muovono verso nord
   [if (pycor >= -8) and (pycor < -7)                  ;; le auto posizionate sulle strade orizzontali si muovono verso est
     [set heading 90]
    if (pycor >= 8) and (pycor < 9)
     [set heading 90]
    if (pxcor >= -8) and (pxcor < -7)
     [set heading 0]
    if (pxcor >= 8) and (pxcor < 9)
     [set heading 0]
   ]

end

to go
  
  ;reset
  move-cars
  walk
  change-color
  await-cars
  await-patches
  be-wrong
  execute
  improve
  tick
      
end

to move-cars
  ask cars                                                ;le auto si muovono solo lungo le strade
   [if pycor = 8                                                
    [fd 1]
   if pycor = -8
    [fd 1]]
   
 ask cars
  [if pxcor >= -8 and pxcor < -7 
    [set heading 0]
     fd 1
   if pxcor >= 8 and pxcor < 9
   [set heading 0] 
      fd 1]

end 
  
to walk                          ;; i pedoni si muovono a caso di 1 step alla volta
  ask people
  [fd 1]
  
end

to improve
   
   ask people
   [
    if #red > #white [ifelse random-float 1 > 0.5 [set color white] [set color red] ]
    if #red < #white [ifelse random-float 1 < 0.5 [set color white] [set color red] ]
   ]
   
end

to change-color                                                      ;; quando un pedone attraversa la strada la patch cambia colore
 
 ask people with [ pycor >= -8 and pycor < -7]
  [set pcolor green] 
  ask people with [ pycor >= 8 and pycor < 9]
  [set pcolor yellow] 
  ask people with [ pxcor >= -8 and pxcor < -7]
  [set pcolor brown] 
  ask people with [ pxcor >= 8 and pxcor < 9]
  [set pcolor pink]
 
end

to await-cars
  
  ask cars with [pycor = 8]
  [if pcolor = yellow
    [wait 0.0005]]
  ask cars with [pycor = -8]
  [if pcolor = green
    [wait 0.0005]]
  ask cars with [pxcor = -8]
  [if pcolor = brown
    [wait 0.0005]]
  ask cars with [pxcor = 8]
  [if pcolor = pink
    [wait 0.0005]]
  
end

to  await-patches
 
  ask patches with [(pxcor < -3 and pxcor >= 2) and pycor = 8]
  [wait 0.0002]
  ask patches with [(pxcor < -3 and pxcor >= 2) and pycor = -8]
  [wait 0.0002]
  ask patches with [pxcor = -8 and (pycor >= -2 and pycor < 2)]
  [wait 0.0002]
  ask patches with [pxcor = 8 and (pycor >= -3 and pycor < 2)]
  [wait 0.0002]
 
end

to be-wrong
   
   ask people 
   [if pcolor = yellow
     [set color red
       set #red #red + 1]]
   ask people 
   [if pcolor = green
     [set color red
       set #red #red + 1]]
   ask people
   [if pcolor = brown
     [set color red
       set #red #red + 1]]
   ask people
   [if pcolor = pink
     [set color red
       set #red #red + 1]]
   
end

to execute
  
  ask people 
  [ if (pxcor >= -3 and pxcor < 2) and pycor = 8
    [set color white
      set #white #white + 1]]
  
  ask people
  [ if pxcor = -8 and (pycor >= -2 and pycor < 2)
    [set color white
      set #white #white + 1]]
  
  ask people
  [if pxcor = 8 and (pycor >= -3 and pycor < 2)
    [set color white
      set #white #white + 1]]
  
  ask people
  [if (pxcor >= -3 and pxcor < 2) and pycor = -8
    [set color white
      set #white #white + 1]]
  
end


to reset
  
  set #red 0
  set #white 0
  
end