LevelCrossing ver.2.1

No preview image

1 collaborator

Uri_dolphin3 Uri Wilensky (Author)

Tags

(This model has yet to be categorized with any tags)
Model group CCL | Visible to everyone | Changeable by group members (CCL)
Model was written in NetLogo 3.1.4 • Viewed 329 times • Downloaded 26 times • Run 0 times
Download the 'LevelCrossing ver.2.1' modelDownload this modelEmbed this model

Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)


WHAT IS IT?

It is a multi-agent model of the level crossing system (ver. 2.1). Its ancestor (ver.1.1) was uploaded as a community model in late 2005. This essentially improved version was created to have more realistic graphic representation and implement some new functions. The model also makes possible to model traffic flows and make evaluation based on one "simulation day".

HOW IT WORKS?

1) At the beginning it is necessary to make all required settings using sliders and choosers for: ROAD TRAFFIC (speed, kind of road vehicles, arrival interval) and RAILWAY TRAFFIC (train time table, approaching time predictor, speed, number of wagons, kind of train);

2) Then pressing the button "Setup" makes initiation of the model;

3) Simulation itself can be started by pressing the button "Simulation" or "Simulation/step" (in that case repeated pressing of the button "Simulation/step" is required).

HOW TO USE IT

Road vehicles are generated separately for East->West and West->East direction based on used settings. Trains are generated either using one of three train time-tables or by pressing the button "Generate Train" for generation of an extra train. Only one train may be generated and situated at the line.

THINGS TO NOTICE

- drawing histograms showing intensities of road traffic after one hour of simulation;

- displayng the states of particular track sections (approach section, annulation section, distant section) in the right-hand bottom corner of two-dimensional world; the way of displaying how the approach section becomes occupied is different and depends on the use of Approaching Time Predictor (ATPr) - if ATPr is used then red point located somewhere inside the approach section indicates point of activation (depending on train speed), if ATPr is not used the whole length is coloured red in the same way for all train speeds.

THINGS TO TRY

You may try how different settings of parameters can change a traffic flow, resulting values of intensities and finally calculate so called "traffic moment".

EXTENDING THE MODEL

There are still several ideas how the model could be extended in future, e.g.:

- possible choice of a higher number of rails;

- choice of rail traffic direction(s);

- two-way railway operation;

- the way of how warning and rail signals look like and what signalling is given (different countries and different companies use different road-rail interface - present way corresponds to Slovak regulations and is very similar to other Central European countries);

- choice of a different kind of protection (no barriers, half barriers, full barriers);

- results evaluation not only for one day of simulation, but for a longer timer interval;

- implementation of new functions defining behaviour under faulty conditions, under service mode (when maintenance is being performed) etc.

NETLOGO FEATURES

The model was created using NetLogo 3.1.4. Using higher versions of NetLogo may cause error messages and brings necessity to make modifications of the code to ensure troublefree running of the model in higher versions.

RELATED MODELS

Level crossing ver. 1.1 designed by JANOTA A. et al - this model is available as a community model at http://ccl.northwestern.edu/netlogo/models/community/Level_Crossing

Designed at the University of Zilina, EF KRIS 2005

CREDITS AND REFERENCES

Slovak version of the model (ver. 2.0) was theoretically and experimentally elaborated and tested within the MSc. thesis entitled "Multiagentovy model priecestneho zabezpecovacieho zariadenia", written (in Slovak) by Mr. Robert Kubicar under supervision of prof. Ales Janota. The thesis was successfully defended in 2007 at the Department of Control and Information Systems, Faculty of Electrical Engineering, University of Zilina (Slovakia). This version (2.1) represents the latest version - English mutation, containing other modifications and some other functions implemented.

SPECIAL THANKS

Sincere thanks to Robert Kubicar for his work over Slovak model of the version 2.O. Additional thanks also to Esther Verreau from the Center for Connected Learning (CCL) and Computer-Based Modeling for her time and willingness to answer his questions.

More detailed description of the model, including some background explanations, will be available in the paper "JANOTA Ales, MAS Model of the Level Crossing. Journal of ITS Research, Vol. 6, No. 2, November 2008. ISSN 1348-8503", to be published in Japan.

To refer to this model in academic publications, please use: "Janota, A. (2008). NetLogo Level Crossing Model 2.1 http://ccl.northwestern.edu/netlogo/models/....... (please complete link address according to the real position of the file at the server)".

To reference NetLogo software in academic publications, please use: Wilensky, U. (1999). NetLogo. http://ccl.northwestern.edu/netlogo/. Center for Connected Learning and Computer-Based Modeling, Northwestern University, Evanston, IL.

Comments and Questions

