Genetic Heuristic Algorithms

Started school graduate will be drawn to participate in the digital-analog game, the title match is a flight scheduling optimization problem, so the first reaction is that genetic algorithms, three problems during the game with a single objective genetic algorithms, taking advantage of the relatively familiar , hereby recorded for subsequent review. This article uses Python for implementation.

heuristic algorithm

Heuristic algorithm is a technology, which makes the computational cost acceptable to search for the best solution, but does not necessarily guarantee optimum and feasible solution is obtained, even in most cases, the resulting solution can not be explained with the optimal degree of approximation solution.

That global optimal solution of this algorithm is only theoretically possible, it is a local optimal solution in most cases. Heuristics are used more simulated annealing algorithm (SA), genetic algorithms (GA), a list of search algorithm (ST), evolutionary programming (EP), evolution strategies (ES), ant colony algorithm (ACA), artificial neural network (ANN). Here focus on what genetic algorithms (GA).

Genetic Algorithms preparation

Genetic Algorithms (Genetic Algorithm, GA) originated in the computer simulation of biological systems carried out. Random global search and optimization methods which mimic the natural biological evolution mechanism developed, drawing on Darwinian evolution and Mendelian genetics. Its essence is a highly efficient, parallel, global search method, and can automatically acquire the accumulation of knowledge about the search space in the search process and the search process is adaptively controlled in order to achieve an optimal solution.

Specifically, before writing the algorithm, there are four important steps:

  1. Determine the encoding
  2. How to design coding
  3. OK constraints
  4. How to implement constraints

Determine the encoding

For encoding, the impact of the crossover, genetic operators such as mutation operator operation method, large largely determines the efficiency of genetic evolution. Overall, the coding can be divided into three categories: a binary coding method, floating point encoding method, symbol coding method.

  1. Binary coding method: Solution converting from decimal numbers to binary representation that is easy encoding and decoding, and to achieve easy crossover mutation operator, but for some continuous function of the optimization problem, because randomness local search capability so that it is poor as for some high-accuracy problems, when the iterative close to the optimal solution, due to the large phenotypic changes after its variation, discontinuous, it will be far from the optimal solution, reach stability. The Gray code can effectively prevent such phenomenon.

  2. Gray code encoding method: Let's review what Gray code:

    十进制 格雷码   二进制
    
    0       0000    0000
    1       0001    0001
    2       0011    0010
    3       0010    0011
    4       0110    0100
    5       0111    0101
    6       0101    0110
    7       0100    0111
    8       1100    1000
    9       1101    1001
    10      1111    1010

    Wikipedia to solve the Gray code Description of error:

    Traditional systems such as a digital binary representation of 3 is 011, adjacent to the switch number 4, i.e. 100, the device had three bits to be converted, when the process is therefore not fully converted to the device will experience short, 010,001,101,110,111 wherein several other state, i.e. represents 2,1,5,6,7, and therefore a relatively large error range may be a digital encoding method such adjacent digital converter. Gray code of the present invention that is used to reduce the possibility of errors to a minimum, for each coding mode is defined adjacent to a difference of only a digital bit, also known as minimum difference codes, the device can be done when the step number only the minimum number of bits modifiers to improve stability.

    With only one code bit code value is the difference between two consecutive integers corresponding to when a lot of iterations near the optimum value varies between two values ​​adjacent is the largest, when a chromosome aberrations, it turns out current and current performance phenotype is continuous, so that the possibility of error can be reduced to a minimum.

    Gray code is converted to:

    G is a Gray code, B is binary code, n is the calculated current position, \ (\ Oplus \) is the exclusive-OR

    There \ (G (n) = B (n + 1) \ oplus B (n) \)

    That \ (G (n) = B (n + 1) + B (n) \)

  3. Floating-point encoding method

    For some multi-dimensional, high accuracy is required continuous function optimization problem using binary encoding to represent an individual when there will be some disadvantages.

    Binary-coding error when there is a continuous function mapping discretization. When the length of an individual than to know, may not meet the accuracy requirements, and longer length of the individual coding, although they would improve accuracy, but the search space of GA expanded dramatically.

    The so-called floating-point method is a method for each individual gene with a floating point value within a range expressed. In the floating-point encoding method, the gene must ensure that a given value within the range limits, the genetic algorithm used in the crossover and mutation genetic operator must ensure that a new individual gene values ​​generated by the calculation result also in this interval within the limit.

  4. Symbol encoding method

    In fact, this encoding method in solving practical problems when the problem is most of this method such as the optimization of the flight I would use this method in conjunction with practical problems to be encoded, then crossover and mutation have combined with the actual conduct of the preparation, here I will give a concrete example to explain symbol encoding method.

How to design coding

For the conversion value is generally a mathematical function optimization problems are to be solved to binary or gray code iterates and then calculate the fitness (fitness) can be, where I mainly explain the problem, if not a direct value passed to solve the equation to solve.

For chestnuts

If I have 10 flights (a, b, c, ...), and five gates (1,2,3,4,5), I now make scheduling more compact, making use of boarding number of mouth as little as possible. There is a per aircraft departure time (departure_time), 45 minutes before the departure time of the aircraft must arrive at the boarding gate, so we designed a function that is minimized

\[ F = \sum_{i=0}^5\sum_{j=0}^{10}S_{ij}\]

Where i is the boarding port number, j is the number of flights, so \ (S_ij \) time difference is the corresponding gate and flight on a flight, so that we can minimize the time and make more compact arrangement.

So that I can be encoded

ga1.jpg

