There are 2 intersections and you need to force traffic lights to "settle" traffic.

I understood the principle of operation of the genetic algorithm, but when I wanted to test it in practice, the questions immediately appeared:

  1. Where should the algorithm itself be processed: at every frame update, or when a traffic jam occurs, or when a car drives up to any traffic lights?

  2. What should I cross? I also read that in the algorithm we have to cross arrays of data like 1,2,3,4,5,6 and 6,7,8,9,0,1 and get -> 1,2,3 | 9,0 , 1 and 6,7,8 | 4,5,6. So what do I have to cross?

    1 answer 1

    In essence, you need to grow an algorithm. What does the traffic light: switches colors and waits for each of them for some time.

    So the intersection has a horizontal road and a vertical one. Step by Step:

    1. Let it all start with the fact that horizontal sweaters are green, and vertical - red. Green lit T1 time.
    2. Then yellow turns on in horizontal (vertical light is still red). It burns T2 time.
    3. Red at horizontal, yellow at vertical. T3.
    4. Red at horizontal, green at vertical. T4.
    5. Red at horizontal, yellow at vertical. T5.
    6. Yellow in horizontal, red in vertical. T6.

    Total, the algorithm is encoded by a tuple (T1, T2, T3, T4, T5, T6), i.e. DNA from six genes.

    Now we need to define three parameters of the algorithm:

    1. Fitness function. Suppose we need to skip the maximum of machines per unit of time. Which of the algorithms it is higher, and that is cool.
    2. Mutation. Here the random gene changes a little.
    3. Crossbreeding We give birth to a son (the algorithm is masculine), his genes will be randomly selected from parents.

    Now the simulation:

    1. Initially, we have N random algorithms.
    2. We start a cycle in which we determine the effectiveness of each algorithm (or only new ones).
    3. We eliminate, and slyly - the worse the algorithm, the greater the likelihood of its exclusion. However, it should not be 0 or 1.
    4. We cross, mutate (again, probability).

    This is one of the solutions.

    • So, well, everything is in principle more or less clear, but I have a question about “Crossing. We give birth to a son (the algorithm is masculine), his genes will be randomly selected from the parents.”
    • Yes of course. Each algorithm is (T1, T2, ..T6). We choose two parents, then the child T1 is either T1 of the mother, or T1 of the father (by chance). T2 is also either mother’s T2 or father’s T2 (by chance, regardless of the first gene). With a probability of 1 / (2 ^ 6) the child will be equal to the mother, with the same probability - the father. Similarly, the second child may be the “complete opposite of the first,” that is, if the first took T1 from the father, the second will take it from the mother (you cited this case in your example). - Manushin Igor
    • @ManushingIgor That is, first running the simulator, we start any traffic from the machines and generate random algorithms, then after a minute we slow down everything, run what you call “simulation”, it analyzes which algorithm is the coolest, crosses the algorithms, etc. (in short, I will not repeat) and so repeat until the algorithm is revealed, which will be the most steep? - alex-rudenkiy
    • Yes everything is correct. Exactly. There are a bunch of add-ons, extensions, optimizations. See, for example, island model (for example, here - rain.ifmo.ru/cat/view.php/theory/unsorted/genetic-2005 ) - Manushin Igor
    • By the way, and which is better: neural networks or a genetic algorithm? - alex-rudenkiy