Please start the discussion about this model! (You'll first need to log in.)

Click to Run Model

globals
            [ 
unit_time              ;incrementation of built-in NetLogo units for expressing total time of simulation 
time_of_simulation     ;total time in hours (not real one), given for a specified number of built-in units per one hour of simulation 
one_hour               ;in procedure "setup" there is an assignment of how many built-in units is equivalent to one hour 
one_minute             ;in procedure "setup" there is an assignment of how many built-in units is equivalent to one minute 
one_day                ;in procedure "setup" there is an assignment of how many built-in units is equivalent to one day
time_day               ;in procedure "time" a number of the total simulation days is written to this variable 
actual_day_unit        ;incrementation of built-in units for an actual hour (variable is set to zero when a day is over)
actual_day_hour        ;in procedure "time" an actual number of simulation hours is written to this variable
actual_day_unit_minute ;incrementation of built-in units for an actual minute (variable is set to zero when an hour is over)
actual_day_minute      ;in procedure "time" an actual number of simulation minutes is written to the variable
No_of_cars_to_East No_of_trucks_to_East ;Number of cars/trucks in EAST direction, once a simulation day is over the variable remains unchaged  
No_of_cars_to_West No_of_trucks_to_West ;Number of cars/trucks in WEST direction, once a simulation day is over the variable remains unchaged 
freq freq<             ;variable that is being compared with the following variable "Time-interval" and road vehicles are generated based on match
Time-interval Time-interval<
congestion-we congestion-ew ;determines how many road vehicles have not been generated (in this case the result is not output to monitor window)                
MaxSpeed  MaxSpeed<         ;variable used to set maximum speed of road vehicles
choose-vehicleWE choose-vehicleEW   ;variable used to choose between a car and a truck randomly
generationF            ;if the variable is set to the value 1, the good wagons for a freight train are generated behind the locomotive (0 for no generating)
generationP            ;if the variable is set to the value 1, the passenger wagons are generated for a pasenger train behind the locomotive (0 for no generating) 
generationFS           ;the same as above, but for a special (extra generated) freight train
generationPS           ;the same as above, but for a special (extra generated) passenger train
freight-train          ;variable used to detect what kind of rail vehicle is actually the line (due to function of Approach Time Predictor - for different speeds)
passenger-train        ;variable used to detect what kind of rail vehicle is actually at the line (due to function of Approach Time Predictor - for different speeds) 
occupancy
delta_t                ;coefficient used to multiply a train speed 
counter-wagonsF       ;these four variables record a number of wagons (of a freight train (F), passenger train (P), specially generated freight train (FS) or specially
counter-wagonsP       ;generated passenger train (PS)); if value becomes equivalent to the required number, the variable (F,P,FS,PS) is given the value zero 
counter-wagonsFS
counter-wagonsPS
no_of_passenger_trains ;in procedure "move-for-train" a number of passenger wagons/day is written to this variable
no_of_freight_trains   ;in procedure "move-for-train" a number of goods wagons/day is written to this variable
blinkA                 ;variable used to set interval for "active signalling" (white flashing light)
blinkW                 ;variable used to set interval for the warning state of level crossing 
tcb                    ;warning time to close barriers (time from the moment of warning activation to the moment when barriers start going down) 
A                      ;approach section, by default value="vacant" 
B                      ;annulation section, by default value="vacant"
C                      ;distant section, by default value="vacant"
timeATPr               ;variable is incremented after the approach section A has been occupied 
ativation-point-ATPr   ;if variable becomes equivalent o value 1, warning state is activated at the level crossing (delay depends on train speed)
carsE                  ;drawing road vehicles at the certain hour, reset of variable occurs when hour is over  
trucksE
carsW
trucksW value
all-cars               ;summing of variables mentioned above, necessary for drawing histograms 
all-trucks
all-vehicles
top_intensity          ;variable reports the highest achieved value via reporter 
moment
valueI                 ;value of lapses (time intervals) between road vehicle arrivals 
resetting              ;variable used to reset values of "no_of_passenger_trains" a "no_of_freight_trains"
        ]
         
breed [rail]
breed [rail1]          ;rails over road  
breed [obstacle-EW]
breed [obstacle-WE] 
breed [ST240]          ;ST means "signalling table", situated 240 m prior to the level crossing 
breed [ST160]          ;signalling table situated 160 m prior to the level crossing
breed [ST80]           ;signalling table situated 80 m prior to the level crossing       
breed [cars-WE]        ;cars going from west to east
breed [trucks-WE]      ;trucks going from west to east
breed [cars-EW]        ;cars going from east to west
breed [trucks-EW]      ;trucks going from east to west
breed [wsignal]        ;warning signal - here alternatively flashing red light 
breed [rail-signal]    ;rail signal - here 2 yellow position lights + 1 white light (being on or off) 
breed [barriers-EW]    ;barriers for direction East-West  
breed [barriers-WE]    ;barriers for direction West-East  
breed [trainF]         ;freight train
breed [wagonF]         ;wagon of freight train
breed [trainP]         ;passenger train
breed [wagonP]         ;wagon of passenger train
breed [state-of-wsignal-EW]        ;state of the warning signal (information for road users going from East to West)
breed [state-of-wsignal-WE]        ;state of the warning signal (information for road users going from West to East)
breed [state-of-rsignal]           ;state of the rail signal (information for train crew)
cars-WE-own [speed accel?]      
trucks-WE-own [speed accel?]
cars-EW-own [speed< accel?<]
trucks-EW-own [speed< accel?<]
;------------------------------------------------------------------------------------------
; PART 1                            SETUP
;------------------------------------------------------------------------------------------

to setup 
 reset-timer
 ca 
 crt 1    ;turtle used to draw road communication (curves) 
   set actual_day_unit 0
   set actual_day_unit_minute 0
   set one_hour 8417   ;all what is needed - to define how many time unit create one hour and one day mentioned below ;shortened simulation 20;
                                           ; and in procedure “time“ all times in model are re-calculated automatically 
   set one_day  202008 ;shortened simulation 480;
   set one_minute one_hour / 60   
   set No_of_trucks_to_West 0 
   set No_of_cars_to_West 0
   set No_of_cars_to_East 0 
   set No_of_trucks_to_East 0 
   set time_of_simulation  0
   set delta_t 0.003
   set freq 0 set freq< 0
   set generationF 0
   set counter-wagonsP 0
   set-default-shape ST240 "signal table 240m"
   set-default-shape ST160 "signal table 160m"
   set-default-shape ST80 "signal table 80m"
   set-default-shape cars-WE "car0"
   set-default-shape trucks-WE "carn01"
   set-default-shape cars-EW "car<"
   set-default-shape trucks-EW "carn<"
   set-default-shape rail "rail3"
   set-default-shape rail1 "rail4"
   set-default-shape trainF "locomotive1"; definition of turtle for a passenger train
   set-default-shape wagonF "freight car"
   set-default-shape trainP "locomotive2"; definition of turtle for a freight train
   set-default-shape wagonP "coach"
   set-default-shape wsignal "warning signal"
   set-default-shape rail-signal"rail signal"
   set-default-shape barriers-WE "barriers"
   set-default-shape barriers-EW "barriers"
   set-default-shape state-of-wsignal-EW "black point"
   set-default-shape state-of-wsignal-WE "black point"
   set-default-shape state-of-rsignal "black point"
   curve     
   background
   centerline
   STs
   rails
   wsignals
   rail-signal-draw
end 
;-------------------------------------------------------------------------------------------
; PART 2               DRAWING ROAD CURVE (LEFT AND RIGHT)    
;-------------------------------------------------------------------------------------------  

to curve
      ask turtles [set heading 90 part1]
      ask turtles [set heading 10 part2]
      ask turtles [set heading 90 track]
end      

to part1
            setxy  ( -1 * max-pxcor + 34  ) (-1 * max-pycor + 5 )
            set pen-size 39
            set color black 
            pd repeat 7 [fd 1.16 lt 12 ]
            pu
end 

to part2
         setxy  0 8
         set pen-size 39
         set color black 
         pd repeat 7 [fd 1.15 rt 12]
         pu
end 

to track 
       setxy  10.5 -13 ;drawing of the track 
                    set pen-size 1.5
                    set shape "squareR"
                    set color 38 
                    pd repeat 2  [fd 15]
                    pu 
       setxy  10.5 -12.9; use of two lines one alongside of another
                    set pen-size 1.5
                    set shape "squareR"
                    set color 38 
                    pd repeat 2  [fd 15]
                    pu                
end 

to background 
       ask patches 
        [ set pcolor green 
       if ((pxcor < -4 and pycor < -10) and (pxcor < -4 and pycor >= -15 ))    ;access road from the left
        [ set pcolor black ] 
       if ((pxcor  > 4 and pycor > 10) and (pxcor > 4 and pycor <= 15 ))       ;access road from the right
        [ set pcolor black ] 
       if ( (pxcor >= -2 and pycor < 9 )and (pxcor <= 2 and pycor > -9 ))      ;road directly to the top (section will be grey)
        [ set pcolor gray  ]
       if ( (pxcor >= -2 and pycor > 2 ) and (pxcor <= 2  and  pycor <= 8 ))   ;re-draw the curve from the bottom   
        [ set pcolor black ]
       if ( (pxcor >= -2 and pycor < -2 ) and (pxcor <= 2  and  pycor >= -8 )) ;re-draw the curve from the top 
        [ set pcolor black ]
       if ((pxcor  > 10 and pycor >= -18) and (pxcor > 10 and pycor <= -7 ))   ;monitor window  
        [ set pcolor black ]  
       if ((pxcor  > 10 and pycor > -14) and (pxcor < 24 and pycor < -12 ))[set pcolor gray ]  ;technical means TP1
       if ((pxcor  >= 25 and pycor > -14) and (pxcor < 29 and pycor < -12 ))[set pcolor gray ] ;technical means TP2
       if ((pxcor  >= 30 and pycor > -14) and (pxcor <= 40 and pycor < -12 ))[set pcolor gray ];technical means TP3
       
       ]
end  
;-------------------------------------------------------------------------------------------
; PART 3                        DEFINITION OF THE HALF-WAY LINE   
;-------------------------------------------------------------------------------------------        

to centerline
       ask turtles [set heading 90 part3]
       ask turtles [set heading 80 part4]
       
       ask turtles [set heading 10 part5]
       ask turtles [set heading 90 part6]
end 

to part3
                   setxy  -40.4 -13
                   set pen-size 1
                   set shape "point"
                   set color white 
                   pd repeat 1 [fd 1 ] pu repeat 1 [fd 1 ] pd repeat 1 [fd 1 ]
                    pu repeat 1 [fd 1 ] pd repeat 1 [fd 1 ] pu repeat 1 [fd 1 ]
                     pd repeat 1 [fd 1 ] pu repeat 1 [fd 1 ] pd repeat 1 [fd 1 ]
                      pu repeat 1 [fd 1 ] pd repeat 1 [fd 1 ] pu repeat 1 [fd 1 ]
                       pd repeat 1 [fd 1 ] pu repeat 1 [fd 1 ] pd repeat 1 [fd 1 ]
                      pu repeat 1 [fd 1 ] pd repeat 1 [fd 1 ] pu repeat 1 [fd 1 ]
                     pd repeat 1 [fd 1 ] pu repeat 1 [fd 1 ] pd repeat 1 [fd 1 ]
                    pu repeat 1 [fd 1 ] pd repeat 1 [fd 1 ] pu repeat 1 [fd 1 ]
                   pd
end  

to part4
                   setxy  ( -1 * max-pxcor + 35  ) (-1 * max-pycor + 5 )
                   set pen-size 1.5
                   set shape "point"
                   set color white 
                   pd repeat 7 [fd 1.13 lt 12]
end 

to part5
                   setxy  0 8
                   set pen-size 1.5
                   set shape "point"
                   set color white 
                   pd repeat 7 [fd 1.12 rt 12]
                   pu
end   

to part6
                   setxy  5  13
                   set pen-size 1
                   set shape "point"
                   set color white 
                   pd repeat 11 [fd 1 ]
                   pu repeat 1 [fd 1 ] pd repeat 1 [fd 1 ] pu repeat 1 [fd 1 ]
                    pd repeat 1 [fd 1 ] pu repeat 1 [fd 1 ] pd repeat 1 [fd 1 ]
                     pu repeat 1 [fd 1 ] pd repeat 1 [fd 1 ] pu repeat 1 [fd 1 ]
                      pd repeat 1 [fd 1 ] pu repeat 1 [fd 1 ] pd repeat 1 [fd 1 ]
                       pu repeat 1 [fd 1 ] pd repeat 1 [fd 1 ] pu repeat 1 [fd 1 ]
                      pd repeat 1 [fd 1 ] pu repeat 1 [fd 1 ] pd repeat 1 [fd 1 ]
                     pu repeat 1 [fd 1 ] pd repeat 1 [fd 1 ] pu repeat 1 [fd 1 ]
                    pd repeat 1 [fd 1 ] pu repeat 1 [fd 1 ] pd repeat 1 [fd 1 ]
                   pu
end 
;-------------------------------------------------------------------------------------------
; PART 4                            DRAWING RAILS       
;-------------------------------------------------------------------------------------------

to rails
        create-custom-rail 25 [  setxy min-pxcor + 1.2  0 
                                set heading 90 
                                set size 3 
                                non-overlapping1   ]
        create-custom-rail1 4 [ set color gray + 1
                                     setxy -1.5  0 
                                     set heading 90 
                                     set size 3 
                                non-overlapping1   ]                        
        create-custom-rail 25 [  setxy 4.2  0 
                                set heading 90 
                                set size 3 
                                non-overlapping1   ]                        
end 

to non-overlapping1
         if any? other-turtles-here [ fd 1.5 non-overlapping1  ]
end 
;-------------------------------------------------------------------------------------------
; PART 5      DRAWING OF SIGNAL TABLES FOR ROAD USERS & DRAWING OF WARNING AND RAIL SIGNALS
;-------------------------------------------------------------------------------------------

to STs; drawing signal tables for road communication 
  create-custom-ST240 1  [setxy -6 -16.2 set heading 90  set size 5 ]
  create-custom-ST160 1  [setxy 0.5 -14  set heading 50  set size 5 ]
  create-custom-ST80  1  [setxy 3.2 -8   set heading 0   set size 5 ]
  create-custom-ST240 1  [setxy 6 16.2   set heading 270 set size 5 ]
  create-custom-ST160 1  [setxy -0.5 14  set heading 230 set size 5 ]
  create-custom-ST80  1  [setxy -3.2 8   set heading 180 set size 5 ]
end 

to wsignals ;custom turtle "wsignal" is defined inside setup, 
            ;it remembers its position all the time therefore it is not necessary to use Ask 
   create-custom-wsignal 1 [setxy 5 -3  set heading 0 set size 4 ]
   create-custom-wsignal 1 [setxy -5 3  set heading 180 set size 4 ]
   create-obstacle-EW 1 
   ask obstacle-EW [setxy -9 6  set shape "x" set color green set size 1] ;turtle "obstacle" is also created inside setup,
                                 ;it changes its position during the warning period and avoid vehicles from enterring the rails area 
   create-obstacle-WE 1 
   ask obstacle-WE [setxy 9 -6 set shape "x" set color green set size 1]
   create-barriers-WE 1 
   ask barriers-WE [setxy 3.5 0 set heading 90 set size 6  ] 
   create-barriers-EW 1 
   ask barriers-EW [setxy -3.5 0 set heading 270 set size 6  ]  
   create-custom-state-of-wsignal-WE 1 [setxy 5 -3.3 set size 4.6 set color black] ;creation of the signal sign "black" at the place of active signalling 
   create-custom-state-of-wsignal-EW 1 [setxy -5 3.3 set size 4.6 set color black] ;this signal will be drawn accoring to the state of the level crossing 
end 

to rail-signal-draw ;drawing the rail signal
 create-custom-rail-signal 1 [setxy -38 -2.5 set heading 90 set size 4.3]
 create-custom-state-of-rsignal 1 [setxy -36.90 -2.45 set size 4.5 set color black]
end 
 
;-------------------------------------------------------------------------------------------
; PART 6                     THE MAIN BODY OF THE PROGRAMME 
;-------------------------------------------------------------------------------------------

to Simulation
   Simulation/step
end  
            ;using the procedure "Simulation/step" all required procedures are called
            ;these procedures are performed during the entire running of the programme  

to Simulation/step
    time
    occupancy-check
    wagons-newF
    wagons-newP
    wagons-newF-special
    wagons-newP-special
    time-table 
    arrival-hour
    move-for-train 
    set-speed-of-vehicles
    CCM
    set freq freq + 1
    if freq > Time-interval   [new-vehicles cycle set freq 0 set choose-vehicleWE  random 3 ]
    movement
    turning
    set freq< freq< + 1
    if freq< > Time-interval< [new-vehicles< cycle< set freq< 0 set choose-vehicleEW random 3]
    movement<
    turning<
    set-variables
end 
;-------------------------------------------------------------------------------------------
; PART 7                PROCEDURES USED TO BOLT BARRIERS 
;-------------------------------------------------------------------------------------------

to barriers
  ask obstacle-EW [setxy -1.5 3.5 set shape "x" set size 1 set color black]
  ask obstacle-WE [setxy 1.5 -3.5 set shape "x" set size 1 set color black]
  set tcb tcb + 1
  if tcb = 50 [ask barriers-WE [setxy 0.8 -2.8 set heading 0  set size 6 ] ;turtle generated in setup using procedure wsignals 
              ask barriers-EW [setxy -0.8 2.8 set heading 180 set size 6 ]set tcb 0]
end 

to barriersUP
  ask barriers-WE [setxy 3.5 0 set heading 90 set size 6 ] ;turtle generated in setup using procedure wsignals 
  ask barriers-EW [setxy -3.5 0 set heading 270 set size 6 ]
  ask obstacle-EW [setxy -9 6 set shape "x" set size 1 set color green]
  ask obstacle-WE [setxy 9 -6 set shape "x" set size 1 set color green]
end 

;-------------------------------------------------------------------------------------------
; PART 8              USE OF APPROACH TIME PREDICTOR ATPr OR ITS ABSENCE  
;-------------------------------------------------------------------------------------------

to CCM ; central control module contains (un)selection of ATPr  
       ;the following conditions define states of all three sections (A,B,C)           
ifelse ((any? wagonF with [ (pxcor > min-pxcor and ycor = 0.5) and (pxcor < -4 and ycor = 0.5) ] ) or
        (any? wagonP with [ (pxcor > min-pxcor and ycor = 0.5) and (pxcor < -4 and ycor = 0.5) ] ) or
        (any? trainF  with [ (pxcor > min-pxcor and ycor = 0.5) and (pxcor < -4 and ycor = 0.5) ] ) or
        (any? trainP  with [ (pxcor > min-pxcor and ycor = 0.5) and (pxcor < -4 and ycor = 0.5) ] ))
        [set A "occupied"]
        [set A "vacant"] 
ifelse ((any? wagonF with [ (pxcor > -4 and ycor = 0.5) and (pxcor < 7 and ycor = 0.5) ] ) or
        (any? wagonP with [ (pxcor > -4 and ycor = 0.5) and (pxcor < 7 and ycor = 0.5) ] ) or
        (any? trainF  with [ (pxcor > -4 and ycor = 0.5) and (pxcor < 7 and ycor = 0.5) ] ) or
        (any? trainP  with [ (pxcor > -4 and ycor = 0.5) and (pxcor < 7 and ycor = 0.5) ] ) )
        [set B "occupied"]
        [set B "vacant"]                  
ifelse ((any? wagonF with [ (pxcor > 7 and ycor = 0.5) and (pxcor < max-pxcor and ycor = 0.5) ] ) or
        (any? wagonP with [ (pxcor > 7 and ycor = 0.5) and (pxcor < max-pxcor and ycor = 0.5) ] ) or
        (any? trainF  with [ (pxcor > 7 and ycor = 0.5) and (pxcor < max-pxcor and ycor = 0.5) ] ) or
        (any? trainP  with [ (pxcor > 7 and ycor = 0.5) and (pxcor < max-pxcor and ycor = 0.5) ] ))
        [set C "occupied"]
        [set C "vacant"] 
     use-of-ATPr ;call of procedure given below
end 

to use-of-ATPr
  if (Yes/No = "YES")[ATPr-yes]   
  if (Yes/No = "NO")[ATPr-no] 
end   

to ATPr-no ;fulfilling of the following conditions enables to choose one of three states of the level crossing system used in this model 
  if (A = "occupied" or B = "occupied") [warning-state] 
  if (A = "vacant" and B = "vacant" and C = "occupied") [annulation-state] 
  if (A = "vacant" and B = "vacant" and C = "vacant") [basic-state]
;fulfilment of the following conditions make possible visualization of train movement in bottom right-hand corner of the dispaly window                             
ifelse A = "occupied" [ask patches with [((pxcor  > 10 and pycor > -14) and (pxcor < 24 and pycor < -12 ))][ set pcolor red ]]
                [ask patches with [((pxcor  > 10 and pycor > -14) and (pxcor < 24 and pycor < -12 ))][ set pcolor gray ]]
ifelse B = "occupied" [ask patches with [((pxcor  >= 25 and pycor > -14)and (pxcor < 29 and pycor < -12 ))][ set pcolor red ]]
                [ask patches with [((pxcor  >= 25 and pycor > -14)and (pxcor < 29 and pycor < -12 ))][ set pcolor gray ]]
ifelse C = "occupied" [ask patches with [((pxcor  >= 30 and pycor > -14)and (pxcor <= 40 and pycor < -12 ))][ set pcolor red ]]
                [ask patches with [((pxcor  >= 30 and pycor > -14)and (pxcor <= 40 and pycor < -12 ))][ set pcolor gray ]]        
end 

to ATPr-yes
  if A = "occupied" [set timeATPr timeATPr + 1 ] 
  if (freight-train = 1 and (((For_Freight_Train  = 10  )and timeATPr = 1136 -  71)or
                             ((For_Freight_Train  = 20  )and timeATPr = 568  - 71) or 
                             ((For_Freight_Train  = 30  )and timeATPr = 378  - 71) or 
                             ((For_Freight_Train  = 40  )and timeATPr = 284  - 71) or 
                             ((For_Freight_Train  = 50  )and timeATPr = 227  - 71) or 
                             ((For_Freight_Train  = 60  )and timeATPr = 189  - 71) or 
                             ((For_Freight_Train  = 70  )and timeATPr = 162  - 71) or 
                             ((For_Freight_Train  = 80  )and timeATPr = 142  - 71) or 
                             ((For_Freight_Train  = 90  )and timeATPr = 126  - 71) or 
                             ((For_Freight_Train  = 100 )and timeATPr = 113  - 71) or 
                             ((For_Freight_Train  = 110 )and timeATPr = 103  - 71) or 
                             ((For_Freight_Train  = 120 )and timeATPr = 94   - 71) or 
                             ((For_Freight_Train  = 130 )and timeATPr = 87   - 71) or 
                             ((For_Freight_Train  = 140 )and timeATPr = 81   - 71) or 
                             ((For_Freight_Train  = 150 )and timeATPr = 75   - 71) or 
                             ((For_Freight_Train  = 160 )and timeATPr = 5)));speed of freight train in interface is max 80,
                                                                            ;however here it is processed for all speeds  
                             [set ativation-point-ATPr 1]
                             
   if (passenger-train = 1 and  (((For_Passenger_Train = 10  )and timeATPr = 1136 - 71)or
                             ((For_Passenger_Train  = 20  )and timeATPr = 568  - 71) or 
                             ((For_Passenger_Train  = 30  )and timeATPr = 378  - 71) or 
                             ((For_Passenger_Train  = 40  )and timeATPr = 284  - 71) or 
                             ((For_Passenger_Train  = 50  )and timeATPr = 227  - 71) or 
                             ((For_Passenger_Train  = 60  )and timeATPr = 189  - 71) or 
                             ((For_Passenger_Train  = 70  )and timeATPr = 162  - 71) or 
                             ((For_Passenger_Train  = 80  )and timeATPr = 142  - 71) or 
                             ((For_Passenger_Train  = 90  )and timeATPr = 126  - 71) or 
                             ((For_Passenger_Train  = 100 )and timeATPr = 113  - 71) or 
                             ((For_Passenger_Train  = 110 )and timeATPr = 103  - 71) or 
                             ((For_Passenger_Train  = 120 )and timeATPr = 94   - 71) or 
                             ((For_Passenger_Train  = 130 )and timeATPr = 87   - 71) or 
                             ((For_Passenger_Train  = 140 )and timeATPr = 81   - 71) or 
                             ((For_Passenger_Train  = 150 )and timeATPr = 75   - 71) or 
                             ((For_Passenger_Train  = 160 )and timeATPr = 1))) 
                             [set ativation-point-ATPr 1]
                             
;fulfilment of the following conditions make possible visualization of train movement in bottom right-hand corner of the dispaly window                             
if(freight-train = 1 and For_Freight_Train  = 160  and timeATPr >= 1)or
  (passenger-train = 1 and For_Passenger_Train  = 160 and timeATPr >= 1        )[ask (patch 11 -13) [set pcolor red ]]
if(freight-train = 1 and For_Freight_Train  = 150  and timeATPr >= 75  - 71)or
  (passenger-train = 1 and For_Passenger_Train  = 150 and timeATPr >= 75  - 71 )[ask (patch 11 -13) [set pcolor red ]]
if(freight-train = 1 and For_Freight_Train  = 140  and timeATPr >= 81  - 71)or
  (passenger-train = 1 and For_Passenger_Train  = 140 and timeATPr >= 81  - 71 )[ask (patch 12 -13) [set pcolor red ]]
if(freight-train = 1 and For_Freight_Train  = 130  and timeATPr >= 87  - 71)or
  (passenger-train = 1 and For_Passenger_Train  = 130 and timeATPr >= 87  - 71 )[ask (patch 13 -13) [set pcolor red ]]
if(freight-train = 1 and For_Freight_Train  = 120  and timeATPr >= 94  - 71)or
  (passenger-train = 1 and For_Passenger_Train  = 120 and timeATPr >= 94  - 71 )[ask (patch 14 -13) [set pcolor red ]]
if(freight-train = 1 and For_Freight_Train  = 110  and timeATPr >= 103 -  71)or
  (passenger-train = 1 and For_Passenger_Train  = 110 and timeATPr >= 103  - 71 )[ask (patch 15 -13) [set pcolor red ]]
if(freight-train = 1 and For_Freight_Train  = 100  and timeATPr >= 113 -  71)or
  (passenger-train = 1 and For_Passenger_Train  = 100 and timeATPr >= 113  - 71 )[ask (patch 16 -13) [set pcolor red ]]
if(freight-train = 1 and For_Freight_Train  = 90   and timeATPr >= 126 -  71)or
  (passenger-train = 1 and For_Passenger_Train  = 90 and timeATPr >= 126  - 71 )[ask (patch 15 -13) [set pcolor red ]]
if(freight-train = 1 and For_Freight_Train  = 80   and timeATPr >= 142 -  71)or
  (passenger-train = 1 and For_Passenger_Train  = 80 and timeATPr >= 142  - 71 )[ask (patch 17 -13) [set pcolor red ]]
if(freight-train = 1 and For_Freight_Train  = 70   and timeATPr >= 162 -  71)or
  (passenger-train = 1 and For_Passenger_Train  = 70 and timeATPr >= 162  - 71 )[ask (patch 17 -13) [set pcolor red ]]
if(freight-train = 1 and For_Freight_Train  = 60   and timeATPr >= 189 -  71)or
  (passenger-train = 1 and For_Passenger_Train  = 60 and timeATPr >= 189  - 71 )[ask (patch 18 -13) [set pcolor red ]]
if(freight-train = 1 and For_Freight_Train  = 50   and timeATPr >= 227 -  71)or
  (passenger-train = 1 and For_Passenger_Train  = 50 and timeATPr >= 227  - 71 )[ask (patch 19 -13) [set pcolor red ]]
if(freight-train = 1 and For_Freight_Train  = 40   and timeATPr >= 284 -  71)or
  (passenger-train = 1 and For_Passenger_Train  = 40 and timeATPr >= 284  - 71 )[ask (patch 20 -13) [set pcolor red ]]
if(freight-train = 1 and For_Freight_Train  = 30   and timeATPr >= 378 -  71)or
  (passenger-train = 1 and For_Passenger_Train  = 30 and timeATPr >= 378  - 71 )[ask (patch 21 -13) [set pcolor red ]]
if(freight-train = 1 and For_Freight_Train  = 20   and timeATPr >= 568 -  71)or
  (passenger-train = 1 and For_Passenger_Train  = 20 and timeATPr >= 568  - 71 )[ask (patch 21 -13) [set pcolor red ]]
if(freight-train = 1 and For_Freight_Train  = 10   and timeATPr >= 1136 -  71)or
  (passenger-train = 1 and For_Passenger_Train  = 10 and timeATPr >= 1136  - 71 )[ask (patch 22 -13) [set pcolor red ]]
  
  if ativation-point-ATPr = 1 [warning-state] 
  if (A = "vacant" and B = "vacant" and C = "occupied") [annulation-state ] 
  if (ativation-point-ATPr = 0 and B = "vacant" and C = "vacant") [basic-state]
ifelse B = "occupied" [ask patches with [((pxcor  >= 25 and pycor > -14)and (pxcor < 29 and pycor < -12 ))][ set pcolor red ]]
                [ask patches with [((pxcor  >= 25 and pycor > -14)and (pxcor < 29 and pycor < -12 ))][ set pcolor gray ]]
ifelse C = "occupied" [ask patches with [((pxcor  >= 30 and pycor > -14)and (pxcor <= 40 and pycor < -12 ))][ set pcolor red ]]
                [ask patches with [((pxcor  >= 30 and pycor > -14)and (pxcor <= 40 and pycor < -12 ))][ set pcolor gray ]]   
end 

;-------------------------------------------------------------------------------------------
;  PART 9        ACTIVE SIGNALLING, WARNING ACTIVATION, WARNING TERMINATION
;-------------------------------------------------------------------------------------------
             ;Note: so called "active signalling" is represented by one white flashing light

to basic-state
   set tcb 0
   set blinkA blinkA + 1
    if blinkA = 20 [ask state-of-wsignal-WE [setxy 5 -3.3 set size 4.6 set color white ]
                  ask state-of-wsignal-EW [setxy -5 3.3 set size 4.6 set color white ]  ]
   if blinkA = 40 [ask state-of-wsignal-WE [setxy 5 -3.3 set size 4.6 set color black set blinkA 0 ]
                  ask state-of-wsignal-EW [setxy -5 3.3 set size 4.6 set color black set blinkA 0 ]    ]
   ask state-of-rsignal [setxy -36.90 -2.45 set size 4.5 set color white] ;relation Level Crossing - Rail Signal
end    

to warning-state
    if (blinkA >= 20 and blinkA <= 40) [ask state-of-wsignal-WE [set color black ] ;condition deactivates active signalling after occupying A                                      
ask state-of-wsignal-EW [set color black ] set blinkA 0 set blinkW 18 ]
    set blinkW blinkW + 1 ;variable blink is used for both active signalling and warning signalling
    if blinkW = 20 [ask state-of-wsignal-WE [setxy 4.4 -2.5 set size 4.8 set color red beep beep beep]
                   ask state-of-wsignal-EW [setxy -4.6 2.6 set size 4.8 set color red ]]
    if blinkW = 40 [ask state-of-wsignal-WE [setxy 5.5 -2.5 set size 4.8 set color red set blinkW 0 ]
                   ask state-of-wsignal-EW [setxy -5.7 2.6 set size 4.8 set color red set blinkW 0 ]    ] 
    barriers 
ask state-of-rsignal [setxy -36.90 -2.45 set size 4.5 set color white] ;relation Level Crossing - Rail Signal             
end   

to annulation-state
  set timeATPr 0
  set ativation-point-ATPr 0
  set tcb 0
  ask state-of-wsignal-WE [set color black ]
  ask state-of-wsignal-EW [set color black ]
  ask patches with [((pxcor  > 10 and pycor > -14) and (pxcor < 24 and pycor < -12 ))][ set pcolor gray ]   
  barriersUP
  ask state-of-rsignal [setxy -36.90 -2.45 set size 4.5 set color white] ;relation Level Crossing - Rail Signal 
end 

;-------------------------------------------------------------------------------------------
;  PART 10                       GENERATING OF FREIGHT AND PASSENGER TRAINS   
;-------------------------------------------------------------------------------------------

to occupancy-check   ;after occupying the track (here particular patches) te variable "occupancy " is set to "1"   
                     ;this variable can be used in any procedure, where we need to test if a train is or is not
                     ;in the monitored part of the railway line
    ifelse ((any? wagonF with [ (pxcor > min-pxcor and ycor = 0.5) and (pxcor < max-pxcor and ycor = 0.5)  ] ) or
            (any? wagonP with [ (pxcor > min-pxcor and ycor = 0.5) and (pxcor < max-pxcor and ycor = 0.5)  ] ) or
            (any? trainF with [ (pxcor > min-pxcor and ycor = 0.5) and (pxcor < max-pxcor and ycor = 0.5) ] ) or
            (any? trainP with [ (pxcor > min-pxcor and ycor = 0.5) and (pxcor < max-pxcor and ycor = 0.5)  ] ))
           
           [set occupancy 1]
           [set occupancy 0] 
end 

to generated-F-train
     create-custom-trainF 1 [set color white 
                           setxy -39 0.5
                           set size 4
                           set heading 90
                           set generationF 1
                           set freight-train 1] ;
end    

to wagons-newF ;procedure ensures generating of freight wagons (freight cars) following the locomotive   
     if not (((any? wagonF-at min-pxcor 0.5) or (any? wagonF-at (min-pxcor + 1) 0.5) or (any? wagonF-at (min-pxcor + 2) 0.5)
           or (any? wagonF-at (min-pxcor + 3) 0.5) or(any? wagonF-at (min-pxcor + 3.49) 0.5)
           or (any? trainF-at min-pxcor 0.5) or (any? trainF-at (min-pxcor + 1) 0.5) or (any? trainF-at (min-pxcor + 2) 0.5)
           or (any? trainF-at (min-pxcor + 3) 0.5) or(any? trainF-at (min-pxcor + 3.5) 0.5)) or generationF = 0 or Freight-cars = 0)
             [create-custom-wagonF 1 [
                                          set color white 
                                          setxy -39 0.5
                                          set size 3.3
                                          set heading 90
                                          set counter-wagonsF counter-wagonsF + 1 ]]
         if counter-wagonsF = Freight-cars [set generationF 0 set counter-wagonsF 0]
         if (occupancy = 0 ) [set freight-train 0]
end 

to generated-P-train
    create-custom-trainP 1 [set color white 
                           setxy -39 0.5
                           set size 3.55
                           set heading 90 
                           set generationP 1
                           set passenger-train 1 ]              
end  

to wagons-newP ;procedure ensures generating of passenger wagons (coaches) following the locomotive   
     if not (((any? wagonP-at min-pxcor 0.5) or (any? wagonP-at (min-pxcor + 1) 0.5) or (any? wagonP-at (min-pxcor + 2) 0.5)
           or (any? wagonP-at (min-pxcor + 3) 0.5) or(any? wagonP-at (min-pxcor + 3.49) 0.5)
           or (any? trainP-at min-pxcor 0.5) or (any? trainP-at (min-pxcor + 1) 0.5) or (any? trainP-at (min-pxcor + 2) 0.5)
           or (any? trainP-at (min-pxcor + 3) 0.5) or(any? trainP-at (min-pxcor + 3.5) 0.5)) or generationP = 0 or Coaches = 0)
             [create-custom-wagonP 1 [ 
                                      set color white 
                                      setxy -39 0.5
                                      set size 3.5
                                      set heading 90
                                      set counter-wagonsP counter-wagonsP + 1 ]]
         if counter-wagonsP = Coaches[set generationP 0 set counter-wagonsP 0]
         if (occupancy = 0 ) [set passenger-train 0] 
end      

to move-for-train
    ask trainF  [ fd  (For_Freight_Train * delta_t) ]
    ask wagonF [ fd  (For_Freight_Train * delta_t) ] 
    ask trainF [ if xcor > max-pxcor - 0.8 [set no_of_freight_trains no_of_freight_trains + 1  die ]]
    ask wagonF [ if xcor > max-pxcor - 0.8 [ die ]]
    ask trainP  [ fd  (For_Passenger_Train * delta_t) ]
    ask wagonP [ fd  (For_Passenger_Train * delta_t) ]
    ask trainP [ if xcor > max-pxcor - 1 [  set no_of_passenger_trains no_of_passenger_trains + 1 die]]
    ask wagonP [ if xcor > max-pxcor - 1 [ die  ]]
end  

;-------------------------------------------------------------------------------------------
; PART 11                 GENERATING OF THE SPECIAL TRAIN   
;-------------------------------------------------------------------------------------------

to special-train
     ifelse occupancy = 1
            [user-message "There is a rail vehicle at the line and required train cannot be dispatched. Check actual time of simulation and compare it with train time-table. To continue simulation press OK - to stop simulation press HALT. "
            stop]
            [if Train = "freight" [generated-F-train-special]
             if Train = "passenger" [generated-P-train-special]
            ]
end 

to generated-F-train-special
     create-custom-trainF 1 [set color white 
                           setxy -39 0.5
                           set size 4
                           set heading 90
                           set generationFS 1
                           set freight-train 1]
end       

to wagons-newF-special ;procedure ensures generating of freight wagons (freight cars) following the locomotive   
     if not (((any? wagonF-at min-pxcor 0.5) or (any? wagonF-at (min-pxcor + 1) 0.5) or (any? wagonF-at (min-pxcor + 2) 0.5)
           or (any? wagonF-at (min-pxcor + 3) 0.5) or(any? wagonF-at (min-pxcor + 3.49) 0.5)
           or (any? trainF-at min-pxcor 0.5) or (any? trainF-at (min-pxcor + 1) 0.5) or (any? trainF-at (min-pxcor + 2) 0.5)
           or (any? trainF-at (min-pxcor + 3) 0.5) or(any? trainF-at (min-pxcor + 3.5) 0.5)) or generationFS = 0 or Number_of_Wagons = 0)
             [create-custom-wagonF 1 [
                                          set color white 
                                          setxy -39 0.5
                                          set size 3.3
                                          set heading 90
                                          set counter-wagonsFS counter-wagonsFS + 1 ]]
         if counter-wagonsFS = Number_of_Wagons [set generationFS 0 set counter-wagonsFS 0]
end 

to generated-P-train-special
    create-custom-trainP 1 [set color white 
                           setxy -39 0.5
                           set size 3.55
                           set heading 90 
                           set generationPS 1
                           set passenger-train   1]              
end  

to wagons-newP-special ;procedure ensures generating of passenger wagons following the locomotive    
       if not (((any? wagonP-at min-pxcor 0.5) or (any? wagonP-at (min-pxcor + 1) 0.5) or (any? wagonP-at (min-pxcor + 2) 0.5)
           or (any? wagonP-at (min-pxcor + 3) 0.5) or(any? wagonP-at (min-pxcor + 3.49) 0.5)
           or (any? trainP-at min-pxcor 0.5) or (any? trainP-at (min-pxcor + 1) 0.5) or (any? trainP-at (min-pxcor + 2) 0.5)
           or (any? trainP-at (min-pxcor + 3) 0.5) or(any? trainP-at (min-pxcor + 3.5) 0.5)) or generationPS = 0 or Number_of_Wagons = 0)
             [create-custom-wagonP 1 [ 
                                      set color white 
                                      setxy -39 0.5
                                      set size 3.5
                                      set heading 90
                                      set counter-wagonsPS counter-wagonsPS + 1 ]]
         if counter-wagonsPS = Number_of_Wagons [set generationPS 0 set counter-wagonsPS 0]
end      

;-------------------------------------------------------------------------------------------
;  PART 12                       PROCEDURE USED TO SET TIME-TABLE    
;-------------------------------------------------------------------------------------------

to time-table 
  if (==TGT== = "====1====")[tgt-1]   
  if (==TGT== = "====2====" )[tgt-2]
end 

to tgt-1
  if ((((int actual_day_hour = 0 ) and ((actual_day_minute >= 30)and(actual_day_minute <= 31)) )and occupancy = 0))
    [generated-F-train set For_Freight_Train 80]
  if ((((int actual_day_hour = 1 ) and ((actual_day_minute >= 30)and(actual_day_minute <= 31)) )and occupancy = 0))
    [generated-F-train set For_Freight_Train 80]  
  if ((((int actual_day_hour = 3 )and ((actual_day_minute >= 0)and(actual_day_minute <= 1)))and occupancy = 0))
    [generated-F-train set For_Freight_Train 80]  
  if ((((int actual_day_hour = 23 ) and ((actual_day_minute >= 45)and(actual_day_minute <= 46)) )and occupancy = 0))
    [generated-F-train set For_Freight_Train 80] ;freight trains in time-table TGT1
    
  if ((((int actual_day_hour = 4 ) and  ((actual_day_minute >= 30)and(actual_day_minute <= 31)) )and occupancy = 0))
    [generated-P-train set For_Passenger_Train 160]  
  if ((((int actual_day_hour = 5 ) and  ((actual_day_minute >= 30)and(actual_day_minute <= 31)) )and occupancy = 0))
    [generated-P-train set For_Passenger_Train 160]  
  if ((((int actual_day_hour = 6 ) and  ((actual_day_minute >= 30)and(actual_day_minute <= 31)) )and occupancy = 0))
    [generated-P-train set For_Passenger_Train 160]
  if ((((int actual_day_hour = 8 ) and  ((actual_day_minute >= 35)and(actual_day_minute <= 36)) )and occupancy = 0))
    [generated-P-train set For_Passenger_Train 160]  
  if ((((int actual_day_hour = 10 ) and ((actual_day_minute >= 35)and(actual_day_minute <= 36)) )and occupancy = 0))
    [generated-P-train set For_Passenger_Train 160] 
  if ((((int actual_day_hour = 12 ) and ((actual_day_minute >= 35)and(actual_day_minute <= 36)) )and occupancy = 0))
    [generated-P-train set For_Passenger_Train 160] 
  if ((((int actual_day_hour = 14 ) and ((actual_day_minute >= 40)and(actual_day_minute <= 41)) )and occupancy = 0))
    [generated-P-train set For_Passenger_Train 160] 
  if ((((int actual_day_hour = 16 ) and ((actual_day_minute >= 35)and(actual_day_minute <= 36)) )and occupancy = 0))
    [generated-P-train set For_Passenger_Train 160] 
  if ((((int actual_day_hour = 18 ) and ((actual_day_minute >= 35)and(actual_day_minute <= 36)) )and occupancy = 0))
    [generated-P-train set For_Passenger_Train 160]
  if ((((int actual_day_hour = 20 ) and ((actual_day_minute >= 30)and(actual_day_minute <= 31)) )and occupancy = 0))
    [generated-P-train set For_Passenger_Train 160] ;speed trains (passenger trains) in time-table TGT1 
    
  if ((((int actual_day_hour = 5 ) and  ((actual_day_minute >= 0)and(actual_day_minute <= 1)))and occupancy = 0))
    [generated-P-train set For_Passenger_Train 100]
  if ((((int actual_day_hour = 6 ) and  ((actual_day_minute >= 15)and(actual_day_minute <= 16)) )and occupancy = 0))
    [generated-P-train set For_Passenger_Train 100]
  if ((((int actual_day_hour = 7 ) and  ((actual_day_minute >= 0)and(actual_day_minute <= 1)) )and occupancy = 0))
    [generated-P-train set For_Passenger_Train 100] 
  if ((((int actual_day_hour = 11 ) and  ((actual_day_minute >= 0)and(actual_day_minute <= 1)) )and occupancy = 0))
    [generated-P-train set For_Passenger_Train 100]   
  if ((((int actual_day_hour = 13 ) and  ((actual_day_minute >= 35)and(actual_day_minute <= 36)) )and occupancy = 0))
    [generated-P-train set For_Passenger_Train 100] 
  if ((((int actual_day_hour = 14 ) and  ((actual_day_minute >= 25)and(actual_day_minute <= 26)) )and occupancy = 0))
    [generated-P-train set For_Passenger_Train 100] 
  if ((((int actual_day_hour = 15 ) and  ((actual_day_minute >= 40)and(actual_day_minute <= 41)) )and occupancy = 0))
    [generated-P-train set For_Passenger_Train 100]
  if ((((int actual_day_hour = 17 ) and  ((actual_day_minute >= 0)and(actual_day_minute <= 1)) )and occupancy = 0))
    [generated-P-train set For_Passenger_Train 100]  
  if ((((int actual_day_hour = 17 ) and  ((actual_day_minute >= 30)and(actual_day_minute <= 31)) )and occupancy = 0))
    [generated-P-train set For_Passenger_Train 100]
  if ((((int actual_day_hour = 18 ) and  ((actual_day_minute >= 50)and(actual_day_minute <= 51)) )and occupancy = 0))
    [generated-P-train set For_Passenger_Train 100] 
  if ((((int actual_day_hour = 22 ) and  ((actual_day_minute >= 30)and(actual_day_minute <= 31)) )and occupancy = 0))
    [generated-P-train set For_Passenger_Train 100] ;passenger trains in time-table TGT1 
end 

to tgt-2
  if ((((int actual_day_hour = 0 ) and ((actual_day_minute >= 50)and(actual_day_minute <= 51)) )and occupancy = 0))
    [generated-F-train set For_Freight_Train 80]
  if ((((int actual_day_hour = 2 ) and ((actual_day_minute >= 0)and(actual_day_minute <= 1)) )and occupancy = 0))
    [generated-F-train set For_Freight_Train 80]  
  if ((((int actual_day_hour = 3 )and ((actual_day_minute >= 30)and(actual_day_minute <= 31)))and occupancy = 0))
    [generated-F-train set For_Freight_Train 80]  
  if ((((int actual_day_hour = 5 ) and ((actual_day_minute >= 30)and(actual_day_minute <= 31)) )and occupancy = 0))
    [generated-F-train set For_Freight_Train 80]
  if ((((int actual_day_hour = 9 ) and ((actual_day_minute >= 30)and(actual_day_minute <= 31)) )and occupancy = 0))
    [generated-F-train set For_Freight_Train 80]
  if ((((int actual_day_hour = 12 ) and ((actual_day_minute >= 5)and(actual_day_minute <= 6)) )and occupancy = 0))
    [generated-F-train set For_Freight_Train 80]  
  if ((((int actual_day_hour = 13 )and ((actual_day_minute >= 30)and(actual_day_minute <= 31)))and occupancy = 0))
    [generated-F-train set For_Freight_Train 80]  
  if ((((int actual_day_hour = 17 ) and ((actual_day_minute >= 0)and(actual_day_minute <= 1)) )and occupancy = 0))
    [generated-F-train set For_Freight_Train 80]
  if ((((int actual_day_hour = 18 )and ((actual_day_minute >= 30)and(actual_day_minute <= 31)))and occupancy = 0))
    [generated-F-train set For_Freight_Train 80]  
  if ((((int actual_day_hour = 19 ) and ((actual_day_minute >= 50)and(actual_day_minute <= 51)) )and occupancy = 0))
    [generated-F-train set For_Freight_Train 80] 
  if ((((int actual_day_hour = 21 )and ((actual_day_minute >= 10)and(actual_day_minute <= 11)))and occupancy = 0))
    [generated-F-train set For_Freight_Train 80]  
  if ((((int actual_day_hour = 23 ) and ((actual_day_minute >= 30)and(actual_day_minute <= 31)) )and occupancy = 0))
    [generated-F-train set For_Freight_Train 80] ;freight trains in time-table TGT2

  if ((((int actual_day_hour = 4 ) and  ((actual_day_minute >= 10)and(actual_day_minute <= 11)))and occupancy = 0))
    [generated-P-train set For_Passenger_Train 100]
  if ((((int actual_day_hour = 5 ) and  ((actual_day_minute >= 0)and(actual_day_minute <= 1)) )and occupancy = 0))
    [generated-P-train set For_Passenger_Train 100]
  if ((((int actual_day_hour = 6 ) and  ((actual_day_minute >= 30)and(actual_day_minute <= 31)) )and occupancy = 0))
    [generated-P-train set For_Passenger_Train 100] 
  if ((((int actual_day_hour = 10 ) and  ((actual_day_minute >= 0)and(actual_day_minute <= 1)) )and occupancy = 0))
    [generated-P-train set For_Passenger_Train 100]   
  if ((((int actual_day_hour = 14 ) and  ((actual_day_minute >= 0)and(actual_day_minute <= 1)) )and occupancy = 0))
    [generated-P-train set For_Passenger_Train 100] 
  if ((((int actual_day_hour = 19 ) and  ((actual_day_minute >= 50)and(actual_day_minute <= 51)) )and occupancy = 0))
    [generated-P-train set For_Passenger_Train 100]                       
end 

;-------------------------------------------------------------------------------------------
;  PART 13                              TIME PROCEDURES   
;-------------------------------------------------------------------------------------------     

to time
 set resetting time_day 
 if (int resetting) + 0.002 >= resetting  [clear-all-plots set no_of_passenger_trains 0 set  no_of_freight_trains 0 set top_intensity 0 set resetting 0]
 set unit_time unit_time + 1 
 set actual_day_unit_minute actual_day_unit_minute + 1
 set actual_day_unit actual_day_unit + 1   
 set time_of_simulation ( unit_time / one_hour ) ;total time in hours (not real time) given for chosen number of units in one simulation hour 
 set time_day  ( unit_time / one_day )           ;sets total number of passed days 
 set actual_day_minute (actual_day_unit_minute / one_minute )
 set actual_day_hour (actual_day_unit / one_hour)
  if actual_day_minute >= 60 [do-plot  
                               set carsE 0 set trucksE 0
                               set carsW 0 set trucksW 0
                               set actual_day_unit_minute 0 
                               set actual_day_minute 0] ;60 is in minutes (not real but for the given model)
 if actual_day_hour >= 24 [set actual_day_hour 0 set actual_day_unit 0 calculate-results  ] ;24 is in hours (not real but for the given model)                               
end 

;-------------------------------------------------------------------------------------------
;   PART 14.              MOVEMENT FOR ROAD VEHICLES IN BOTH DIRECTIONS 
;-------------------------------------------------------------------------------------------   

to set-speed-of-vehicles
  set MaxSpeed ((MAX_SPEED / 1000) * 1.5)
  set MaxSpeed< ((MAX-SPEED / 1000) * 1.5)
end   

to movement
 ask cars-WE[  
     if xcor > max-pxcor - 1 
        [set No_of_cars_to_East No_of_cars_to_East + 1 set carsE carsE + 1 die ]
         set accel? true 
     if
       (any? cars-WE-at 0 1) or (any? cars-WE-at 0 2) or (any? cars-WE-at 0 3)or 
       (any? cars-WE-at 0.5 1) or (any? cars-WE-at 0.5 2) or (any? cars-WE-at 0.5 3)or
       (any? cars-WE-at 3 2) or (any? cars-WE-at 3 1) or (any? cars-WE-at 3 0.5) or (any? cars-WE-at 3 0)or
       (any? cars-WE-at 2 2) or (any? cars-WE-at 2 1) or (any? cars-WE-at 2 0.5) or (any? cars-WE-at 2 0)or
       (any? cars-WE-at 1 2) or (any? cars-WE-at 1 1) or (any? cars-WE-at 1 0.5) or (any? cars-WE-at 1 0)or
        ;reporters used to stop a car behind the truck follow
       (any?  trucks-WE-at 0 1) or (any?  trucks-WE-at 0 2) or (any?  trucks-WE-at 0 3)or
       (any?  trucks-WE-at 0.5 1) or (any?  trucks-WE-at 0.5 2) or (any?  trucks-WE-at 0.5 3)or
       ;always takes these reportes. If the barrier goes down immediately in front of the car, then condition is not 
       ;fulfilled any more since distance < 3 and reporters given below are used  
       (any?  trucks-WE-at 3 2) or (any?  trucks-WE-at 3 1) or (any?  trucks-WE-at 3 0.5)
        or (any?  trucks-WE-at 3 0)or
       (any?  trucks-WE-at 2 2) or (any?  trucks-WE-at 2 1) or (any?  trucks-WE-at 2 0.5) 
        or (any?  trucks-WE-at 2 0)or
       (any?  trucks-WE-at 1 2) or (any?  trucks-WE-at 1 1) or (any?  trucks-WE-at 1 0.5)
        or (any?  trucks-WE-at 1 0)or
             
       (any? obstacle-WE-at 0 1) or (any? obstacle-WE-at 0 2)or(any? obstacle-WE-at 0.5 1)or
       (any? obstacle-WE-at 0.5 2)
       [set speed 0 set accel? false] 
       ;if at least one condition of those given above following if is fulfilled, cars will stop (zero speed)
     if
         (any? cars-WE-at 4 0) or   ;slowing-down of 4patch prior to next car and car "looks" only directly ahead it
         (any? trucks-WE-at 4 0) or 
         (any? cars-WE-at 4 0.5) or ;slowing-down of 4patch to next car and “looks“ also from -14.5 to 14, usually uses intervals 0.5
         (any? trucks-WE-at 4 0.5)or
         (any? cars-WE-at 4 1) or 
         (any? trucks-WE-at 4 1)or
         (any? obstacle-WE-at 0.5 3);condition for slowing cars ahead truck in front of barriers too  
         [set speed 0.01 set accel? false] 
     if accel? [speed-up] 
      ;nasledujúca procedúra musí by uvedená ako podmienka nasledujúceho príkazu "fd speed" lebo inak vozidlá nebudú spomalova 
     if  xcor > -6 and xcor < 2 and ycor < 2 [ set speed speed / 3]
         fd speed ] 
 ask trucks-WE[  
     if xcor > max-pxcor - 1 
        [set No_of_trucks_to_East No_of_trucks_to_East + 1 set trucksE trucksE + 1 die ] 
        set accel? true 
     if
       (any?  trucks-WE-at 0 1) or (any?  trucks-WE-at 0 2)
        or (any?  trucks-WE-at 0 3)or
       (any?  trucks-WE-at 0.5 1) or(any?  trucks-WE-at 0.5 2)
        or (any?  trucks-WE-at 0.5 3)or
       (any?  trucks-WE-at 3 2) or (any?  trucks-WE-at 3 1) or (any?  trucks-WE-at 3 0.5)
        or (any?  trucks-WE-at 3 0)or
       (any?  trucks-WE-at 2 2) or (any?  trucks-WE-at 2 1) or (any?  trucks-WE-at 2 0.5)
        or (any?  trucks-WE-at 2 0)or
       (any?  trucks-WE-at 1 2) or (any?  trucks-WE-at 1 1) or (any?  trucks-WE-at 1 0.5)
        or (any?  trucks-WE-at 1 0  )or
              ;nasleduju reportery pouziti na zastavenie nakladneho pred osobnym 
       (any? cars-WE-at 0 1) or (any? cars-WE-at 0 2) or (any? cars-WE-at 0 3)or     
       (any? cars-WE-at 0.5 1) or (any? cars-WE-at 0.5 2) or (any? cars-WE-at 0.5 3)or
       (any? cars-WE-at 3 2) or (any? cars-WE-at 3 1) or (any? cars-WE-at 3 0.5) or (any? cars-WE-at 3 0)or
       (any? cars-WE-at 2 2) or (any? cars-WE-at 2 1) or (any? cars-WE-at 2 0.5) or (any? cars-WE-at 2 0)or
       (any? cars-WE-at 1 2) or (any? cars-WE-at 1 1) or (any? cars-WE-at 1 0.5) or (any? cars-WE-at 1 0)or
            
       (any? obstacle-WE-at 0.5 1)or (any? obstacle-WE-at 0.5 2)
       ;úplné zastavenie pred závorou vo vzdialenosti 1 
        [set speed 0 set accel? false]
     if (any? trucks-WE-at 4 0)or;spomalovanie pred autami a aj pred zavorami pocas vystrahy 
        (any? cars-WE-at 4 0) or
        (any? trucks-WE-at 4 0.5)or
        (any? cars-WE-at 4 0.5) or
        (any? trucks-WE-at 4 1)or
        (any? cars-WE-at 4 1) or
        (any? obstacle-WE-at 0.5 3)
        [set speed 0.01 set accel? false] 
      if accel? [speed-up]
      if  xcor > -6 and xcor < 2 and ycor < 2 [ set speed speed / 3 ] 
          fd speed ] 
end 

to speed-up
  ifelse (speed >= MaxSpeed)
  [set speed MaxSpeed fd speed]  ;ak je rýchlos väèšia, alebo rovná ako zadaná rýchlos
                                           ;zo slideru "RYCHLOST_MAX" potom nastaví "speed" na hodnotu 
                                           ;ktorá je na slideri èiže spomalí (obmedzenie rýchlosti)
  [set speed speed + 0.07 fd speed];ak je rýchlos menšia potom vozidlo zrých¾uje krokom 0.07
end 

to movement<
ask cars-EW[
     if xcor < 0 + min-pxcor + 1 [set No_of_cars_to_West No_of_cars_to_West + 1 set carsW carsW + 1 die ]
     set accel?< true
     if 
       (any? cars-EW-at 0.5 -1) or (any? cars-EW-at 0.5 -2) or (any? cars-EW-at 0.5 -3)or
       (any? cars-EW-at -3 -2) or (any? cars-EW-at -3 -1) or (any? cars-EW-at -3 -0.5) or (any? cars-EW-at -3 0)or
       (any? cars-EW-at -2 -2) or (any? cars-EW-at -2 -1) or (any? cars-EW-at -2 -0.5) or (any? cars-EW-at -2 0)or
       (any? cars-EW-at -1 -2) or (any? cars-EW-at -1 -1) or (any? cars-EW-at -1 -0.5) or (any? cars-EW-at -1 0)or
        ;nasleduju reportery pouzity na zastavenie osobneho pred nakladnym v smere Vychod-Zapad
       (any? trucks-EW-at 0.5 -1)or (any? trucks-EW-at 0.5 -2) or (any? trucks-EW-at 0.5 -3)or
       (any? trucks-EW-at -3 -2) or (any? trucks-EW-at -3 -1) or (any? trucks-EW-at -3 -0.5) or
       (any? trucks-EW-at -3 0)  or
       (any? trucks-EW-at -2 -2) or (any? trucks-EW-at -2 -1) or (any? trucks-EW-at -2 -0.5) or 
       (any? trucks-EW-at -2 0)  or
       (any? trucks-EW-at -1 -2) or (any? trucks-EW-at -1 -1) or (any? trucks-EW-at -1 -0.5) or 
       (any? trucks-EW-at -1 0)  or
          
       (any? obstacle-EW-at 0.5 -1) or (any? obstacle-EW-at 0.5 -2)or(any? obstacle-EW-at 0 -1)or
       (any? obstacle-EW-at 0 -2);zastavenie pred priecestím, ak je PZZ vo výstražnom stave 
       [set speed< 0 set accel?< false]
     if (any? cars-EW-at -4 0)or;spomalovanie 
        (any? trucks-EW-at -4 0)or
        (any? cars-EW-at -4 -0.5)or
        (any? trucks-EW-at -4 -0.5)or
        (any? cars-EW-at -4 -4)or
        (any? trucks-EW-at -4 -1)or
        (any? obstacle-EW-at 0.5 -3)
        [set speed< 0.01 set accel?< false] 
     if accel?< [speed-up<]
     if  xcor < 6 and xcor > -2 and ycor > -2 [ set speed< speed< / 3 ] fd speed< ]
            
ask trucks-EW[
     if xcor < 0 + min-pxcor + 1 [set No_of_trucks_to_West No_of_trucks_to_West + 1 set trucksW trucksW + 1  die ]
     set accel?< true
     if 
       (any? trucks-EW-at 0.5 -1)or (any? trucks-EW-at 0.5 -2) or (any? trucks-EW-at 0.5 -3)or
       (any? trucks-EW-at -3 -2) or (any? trucks-EW-at -3 -1) or (any? trucks-EW-at -3 -0.5) or
       (any? trucks-EW-at -3 0)  or
       (any? trucks-EW-at -2 -2) or (any? trucks-EW-at -2 -1) or (any? trucks-EW-at -2 -0.5) or 
       (any? trucks-EW-at -2 0)  or
       (any? trucks-EW-at -1 -2) or (any? trucks-EW-at -1 -1) or (any? trucks-EW-at -1 -0.5) or
       (any? trucks-EW-at -1 0)  or
           
       (any? cars-EW-at 0.5 -1) or (any? cars-EW-at 0.5 -2) or (any? cars-EW-at 0.5 -3)or
       (any? cars-EW-at -3 -2) or (any? cars-EW-at -3 -1) or (any? cars-EW-at -3 -0.5) or (any? cars-EW-at -3 0)or
       (any? cars-EW-at -2 -2) or (any? cars-EW-at -2 -1) or (any? cars-EW-at -2 -0.5) or (any? cars-EW-at -2 0)or
       (any? cars-EW-at -1 -2) or (any? cars-EW-at -1 -1) or (any? cars-EW-at -1 -0.5) or (any? cars-EW-at -1 0)or
            
       (any? obstacle-EW-at 0.5 -1) or (any? obstacle-EW-at 0.5 -2)or(any? obstacle-EW-at 0 -1) or
       (any? obstacle-EW-at 0 -2);stopping in front of level crossing if there is the warning state there 
       [set speed< 0 set accel?< false]
     if
       (any? cars-EW-at -4 0)or;spomalovanie 
       (any? trucks-EW-at -4 0)or
       (any? cars-EW-at -4 -0.5)or
       (any? trucks-EW-at -4 -0.5)or
       (any? cars-EW-at -4 -4)or
       (any? trucks-EW-at -4 -1)or
       (any? obstacle-EW-at 0.5 -3)
       [set speed< 0.01 set accel?< false] 
     if accel?< [speed-up<]
     if xcor < 6 and xcor > -2 and ycor > -2 [ set speed< speed< / 3 ] fd speed< ]
end    

to speed-up<
  ifelse (speed< >= MaxSpeed<)
  [set speed< MaxSpeed< fd speed<] ;if the speed is higher or equivalent to the value of defined speed 
                                   ;then the speed will be taken from the slider "MAX-SPEED, i.e. 
                                   ;the speed becomes reduced 
  [set speed< speed< + 0.07 fd speed<] ;if the speed is lower that vehicle speeds up with the step 0.07
end 
;-------------------------------------------------------------------------------------------
;  PART 15                    VEHICLES' FOLLOWING THE TRAJECTORY
;-------------------------------------------------------------------------------------------                       

to turning<
  ask turtles [
     if (distancexy 3.0800 14 ) < 0.5 [ set heading 260      ]
      if (distancexy 2.9310 13.8857) < 0.5 [ set heading 250  ]
       if (distancexy 1.6865 13.3565) < 0.5 [ set heading 240  ]
        if (distancexy 0.7027 12.8073) < 0.5 [ set heading 230  ]
         if (distancexy -0.7027 11.6843) < 0.5 [ set heading 220 ]
         if (distancexy -0.5909 11.3840) < 0.5 [ set heading 210 ]
        if (distancexy -0.8998 10.6863) < 0.5 [ set heading 200 ]
      if (distancexy -1.1149 9.8569) < 0.5 [ set heading 190  ]
     if (distancexy -1.3622 7.9842) < 0.5 [ set heading 180  ]
     ;from the right to the left - direction "270=>180" 
     if (distancexy -1.2806 -8.7548) < 0.5[ set heading 190   ]
       if (distancexy -1.3785 -9.4042) < 0.5 [ set heading 200  ]
        if (distancexy -1.4591 -9.6735) < 0.5 [ set heading 210  ]
         if (distancexy -1.6323 -10.0060) < 0.5 [ set heading 220 ]
         if (distancexy -1.9210 -10.3755) < 0.5 [ set heading 230 ]
        if (distancexy -2.2694 -10.6892) < 0.5 [ set heading 240 ]
       if (distancexy -3.5068 -11.0681) < 0.5 [ set heading 250 ]
      if (distancexy -4.3714 -11.7142) < 0.5 [ set heading 260 ]
     if (distancexy -5.5706 -11.9423) < 0.7 [ set heading 270 ]]
     ;from the right to the left - directon "180=>270"
end  

to turning
  ask turtles [
     if (distancexy -5 -14.5) < 0.5 [ set heading 80  ]
      if (distancexy -3.500 -14.326) < 1 [ set heading 70 ]
       if (distancexy -2.0653 -13.4594) < 1 [ set heading 60  ]
        if (distancexy  -0.5396 -12.3938) < 0.5 [ set heading 50]
         if (distancexy  0.2693 -11.6316) < 0.5 [ set heading 40 ]
         if (distancexy  1.0518 -10.5996) < 0.5 [ set heading 30 ]
        if (distancexy  1.3673 -9.9254) < 0.5 [ set heading 20 ]
      if (distancexy  1.4979 -9.3796) < 0.5 [ set heading 10 ]
     if (distancexy  1.7235 -7.7321) < 1 [ set heading 0 ]
     ;from the left to the right - direction "0" 
     if (distancexy 1.4771 8.5448) < 0.5[ set heading 20   ]
        if (distancexy 2.1492 9.6967) < 0.5 [ set heading 30  ]
         if (distancexy 2.4152 10.6007) < 0.5 [ set heading 40  ]
        if (distancexy 3.2354 11.0406) < 0.5 [ set heading 60 ]
      if (distancexy 3.4108 11.0380) < 0.5 [ set heading 70  ]
     if (distancexy 4.2060 11.4650) < 0.5 [ set heading 90  ]]
     ;from the left to the right - direction "90"
end                          
;-------------------------------------------------------------------------------------------
;  PART 16        GENERATING ROAD VEHICLES FOR BOTH DIRECTIONS
;-------------------------------------------------------------------------------------------           

to new-vehicles ;vehicles in direction EAST
   if (East = "cars")[cars] ;choose of the car using the chooser 
   if (East = "trucks")[trucks]
   if (East = "cars/trucks")[cars-trucks]
end 

to cars 
        let my-colors [ 15 35 45 55 97]
        ifelse 
        ((any? cars-WE-at min-pxcor -14.5) or (any? cars-WE-at (min-pxcor + 1) -14.5)or(any? cars-WE-at (min-pxcor + 2) -14.5))or 
        ((any? trucks-WE-at min-pxcor -14.5)or(any? trucks-WE-at (min-pxcor + 1) -14.5)or(any? trucks-WE-at(min-pxcor + 2) -14.5))
       [set congestion-we congestion-we + 1]
       [create-custom-cars-WE 1 [set heading 90 set size 1.4 setxy -39.5 -14.5 set speed 0 set color one-of my-colors]]
end 

to trucks 
        let my-colors [ 15 35 45 55 97 ]
        ifelse 
        ((any? trucks-WE-at min-pxcor -14.5)or(any? trucks-WE-at (min-pxcor + 1) -14.5)or(any? trucks-WE-at(min-pxcor + 2) -14.5))
        or((any? cars-WE-at min-pxcor -14.5) or (any? cars-WE-at (min-pxcor + 1) -14.5)or(any? cars-WE-at (min-pxcor + 2) -14.5))
       [set congestion-we congestion-we + 1]
       [create-custom-trucks-WE 1 [set heading 90 set size 2 setxy -39.5 -14.5 set speed 0 set color one-of my-colors]]  
end 

to cars-trucks
        if (choose-vehicleWE <= 1)   [cars]
        if (choose-vehicleWE  = 2) [trucks]
end  

to new-vehicles<                          ;nove auta v smere Západ
   if (West = "cars")[cars<];vyber auta pomocou chooseru 
   if (West = "trucks")[trucks<]
   if (West = "cars/trucks")[cars-trucks<]
end 

to cars< 
        let my-colors [ 15 35 45 55 97]
        ifelse
        ((any? cars-EW-at max-pxcor 14) or (any? cars-EW-at (max-pxcor - 1) 14)or(any? cars-WE-at (max-pxcor - 2) 14))or 
        ((any? trucks-EW-at max-pxcor 14)or(any? trucks-EW-at (max-pxcor - 1) 14)or(any? trucks-EW-at(min-pxcor - 2) 14))
       [set congestion-ew congestion-ew + 1]
       [create-custom-cars-EW 1 [set heading 270 set size 1.4 setxy  39 14 set speed< 0 set color one-of my-colors]]
end 

to trucks< 
        let my-colors [ 15 35 45 55 97]
        ifelse 
        ((any? trucks-EW-at max-pxcor 14)or(any? trucks-EW-at (max-pxcor - 1) 14)or(any? trucks-EW-at(min-pxcor - 2) 14))
        or((any? cars-EW-at max-pxcor 14) or (any? cars-EW-at (max-pxcor - 1) 14)or(any? cars-WE-at (max-pxcor - 2) 14)) 
       [set congestion-ew congestion-ew + 1]
       [create-custom-trucks-EW 1 [set heading 270 set size 2 setxy 39 14 set speed< 0 set color one-of my-colors]]  
end 

to cars-trucks<
        if (choose-vehicleEW <= 1)   [cars<]
        if (choose-vehicleEW  = 2) [trucks<]
end 
;-------------------------------------------------------------------------------------------
;  PART 17     PROCESURES FOR DETERMINING OF REGULAR OR OCCASSIONAL ARRIVAL OF VEHICLE 
;-------------------------------------------------------------------------------------------                 

to arrival-hour 
  if actual_day_hour < 5.5 [set valueI 7000]
  if actual_day_hour = 5.5 [set Time-interval 0 set Time-interval< 0]
  if ((actual_day_hour > 5.5)and(actual_day_hour < 6.5)) [set valueI 100]
  if actual_day_hour = 6.5 [set Time-interval 0 set Time-interval< 0]
  if ((actual_day_hour > 6.5)and(actual_day_hour < 8.5)) [set valueI 70]
  if actual_day_hour = 8.5 [set Time-interval 0 set Time-interval< 0]
  if ((actual_day_hour > 8.5)and(actual_day_hour < 13.5)) [set valueI 400]
  if actual_day_hour = 13.5 [set Time-interval 0 set Time-interval< 0]
  if ((actual_day_hour > 13.5)and(actual_day_hour < 15.5)) [set valueI 70]
  if actual_day_hour = 15.5 [set Time-interval 0 set Time-interval< 0]
  if ((actual_day_hour > 15.5)and(actual_day_hour < 20)) [set valueI 400]
  if actual_day_hour = 20 [set Time-interval 0 set Time-interval< 0]
  if ((actual_day_hour > 20)and(actual_day_hour < 23)) [set valueI 1500]
  if actual_day_hour = 23 [set Time-interval 0 set Time-interval< 0]
end                   

to cycle 
     if (Vehicle_arrival = "regular")[regular]   
     if (Vehicle_arrival = "occasional" )[occasional]
end 

to occasional 
         set Time-interval random-exponential valueI ;setting of time interval between arrival of vehicles, 
                                                     ;in this case according to "quasi" poisson distribution 
end 

to regular 
          set Time-interval _interval_
end 

to cycle< 
      if (Vehicle-arrival = "regular")[regular<]   
      if (Vehicle-arrival = "occasional" )[occasional<]
end 

to occasional< 
           set Time-interval< random-exponential valueI ;setting of time interval between arrival of vehicles, 
                                                        ;in this case according to "quasi" poisson distribution 
end 

to regular< 
     set Time-interval< -interval-
end      
;-------------------------------------------------------------------------------------------
; PART 18      PROCEDURES FOR DRAWING HISTOGRAMS AND SETTING VARIABLES FOR DRAWING  
;-------------------------------------------------------------------------------------------    

to do-plot
  set-current-plot "Histogram of Intensities of Real Vehicles/Hour"
  set-plot-x-range  0 24
  set-current-plot-pen "Cars"
  plot all-cars  
  set-current-plot-pen "Trucks" 
  plot all-trucks        
  set-current-plot-pen "Total" 
  plot all-cars + all-trucks
  set-current-plot "Histogram of intensities of Unit Vehicles/Hour"
  set-plot-x-range  0 24
  set-current-plot-pen "UnitVehs" 
  plot all-vehicles 
end 

to set-variables
    set all-cars (carsE + carsW)  
    set all-trucks (trucksE + trucksW)
    set all-vehicles  (all-cars + (all-trucks * 2))
    if top_intensity < all-vehicles  [set top_intensity all-vehicles]
    set moment ((10 * top_intensity)*(no_of_freight_trains + no_of_passenger_trains ))
end   

to calculate-results
    export-all-plots "d:/histogram.csv"
    user-message "Actual simulation day is over. Calculated traffic moment is  "+ moment + ". Press HALT and record all monitored quantities. Values from histograms were exported to the file d:/histogram.csv  To continue simulation of the next day press again Simulation. "
    stop
end   

There is only one version of this model, created over 14 years ago by Uri Wilensky.

Attached files

No files

This model does not have any ancestors.

This model does not have any descendants.