[PYTHON] Queuing theory part 4

[Congestion Studies](https://www.amazon.co.jp/%E6%B8%8B%E6%BB%9E%E5%AD%A6-%E6%96%B0%E6%BD%AE%E9 % 81% B8% E6% 9B% B8-% E8% A5% BF% E6% 88% 90-% E6% B4% BB% E8% A3% 95 / dp / 4106035707) an application of some sort of queuing theory I will explain about.

What is traffic jam study?

In the book linked above, congestion is defined as "a condition that occurs when a population of individuals with excluded volume effect that operates under certain conditions exceeds a certain density." This means that "a peculiarity that occurs when the density of individuals operating under a certain condition exceeds a certain density (critical density) in a group that exists without overlapping (multiple individuals occupy the same space). It is a phenomenon. " You probably don't know what you're talking about yet, so let's take a car as an example. Note that the premise is that the line consists of only individuals = cars and groups = cars. Then, traffic jam is a phenomenon that occurs when a vehicle exceeds a certain traffic density under the rule of moving forward when the front is open, and it becomes difficult for many cars to move forward or the speed is higher than the legal speed. It is a peculiar situation such as being able to run only at a significantly slower speed. " I will talk about this car in the future. If you want to know the traffic jams that can be handled by traffic jams other than cars, go to Google or go to Amazon [Traffic jams](https://www.amazon.co.jp/%E6%B8%8B%E6%BB%9E % E5% AD% A6-% E6% 96% B0% E6% BD% AE% E9% 81% B8% E6% 9B% B8-% E8% A5% BF% E6% 88% 90-% E6% B4% Please purchase and read BB% E8% A3% 95 / dp / 4106035707).

A simple simulation of car traffic

This time I will use cellular automata as the simplest simulation. Cellular automata are called life games if they are two-dimensional rules, but this time the rule is one-dimensional and the rule is "If the previous one is empty, proceed". For cellular automata [Wikipedia](https://ja.wikipedia.org/wiki/%E3%82%BB%E3%83%AB%E3%83%BB%E3%82%AA%E3%83%BC % E3% 83% 88% E3% 83% 9E% E3% 83% 88% E3% 83% B3) See.

Python program for simulation

First of all, put the code.

trafic.py


# -*- coding: utf-8 -*-

#Change of state of traffic jam by one-dimensional cellular automaton
import random
import copy

MAX = 45
SIM_TIME = 5
P = 0.6

#State 0 is an empty space without a car
#State 1 represents a vehicle
trafic = [0 for i in range(MAX)]


def list2string(l):
    string = ""
    for state in l:
        if state == 0:
            s = "□ "
        elif state == 1:
            s = "■ "
        string += s
    return string


def setRandom(l, p):
    for i, state in enumerate(l):
        if p < random.random():
            l[i] = 0
        else:
            l[i] = 1


def nextTime(l):
    temp = copy.deepcopy(l)
    for i, state in enumerate(l):
        if state == 0:
            #On the far left
            if i == 0:
                temp[i] = 0
            #Check backward at other times
            elif l[i-1] == 0:
                temp[i] = 0
            else:
                temp[i] = 1

        elif state == 1:
            #On the far right
            if i == len(l)-1:
                temp[i] = 0
            #Check forward at other times
            elif l[i+1] == 0:
                temp[i] = 0
            else:
                temp[i] = 1
    return temp

if __name__ == "__main__":
    #Execution part
    setRandom(trafic, P)
    print(list2string(trafic))

    for i in range(SIM_TIME):
        #Update information
        trafic = nextTime(trafic)
        #Draw information as text
        print(list2string(trafic))

I will explain the code.

Describes the variables and constants defined first. The variable MAX represents the maximum trafic size defined later, that is, the maximum length of cars lined up. The next SIM_TIME is the length of time to simulate and P is the traffic density. And the list called trafic is a list that takes only two types of states, (1) with a car and (0) without a car, and is unified to 0 as initialization at first.

The defined function is explained below. The first list2string function is a function that converts □ and ■ to a character and returns a character for a list that has only 0 and 1 states given as arguments. The setRandom function is a function that sets whether there is a car or not with the value of the second argument as the boundary. The nextTime function is a function that checks whether to proceed according to the state of the car at that time and returns the next state. You can see the case classification by looking at the comments.

The last is the execution part. There is no need to explain here, and I think comments are sufficient. You could rewrite this code to make it a ring road, or break the loop when all the cars are gone.

Summary and explanation of simulation results

If you change some values of P and execute it, the behavior will change significantly when P is 0.5. (Since it uses random numbers, it may not change much.)

Consider why it changes. In the first place, it is indispensable that the front is open in order for the car to move. So I'll try to fill the open front of the car with any car. Then, the state should be the state where the presence or absence of the car appears alternately. Of course, in this state, you can proceed smoothly without any traffic jams. But what about putting a car in there? I think traffic jams will occur no matter where you put your car. The value of 0.5 at this time is called the critical density of the traffic. If the critical density is not exceeded, if it takes a long time, even if there is some congestion, it will be eliminated. However, if it exceeds, it will not be resolved forever. In fact, it is known that this result holds true even in reality, although the value of the critical density is different. Therefore, in places where traffic congestion occurs frequently, traffic congestion is eliminated in consideration of this concept of critical density.

Finally

A more detailed explanation of the explanation given here is given at the beginning [Congestion Studies](https://www.amazon.co.jp/%E6%B8%8B%E6%BB%9E%E5%AD%A6- % E6% 96% B0% E6% BD% AE% E9% 81% B8% E6% 9B% B8-% E8% A5% BF% E6% 88% 90-% E6% B4% BB% E8% A3% 95 You can also find it in the book / dp / 4106035707). There are other interesting traffic jams such as "favorable traffic jams", so please read them.

Recommended Posts

Queuing theory part 4
Queuing theory part 3
datetime part 1
numpy part 1
argparse part 1
numpy part 2
Test Mathematical Part 2 (Mathematical model of item reaction theory)