DAC Exo6: La Circulation

#ConcurrentProgramming #Simulation #WIP

Globally defined variables
Semaphores {} = {}
Integers {} = {}
Changement Traversee1 Traversee2

Warnings

    - You must declare feu as {1, 2} (see the Problem section).

    - For technical reasons, you need to call sleep(0) if you're going to use an "active wait" loop of some kind that does not contain any call to our predefined functions.

    - The 'stop' and 'restart' functionalities are not fully implemented and so you will need to refresh the page to test different algorithms (TO BE FIXED later~).
    (You can still call Sim.stop() from the console and it will kinda work.)

    - Keep this in mind if you're using loops and conditionals:
    Sorting each road's vehicles with multiple dynamic queues (semaphores) is tricky. The ordering priority I used depends on calls to p(..);
    e.g. p(sFeu); p(sVide); creates orderVec=['sFeu', 'sVide'] that will be used to sort each vehicle in each road.
  

Problem / Exo

    EN FRANÇAIS: Exercice 2. Synchronisation : la circulation (#archivé)


    Traffic Lights:
    At an intersection, traffic is controlled by traffic lights...

    Rules:
    - The intersection can contain at most one vehicle.
    - Vehicles arrive at both roads in a random manner.
    - Traffic lights of each road periodically change color (red or green), and hold that color for a finite period of time.
    - The traffic lights controller (Changement) should wait only for the current vehicle at the intersection to leave before changing lights.

    For the sake of simplicity, we assume that:
    - it is an intersection of two one-lane roads
    - vehicles don't change direction, and keep moving in a straight line

    This system can be modeled as a set of parallel processes:
    - A process P that executes the Changement procedure which handles both traffic lights.
    - A process for each vehicle (Traverseei if on voiei; where i = {1, 2})
    - Traffic light of voiei is green when feu == i.
    (Hint: Traffic lights display a yellow circle when 'Changement' is blocked on a semaphore)

    Predefined functions: p(sema), v(sema), sleep(secs), in addition to Traversee's circuler().

    Write Changement, as well as the Traversee1 and Traversee2 procedures to synchronize the system using semaphores.

    Gambatte~
  

Notes

    This web app is supposed be useful (as in [2]) to students, so they can actually test their algorithms and see how they work in a dynamic system,
    and not just a viz like I did with DAC Exo3.
    
    * Why? Why not. Also, because the idea of simulating a dynamic system sounded cool (my DAC teacher mentioned it).

    * It makes use of:
      - JavaScript's with feature which is deprecated and very discouraged
      - AsyncFunction
      - Promise
      - SillySemaphore: My stupid Promise-based and queue-based semaphore
      - CSS order
      - CSS transition

    * Developed and tested with Brave Browser v1.1 (Chromium v79.0)
    

Refs

These are some of the cool documents and topics I checked while making this...
    - [1]: Steve Carr and Jean Mayo and Ching-kuang Shene, ThreadMentor: A Pedagogical Tool for Multithreaded Programming (tutorial)

    - [2]: B. P. Miller, What to Draw? When to Draw? An Essay on Parallel Program Visualization

    - Wikipedia, Queueing theory

    - Wikipedia, Taxicab geometry

    - Wikipedia, Computer simulation
    - Wikipedia, Web-based simulation
    - Wikipedia, Discrete-event simulation
    - SIM.JS: on Wikipedia, its website

    - Traffic Jams in Javascript | bit-player

    - Wanis Kabbaj: What a driverless world could look like | TED Talk
    

Credits

    - 'City car', 'Truck', 'Traffic lights green', and 'Traffic lights red' icons by Delapouite under CC BY 3.0
    
- @ZEIT's async-sema package (under the MIT license) heavily inspired my SillySemaphore