Chromosome 10 is the corresponding 10 flights, the content of which is its corresponding gate number, departure time interval so that we can calculate the same gate adjacent aircraft worse.

Example No. 2 corresponding gate is plane a, plane i, we use $ i_ {arrival_time} - a_ {arrival_time} $ can obtain the time difference corresponding to a statistical calculation No. 1, No. 3, No. 4 and No. 5 , together is our objective function value of the.

OK constraints

For solving different problems have different ways of restraint, restraint methods are common

  • Range limit
  • Conditions

Range limits , for example, we may only evaluate the (0, 0.3] and [0.5, 0.8) between, then we added either at the time of solving penalty term, i.e., non-corresponding to the calculated range when multiplied by a large value to limit, or to the time corresponding to the crossover and mutation to be limiting in determination of crossover and mutation.

Conditions , for example, a title, if we constraints, i.e. the aircraft has wide and narrow points, we also have a corresponding gate width of the points, corresponding to the aircraft only stop at the corresponding gate, so here we have to meet the coding constraints for encoding, this can also be written for the preparation of crossover and mutation.

How to implement constraints

Implementation constraints that is realized in three places, respectively

  1. Gene feasible solution initialize (inital)
  2. CROSS gene (Crossover)
  3. Gene mutation (mutate)

Here briefly explain how to initialize constraint feasible solution implementation, we can write a simple while loop to find a viable solution:

POP_SIZE = 100 # 可行解数目
DNA_SIZE = 10 #dna长度,这里为航班数量,即为10
all_airport_id = [1,2,3,4,5] # 登机口id
aircraft_to_airport_dict = {
    1:[1,2,3],
    2:[2,3,4],
    ...
}# 这里是每架飞机对应的登机口id,这里我就简写了
def inital(self):
    pop = [] # 可行解list
    while len(pop) != POP_SIZE:
        dna = [0 for i in range(DNA_SIZE)]
        all_aircraft_id = list(range(1, 11)) # 所有飞机id
        temp_airport_id_set = set() # 遍历过的存储飞机对应的可行的飞机场
        for aircraft_id in all_aircraft_id:
            workable_airport = aircraft_to_airport_dict[aircraft_id] #可行的登机口id
            while 1: #直到要找到合适的机场停机
                airport_id = random.choice(list(workable_airport)) # 随机生成一个登机口id
                temp_airport_id_set.add(airport_id)
                if len(temp_airport_id_set) == len(workable_airport):
                    dna[aircraft_id-1] = 0 #如果全都不合适,就跳出
                    break
                if constraint(aircraft_id, airport_id): #如果满足约束
                    dna[aircraft_id-1] = airport_id

Here constraint constraint constraint function may be many, such as time constraints, area constraints and the like.

Genetic Algorithm

Personally I think that the overall genetic algorithm is divided into five sections:

  1. Initialization feasible solution
  2. The objective function, i.e., computing the fitness (Fitness)
  3. Survival of the fittest
  4. Cross-evolution
  5. variation

Specific procedures:

生成n组可行解dna
迭代m次:
    计算每个dna的fitness
    适者生存
    循环n个dna:
        交叉进化
        变异

Next, mainly to talk about survival of the fittest, evolution crossover and mutation

Survival of the fittest

Natural selection, survival of the fittest

Means that the survival of the fittest Fitness calculated, then the probability of a randomly chosen, so that the larger the probability to be selected greater probability. To achieve the following specific, I made a countdown of fitness, here is because my calculation is minimized, the smaller is the best for me, so then taking the inverse of the probability calculation only logical.

def select(self, pop, fitness):    
        fitness = 1 / fitness
        idx = np.random.choice(np.arange(POP_SIZE), size=POP_SIZE, replace=True,
                           p=fitness/fitness.sum())
        return pop[idx]

Cross-evolution

There are many cross-evolutionary ways, can be seen in this article Genetic Algorithms Several crossover summary , talking about has been very wide.

Here I mainly talk about the one-point crossover, which randomly selects a point p on the parents (parent) dna, then exchanged with the children (child) gene after the dna p points, for it is also very simple. Below, o 2 points, and the blue will exchange portion and a yellow portion.

ga2.jpg

CROSS_RATE = 0.8 #交叉概率
def crossover(self, parent, pop):  
    if np.random.rand() < CROSS_RATE:
        i_ = random.randint(0, POP_SIZE - 1) # 孩子基因,child
        p = np.random.randint(0, DNA_SIZE)  # 单点交叉
        parent[p:DNA_SIZE] = pop[i_][p:DNA_SIZE]
        return parent
    return parent

variation

The world there are so many unexpected mutation, it is difficult to say what makes us to a dead end, people as long as we breathe, what can hope.

- Montaigne "The Complete Works of Montaigne's essay"

Many mutations are neutral, but some may cause damage to future generations, while others may be beneficial. Control gene mutations can cause great impact on the organism. Beneficial mutations can help to better adapt to the biological environment, and is likely to be passed on to future generations.

Variation is randomly selected on a point dna genetic mutation, the reason for this is to prevent local optimum, because if someone mutation in this world, then do not rule out that we are now more than possible to adapt the world. c point is the point mutation.

ga3.jpg

MUTATION_RATE = 0.01 #突变概率
def mutate(self, child):
    for c in range(DNA_SIZE):
        if np.random.rand() < MUTATION_RATE:
            workable_airport = aircraft_to_airport_dict[aircraft_id] #可行的登机口id
            random_airport_id = random.choice(workable_airport) #随机一个机场
            child[c] = random_airport_id

Guess you like

Origin www.cnblogs.com/harrylyx/p/12397319.html